20 questions · untimed

Java interview: Java fundamentals round

Pick an option and check it. The explanation shows after each answer. Take as long as you like; nothing is timed and nothing is sent.

20 questions

Question 1 · easy

What does this Java 21 program print?

public class Main {
    public static void main(String[] args) {
        String a = "java";
        String b = "java";
        String c = new String("java");
        System.out.println((a == b) + " " + (a == c) + " " + a.equals(c));
    }
}

Question 2 · easy

What does this Java 21 program print?

public class Main {
    public static void main(String[] args) {
        String s = "Hi";
        s.concat(" there");
        StringBuilder sb = new StringBuilder("Hi");
        sb.append(" there");
        System.out.println(s + " | " + sb);
    }
}

Question 3 · medium

What does this Java 21 program print?

public class Main {
    public static void main(String[] args) {
        Integer a = 127, b = 127;
        Integer c = 128, d = 128;
        System.out.println((a == b) + " " + (c == d) + " " + c.equals(d));
    }
}

Question 4 · hard

This Java 21 code prints null instead of home. What is the bug?

static class Point {
    final int x, y;
    Point(int x, int y) { this.x = x; this.y = y; }
    @Override public boolean equals(Object o) {
        return o instanceof Point p && p.x == x && p.y == y;
    }
}

// in main
Map<Point, String> places = new HashMap<>();
places.put(new Point(1, 2), "home");
System.out.println(places.get(new Point(1, 2)));

Question 5 · easy

What does the equals and hashCode contract in Java require?

Question 6 · easy

What does this Java 21 program print?

public class Main {
    static class Parent {
        Parent() { System.out.print("Parent "); }
    }
    static class Child extends Parent {
        Child() { System.out.println("Child"); }
    }
    public static void main(String[] args) {
        new Child();
    }
}

Question 7 · medium

An interviewer asks about static and dynamic binding. Which of these is decided at compile time?

Question 8 · easy

Which statement about abstract classes and interfaces in Java 21 is correct?

Question 9 · hard

What does this Java 21 program print?

public class Main {
    static class A {
        String name = "A";
        String get() { return name; }
    }
    static class B extends A {
        String name = "B";
        String get() { return name; }
    }
    public static void main(String[] args) {
        A x = new B();
        System.out.println(x.name + " " + x.get());
    }
}

Question 10 · medium

This Java 21 code prints false, although the list holds an equal point. What is the bug?

static class Point {
    int x, y;
    Point(int x, int y) { this.x = x; this.y = y; }
    public boolean equals(Point p) { return p.x == x && p.y == y; }
    @Override public int hashCode() { return 31 * x + y; }
}

// in main
List<Point> points = List.of(new Point(1, 2));
System.out.println(points.contains(new Point(1, 2)));

Question 11 · easy

What does this Java 21 program print?

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Map<String, Integer> stock = new TreeMap<>();
        stock.put("banana", 2);
        stock.put("apple", 5);
        stock.put("cherry", 1);
        System.out.println(stock);
    }
}

Question 12 · medium

This Java 21 code throws ConcurrentModificationException. Which fix is correct?

List<Integer> nums = new ArrayList<>(List.of(1, 2, 3, 4));
for (Integer n : nums) {
    if (n % 2 == 0) {
        nums.remove(n);
    }
}
System.out.println(nums);

Question 13 · medium

What does this Java 21 program print?

import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>(List.of(10, 20, 30));
        list.remove(1);
        list.remove(Integer.valueOf(10));
        System.out.println(list);
    }
}

Question 14 · easy

Which statement comparing ArrayList and LinkedList is correct?

Question 15 · medium

You add employee records to a map keyed by employee ID in no particular order, and a report must list them in ascending ID order. Which map is the best choice?

Question 16 · easy

Which statement about checked and unchecked exceptions in Java is correct?

Question 17 · hard

What does this Java 21 program print?

public class Main {
    static int f() {
        int x = 1;
        try { return x; } finally { x = 2; }
    }
    static int g() {
        try { return 1; } finally { return 2; }
    }
    public static void main(String[] args) {
        System.out.println(f() + " " + g());
    }
}

Question 18 · medium

What does this Java 21 program print?

public class Main {
    static class Box { int v; Box(int v) { this.v = v; } }
    static void change(Box b) { b.v = 10; b = new Box(20); b.v = 30; }
    static void bump(int n) { n++; }
    public static void main(String[] args) {
        Box box = new Box(1);
        int n = 1;
        change(box);
        bump(n);
        System.out.println(box.v + " " + n);
    }
}

Question 19 · medium

A method opens a BufferedReader on a file and reads a line, and the read may throw IOException. What is the recommended way to make sure the reader is always closed?

Question 20 · medium

Inside this method, where do age, the variable s and the Student object live?

void register() {
    int age = 21;
    Student s = new Student("Asha");
}

Next: Coding round (Java)