Here are the top Java interview questions to prepare for your next role.
1️⃣ Which keyword is used in Java to prevent a variable from being modified after it has been initialized?
- A) const
- B) immutable
- C) static
- D) final
2️⃣ What is the maximum number of public classes allowed in a single Java source file?
- A) Two
- B) One
- C) Unlimited
- D) It depends on the JVM
3️⃣ Can you override a static method in Java?
- A) Only if the class is declared as final
- B) Yes, static methods support full overriding
- C) No, static methods cannot be overridden
- D) Yes, but only when using interfaces
4️⃣ What is the main difference between StringBuilder and StringBuffer in Java?
- A) StringBuffer is immutable while StringBuilder is mutable
- B) StringBuilder stores characters in Unicode while StringBuffer does not
- C) StringBuffer is synchronized while StringBuilder is not
- D) StringBuilder can only be used in single-threaded applications
5️⃣ Can a HashMap contain null keys in Java?
- A) Only when using Java 17 or later
- B) Only if using LinkedHashMap
- C) Yes, it allows one null key
- D) No, null keys are never allowed