Java Interview Preparation Guide
Introduction to Java
Java is a high-level, class-based, object-oriented programming language developed by Sun
Microsystems in 1995. It is platform-independent because of the JVM (Java Virtual Machine). Java
is used in web development, mobile apps (Android), enterprise systems, and more.
Key Features:
- Platform Independent
- Secure and Robust
- Multithreaded
- Portable
- High Performance
Core OOP Concepts
1. Encapsulation - Wrapping data and methods into a single unit called class.
2. Inheritance - Creating new classes from existing ones to promote reusability.
3. Polymorphism - Performing tasks in multiple ways (method overloading and overriding).
4. Abstraction - Hiding implementation details and showing only the necessary features.
Exception Handling
Exception handling in Java is done using try, catch, throw, throws, and finally keywords.
Types of Exceptions:
- Checked Exception: Checked at compile-time (IOException, SQLException).
- Unchecked Exception: Occur at runtime (NullPointerException, ArithmeticException).
- Errors: Serious issues not handled by programs (OutOfMemoryError).
Example:
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Execution Completed");
}
Collections Framework
Collections in Java are used to store and manipulate groups of objects.
Important interfaces:
- List: Ordered collection (ArrayList, LinkedList).
- Set: Unique elements (HashSet, TreeSet).
- Map: Key-value pairs (HashMap, TreeMap).
- Queue: FIFO structure (PriorityQueue, LinkedList).
Example:
List names = new ArrayList<>();
names.add("Ashish");
names.add("Rahul");
System.out.println(names);
Multithreading
Multithreading is a feature of Java that allows concurrent execution of two or more threads.
Threads can be created by:
1. Extending the Thread class.
2. Implementing the Runnable interface.
Example:
class MyThread extends Thread {
public void run() {
System.out.println("Thread running");
}
}
public class Demo {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start();
}
}
50+ Interview Questions with Answers
1. What are the differences between JDK, JRE, and JVM?
Answer: JVM is the runtime environment, JRE includes JVM + libraries, JDK includes JRE +
development tools.
2. Difference between abstract class and interface?
Answer: Abstract classes can have concrete methods, interfaces cannot (until Java 8 default
methods).
3. Explain garbage collection in Java.
Answer: Automatic memory management process that removes unused objects from memory.
4. Difference between HashMap and HashTable?
Answer: HashMap is not synchronized, allows null keys and values. HashTable is synchronized
and does not allow nulls.
...
50. What are lambda expressions?
Answer: Introduced in Java 8, lambda expressions enable functional programming by providing a
clear and concise way to represent a method using syntax (parameters) -> expression.
MCQs with Answers
1. Which of these is not a Java feature?
A) Object-oriented B) Use of pointers C) Portable D) Dynamic
Answer: B
2. Which package contains the Random class?
A) java.util B) java.lang C) java.io D) java.net
Answer: A
3. Which keyword is used to inherit a class in Java?
A) super B) this C) extends D) implement
Answer: C
...
20. What is the size of int in Java?
A) 16-bit B) 32-bit C) 64-bit D) Platform dependent
Answer: B
Practical Coding Questions with Solutions
1. Write a Java program to check if a number is prime.
public class PrimeCheck {
public static void main(String[] args) {
int num = 29;
boolean flag = true;
for(int i = 2; i <= num/2; i++){
if(num % i == 0){
flag = false;
break;
}
}
if(flag) System.out.println(num + " is Prime");
else System.out.println(num + " is not Prime");
}
}
2. Reverse a string using recursion.
...
5. Implement factorial using recursion.
Java Cheat Sheet
Data Types:
int, float, double, char, boolean, String
Access Modifiers:
public, private, protected, default
Loops:
for, while, do-while
Keywords:
static, final, abstract, synchronized, volatile
Tips for Cracking Java Interviews
1. Focus on core Java concepts and OOP principles.
2. Practice coding problems on arrays, strings, and collections.
3. Be clear about JVM internals and memory management.
4. Revise exception handling and multithreading.
5. Mock interviews are essential to improve confidence.