Wednesday, 4 October 2023

Java-QP-Part1

 Q1) Attempt any Eight : [8 × 2 = 16]

1.What is Java? Why Java is a platform neutral language?

1)Java is a high-level, object-oriented programming language that was developed by Sun Microsystems.

2)It was designed to be platform-independent, allowing developers to write code once and run it on any device or operating system that has a Java Virtual Machine (JVM) installed.

3)Java is Platform neutral because the same Java code will run on multiple platforms

2.What is access specifiers? List them.

There are four types of Java access modifiers:

1.Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.

2.Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.

3.Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.

4.Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.

3.Define Keyword-Static.

The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class.

The static can be:

1.Variable (also known as a class variable)

2.Method (also known as a class method)

3.Block

4.Nested class

4.Why we set environment variable in Java?

 We set environment variables in Java for:

1.Configuration: To store settings like database connections, API keys, and environment-specific values.

2.Security: To protect sensitive information like passwords and API tokens.

3.Portability: To make applications easily adaptable to different environments.

4.Dynamic Configuration: To change application settings without code changes.

5.Third-Party Integration: To configure interactions with external services and libraries.

6.Compliance and Auditing: To adhere to security and regulatory requirements.

7.Ease of Maintenance: To manage and update settings conveniently.

To access environment variables in Java, you typically use the System.getenv() method to retrieve their values. Environment variables can be set at the operating system level or by various deployment tools and platforms (e.g., Docker, Kubernetes, Heroku) to provide configuration to your Java application.

 

5.Write advantages of Inheritance.

•Another advantages of inheritance is extensibility, you can add new features or change the existing features easily in subclasses.

•Using inheritance we can achieve runtime polymorphism(method overriding).

•Inheritance makes easy to maintain the code, as the common codes are written at one place.

•The most important use of inheritance in Java is code reusability. The code that is present in the parent class can be directly used by the child class.

•Method overriding is also known as runtime polymorphism. Hence, we can achieve Polymorphism in Java with the help of inheritance.

 6.Define class and object with one example.

•Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors – wagging the tail, barking, eating. An object is an instance of a class.

•Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support.

•An object is called an instance of a class. For example, suppose Bicycle is a class then MountainBicycle, SportsBicycle, TouringBicycle, etc can be considered as objects of the class.

•Creating an Object in Java

Bicycle sportsBicycle = new Bicycle();

Bicycle touringBicycle = new Bicycle();

Creating a class in Java

class Bicycle {

  private int gear = 5;

  // behavior or method

  public void braking() {

    System.out.println("Working of Braking");

  }

}

7.What is Swing?

Java Swing is a part of Java Foundation Classes (JFC) that is used to create window-based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and entirely written in java.

Unlike AWT, Java Swing provides platform-independent and lightweight components.

The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.

8.When buffered reader is used?

BufferedReader is a class in Java that is used for reading text from character input streams with improved efficiency and performance.

Reading Text Files: When you want to read data from a text file, you can use BufferedReader to read lines of text efficiently. It's commonly used in file input operations.

try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) { String line; while ((line = br.readLine()) != null) { // Process each line of text } } catch (IOException e) { e.printStackTrace(); }

9.What is main difference between exception and error?

Error

1.An error cannot be handled at runtime.

2.An error can occur both at compile time and during runtime.

3.There are 3 types of Errors: Syntax Error, Runtime Error and Logical Error

4.An Error is such an event that no one can control or guess when it is going to happen.

Exception

1.An exception can be handled at runtime

2.Although all the Exceptions occur at runtime. But checked Exceptions can be detected at compile time.

3.There are 2 types of Exceptions: Checked Exceptions and Unchecked Exceptions

4.An exception has the capacity to distract the normal flow of the program and change its direction to somewhere else when an exceptional case has occurred.

5.An Exception can be guessed, handled, and utilized in order to change the original flow of the program.

10.What is Panel?

Panel is the simplest container class. A panel provides space in which an application can attach any other component, including other panels.

11.What is JDK? How to build and run java program?

The Java Development Kit (JDK) is a software development environment that offers a collection of tools and libraries necessary for developing Java applications. You need the JDK to convert your source code into a format that the Java Runtime Environment (JRE) can execute.

 

We must follow the steps given below to run a Java program.

Open the notepad and write a Java program into it.

Save the Java program by using the class name followed by .java extension.

Open the CMD, type the commands and run the Java program.

class Simple{ 

    public static void main(String args[]){ 

     System.out.println("Hello Java"); 

    } 

To compile:   javac Simple.java

To execute:   java Simple

          Output: Hello Java

 

12.What is use of classpath?

The classpath in Java specifies where the Java Virtual Machine (JVM) should look for classes and libraries when running a Java program. It ensures that the JVM can locate and load the necessary classes and resources for the program to execute correctly.

 

13.What is collection? Explain collection frame work in details.

A collection is an object that can hold references to other objects. The collection interfaces declare the operations that can be performed on each type of collection.

The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.

Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.

A Collection represents a single unit of objects, i.e., a group.

 

The Collection framework represents a unified architecture for storing and manipulating a group of objects. It has:

Interfaces and its implementations, i.e., classes

Algorithm

14.What is the use of Reader and Writer class?

In Java, the Reader and Writer classes are used for character stream input and output operations, respectively. They are designed to handle character data, making them suitable for reading and writing text-based information, such as files, network streams, or user input/output. These classes provide methods for reading and writing characters, making them essential for text-based I/O tasks.

15.What is the use of layout manager?

The Layout managers enable us to control the way in which visual components are arranged in the GUI forms by determining the size and position of components within the containers.

Layout managers help you create flexible and platform-independent GUIs by handling the placement and resizing of components automatically. Common layout managers in Java include FlowLayout, BorderLayout, GridLayout, and GridBagLayout, each offering different ways to organize and control the placement of components within a container.

16.What is difference between paint ( ) and repaint ( ).

paint()

The paint() method is called when some action is performed on the window.

This method supports painting via graphics object.

 

repaint()

Whenever a repaint method is called, the update method is also called along with paint() method.

This method is used to cause paint() to be invoked by the AWT painting thread.

 

17.Define keyword throw.

The throw keyword in Java is used to explicitly throw an exception from a method or any block of code. We can throw either checked or unchecked exception

 

18.Define polymorphism.

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.