Core Java Interview Questions
1. What are the features of Java?
Platform Independent
Object-Oriented
Secure
Robust
Multithreaded
High Performance (JIT Compiler)
Distributed
2. What is JVM?
JVM (Java Virtual Machine) executes Java bytecode and makes Java platform independent.
Flow:
.java → Compiler → .class (Bytecode) → JVM → Output
3. Difference between JDK, JRE, and JVM?
JDK JRE JVM
Development Kit Runtime Environment Executes Bytecode
Contains JRE + Tools Contains JVM + Libraries Part of JRE
Used for Development Used for Running Apps Executes Program
4. What is OOPs?
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
5. What is Encapsulation?
Binding data and methods into a single unit.
class Employee{
private int id;
public int getId() {
return id;
}
public void setId(int id) {
[Link] = id;
}
}
6. What is Inheritance?
One class acquires properties of another class.
class Parent{
void display(){
[Link]("Parent");
}
}
class Child extends Parent{
}
7. What is Polymorphism?
One method behaves differently.
Method Overloading
add(int a, int b)
add(int a, int b, int c)
Method Overriding
class Parent{
void show(){}
}
class Child extends Parent{
void show(){}
}
8. What is Abstraction?
Hiding implementation details and showing functionality.
abstract class Vehicle{
abstract void start();
}
9. Difference between Abstract Class and Interface?
Abstract Class Interface
Can have concrete methods Mostly abstract methods
Supports constructors No constructors
Single inheritance Multiple inheritance
Uses abstract keyword Uses interface keyword
10. What is Constructor?
Special method invoked automatically when object is created.
class Test{
Test(){
[Link]("Constructor");
}
}
11. Types of Constructors?
Default Constructor
Parameterized Constructor
12. Difference between this and super?
this super
Current Class Parent Class
Current object reference Parent object reference
13. Difference between == and equals()?
String s1="Java";
String s2="Java";
==
Compares references
equals()
Compares content
14. String vs StringBuffer vs StringBuilder?
String StringBuffer StringBuilder
Immutable Mutable Mutable
Slow Thread Safe Faster
Less Performance Medium High
15. Why String is Immutable?
Security
Thread Safety
Performance
String Pool Optimization
Collection Framework
16. Difference between List and Set?
List Set
Allows duplicates No duplicates
Maintains insertion order Not guaranteed
17. ArrayList vs LinkedList?
ArrayList LinkedList
Dynamic Array Doubly Linked List
Fast Retrieval Fast Insertion/Deletion
More used in automation Less used
18. HashMap vs Hashtable?
HashMap Hashtable
Not Thread Safe Thread Safe
Allows Null No Null
Faster Slower
19. Difference between HashMap and LinkedHashMap?
HashMap LinkedHashMap
No Order Maintains Insertion Order
20. Difference between HashSet and TreeSet?
HashSet TreeSet
Unordered Sorted
Faster Slower
21. What is Iterator?
Used to traverse collection elements.
Iterator<String> itr=[Link]();
while([Link]()){
[Link]([Link]());
}
Exception Handling
22. What is Exception?
Unexpected event occurring during program execution.
23. Checked vs Unchecked Exception?
Checked
IOException
SQLException
Unchecked
NullPointerException
ArrayIndexOutOfBoundsException
ArithmeticException
24. Difference between throw and throws?
throw throws
Used to explicitly throw exception Declares exception
Inside method Method signature
25. Can we have multiple catch blocks?
Yes.
try{
}
catch(IOException e){
}
catch(Exception e){
}
Java 8 Interview Questions
26. What is Lambda Expression?
(a,b) -> a+b
Used to reduce code.
27. What is Functional Interface?
Interface containing only one abstract method.
Example:
Runnable
Comparator
28. What is Stream API?
Used for processing collections.
[Link]()
.filter(x -> [Link]("A"))
.forEach([Link]::println);
29. Difference between map() and filter()?
filter()
Filters data
map()
Transforms data
[Link]()
.filter(x->x>10)
.map(x->x*2);
30. What is Optional Class?
Avoids NullPointerException.
Optional<String> name =
[Link](value);
Multithreading
31. What is Thread?
Lightweight sub-process.
32. Ways to Create Thread?
Extending Thread
class Test extends Thread{
public void run(){}
}
Implementing Runnable
class Test implements Runnable{
public void run(){}
}
33. Difference between start() and run()?
start() run()
Creates New Thread Normal Method Call
Concurrent Execution Sequential Execution
Frequently Asked for Automation Test
Engineers
34. Why use HashMap in Selenium?
Store test data
Store locators
Key-value pair management
35. Why use Collections in Framework?
Store dynamic test data
Read Excel/JSON data
Handle multiple elements
36. Why use Java 8 in Automation?
Lambda Expressions
Stream API
Functional Interfaces
Better Collection Handling
37. What is Immutable Class?
A class whose object cannot be modified after creation.
Example:
String
38. Difference between final, finally, and finalize?
final finally finalize
Keyword Block Method
Prevent inheritance/change Executes always Garbage Collection
39. What is Method Overloading?
Same method name, different parameters.
add(int a,int b)
add(int a,int b,int c)
40. What is Method Overriding?
Same method signature in parent and child class.
@Override
public void show(){