0% found this document useful (0 votes)
6 views14 pages

Java Interview Top 100

The document contains a comprehensive list of the top 100 Java interview questions and answers, covering fundamental concepts such as static vs. non-static methods, method overloading and overriding, exception handling, and data structures. It serves as a resource for individuals preparing for Java-related interviews by providing concise explanations of key topics. Each question is followed by a clear and informative answer, making it a useful guide for both beginners and experienced programmers.

Uploaded by

Anshu Parihar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views14 pages

Java Interview Top 100

The document contains a comprehensive list of the top 100 Java interview questions and answers, covering fundamental concepts such as static vs. non-static methods, method overloading and overriding, exception handling, and data structures. It serves as a resource for individuals preparing for Java-related interviews by providing concise explanations of key topics. Each question is followed by a clear and informative answer, making it a useful guide for both beginners and experienced programmers.

Uploaded by

Anshu Parihar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY

BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY


BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE
2025
BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

JAVA TOP 100 QUESTIONS AND ANSWERS

10
July 20, BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY
2025 BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

JAVA TOP 100 INTERVIEW QUESTIONS AND ANSWERS

Q1. Can we define static variables inside the static method or not?
A1. No. Static variables are class-level and must be declared at the class level, not inside
a method (static or not). You can access them inside static methods, but you can't declare
them there.

Q2. In a project, when will we use instance and static variables?


A2.

 Use instance variables when each object should maintain its own copy (object-
specific data).
 Use static variables when the data is shared across all objects (common data).

Q3. What is constructor overloading?


A3. Constructor overloading means having multiple constructors in a class with different
parameter lists. This allows creating objects in different ways.

Q4. Difference between static and non-static methods.


A4.

 Static methods belong to the class; they can be called without creating an object.
 Non-static methods belong to an instance and require an object to be invoked.
 Static methods cannot access non-static members directly.

Q5. What is method overloading? Can it be done by changing return type?


A5. Method overloading means having multiple methods with the same name but
different parameters. It cannot be done by changing only the return type.

Q6. What is method overriding?


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

Q7. Can we override static methods?


A7. No. Static methods cannot be overridden as they belong to the class, not the instance.
However, they can be hidden using another static method in the subclass.

Q8. What is dynamic method dispatch?


A8. Dynamic method dispatch is a mechanism by which a call to an overridden method is
resolved at runtime, not compile time. This is used in runtime polymorphism.

Q9. Can we declare a class as static?


A9. Only nested classes (inner classes) can be declared static. Top-level classes cannot be
static.
July 20, BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY
2025 BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

Q10. What is the difference between compile-time and runtime polymorphism?


A10.

 Compile-time polymorphism is achieved by method overloading.


 Runtime polymorphism is achieved by method overriding using inheritance and
dynamic method dispatch.

Q11. Can constructors be inherited?


A11. No, constructors are not inherited. Each class must define its own constructors, but
a subclass can call a superclass constructor using super().

Q12. Can we call the constructor explicitly?


A12. Yes, we can call constructors explicitly using this() (for another constructor in the
same class) or super() (to call a superclass constructor).

Q13. What is the difference between final, finally, and finalize?


A13.

 final: Keyword used to declare constants, prevent method overriding, or


inheritance.
 finally: Block used with try-catch to execute code after try, regardless of
exception.
 finalize(): Method called by Garbage Collector before object destruction.

Q14. Can we overload the main method?


A14. Yes, the main method can be overloaded, but only the standard public static
void main(String[] args) is used as the entry point by JVM.

Q15. What is the difference between abstraction and encapsulation?


A15.

 Abstraction hides implementation details and shows only functionality.


 Encapsulation binds data and methods together and restricts direct access to data.

Q16. Can abstract classes have constructors?


A16. Yes, abstract classes can have constructors. They are called when an object of a
subclass is created.

Q17. Can we instantiate an abstract class?


A17. No, abstract classes cannot be instantiated. They must be extended by a subclass.

Q18. Can an interface have method definitions in Java 8 and above?


A18. Yes. From Java 8 onwards, interfaces can have default and static methods with
method definitions.
July 20, BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY
2025 BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

Q19. Can we declare a constructor inside an interface?


A19. No, interfaces cannot have constructors because they cannot be instantiated directly.

Q20. Difference between interface and abstract class?


