Java Interview Questions
, Basic Interview Questions
Q1. What are the data types in Java? Java has two types of data
types:
1. Primitive Data Types: These include byte, short, int, long, float, double,
char, and boolean. They store simple values and are not objects.
2. Non-Primitive Data Types: These include String, Array, Class, and
Interface. They are derived from primitive data types and provide
more functionalities.
Q2. What are wrapper classes?
Wrapper classes provide an object representation of primitive data types,
such as Integers, Doubles, and Booleans. These classes allow primitives to be
used in collections and provide useful utility methods.
Q3. Are there dynamic arrays in Java?
Java arrays are fixed indynamically. Q4 size. However, ArrayList (from the
Java.util package) provides a dynamic array implementation where elements
can be added or removed dynamically.
Q4. What is JVM?
The Java Virtual Machine (JVM) is a part of the Java Runtime Environment
(JRE). It is responsible for executing Java bytecode by converting it into
machine code specific to the operating system.
Q5. Why is Java platform-independent?
Java achieves platform independence through bytecode. The Java compiler
converts code into bytecode, which the JVM interprets for the underlying OS,
making Java write-once, run-anywhere.
,Q6. What are local and global variables?
● Local variables are declared inside methods or blocks and are
accessible only within their scope.
● Global variables (also called instance variables) are declared within a
class but outside any method and have a wider scope.
Q7. What is data encapsulation?
Encapsulation is an OOP principle where data (variables) and code (methods)
are bundled into a single unit (class). It restricts direct access to data using
access modifiers (private, protected).
Q8. What is function overloading?
Function overloading allows multiple methods to have the same name but
different parameter lists. The compiler differentiates them based on the
number or type of parameters.
Example:
public class Figure
{
public int area(int a, int b)
{
int rectangleArea = a*b;
return rectangleArea;
}
public int area(int a)
{
int squareArea = a*a;
return squareArea;
}
public static void main(String[] args ){
Figure f = new Figure();
System.out.println("Area of square " + f.area(5));
System.out.println("Area of Rectangle " + f.area(5,3));
}
}
, Q9. What is function overriding?
Overriding allows a subclass to provide a specific implementation of a
method defined in its superclass. It enables dynamic method dispatch
(runtime polymorphism).
Q10. Why is the main method static in Java?
The main method is static so that it can be called without creating an
instance of the class, allowing the program to start execution without object
instantiation.
Q11. What is the difference between the throw and throws keywords
in Java?
Feature throw throws
Purpose Used to explicitly Declares that a
throw an exception method may throw an
exception
Usage throw new public void
Exception("Error") myMethod() throws
IOException
Number of Exceptions Can throw one Can declare
exception at a time multiple exceptions
using commas
Q12. What do you mean by singleton class?
A singleton class ensures that only one instance of the class exists throughout
the application's lifecycle. It is implemented using a private constructor, a
static instance variable, and a public static method that returns the single
instance. The most common way to create a singleton class is using the lazy
initialization or eager initialization approach.
Q13. Does every try block need a catch block?
No, a try block does not necessarily need a catch block. It can be followed by
either a catch block, a final block, or both. A catch block handles exceptions
that may arise in the try block, while a final block ensures that certain code
, Basic Interview Questions
Q1. What are the data types in Java? Java has two types of data
types:
1. Primitive Data Types: These include byte, short, int, long, float, double,
char, and boolean. They store simple values and are not objects.
2. Non-Primitive Data Types: These include String, Array, Class, and
Interface. They are derived from primitive data types and provide
more functionalities.
Q2. What are wrapper classes?
Wrapper classes provide an object representation of primitive data types,
such as Integers, Doubles, and Booleans. These classes allow primitives to be
used in collections and provide useful utility methods.
Q3. Are there dynamic arrays in Java?
Java arrays are fixed indynamically. Q4 size. However, ArrayList (from the
Java.util package) provides a dynamic array implementation where elements
can be added or removed dynamically.
Q4. What is JVM?
The Java Virtual Machine (JVM) is a part of the Java Runtime Environment
(JRE). It is responsible for executing Java bytecode by converting it into
machine code specific to the operating system.
Q5. Why is Java platform-independent?
Java achieves platform independence through bytecode. The Java compiler
converts code into bytecode, which the JVM interprets for the underlying OS,
making Java write-once, run-anywhere.
,Q6. What are local and global variables?
● Local variables are declared inside methods or blocks and are
accessible only within their scope.
● Global variables (also called instance variables) are declared within a
class but outside any method and have a wider scope.
Q7. What is data encapsulation?
Encapsulation is an OOP principle where data (variables) and code (methods)
are bundled into a single unit (class). It restricts direct access to data using
access modifiers (private, protected).
Q8. What is function overloading?
Function overloading allows multiple methods to have the same name but
different parameter lists. The compiler differentiates them based on the
number or type of parameters.
Example:
public class Figure
{
public int area(int a, int b)
{
int rectangleArea = a*b;
return rectangleArea;
}
public int area(int a)
{
int squareArea = a*a;
return squareArea;
}
public static void main(String[] args ){
Figure f = new Figure();
System.out.println("Area of square " + f.area(5));
System.out.println("Area of Rectangle " + f.area(5,3));
}
}
, Q9. What is function overriding?
Overriding allows a subclass to provide a specific implementation of a
method defined in its superclass. It enables dynamic method dispatch
(runtime polymorphism).
Q10. Why is the main method static in Java?
The main method is static so that it can be called without creating an
instance of the class, allowing the program to start execution without object
instantiation.
Q11. What is the difference between the throw and throws keywords
in Java?
Feature throw throws
Purpose Used to explicitly Declares that a
throw an exception method may throw an
exception
Usage throw new public void
Exception("Error") myMethod() throws
IOException
Number of Exceptions Can throw one Can declare
exception at a time multiple exceptions
using commas
Q12. What do you mean by singleton class?
A singleton class ensures that only one instance of the class exists throughout
the application's lifecycle. It is implemented using a private constructor, a
static instance variable, and a public static method that returns the single
instance. The most common way to create a singleton class is using the lazy
initialization or eager initialization approach.
Q13. Does every try block need a catch block?
No, a try block does not necessarily need a catch block. It can be followed by
either a catch block, a final block, or both. A catch block handles exceptions
that may arise in the try block, while a final block ensures that certain code