Java Keywords Interview Questions

Introduction

Java keywords are reserved words defined by the language that have a predefined meaning in the compiler. These keywords control program structure, memory behavior, flow control, class definitions, inheritance, and access control. Interviewers frequently test understanding of keyword rules, use cases, restrictions, and differences between similar keywords.

What Interviewers Expect

  • Clear understanding of the purpose and behavior of each keyword.
  • Ability to differentiate similar keywords like final, finally, finalize.
  • Knowledge of access modifiers and non-access modifiers.
  • Awareness of compile-time and runtime effects of keyword usage.
  • Correct usage examples and edge cases.

Table of Contents

  • Interview Questions
  • Scenario-Based Interview Questions
  • Common Mistakes
  • FAQs

Interview Questions

Q1. What are Java keywords?

  • Java keywords are reserved words that cannot be used as identifiers such as variable names, class names, or method names.
  • These keywords have predefined roles in the compiler and runtime environment.
  • There are 67 keywords in modern Java versions, including contextual keywords.
  • Keywords control structure, inheritance, memory management, and error handling.
  • Using a keyword incorrectly leads to compile-time errors.

Q2. What is the purpose of the final keyword?

  • final restricts modification: variables cannot be reassigned, methods cannot be overridden, and classes cannot be extended.
  • final variables must be initialized before use and behave like constants.
  • final methods improve security and prevent accidental overrides.
  • final classes (e.g., String) prevent inheritance for safety and performance.
  • final improves performance by enabling compiler optimizations.

Q3. Difference between final, finally, and finalize?

  • final: keyword preventing modification (variables, methods, classes).
  • finally: block that executes regardless of exceptions in try-catch.
  • finalize: a method invoked by GC before object destruction (deprecated).
  • Only final is a keyword; finally is control flow; finalize is a method.
  • finalize should not be used due to unpredictability.

Q4. What is static keyword used for?

  • static makes variables and methods belong to the class rather than objects.
  • Static members load during class loading, not object creation.
  • Static blocks initialize static data before main executes.
  • Static methods cannot access non-static fields directly.
  • Static memory is stored in method area and shared across instances.

Q5. What does the abstract keyword do?

  • abstract marks classes or methods that cannot be instantiated directly.
  • An abstract class can have both abstract and concrete methods.
  • Abstract methods have no body and must be overridden in subclasses.
  • Used to define templates for subclasses.
  • Abstract constructors are not allowed.

Q6. What is the purpose of the synchronized keyword?

  • synchronized ensures that only one thread accesses a critical section at a time.
  • It prevents race conditions by locking a method or block.
  • Locking occurs on the object monitor or class monitor for static methods.
  • Increases thread-safety but reduces performance due to locking overhead.
  • Used in multithreading environments like counters, shared data, etc.

Q7. What does the transient keyword mean?

  • transient prevents a field from being serialized.
  • Used when specific fields should not be stored in byte-streams.
  • During deserialization, transient fields restore to default values.
  • Useful for passwords, security tokens, or temporary state.
  • Works only with objects implementing Serializable.

Q8. What is the volatile keyword?

  • volatile ensures that variables are read directly from main memory, not CPU cache.
  • Provides visibility guarantee between threads.
  • Prevents compiler-level reordering of operations.
  • Typically used in flags or shared state across threads.
  • Does not ensure atomicity; use synchronized for atomic updates.

Q9. What is the super keyword?

  • super refers to the immediate parent class object.
  • Used to call parent class constructors explicitly.
  • Used to access overridden methods or hidden fields.
  • Must be the first statement inside a constructor.
  • Helps avoid ambiguity in inheritance hierarchies.

Q10. What is the purpose of the this keyword?

  • this refers to the current object instance.
  • Used to differentiate between instance variables and parameters.
  • Used to call another constructor in the same class.
  • Useful when passing the current object as an argument.
  • Cannot be used inside static methods.

Scenario-Based Interview Questions

Scenario 1: final variable initialization

  • A final variable must be initialized before use.
  • Initialization can occur in constructor or static block.
  • Uninitialized final variables cause compilation errors.
  • Useful for configuration constants.
  • Interviewers test understanding of initialization timing.

Scenario 2: synchronized method in shared resource

  • Two threads accessing a synchronized method will run sequentially.
  • Prevents incorrect updates of shared counters.
  • Lock is taken on the object instance.
  • May cause thread starvation if used incorrectly.
  • Affects overall throughput of application.

Scenario 3: volatile variable in signaling

  • volatile ensures immediate visibility across threads.
  • Useful for shutdown flags and inter-thread signals.
  • No need for synchronized when only visibility is required.
  • Not suitable for compound operations requiring atomicity.
  • Common in JVM-based concurrency questions.

Common Mistakes

  • Using == instead of equals for string comparison.
  • Misunderstanding final vs finally vs finalize.
  • Expecting synchronized to improve performance.

Quick Revision Snapshot

  • final restricts modification.
  • static belongs to class, not objects.
  • abstract defines incomplete methods.
  • transient skips serialization.
  • volatile ensures memory visibility.
  • synchronized ensures thread-safe access.
  • super calls parent class methods/constructors.
  • this refers to current instance.

FAQs

Q1: How many keywords does Java have?

Java currently has around 67 keywords including literals and restricted keywords.

Q2: Are all keywords reserved?

Yes, all keywords are reserved and cannot be used as identifiers.

Q3: Is null a keyword?

No, null is a literal, not a keyword.

Conclusion

Java keywords play a crucial role in defining program structure, memory behavior, and access rules. A clear understanding of keywords helps avoid errors, improves code readability, and builds a strong foundation for mastering Java concepts required in interviews and real-world development.

Scroll to Top