0% found this document useful (0 votes)
27 views

JAVA Interview Questions

JAVA Interview questions
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

JAVA Interview Questions

JAVA Interview questions
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Core Java Concepts

1. What is Java?

Java is a high-level, object-oriented programming language known for its platform


independence, achieved through the Java Virtual Machine (JVM), allowing code to run on
any device with a JVM installed.

2. Explain the main features of Java.

Java is object-oriented, platform-independent, secure, robust (garbage collection and


exception handling), and multi-threaded.

3. What is JVM, JRE, and JDK?

 JVM (Java Virtual Machine): Runs Java bytecode, enabling platform independence.
 JRE (Java Runtime Environment): Includes JVM and libraries required to run Java
applications.
 JDK (Java Development Kit): Contains JRE and development tools like the
compiler (javac) and debugger.

Object-Oriented Programming (OOP)


4. Explain the four pillars of OOP.

1. Encapsulation: Wrapping data (fields) and methods in a class, with access control.
2. Inheritance: Enabling a new class to inherit properties and behavior from an existing
class.
3. Polymorphism: Allowing methods to do different things based on the object they’re
acting upon (method overriding and overloading).
4. Abstraction: Hiding implementation details, exposing only essential features using
abstract classes and interfaces.

5. What is the difference between overloading and overriding?

 Overloading: Multiple methods with the same name but different parameters within a
class.
 Overriding: A subclass provides a specific implementation of a method defined in its
superclass.
6. What is an abstract class, and how is it different from an interface?

 Abstract Class: Cannot be instantiated, may have both abstract and concrete
methods.
 Interface: Declares a set of abstract methods (Java 8+ allows default and static
methods in interfaces). It’s implemented by classes that promise to provide specific
behavior.

Memory Management
7. Explain garbage collection in Java.

Garbage collection is the process by which Java removes unreferenced objects from memory
automatically to free up space. This is managed by the JVM.

8. What are final, finally, and finalize in Java?

 final: Keyword to restrict a variable (constant), method (no overriding), or class (no
inheritance).
 finally: A block in exception handling that always executes, even if an exception is
thrown.
 finalize(): A method called by the garbage collector before an object is removed
from memory (deprecated in Java 9+).

Exception Handling
9. What is an exception, and what are the types of exceptions in Java?

An exception is an abnormal event that disrupts the program's normal flow. Java has:

 Checked Exceptions: Exceptions checked at compile time (e.g., IOException,


SQLException).
 Unchecked Exceptions: Runtime exceptions (e.g., NullPointerException,
ArrayIndexOutOfBoundsException).
 Errors: Serious issues beyond the programmer’s control (e.g., OutOfMemoryError).

10. How does the try-catch-finally block work?

 try: Code that might throw an exception is enclosed here.


 catch: Handles specific exceptions thrown in the try block.
 finally: Executes whether or not an exception is thrown (often used to release
resources).
Java Collections Framework
11. What is the Java Collections Framework?

The Collections Framework provides a set of classes and interfaces for working with groups
of objects (data structures like List, Set, Map).

12. Difference between List, Set, and Map.

 List: Ordered collection, allows duplicates (e.g., ArrayList, LinkedList).


 Set: Unordered collection, no duplicates (e.g., HashSet, TreeSet).
 Map: Key-value pairs, unique keys (e.g., HashMap, TreeMap).

13. How does HashMap work?

HashMap stores entries (key-value pairs) in an array of linked lists. Each key’s hash code
determines the array index, with equals() used to resolve hash collisions.

Multithreading and Concurrency


14. What is a thread in Java?

A thread is a lightweight process that enables concurrent execution within a program. Java
threads are managed by the Thread class and the Runnable interface.

15. Explain the life cycle of a thread.

A thread goes through states: New → Runnable → Blocked → Waiting → Timed Waiting
→ Terminated.

16. How do synchronized and volatile work in Java?

 synchronized: Ensures that only one thread accesses a synchronized block or


method at a time.
 volatile: Tells the JVM to read the variable’s value directly from main memory,
ensuring visibility across threads.
Java 8 Features
17. What are lambda expressions in Java 8?

Lambda expressions are a concise way to represent functional interfaces (interfaces with one
abstract method), enabling you to write inline implementations.

java
Copy code
Runnable r = () -> System.out.println("Lambda example");

18. What is the purpose of Stream API?

The Stream API provides methods for processing sequences of elements (like lists) with
operations such as filter, map, reduce, and collect.

java
Copy code
List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
numbers.stream().filter(x -> x % 2 == 0).forEach(System.out::println);

19. What are default and static methods in interfaces?

Java 8 introduced default methods (methods with a body) and static methods in interfaces to
provide additional functionality without breaking existing implementations.

Memory Model and Performance


20. Explain the Java Memory Model (JMM).

The JMM defines how Java threads interact through memory, outlining rules for reading and
writing variables to ensure consistent visibility across threads.

21. What is the difference between stack and heap memory in Java?

 Stack Memory: Stores method calls, local variables, and references; faster, managed
per thread.
 Heap Memory: Stores objects and class variables; shared among threads, managed
by the garbage collector.
Advanced Topics
22. What is the difference between == and equals()?

 == checks if two references point to the same memory location.


 equals() checks if two objects are logically equal (often overridden in classes).

23. What is Serializable in Java?

Serializable is a marker interface that allows objects to be converted to a byte stream,


making it possible to save and transfer objects.

24. Explain Enum in Java.

Enums define a fixed set of constants. They can have methods, constructors, and implement
interfaces.

java
Copy code
public enum Day {
MONDAY, TUESDAY, WEDNESDAY;
}

25. What is reflection in Java?

Reflection allows Java code to inspect and modify the runtime behavior of applications (e.g.,
accessing private methods), often used in frameworks like Spring.

26. What is the purpose of a ClassLoader?

A ClassLoader loads classes into memory at runtime. Java has different loaders, such as the
Bootstrap, Extension, and Application ClassLoader.

Popular Coding Questions


27. Reverse a String in Java.
java
Copy code
public static String reverse(String str) {
return new StringBuilder(str).reverse().toString();
}

28. Check if a number is a palindrome.


java
Copy code
public static boolean isPalindrome(int num) {
int original = num, reversed = 0;
while (num > 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
return original == reversed;
}

29. Find the factorial of a number.


java
Copy code
public static int factorial(int n) {
return (n == 0) ? 1 : n * factorial(n - 1);
}

These questions provide a strong foundation across various Java topics and give you hands-
on coding practice with core concepts likely to be covered in interviews. Good luck!

You might also like