A20.

 Interface: All methods are abstract (until Java 7), supports multiple inheritance.
 Abstract class: Can have concrete methods and fields, supports single inheritance.

Q21. Can we create an object of an abstract class using a reference?


A21. No, we cannot create an object of an abstract class directly, but we can use a
reference of the abstract class to point to a subclass object.

Q22. Can we declare a class as both abstract and final?


A22. No, a class cannot be both abstract and final because abstract requires
subclassing, and final prevents it.

Q23. What is a marker interface?


A23. A marker interface is an interface with no methods or fields. It is used to mark a
class with metadata, e.g., Serializable.

Q24. What is the difference between checked and unchecked exceptions?


A24.

 Checked exceptions are checked at compile-time (e.g., IOException).


 Unchecked exceptions are checked at runtime (e.g., NullPointerException).

Q25. Can we write try without catch or finally?


A25. No, try must be followed by either a catch block, a finally block, or both.

Q26. Can we throw a null exception?


A26. Yes, we can throw a null, but it results in a NullPointerException.

Q27. What is the base class of all exceptions in Java?


A27. The base class for all exceptions is Throwable. It has two main subclasses: Error
and Exception.

Q28. What is the difference between throw and throws?


A28.
July 20, BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY
2025 BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

 throw is used to explicitly throw an exception.


 throws is used to declare exceptions in the method signature.

Q29. What is the purpose of the finally block?


A29. The finally block contains code that always executes after try/catch, whether or
not an exception is thrown.

Q30. What is the difference between throw and try-catch?


A30.

 throw is used to explicitly generate an exception.


 try-catch is used to handle exceptions when they occur.

Q31. What is a custom exception in Java?


A31. A custom exception is a user-defined exception class that extends Exception or
RuntimeException to handle specific application errors.

Q32. Can we overload the main method?


A32. Yes, we can overload the main method, but only the standard public static void
main(String[] args) is used to start execution.

Q33. What is the difference between method overloading and overriding?


A33.

 Overloading: Same method name, different parameters, within the same class.
 Overriding: Same method signature in subclass to change behavior.

Q34. Can we override a static method?


A34. No, static methods are not overridden; they are hidden (method hiding) if redefined
in a subclass.

Q35. What is the use of the super keyword?


A35. The super keyword is used to refer to the immediate parent class's methods or
constructor.

Q36. What is the final keyword in Java?


A36. The final keyword is used to declare constants, prevent method overriding, and
inheritance of classes.
July 20, BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY
2025 BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

Q37. What is the difference between final, finally, and finalize()?


A37.

 final: Restricts change (variable), inheritance (class), and overriding (method).


 finally: A block that always executes in exception handling.
 finalize(): A method called before an object is garbage collected.

Q38. What is a constructor?


A38. A constructor is a special method used to initialize objects. It has the same name as
the class and no return type.

Q39. What is constructor overloading?


A39. Constructor overloading means having multiple constructors in a class with
different parameter lists.

Q40. Can a constructor be private?


A40. Yes, a constructor can be private. It is used in Singleton design patterns or to restrict
instantiation.

Q41. What is the Singleton design pattern?


A41. The Singleton pattern ensures only one instance of a class exists and provides a
global access point to it.

Q42. What is a package in Java?


A42. A package is a namespace that organizes classes and interfaces. It helps avoid name
conflicts and control access.

Q43. What is the difference between import java.util. and import java.util.ArrayList?*
A43.

 import java.util.* imports all classes from the util package.


 import java.util.ArrayList imports only the ArrayList class.

Q44. What is the access modifier for interfaces?


A44. Interfaces are implicitly public, and all methods are public and abstract by
default.

Q45. Can an interface have constructors?


A45. No, interfaces cannot have constructors because they cannot be instantiated.
July 20, BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY
2025 BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

Q46. What is a marker interface?


A46. A marker interface has no methods or fields. It is used to mark a class for special
treatment by JVM or tools (e.g., Serializable).

Q47. What is a functional interface?


A47. A functional interface is an interface with exactly one abstract method. It can have
default and static methods. Example: Runnable.

Q48. What is lambda expression in Java?


A48. A lambda expression is a short block of code that takes in parameters and returns a
value. It simplifies functional programming.

Q49. What is the difference between List and Set in Java?


A49.

 List: Allows duplicates, maintains insertion order.


 Set: No duplicates, may or may not maintain order (e.g., HashSet, TreeSet).

