Core Java
JAVA CORE — COMPLETE CGI INTERVIEW QUESTIONS + ANSWERS
1. What is Java?
Answer
“Java is a high-level, object-oriented, platform-independent programming language developed by
Sun Microsystems. It follows the principle of Write Once Run Anywhere using JVM.”
2. Why is Java Platform Independent?
Answer
“Java is platform independent because Java code is compiled into bytecode, which runs on JVM. JVM
converts bytecode into machine code according to the operating system.”
3. What is JVM?
Answer
“JVM stands for Java Virtual Machine. It executes Java bytecode and provides platform
independence.”
4. What is JRE?
Answer
“JRE stands for Java Runtime Environment. It contains JVM and required libraries to run Java
applications.”
5. What is JDK?
Answer
“JDK stands for Java Development Kit. It contains JRE along with development tools like compiler and
debugger.”
6. Difference Between JVM, JRE, and JDK
Answer
JVM → Executes bytecode
JRE → Provides environment to run Java programs
JDK → Provides tools to develop Java programs
7. What are Features of Java?
Answer
Platform independent
Object-oriented
Secure
Robust
Multithreaded
Portable
High performance
8. What is OOP?
Answer
“OOP stands for Object Oriented Programming. It is based on objects and classes. Main concepts are
encapsulation, inheritance, polymorphism, and abstraction.”
9. What is a Class?
Answer
“A class is a blueprint or template used to create objects.”
10. What is an Object?
Answer
“An object is an instance of a class.”
11. What is Constructor?
Answer
“A constructor is a special method used to initialize objects. It has same name as class and no return
type.”
12. Types of Constructors
Answer
Default constructor
Parameterized constructor
13. Constructor vs Method
Answer
Constructor initializes object
Method performs operations
Constructor has no return type
14. What is Inheritance?
Answer
“Inheritance allows one class to acquire properties and methods of another class.”
15. Types of Inheritance in Java
Answer
Single
Multilevel
Hierarchical
(Java does not support multiple inheritance through classes.)
16. What is Polymorphism?
Answer
“Polymorphism means one object behaving in multiple forms.”
17. Types of Polymorphism
Answer
Compile-time polymorphism (method overloading)
Runtime polymorphism (method overriding)
18. What is Method Overloading?
Answer
“Method overloading means same method name with different parameters in same class.”
19. What is Method Overriding?
Answer
“Method overriding means redefining parent class method in child class.”
20. Difference Between Overloading and Overriding
Answer
Overloading Overriding
Same class Parent-child class
Different parameters Same parameters
Compile-time Runtime
21. What is Abstraction?
Answer
“Abstraction means hiding implementation details and showing only essential functionality.”
22. What is Encapsulation?
Answer
“Encapsulation means wrapping data and methods together into a single unit.”
23. What is Interface?
Answer
“Interface is used to achieve abstraction and multiple inheritance in Java.”
24. What is Abstract Class?
Answer
“Abstract class is a class that cannot be instantiated and may contain abstract and concrete
methods.”
25. Difference Between Interface and Abstract Class
Answer
Interface Abstract Class
Supports multiple inheritance Does not fully support
Only abstract methods earlier Can contain concrete methods
No constructor Can have constructor
26. What is this Keyword?
Answer
“this keyword refers to current object of the class.”
27. What is super Keyword?
Answer
“super keyword refers to parent class object.”
28. What is Static Keyword?
Answer
“static keyword makes variable or method belong to class instead of object.”
29. What is Final Keyword?
The final keyword in Java is a non-access modifier used to restrict further modification of a class,
method, or variable
Answer
final variable → cannot change value
final method → cannot override
final class → cannot inherit
30. What is String?
Answer
“String is a sequence of characters in Java.”
31. Why String is Immutable?
Answer
“String is immutable for security, synchronization, and memory optimization.”
32. Difference Between String and StringBuilder
Answer
String StringBuilder
Immutable Mutable
Slow for modifications Faster for modifications
33. Difference Between == and equals()
Answer
== compares references
equals() compares values/content
34. What is Exception?
Answer
“Exception is an unwanted event that interrupts normal program execution.”
35. What is Exception Handling?
Answer
“Exception handling manages runtime errors using try, catch, finally, throw, and throws.”
36. Difference Between Checked and Unchecked Exceptions
Answer
Checked Unchecked
Compile-time Runtime
Checked Unchecked
IOException NullPointerException
37. What is try-catch-finally?
Answer
try → risky code
catch → handles exception
finally → always executes
38. Difference Between throw and throws
Answer
throw → used to explicitly throw exception
throws → declares exceptions
39. What is Multithreading?
Answer
“Multithreading allows multiple threads to execute simultaneously.”
40. What is Thread?
Answer
“Thread is a lightweight subprocess.”
41. Ways to Create Thread
Answer
Extending Thread class
Implementing Runnable interface
42. What is Synchronization?
Answer
“Synchronization prevents multiple threads from accessing shared resources simultaneously.”
43. What is Collection Framework?
Answer
“Collection framework provides classes and interfaces to store and manipulate groups of objects.”
44. Difference Between Array and ArrayList
Answer
Array ArrayList
Fixed size Dynamic size
Stores primitive Stores objects
45. What is ArrayList?
Answer
“ArrayList is a dynamic array implementation in Java.”
46. Difference Between ArrayList and LinkedList
Answer
ArrayList LinkedList
Faster retrieval Faster insertion/deletion
Uses dynamic array Uses doubly linked list
47. What is HashMap?
Answer
“HashMap stores key-value pairs using hashing.”
48. How HashMap Works Internally?
Answer
“HashMap uses hashCode() to calculate bucket index and stores data in buckets.”
49. Difference Between HashMap and Hashtable
Answer
HashMap Hashtable
Not synchronized Synchronized
Allows null No null allowed
50. What is HashSet?
Answer
“HashSet stores unique unordered elements.”
51. What is TreeSet?
Answer
“TreeSet stores unique sorted elements.”
52. Difference Between List and Set
Answer
List Set
Allows duplicates No duplicates
Ordered Mostly unordered
53. What is Comparable?
Answer
“Comparable is used for default sorting using compareTo() method.”
54. What is Comparator?
Answer
“Comparator is used for custom sorting using compare() method.”
55. Difference Between Comparable and Comparator
Answer
Comparable Comparator
compareTo() compare()
Default sorting Custom sorting
56. What is Garbage Collection?
Answer
“Garbage Collection automatically removes unused objects from memory.”
57. What is Wrapper Class?
Answer
“Wrapper classes convert primitive data types into objects.”
Examples:
int → Integer
char → Character
58. What is Autoboxing and Unboxing?
Answer
Autoboxing → primitive to object
Unboxing → object to primitive
59. What is Package?
Answer
“Package is a collection of related classes and interfaces.”
60. Access Modifiers in Java
Access modifiers in Java are keywords that set the visibility and accessibility of classes, methods,
constructors, and variables.
Answer
private
Most restrictive level.
Accessible only within the declared class. [1, 2]
default (No keyword)
Applied automatically if no keyword is written.
Accessible only by classes in the same package. [1, 2, 3, 4]
protected
Accessible within the same package.
Accessible by child subclasses located in different packages. [1]
public
Least restrictive level.
Accessible from any class anywhere in the application
VERY HIGH PROBABILITY CGI FOLLOW-UP QUESTIONS
Why String immutable?
Why multiple inheritance not supported?
Why use interface?
Why use StringBuilder?
Why HashMap fast?
Difference between compile-time and runtime polymorphism?
Why synchronization needed?
JAVA CORE — VERY IMPORTANT FOLLOW-UP QUESTIONS + ANSWERS (CGI STYLE)
1. Why is String Immutable?
Answer
“String is immutable for security, thread safety, and memory optimization using String pool.”
Follow-Up
What is String Pool?
Answer
“String pool is a memory area in heap where Java stores string literals to reuse memory.”
Follow-Up
Why StringBuilder Faster Than String?
Answer
“String creates new objects on every modification because it is immutable, while StringBuilder
modifies same object.”
2. Why Multiple Inheritance Not Supported in Java?
Answer
“Java does not support multiple inheritance using classes to avoid ambiguity problems like Diamond
Problem.”
The diamond problem is an ambiguity issue that arises in object-oriented programming languages
when a class inherits from two different parent classes that both define a method with the exact
same signature
Follow-Up
Then How Java Achieves Multiple Inheritance?
Answer
“Java achieves multiple inheritance using interfaces.”
An interface is a completely abstract type in object-oriented programming that acts as a blueprint for
a class.
3. Why Use Interface?
Answer
“Interfaces provide abstraction, loose coupling, and support multiple inheritance.”
Follow-Up
Difference Between Interface and Abstract Class?
Answer
Interface Abstract Class
Multiple inheritance Partial abstraction
No constructor Can have constructor
Mostly abstract methods Abstract + concrete methods
4. Why Use Encapsulation?
Answer
“Encapsulation provides data hiding, security, and controlled access using getters and setters.”
5. Difference Between Compile-Time and Runtime Polymorphism
Answer
Compile-Time Runtime
Method Overloading Method Overriding
Decided during compilation Decided during execution
Follow-Up
Which Polymorphism is Faster?
Answer
“Compile-time polymorphism is faster because method resolution happens during compilation.”
6. Why Method Overriding Used?
Answer
“Method overriding is used to provide specific implementation in child class.”
7. Can Constructor Be Overridden?
Answer
“No, constructors cannot be overridden because they are not inherited.”
8. Can Static Methods Be Overridden?
Answer
“No, static methods belong to class, not object. They can only be hidden.”
9. Why Main Method Static?
Answer
“Main method is static so JVM can call it without creating object.”
10. Why Use Static Keyword?
Answer
“Static members belong to class instead of object and reduce memory usage.”
11. Why Use Final Keyword?
Answer
“final is used to restrict modification, inheritance, or overriding.”
Follow-Up
Can Final Class Be Inherited?
Answer
“No, final class cannot be inherited.”
Example:
final class A {
12. Difference Between Heap and Stack Memory
Answer
Heap Stack
Stores objects Stores method calls/local variables
Shared memory Thread-specific
13. Why Java is Secure?
Answer
“Java is secure because of JVM, bytecode verification, no pointer access, and automatic memory
management.”
14. Why Java is Robust?
Answer
“Java is robust because of exception handling, strong memory management, and type checking.”
15. Difference Between Error and Exception
Answer
Error Exception
Serious issue Recoverable issue
JVM related Application related
16. Why Exception Handling Important?
Answer
“Exception handling prevents abnormal program termination and improves application reliability.”
Follow-Up
Can finally Block Skip Execution?
Answer
“Yes, in cases like [Link]() or JVM crash.”
17. Difference Between throw and throws
Answer
throw throws
Used inside method Used in method declaration
Throws exception explicitly Declares exception
18. What is Custom Exception?
Answer
“Custom exception is user-defined exception created by extending Exception class.”
19. Why Collections Framework Used?
Answer
“Collections framework provides dynamic data structures and utility methods for
storing/manipulating data.”
20. Why ArrayList Faster for Retrieval?
Answer
“ArrayList uses indexing with dynamic arrays, so retrieval is faster.”
21. Why LinkedList Faster for Insertion/Deletion?
Answer
“LinkedList uses node connections, so insertion/deletion does not require shifting elements.”
22. Why HashMap Fast?
Answer
“HashMap uses hashing and bucket indexing for fast insertion and retrieval.”
Follow-Up
What is hashCode()?
Answer
“hashCode() generates integer hash value used for bucket calculation in hashing.”
Follow-Up
What Happens if Two Keys Have Same HashCode?
Answer
“Collision occurs and multiple entries are stored in same bucket.”
23. Difference Between HashMap and TreeMap
Answer
HashMap TreeMap
Unordered Sorted
Faster Slower
Uses hashing Uses Red-Black Tree
24. Difference Between HashSet and HashMap
Answer
HashSet HashMap
Stores only values Stores key-value pairs
25. Why Synchronization Needed?
Answer
“Synchronization prevents race conditions when multiple threads access shared resources.”
26. Difference Between Process and Thread
Answer
Process Thread
Heavyweight Lightweight
Separate memory Shared memory
27. What is Deadlock?
Answer
“Deadlock occurs when two or more threads wait for each other forever.”
28. What is Garbage Collection?
Answer
“Garbage collection automatically removes unused objects from heap memory.”
Follow-Up
Can We Force Garbage Collection?
Answer
“We can request using [Link](), but JVM does not guarantee execution.”
29. What are Wrapper Classes?
Answer
“Wrapper classes convert primitive types into objects.”
Examples:
int → Integer
double → Double
30. What is Autoboxing?
Answer
“Automatic conversion of primitive to wrapper object.”
31. What is Unboxing?
Answer
“Automatic conversion of wrapper object to primitive.”
32. Difference Between == and equals()
Answer
== compares memory references
equals() compares values/content
Follow-Up
Why String Overrides equals()?
Answer
“To compare actual string content instead of object references.”
33. Can We Override Private Methods?
Answer
“No, private methods are not inherited.”
34. Can We Override Final Methods?
Answer
“No, final methods cannot be overridden.”
35. Can We Have Multiple Main Methods?
Answer
“Yes, by method overloading, but JVM calls only:
public static void main(String[] args)
36. Why Java Does Not Support Pointers?
Answer
“To improve security and avoid direct memory manipulation.”
37. What is Marker Interface?
Answer
“Marker interface contains no methods and provides metadata to JVM.”
Example:
Serializable
38. What is Serializable?
Answer
“Serializable converts object into byte stream for storage or transfer.”
39. What is Cloneable?
Answer
“Cloneable interface allows object cloning.”
40. Difference Between Shallow Copy and Deep Copy
Answer
Shallow Copy Deep Copy
Copies references Copies complete objects
VERY IMPORTANT CGI INTERVIEW TIP
CGI interviewers often continue asking:
“Why?”
“Internally what happens?”
“Which is faster?”
“Why used in your project?”
So practice:
practical reasoning,
internal working,
backend examples,
project-related examples.