Interview Q&A / Java

INTERVIEW Q&A · 72 QUESTIONS

Java interview questions

Collections, the JVM, strings, exceptions and threads, the backbone of most campus placement rounds.

  1. What is the difference between final, finally and finalize in Java?

    final is a keyword that stops change (a variable cannot be reassigned, a method cannot be overridden, a class cannot be extended). finally is a block that runs after try/catch, usually for cleanup. finalize() was a method the garbage collector might call before removing an object; it is deprecated for removal and should not be used.

    BeginnerIn-depth3 min read
  2. What are the four access levels in Java and who can see each one?

    private is visible only inside the class. Default (no keyword) adds the same package. protected adds subclasses in other packages. public is visible everywhere.

    BeginnerQuick answer1 min read
  3. What does the transient keyword do in Java?

    transient marks a field to be skipped during serialization. When the object is deserialized, that field gets its default value (null, 0 or false), which is useful for passwords, caches and values that can be recomputed.

    BeginnerQuick answer1 min read
  4. What is the difference between this and super in Java?

    this refers to the current object, and this(...) calls another constructor of the same class. super refers to the parent class part of the object, and super(...) calls a parent constructor.

    BeginnerQuick answer1 min read
  5. Why can a static method not use non-static variables or this in Java?

    A static method belongs to the class and can run without any object, so there is no this and no instance to read fields from. To use an instance member it must be given an object explicitly.

    BeginnerQuick answer1 min read
  6. Why is the main method public static void in Java?

    public so the JVM can call it from outside, static so it can run without creating an object, void because the JVM does not use a return value, and String[] args carries the command-line arguments.

    BeginnerQuick answer1 min read