Q50. What is the difference between ArrayList and LinkedList?


A50.

 ArrayList: Fast for random access, slower for insert/delete in middle.


 LinkedList: Faster insert/delete, slower random access.

Q51. What is the difference between HashSet and TreeSet?


A51.

 HashSet: Unordered, allows null, faster, backed by a hash table.


 TreeSet: Sorted, does not allow null, slower, backed by a Red-Black tree.

Q52. What is the difference between HashMap and Hashtable?


A52.

 HashMap: Not synchronized, allows one null key and multiple null values.
 Hashtable: Synchronized, doesn’t allow null keys or values.

Q53. What is the difference between HashMap and TreeMap?


A53.

 HashMap: Unordered, faster for insert/search.


July 20, BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY
2025 BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

 TreeMap: Sorted by keys, slower due to tree traversal.

Q54. What is the difference between fail-fast and fail-safe?


A54.

 Fail-fast: Throws ConcurrentModificationException if structure is modified


during iteration (e.g., ArrayList).
 Fail-safe: Doesn’t throw exception, uses clone of collection (e.g.,
CopyOnWriteArrayList).

Q55. What is the difference between Iterator and ListIterator?


A55.

 Iterator: Unidirectional, works for all collections.


 ListIterator: Bidirectional, only for List, allows element modification.

Q56. What is the difference between Comparable and Comparator?


A56.

 Comparable: Used for natural ordering, implements compareTo().


 Comparator: Used for custom ordering, implements compare().

Q57. What is the difference between equals() and == in Java?


A57.

 equals(): Compares values (logical comparison).


 ==: Compares references (memory address).

Q58. What is the difference between final, finally, and finalize()?


A58.

 final: Keyword to restrict inheritance, method override, or variable


reassignment.
 finally: Block that always executes after try-catch.
 finalize(): Method called by garbage collector before object destruction.

Q59. What is autoboxing and unboxing in Java?


A59.

 Autoboxing: Converting primitive to wrapper (e.g., int to Integer).


 Unboxing: Converting wrapper to primitive (e.g., Integer to int).

Q60. What is the transient keyword in Java?


A60.
The transient keyword prevents serialization of a field. It is skipped during object
serialization.
July 20, BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY
2025 BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

Q61. What is serialization in Java?


A61.
Serialization is the process of converting an object into a byte stream so that it can be
persisted to a file or sent over a network.

Q62. What is deserialization in Java?


A62.
Deserialization is the reverse of serialization. It reconstructs an object from a byte stream.

Q63. What is the role of the serialVersionUID?


A63.
serialVersionUID is used during deserialization to verify that the sender and receiver of
a serialized object maintain compatibility regarding the class.

Q64. What is the marker interface in Java?


A64.
A marker interface has no methods or fields (e.g., Serializable, Cloneable) and is
used to signal to the JVM or tools that a class has a special property.

Q65. What is the use of the instanceof keyword?


A65.
The instanceof keyword checks whether an object is an instance of a specific class or
subclass.

Q66. What is the difference between method overloading and overriding?


A66.

 Overloading: Same method name, different parameters, within the same class.
 Overriding: Same method signature in subclass to provide specific
implementation.

Q67. Can you override a static method in Java?


A67.
No, static methods cannot be overridden. They can be hidden using another static method
in a subclass.

Q68. What is a constructor in Java?


A68.
A constructor is a special method used to initialize objects. It has the same name as the
class and no return type.
July 20, BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY
2025 BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

Q69. What is a default constructor?


A69.
A default constructor is a no-argument constructor provided by Java if no constructors
are explicitly defined.

Q70. What is the purpose of the this keyword?


A70.
The this keyword refers to the current instance of the class. It is used to resolve name
conflicts between class attributes and method parameters.

Q71. What is the purpose of the super keyword in Java?


A71.
The super keyword refers to the immediate parent class object and is used to access
parent class methods, constructors, or variables.

Q72. What is the difference between this() and super()?


A72.

 this() calls another constructor in the same class.


 super() calls the constructor of the immediate parent class.
Both must be the first statement in a constructor.

Q73. What is a static block in Java?


A73.
A static block is used for static initializations and is executed only once when the class is
loaded into memory.

Q74. What is the difference between String, StringBuilder, and StringBuffer?


