JAVA INTERVIEW QUESTIONS- DAY 2

Spread the love

Top 10 Java Interview Questions for Freshers

1. Explain public static void main(String args[]) in Java?

Answer:

In Java, public static void main(String[] args) is the entry point of any Java application.

  • public: This means the method is accessible from anywhere. It is necessary because the Java Virtual Machine (JVM) needs to access this method to run your program.
  • static: This keyword allows the JVM to call the main method without creating an instance of the class. It belongs to the class rather than to any specific object of the class.
  • void: This means the method does not return any value.
  • main: This is the name of the method. The JVM looks for this method when starting the execution of a Java program.
  • String[] args: This is the parameter passed to the main method. It allows the program to accept command-line arguments. args is an array of String objects, and each element of the array contains one argument from the command line.

 

2. Explain importance of declaring Main Method as Static?

Answer:

 In Java, the main method is declared as static because it needs to be accessible by the JVM without creating an instance of the class. the main method is static, it can be invoked directly by the JVM without needing to create an object of the class. If main were not static, the JVM would have to instantiate the class before calling the method, which could complicate the startup process. The main method is the entry point of the application. Marking it as static ensures it can be called by the JVM when the program starts, even before any objects are created.

3. What is Data type in Java?

Answer:

In Java, data types define the kind of data that can be stored in a variable. Data types define the type and value range of the data for the different types of variable. The data type tells the compiler about the type of data to be stored and the required memory. To store and manipulate different types of data, all variables must have specified data types.

 

4.Which are the Data Types present in Java?

Answer:

Java data types are categorized into two parts −

Primitive Data Types

Primitive data types are predefined by the language and named by a keyword. There are eight primitive data types supported by Java. Below is the list of the primitive data types:

  • byte
  • short
  • int
  • long
  • float
  • double
  • boolean

Non Primitive Data Types

 The non primitive data types are created using defined constructors of the classes. They are used to access objects. These variables are declared to be of a specific type that cannot be changed.

  • Array,
  • Interfaces
  • Strings etc.

 

5. Why char data type require size of 2 bytes in java? 

Answer:

Other languages like C/C++ use only ASCII characters, and to represent all ASCII characters 8 bits is enough. But Java uses the Unicode system not the ASCII code System and to represent the Unicode system 8 bits is not enough to represent all characters so Java uses 2 bytes for characters. Unicode defines a fully international character set that can represent most of the world’s written languages.

6. What are the different operators in Java, list it?

Answer:

  • Arithmetic Operators.
  • Relational (Comparison) Operators.
  • Logical Operators.
  • Bitwise Operators.
  • Assignment Operators.
  • Unary Operators.
  • Ternary Operator.
  • Shift Operators.

 

7.  What is compilation? What does compiler do?

Answer:

Compilation is the process of translating source code written in a high-level programming language like java into machine code, bytecode, or another lower-level language that a computer’s processor can execute.

The compiler transforms human-readable code into a form that a computer can run, facilitating the execution of programs.

8. What is Variable in Java?

Answer:

A variable is a named storage location in a program that holds a value. The value can change during the execution of the program, hence the term “variable.” It acts as a container for storing data, allowing programmers to reference and manipulate that data dynamically. A variable is a way to label data in memory so that it can be easily accessed and modified throughout a program’s runtime.

 

9.   Which types of variables are there in Java? Explain.

Answer:

  • local Variable

A local variable is defined within a function or block and is accessible only within that scope. It exists only while the function is executing. Once the function exits, the local variable is destroyed and its memory is freed.

  • static variable

A static variable is a variable that maintains its state across function calls or is shared among all instances of a class. static variables are defined at the class level rather than the instance level. It exists for the duration of the program’s execution, retaining its value between calls or instances.

  • Instance variable

An instance variable is a variable that is associated with a specific instance of a class. Each object (instance) of the class can have its own values for these variables.  It exists as long as the object exists.

 

10.  Explain difference between instance variable and local variable?

Answer:

Instance variable:

Instance variables are tied to a specific object of a class. They are accessible throughout the class methods. They exist as long as the object is alive. When the object is destroyed, the instance variables are also destroyed. Instance variables are used to store the state or attributes of an object. Each instance can have different values for its instance variables.

Local variable :

 Local variables are defined within a function or block and are only accessible within that specific scope. They exist only during the execution of the function or block. Once the function exits, the local variables are destroyed. Local variables are used for temporary storage of data that is only needed within the function. They help avoid conflicts with other variables and manage memory efficiently.

 

Leave a Comment