Java Interview Questions and Answers for Freshers

The Java questions asked most in campus drives and technical rounds at service and product companies, each with the answer and a short explanation. Includes 'what does this code print?' questions.

  1. Q1. Which of these is NOT a primitive type in Java?

    • A. int
    • B. String
    • C. boolean
    • D. char
    Show answer

    Answer: B. String
    String is a class (an object type); the eight primitives are byte, short, int, long, float, double, char and boolean.

  2. Q2. What is the default value of an int field in a Java class?

    • A. 0
    • B. Garbage value
    • C. null
    • D. undefined
    Show answer

    Answer: A. 0
    Fields get default values: 0 for numbers, false for boolean, null for objects. Local variables have no default and must be initialised.

  3. Q3. Which keyword is used to inherit a class in Java?

    • A. super
    • B. inherits
    • C. extends
    • D. implements
    Show answer

    Answer: C. extends
    class B extends A. A class implements interfaces.

  4. Q4. How many classes can a Java class directly extend?

    • A. None
    • B. Two
    • C. Any number
    • D. One
    Show answer

    Answer: D. One
    Java has single inheritance for classes; use interfaces for multiple types.

  5. Q5. What does JVM stand for?

    • A. Joint Virtual Memory
    • B. Java Verified Module
    • C. Java Variable Method
    • D. Java Virtual Machine
    Show answer

    Answer: D. Java Virtual Machine
    The JVM runs compiled bytecode, which is why Java is 'write once, run anywhere'.

  6. Q6. Which method must every Java application have to start running?

    • A. public void start()
    • B. public static void main(String[] args)
    • C. public static run()
    • D. static int main()
    Show answer

    Answer: B. public static void main(String[] args)
    The JVM looks for public static void main(String[] args).

  7. Q7. What is the difference between == and equals() for Strings?

    • A. == compares references, equals() compares contents
    • B. equals() only works for numbers
    • C. They are the same
    • D. == compares contents, equals() compares references
    Show answer

    Answer: A. == compares references, equals() compares contents
    Two different String objects with the same text are equal() but not ==.

  8. Q8. Which collection does NOT allow duplicate elements?

    • A. ArrayList
    • B. HashSet
    • C. Vector
    • D. LinkedList
    Show answer

    Answer: B. HashSet
    A Set keeps unique elements.

  9. Q9. What happens when you access index 5 of an array of length 5?

    • A. It returns 0
    • B. It returns null
    • C. ArrayIndexOutOfBoundsException
    • D. Compile error
    Show answer

    Answer: C. ArrayIndexOutOfBoundsException
    Valid indexes are 0 to 4; Java checks bounds at run time.

  10. Q10. Which keyword prevents a method from being overridden?

    • A. static
    • B. const
    • C. private
    • D. final
    Show answer

    Answer: D. final
    A final method can't be overridden; a final class can't be extended.

  11. Q11. What is method overloading?

    • A. A child class redefining a parent method
    • B. Calling a method too many times
    • C. A method that returns many values
    • D. Same method name with different parameters in one class
    Show answer

    Answer: D. Same method name with different parameters in one class
    Overloading is resolved at compile time; overriding happens in subclasses and is resolved at run time.

  12. Q12. Which of these is a checked exception?

    • A. IOException
    • B. NullPointerException
    • C. ArithmeticException
    • D. ArrayIndexOutOfBoundsException
    Show answer

    Answer: A. IOException
    Checked exceptions must be caught or declared with throws; the others are RuntimeExceptions.

  13. Q13. Can an interface have a method with a body in Java 8 and later?

    • A. Only private methods
    • B. Yes, default and static methods
    • C. Only if the interface is abstract
    • D. No, never
    Show answer

    Answer: B. Yes, default and static methods
    Java 8 added default and static methods to interfaces.

  14. Q14. Why are Java Strings immutable?

    • A. Because they are primitives
    • B. For security, thread safety and the string pool
    • C. They are not immutable
    • D. To save typing
    Show answer

    Answer: B. For security, thread safety and the string pool
    Immutable strings can be safely shared and cached in the string pool.

  15. Q15. Which class should you use to build a long string inside a loop?

    • A. StringBuilder
    • B. Character
    • C. String
    • D. Scanner
    Show answer

    Answer: A. StringBuilder
    StringBuilder appends without creating a new object each time.

  16. Q16. What does the static keyword mean for a field?

    • A. The field is stored on the stack
    • B. The field is private
    • C. One copy is shared by all objects of the class
    • D. The value can never change
    Show answer

    Answer: C. One copy is shared by all objects of the class
    Static members belong to the class, not to individual objects.

  17. Q17. Which access modifier makes a member visible only inside its own class?

    • A. protected
    • B. private
    • C. default (no modifier)
    • D. public
    Show answer

    Answer: B. private
    private is the most restrictive; protected adds subclasses and the package.

  18. Q18. What is the time complexity of get(key) in a HashMap on average?

    • A. O(n log n)
    • B. O(1)
    • C. O(log n)
    • D. O(n)
    Show answer

    Answer: B. O(1)
    Hashing finds the bucket directly; worst case with many collisions is slower.

  19. Q19. What is garbage collection in Java?

    • A. Removing duplicate elements from a list
    • B. Automatic freeing of objects that are no longer referenced
    • C. Clearing the console
    • D. Deleting unused source files
    Show answer

    Answer: B. Automatic freeing of objects that are no longer referenced
    The JVM reclaims memory automatically; there is no delete in Java.

  20. Q20. Which keyword refers to the current object inside a method?

    • A. super
    • B. current
    • C. self
    • D. this
    Show answer

    Answer: D. this
    this.name = name; assigns the parameter to the field.

  21. Q21. What is the parent class of every class in Java?

    • A. Main
    • B. Class
    • C. Base
    • D. Object
    Show answer

    Answer: D. Object
    Every class extends java.lang.Object directly or indirectly.

  22. Q22. Which of these can NOT be instantiated with new?

    • A. A class with a private field
    • B. A class with static methods
    • C. A final class
    • D. An abstract class
    Show answer

    Answer: D. An abstract class
    Abstract classes exist to be extended; create objects of their concrete subclasses.

  23. Q23. What does this code print?

    int a = 5, b = 2;
    System.out.println(a / b);
    • A. 2
    • B. 3
    • C. 2.0
    • D. 2.5
    Show answer

    Answer: A. 2
    Both operands are int, so / does integer division and drops the .5.

  24. Q24. What does this code print?

    System.out.println("" + 10 + 20);
    • A. Error
    • B. 10 20
    • C. 1020
    • D. 30
    Show answer

    Answer: C. 1020
    The String comes first, so + joins text: "" + 10 + 20 = "1020".

  25. Q25. What does this code print?

    System.out.println(10 + 20 + "Java");
    • A. 1020Java
    • B. Java30
    • C. Java1020
    • D. 30Java
    Show answer

    Answer: D. 30Java
    Evaluated left to right: 10 + 20 = 30 first, then joined with "Java".

  26. Q26. What does this code print?

    String a = "hi";
    String b = new String("hi");
    System.out.println((a == b) + " " + a.equals(b));
    • A. true false
    • B. false true
    • C. false false
    • D. true true
    Show answer

    Answer: B. false true
    new String creates a different object, so == is false; equals() compares the text.

  27. Q27. What does this code print?

    int x = 5;
    int y = x++;
    System.out.println(y + " " + x);
    • A. 5 5
    • B. 6 6
    • C. 6 5
    • D. 5 6
    Show answer

    Answer: D. 5 6
    x++ gives the old value (5) and then increments x to 6.

  28. Q28. What does this code print?

    System.out.println("abc".charAt(1));
    • A. a
    • B. ab
    • C. b
    • D. Error
    Show answer

    Answer: C. b
    charAt counts from 0: index 1 is 'b'.

  29. Q29. What does this code print?

    int[] a = {1, 2, 3};
    System.out.println(a.length);
    • A. Error
    • B. 2
    • C. 3
    • D. 4
    Show answer

    Answer: C. 3
    length is a field for arrays: {1, 2, 3} has 3 elements.

  30. Q30. What does this code print?

    int n = 2;
    switch (n) {
        case 1: System.out.print("One ");
        case 2: System.out.print("Two ");
        case 3: System.out.print("Three"); break;
        default: System.out.print("Default");
    }
    • A. Two Three
    • B. Two Three Default
    • C. Default
    • D. Two
    Show answer

    Answer: A. Two Three
    There is no break after case 2, so execution falls through into case 3, which does break.

  31. Q31. What does this code print?

    java.util.List<Integer> list = new java.util.ArrayList<>(java.util.List.of(1, 2, 3));
    list.remove(1);
    System.out.println(list);
    • A. [1, 3]
    • B. [1, 2]
    • C. [3]
    • D. [2, 3]
    Show answer

    Answer: A. [1, 3]
    remove(1) with an int removes the element at index 1 (the value 2).

  32. Q32. What does this code print?

    class Parent { String name() { return "Parent"; } }
    class Child extends Parent { String name() { return "Child"; } }
    public class Main {
        public static void main(String[] args) {
            Parent p = new Child();
            System.out.println(p.name());
        }
    }
    • A. Child
    • B. Parent
    • C. Error
    • D. ParentChild
    Show answer

    Answer: A. Child
    The object is a Child, so the overridden method runs (runtime polymorphism).

  33. Q33. What does this code print?

    public class Main {
        static String f() {
            try { return "done"; }
            finally { System.out.print("finally "); }
        }
        public static void main(String[] args) {
            System.out.println(f());
        }
    }
    • A. finally
    • B. Error
    • C. done
    • D. finally done
    Show answer

    Answer: D. finally done
    finally runs before the method returns, even when try has a return.

  34. Q34. What does this code print?

    class Counter {
        static int count = 0;
        Counter() { count++; }
    }
    public class Main {
        public static void main(String[] args) {
            new Counter();
            new Counter();
            System.out.println(Counter.count);
        }
    }
    • A. 1
    • B. 0
    • C. 3
    • D. 2
    Show answer

    Answer: D. 2
    static count is shared by every Counter object, and two were created.

  35. Q35. What does this code print?

    public class Main {
        static void change(int v) { v = 20; }
        public static void main(String[] args) {
            int x = 10;
            change(x);
            System.out.println(x);
        }
    }
    • A. 10
    • B. 0
    • C. Error
    • D. 20
    Show answer

    Answer: A. 10
    Java passes a copy of the int value; changing it inside the method doesn't affect x.

  36. Q36. What does this code print?

    public class Main {
        static void change(int[] a) { a[0] = 99; }
        public static void main(String[] args) {
            int[] arr = {1, 2};
            change(arr);
            System.out.println(arr[0]);
        }
    }
    • A. 0
    • B. 99
    • C. 1
    • D. Error
    Show answer

    Answer: B. 99
    The array reference is copied, but it points to the same array, so the change is visible.

  37. Q37. What does this code print?

    String s = "java";
    String t = s.toUpperCase();
    System.out.println(t + " " + s);
    • A. JAVA JAVA
    • B. JAVA java
    • C. java java
    • D. Java java
    Show answer

    Answer: B. JAVA java
    toUpperCase returns a new String; s itself never changes.

  38. Q38. What does this code print?

    for (int i = 0; i < 3; i++) System.out.print(i + (i < 2 ? " " : ""));
    • A. 1 2
    • B. 0 1 2 3
    • C. 0 1 2
    • D. 1 2 3
    Show answer

    Answer: C. 0 1 2
    The loop prints while i < 3: 0, 1, 2.

  39. Q39. What does this code print?

    System.out.println(5.0 / 0);
    • A. NaN
    • B. ArithmeticException
    • C. 0
    • D. Infinity
    Show answer

    Answer: D. Infinity
    Dividing a double by zero gives Infinity; only integer division by zero throws.

  40. Q40. What does this code print?

    java.util.Map<String, Integer> m = new java.util.TreeMap<>();
    m.put("b", 2);
    m.put("a", 1);
    System.out.println(m);
    • A. {b=2, a=1}
    • B. [a, b]
    • C. Error
    • D. {a=1, b=2}
    Show answer

    Answer: D. {a=1, b=2}
    A TreeMap keeps its keys sorted.

Practised these? Now prove it.

Take a timed mock test with new questions every attempt, earn a verified certificate at 70%+, and get noticed by employers.

Start a mock test