A74.

 String: Immutable.
 StringBuilder: Mutable and faster but not thread-safe.
 StringBuffer: Mutable and thread-safe.

Q75. What is a package in Java?


A75.
A package is a namespace that organizes classes and interfaces, helping avoid name
conflicts and controlling access.
July 20, BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY
2025 BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

Q76. What is the difference between import java.util.* and import


java.util.Scanner?
A76.

 import java.util.*: Imports all classes in the java.util package.


 import java.util.Scanner: Imports only the Scanner class.

Q77. What is the purpose of access modifiers in Java?


A77.
Access modifiers (public, private, protected, default) define the visibility and
accessibility of classes, methods, and variables.

Q78. What is the final keyword in Java?


A78.
The final keyword is used to declare constants, prevent method overriding, and prevent
inheritance of a class.

Q79. Can a final variable be assigned a value later?


A79.
Yes, a final variable can be assigned a value later only if it is not initialized during
declaration (e.g., in a constructor).

Q80. What is the difference between == and equals() in Java?


A80.

 == checks reference equality (whether two objects point to the same memory).
 equals() checks logical equality (whether two objects have the same content).

Q81. Can we override a static method in Java?


A81.
No, static methods cannot be overridden. They are bound at compile time using static
binding.

Q82. Can we overload the main method in Java?


A82.
Yes, we can overload the main method, but only the standard main(String[] args) is
used by the JVM as the entry point.

Q83. What is the default value of an instance variable in Java?


A83.
Instance variables are initialized with default values:

 int → 0,
July 20, BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY
2025 BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

 float → 0.0f,
 object references → null,
etc.

Q84. What is the difference between instance and local variables?


A84.

 Instance variables are declared in a class but outside methods and belong to an
object.
 Local variables are declared inside methods and exist only within the method
scope.

Q85. Can an interface have a constructor in Java?


A85.
No, interfaces cannot have constructors because they cannot be instantiated directly.

Q86. What is the purpose of the instanceof keyword?


A86.
The instanceof keyword is used to test whether an object is an instance of a specific
class or subclass.

Q87. What is an abstract method?


A87.
An abstract method has no body and must be implemented by subclasses. It is declared
using the abstract keyword.

Q88. What is the use of the transient keyword in Java?


A88.
The transient keyword is used to skip a variable during serialization, meaning it will
not be saved to the stream.

Q89. What is the use of the volatile keyword in Java?


A89.
The volatile keyword ensures that changes made to a variable by one thread are visible
to other threads immediately.

Q90. What is serialization in Java?


A90.
Serialization is the process of converting an object into a byte stream so it can be saved to
a file or sent over a network.
July 20, BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY
2025 BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

Q91. What is deserialization in Java?


A91.
Deserialization is the process of converting a byte stream back into a Java object.

Q92. What is the finalize() method in Java?


A92.
The finalize() method is called by the garbage collector before an object is removed
from memory. It’s used to perform cleanup operations.

Q93. What is a package in Java?


A93.
A package is a namespace that organizes classes and interfaces. It helps avoid name
conflicts and makes classes easier to locate.

Q94. What are wrapper classes in Java?


A94.
Wrapper classes convert primitive types into objects. Examples: int → Integer, double
→ Double, etc.

Q95. What is autoboxing and unboxing in Java?


A95.
Autoboxing converts primitive types to their corresponding wrapper classes
automatically.
Unboxing converts wrapper objects back to primitives.

Q96. What is the use of the assert keyword in Java?


A96.
The assert keyword is used for debugging to test assumptions in code. If the condition
is false, an AssertionError is thrown.

Q97. What is a classloader in Java?


A97.
A classloader is part of the JVM that loads class files into memory when required. Types
include: Bootstrap, Extension, and System classloaders.

Q98. What is the use of the strictfp keyword?


A98.
The strictfp keyword ensures consistent floating-point calculations across different
platforms.

Q99. What is method reference in Java 8?


A99.
Method reference is a shorthand for a lambda expression that calls a method. Example:
ClassName::methodName.
July 20, BANSAL INSTITUTE OF RESEARCH AND TECHNOLOGY
2025 BANSAL INSTITUTE OF RESEARCH TECHNOLOGY AND SCIENCE

Q100. What is the difference between Comparator and Comparable?


A100.

 Comparable is used to define natural ordering and must be implemented by the


class.
 Comparator is used for custom sorting and can be defined outside the class.

You might also like