Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is the purpose of the 'super' keyword in Java?
The 'super' keyword in Java is used to refer to the immediate parent class object. It is often used to call the parent class's methods, access its fields, and invoke the parent class's constructor.
Example:
Example:
class Subclass extends Superclass {
void display() {
super.display(); // calls the display method of the parent class
}
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 2. What is the 'this' keyword in Java?
'this' refers to the current instance of the class. It is used to differentiate instance variables from local variables when they have the same name. 'this' is also used to invoke current class methods and constructors.
Example:
Example:
public class MyClass {
int x;
void setX(int x) {
this.x = x; // sets the instance variable x
}
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 3. Explain the concept of method overloading in Java.
Method overloading allows a class to have multiple methods with the same name but different parameters. The compiler determines which method to call based on the number and types of arguments passed.
Example:
Example:
class MathOperations {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 4. Explain the 'instanceof' operator in Java.
The 'instanceof' operator is used to test if an object is an instance of a particular class or interface. It returns a boolean value: 'true' if the object is an instance, and 'false' otherwise.
Example:
Example:
if (myObject instanceof MyClass) {
// do something
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 5. What is the purpose of the 'break' statement in Java?
The 'break' statement is used to terminate the loop or switch statement. It is often used to exit a loop prematurely when a certain condition is met.
Example:
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break; // exit the loop when i is 5
}
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 6. Explain the 'String' class and why it is immutable in Java.
The 'String' class in Java represents a sequence of characters. It is immutable to enhance performance and security. Once a 'String' object is created, its value cannot be changed. Any operation that appears to modify the 'String' actually creates a new 'String' object.
Example:
Example:
String str = 'Hello';
str = str.concat(' World'); // creates a new String object
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 7. What is the 'java.lang.Math' class in Java, and how is it used?
'java.lang.Math' is a utility class in Java that provides mathematical functions. It includes methods for basic arithmetic, trigonometry, logarithms, exponentiation, and more. The methods are static and can be used without creating an instance of the 'Math' class.
Example:
Example:
double result = Math.sqrt(25.0); // calculates the square root
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 8. What is the 'Java Virtual Machine (JVM)' and its role in Java?
The JVM is a virtual machine that enables a computer to run Java programs. It interprets Java bytecode and translates it into machine code for the host system. The JVM provides platform independence, as Java programs can run on any device that has a compatible JVM installed.
Example:
No specific example, as the JVM itself is not directly programmable.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Intermediate / 1 to 5 years experienced level questions & answers
Ques 9. What is the difference between an interface and an abstract class?
An abstract class can have both abstract and concrete methods, while an interface can only have abstract methods. In Java, a class can implement multiple interfaces but can extend only one abstract class.
Example:
Abstract class example:
abstract class Shape {
abstract void draw();
}
Interface example:
interface Shape {
void draw();
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 10. Explain the concept of multithreading in Java.
Multithreading in Java allows multiple threads to execute concurrently. It improves the performance and responsiveness of a program. The 'Thread' class and 'Runnable' interface are commonly used to create and manage threads in Java.
Example:
Example:
class MyThread extends Thread {
public void run() {
// thread execution logic
}
}
MyThread t1 = new MyThread();
t1.start();
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 11. Explain the 'try', 'catch', 'finally' blocks in Java Exception Handling.
'try' block contains the code that might throw an exception. 'catch' block handles the exception if it occurs. 'finally' block always executes, whether an exception is thrown or not. It is used for cleanup activities.
Example:
Example:
try {
// code that may throw an exception
}
catch (Exception e) {
// handle the exception
}
finally {
// cleanup code
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 12. What is the purpose of the 'static' keyword in Java?
'static' is used to create class-level variables and methods. It means the variable or method belongs to the class rather than a specific instance. 'static' members can be accessed using the class name.
Example:
Example:
class MyClass {
static int count;
static void incrementCount() {
count++;
}
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 13. What is the 'final' keyword in Java?
'final' is used to restrict the modification of classes, methods, and variables. A 'final' class cannot be inherited, a 'final' method cannot be overridden, and a 'final' variable cannot be reassigned after initialization.
Example:
Example:
public final class ImmutableClass {
// class definition
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 14. What is the difference between 'throw' and 'throws' in Java?
'throw' is used to explicitly throw an exception, while 'throws' is used in method signatures to declare the exceptions that the method might throw. Multiple exceptions can be declared using a comma-separated list in 'throws'.
Example:
Example:
void myMethod() throws CustomException {
// method implementation
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 15. Explain the concept of method overriding in Java.
Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. The overridden method in the subclass should have the same signature (name, return type, and parameters).
Example:
Example:
class Animal {
void makeSound() {
System.out.println('Generic Animal Sound');
}
}
class Dog extends Animal {
void makeSound() {
System.out.println('Bark');
}
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 16. Explain the concept of the 'equals()' and 'hashCode()' contract.
According to the contract, if two objects are equal (according to the 'equals()' method), their hash codes must be equal as well. However, the reverse is not necessarily true: two objects with equal hash codes may not be equal.
Example:
It's important to ensure that the 'equals()' and 'hashCode()' methods are consistently implemented to maintain this contract.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 17. What is the purpose of the 'default' method in Java interfaces?
The 'default' method in Java interfaces provides a default implementation for a method. It allows adding new methods to interfaces without breaking existing implementations.
Example:
Example:
interface MyInterface {
default void myMethod() {
// default implementation
}
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 18. Explain the 'Comparator' interface in Java.
The 'Comparator' interface is used to define custom ordering for objects. It provides two methods: 'compare()' to compare two objects and 'equals()' to check if two objects are equal. 'Comparator' is often used with sorting algorithms or data structures that require custom ordering.
Example:
Example:
class MyComparator implements Comparator{
public int compare(MyClass obj1, MyClass obj2) {
// custom comparison logic
}
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 19. Explain the 'super()' constructor in Java.
'super()' is used to invoke the constructor of the immediate parent class. It should be the first statement in the constructor of the subclass. If not explicitly called, the compiler inserts a 'super()' call by default.
Example:
Example:
class Subclass extends Superclass {
Subclass() {
super(); // invokes the constructor of the parent class
}
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 20. Explain the 'try-with-resources' statement in Java.
The 'try-with-resources' statement is used to automatically close resources (like files or sockets) at the end of the try block. Resources must implement the 'AutoCloseable' interface. It simplifies resource management and reduces the chances of resource leaks.
Example:
Example:
try (BufferedReader br = new BufferedReader(new FileReader('file.txt'))) {
// code that uses 'br'
} catch (IOException e) {
// handle the exception
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 21. Explain the 'java.util.Collections' class in Java.
'java.util.Collections' is a utility class that provides static methods for operating on collections (e.g., lists, sets, and maps). It includes methods for sorting, shuffling, searching, and synchronizing collections.
Example:
Example:
ListmyList = new ArrayList<>();
Collections.addAll(myList, 'Java', 'Python', 'C++');
Collections.sort(myList); // sorts the list
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 22. Explain the concept of 'immutable objects' in Java.
Immutable objects are objects whose state cannot be modified after they are created. Once an immutable object is created, its state remains constant throughout its lifetime. String, Integer, and BigDecimal are examples of immutable classes in Java.
Example:
Example:
final class ImmutableClass {
private final int value;
public ImmutableClass(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Experienced / Expert level questions & answers
Ques 23. What is the difference between '==', 'equals()', and 'hashCode()' methods in Java?
'==' is used to compare object references, 'equals()' is used to compare object content, and 'hashCode()' returns the hash code value of an object. It is recommended to override 'equals()' and 'hashCode()' when dealing with custom classes.
Example:
Example:
@Override
public boolean equals(Object obj) {
// custom implementation
}
@Override
public int hashCode() {
// custom implementation
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 24. Explain the concept of garbage collection in Java.
Garbage collection in Java automatically reclaims the memory occupied by objects that are no longer reachable. The 'java.lang.System.gc()' method and the 'finalize()' method are related to garbage collection.
Example:
No specific example, as garbage collection is a background process handled by the JVM.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 25. What is the purpose of the 'transient' keyword in Java?
'transient' is used to indicate that a variable should not be serialized during object serialization. The value of a transient variable is not persisted when the object is stored.
Example:
Example:
class MyClass implements Serializable {
transient int sensitiveData;
}
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 26. What is the 'volatile' keyword in Java?
'volatile' is used to indicate that a variable's value may be changed by multiple threads simultaneously. It ensures that the variable is always read from and written to the main memory, avoiding thread-local caching.
Example:
Example:
volatile boolean flag = true;
// variable shared among multiple threads
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 27. What is the purpose of the 'assert' statement in Java?
The 'assert' statement is used for debugging purposes to check if a given boolean expression is true. If it's false, an 'AssertionError' is thrown. 'assert' statements can be enabled or disabled during runtime using the '-ea' or '-da' JVM options.
Example:
Example:
int x = -1;
assert x >= 0 : 'x should be non-negative';
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 28. What is the 'classpath' in Java, and how is it set?
The 'classpath' is a parameter in the Java Virtual Machine (JVM) that specifies the location of user-defined classes and packages. It can be set using the '-classpath' or '-cp' option when running Java applications. It includes directories, JAR files, and ZIP archives containing Java classes.
Example:
Example:
java -cp myapp.jar com.example.MyClass
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 29. What is the 'NaN' value in Java, and how is it represented?
'NaN' (Not a Number) is a special floating-point value used to represent undefined or unrepresentable results of mathematical operations. It is typically the result of operations like 0.0/0.0 or Math.sqrt(-1). 'NaN' is represented using the 'Double.NaN' constant.
Example:
Example:
double result = 0.0 / 0.0; // results in NaN
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 30. What is the purpose of the 'java.util.concurrent' package in Java?
The 'java.util.concurrent' package in Java provides a framework for concurrent programming. It includes classes and interfaces for thread management, concurrent collections, synchronization utilities, and high-level concurrency abstractions.
Example:
Example:
ExecutorService executor = Executors.newFixedThreadPool(5);
executor.submit(() -> System.out.println('Hello, concurrent world!'));
executor.shutdown();
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Most helpful rated by users: