shape image

Top 30 Core Java Interview Questions and Answers


1. What is Java?

Java is a high-level, object-oriented programming language known for its platform independence, allowing programs to run on any system with a Java Virtual Machine (JVM).

2. What is the difference between JDK, JRE, and JVM?

JDK (Java Development Kit) includes tools for developing Java applications, JRE (Java Runtime Environment) provides the runtime environment for executing Java programs, and JVM (Java Virtual Machine) is responsible for executing Java bytecode. 

3. Explain the main principles of Object-Oriented Programming (OOP)?

OOP principles include encapsulation, inheritance, and polymorphism. Encapsulation hides implementation details, inheritance allows creating new classes based on existing ones, and polymorphism enables objects of different classes to be treated as objects of a common superclass.

4. What is the difference between a class and an object?

A class is a blueprint or template for creating objects, while an object is an instance of a class.

5. What is a constructor?

A constructor is a special method used to initialize objects. It has the same name as the class and doesn't have a return type.

6. What is method overloading?

Method overloading is when multiple methods in the same class have the same name but different parameter lists.

7. What is method overriding?

Method overriding is when a subclass provides a specific implementation for a method that is already defined in its superclass.

8. What is the 'final' keyword used for?

The 'final' keyword is used to declare a variable, method, or class as unchangeable. For example, a 'final' class cannot be subclassed.

9. What is the 'static' keyword used for?

The 'static' keyword is used to create class-level variables and methods that can be accessed without creating an instance of the class.

10. What is the difference between '==', 'equals()', and 'hashCode()' methods?

'==' compares object references, 'equals()' compares object content, and 'hashCode()' returns a hash code for an object, commonly used in hash-based data structures.

11. Explain the concept of Inheritance.

Inheritance is a mechanism by which a new class (subclass/derived class) can inherit properties and behaviors from an existing class (superclass/base class).

12. What is an abstract class?

An abstract class is a class that cannot be instantiated and is meant to be subclassed. It can contain both abstract and concrete methods.

13. What is an interface?

An interface is a collection of abstract methods that define a contract for classes implementing it. Java allows multiple inheritance through interfaces.

14. What is the 'super' keyword used for?

The 'super' keyword is used to refer to the superclass, to access its methods or variables, or to invoke its constructor.

15. What is a package in Java?

A package is a way to organize related classes and interfaces. It helps in avoiding naming conflicts and provides better modularity.

16. What is the 'this' keyword used for?

The 'this' keyword refers to the current instance of a class. It can be used to access instance variables and methods.

17. What is a static initializer block?

A static initializer block is used to initialize static variables of a class. It is executed when the class is loaded.

18. What is a try-catch block?

A try-catch block is used for exception handling. Code inside the 'try' block is executed, and if an exception occurs, the appropriate 'catch' block is executed.

19. What are checked and unchecked exceptions?

Checked exceptions are checked at compile-time and must be either caught using 'try-catch' or declared using 'throws'. Unchecked exceptions (runtime exceptions) don't require explicit handling.

20. What is the purpose of the 'finally' block?

The 'finally' block contains code that will be executed regardless of whether an exception is thrown or caught. It is often used for cleanup operations.

21. What is the Java Collections Framework?

The Java Collections Framework provides classes and interfaces for implementing and manipulating collections (lists, sets, maps, etc.) of objects.

22. Explain the difference between ArrayList and LinkedList.

ArrayList is implemented as a dynamic array, while LinkedList is implemented as a doubly-linked list. ArrayList provides better random access, while LinkedList is more efficient for insertion and deletion operations.

23. What is a thread?

A thread is a lightweight sub-process that shares resources of its parent process, allowing concurrent execution of multiple tasks.

24. How can you create and start a thread in Java?

You can create a thread by extending the 'Thread' class or implementing the 'Runnable' interface. To start a thread, call the 'start()' method.

25. What is synchronization?

Synchronization is a mechanism used to control access to shared resources by multiple threads to avoid data inconsistency and thread interference.

26. What is the 'volatile' keyword used for?

The 'volatile' keyword is used to declare a variable as not being cached by threads, ensuring that changes made by one thread are visible to other threads.

27. What is a deadlock? How can it be avoided?

A deadlock occurs when two or more threads are blocked, each waiting for a resource held by the other. Deadlocks can be avoided by ensuring a consistent ordering of resource acquisition or by using timeouts.

28. Explain the concept of garbage collection.

Garbage collection is the process of automatically identifying and reclaiming memory occupied by objects that are no longer referenced by the program, preventing memory leaks.

29. What is reflection in Java?

Reflection allows a Java program to inspect and manipulate classes, methods, fields, and other components at runtime, providing a way to perform dynamic operations.

30. Write a Java program to find the factorial of a given number using recursion.

public class FactorialCalculator {
    public static void main(String[] args) {
        int number = 5;
        long factorial = calculateFactorial(number);
        System.out.println("Factorial of " + number + " is: " + factorial);
    }

    public static long calculateFactorial(int n) {
        if (n <= 1) {
            return 1;
        } else {
            return n * calculateFactorial(n - 1);
        }
    }
}

Interview Questions and Answers

Top 30 Core Java Interview Questions and Answers

 

1. What is Java?

Java is a high-level, object-oriented programming language known for its platform independence, allowing programs to run on any system with a Java Virtual Machine (JVM).

 

2. What is the difference between JDK, JRE, and JVM?

JDK (Java Development Kit) includes tools for developing Java applications, JRE (Java Runtime Environment) provides the runtime environment for executing Java programs, and JVM (Java Virtual Machine) is responsible for executing Java bytecode.

 

3. Explain the main principles of Object-Oriented Programming (OOP)?

OOP principles include encapsulation, inheritance, and polymorphism. Encapsulation hides implementation details, inheritance allows creating new classes based on existing ones, and polymorphism enables objects of different classes to be treated as objects of a common superclass.

 

4. What is the difference between a class and an object?

A class is a blueprint or template for creating objects, while an object is an instance of a class.

 

5. What is a constructor?

A constructor is a special method used to initialize objects. It has the same name as the class and doesn't have a return type.

 

6. What is method overloading?

Method overloading is when multiple methods in the same class have the same name but different parameter lists.

 

7. What is method overriding?

Method overriding is when a subclass provides a specific implementation for a method that is already defined in its superclass.

 

8. What is the 'final' keyword used for?

The 'final' keyword is used to declare a variable, method, or class as unchangeable. For example, a 'final' class cannot be subclassed.

 

9. What is the 'static' keyword used for?

The 'static' keyword is used to create class-level variables and methods that can be accessed without creating an instance of the class.

 

10. What is the difference between '==', 'equals()', and 'hashCode()' methods?

'==' compares object references, 'equals()' compares object content, and 'hashCode()' returns a hash code for an object, commonly used in hash-based data structures.

 

11. Explain the concept of Inheritance.

Inheritance is a mechanism by which a new class (subclass/derived class) can inherit properties and behaviors from an existing class (superclass/base class).

 

12. What is an abstract class?

An abstract class is a class that cannot be instantiated and is meant to be subclassed. It can contain both abstract and concrete methods.

 

13. What is an interface?

An interface is a collection of abstract methods that define a contract for classes implementing it. Java allows multiple inheritance through interfaces.

 

14. What is the 'super' keyword used for?

The 'super' keyword is used to refer to the superclass, to access its methods or variables, or to invoke its constructor.

 

15. What is a package in Java?

A package is a way to organize related classes and interfaces. It helps in avoiding naming conflicts and provides better modularity.

 

16. What is the 'this' keyword used for?

The 'this' keyword refers to the current instance of a class. It can be used to access instance variables and methods.

 

17. What is a static initializer block?

A static initializer block is used to initialize static variables of a class. It is executed when the class is loaded.

 

18. What is a try-catch block?

A try-catch block is used for exception handling. Code inside the 'try' block is executed, and if an exception occurs, the appropriate 'catch' block is executed.

 

19. What are checked and unchecked exceptions?

Checked exceptions are checked at compile-time and must be either caught using 'try-catch' or declared using 'throws'. Unchecked exceptions (runtime exceptions) don't require explicit handling.

 

20. What is the purpose of the 'finally' block?

The 'finally' block contains code that will be executed regardless of whether an exception is thrown or caught. It is often used for cleanup operations.

 

21. What is the Java Collections Framework?

The Java Collections Framework provides classes and interfaces for implementing and manipulating collections (lists, sets, maps, etc.) of objects.

 

22. Explain the difference between ArrayList and LinkedList.

ArrayList is implemented as a dynamic array, while LinkedList is implemented as a doubly-linked list. ArrayList provides better random access, while LinkedList is more efficient for insertion and deletion operations.

 

23. What is a thread?

A thread is a lightweight sub-process that shares resources of its parent process, allowing concurrent execution of multiple tasks.

 

24. How can you create and start a thread in Java?

You can create a thread by extending the 'Thread' class or implementing the 'Runnable' interface. To start a thread, call the 'start()' method.

 

25. What is synchronization?

Synchronization is a mechanism used to control access to shared resources by multiple threads to avoid data inconsistency and thread interference.

 

26. What is the 'volatile' keyword used for?

The 'volatile' keyword is used to declare a variable as not being cached by threads, ensuring that changes made by one thread are visible to other threads.

 

27. What is a deadlock? How can it be avoided?

A deadlock occurs when two or more threads are blocked, each waiting for a resource held by the other. Deadlocks can be avoided by ensuring a consistent ordering of resource acquisition or by using timeouts.

 

28. Explain the concept of garbage collection.

Garbage collection is the process of automatically identifying and reclaiming memory occupied by objects that are no longer referenced by the program, preventing memory leaks.

 

29. What is reflection in Java?

Reflection allows a Java program to inspect and manipulate classes, methods, fields, and other components at runtime, providing a way to perform dynamic operations.

 

30. Write a Java program to find the factorial of a given number using recursion.

 


Copyright © Baghel Studio | All Right Reserved