LearnFrenzy
Sign in Take a free test

Object Class and Wrapper Classes · Exercise 2 of 4

Object Class and Wrapper Classes Predict the Output

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each Java program and choose what it prints. Every program was compiled and run on Java 21, so the answers are the real output.

Question 1 Medium

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        Integer a = 100, b = 100, c = 1000, d = 1000;
        System.out.println((a == b) + " " + (c == d) + " " + c.equals(d) + " " + (c == 1000));
    }
}
A true false true true
B true true true true
C false false true true
D true false true false
Question page & discussion
Question 2 Hard

What is the output of the following Java program?

Main.java
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<Short> set = new HashSet<>();
        for (short i = 0; i < 10; i++) {
            set.add(i);
            set.remove(i - 1);            // i - 1 is an int, boxed to Integer
        }
        System.out.println(set.size());
    }
}
A 10
B 1
C 0
D 9
Question page & discussion
Question 3 Hard

What is the output of the following Java program?

Main.java
public class Main {
    static Integer lookup(boolean found) {
        Integer cached = null;
        return found ? 42 : cached;         // int and Integer → the result type is int!
    }

    public static void main(String[] args) {
        System.out.print(lookup(true) + " ");
        try {
            System.out.println(lookup(false));
        } catch (NullPointerException e) {
            System.out.println("NPE");
        }
    }
}
A 42 NPE
B 42 null
C null NPE
D 42 0
Question page & discussion
Question 4 Medium

What is the output of the following Java program?

Main.java
import java.util.HashMap;
import java.util.Map;

class City {
    final String name;
    City(String n) { name = n; }
    @Override public boolean equals(Object o) { return o instanceof City c && c.name.equals(name); }
}

public class Main {
    public static void main(String[] args) {
        Map<City, Integer> pop = new HashMap<>();
        pop.put(new City("Pune"), 7);
        System.out.println(pop.get(new City("Pune")) + " " + pop.containsKey(new City("Pune")) + " " + new City("Pune").equals(new City("Pune")));
    }
}
A null false true
B 7 true true
C 7 false true
D null true true
Question page & discussion
Question 5 Easy

What is the output of the following Java program?

Main.java
class A { }
class B { @Override public String toString() { return "I am B"; } }

public class Main {
    public static void main(String[] args) {
        String a = new A().toString();
        System.out.println(a.substring(0, a.indexOf('@') + 1) + " " + new B() + " " + ("" + null));
    }
}
A A@ I am B null
B A I am B null
C A@ B@ null
D A@ I am B NullPointerException
Question page & discussion
Question 6 Hard

What is the output of the following Java program?

Main.java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] flat = {1, 2, 3};
        int[][] grid = {{1, 2}, {3, 4}};
        int[] flatCopy = flat.clone();
        int[][] gridCopy = grid.clone();
        flatCopy[0] = 99;
        gridCopy[0][0] = 99;
        System.out.println(Arrays.toString(flat) + " " + Arrays.deepToString(grid) + " " + (grid[1] == gridCopy[1]));
    }
}
A [1, 2, 3] [[99, 2], [3, 4]] true
B [99, 2, 3] [[99, 2], [3, 4]] true
C [1, 2, 3] [[1, 2], [3, 4]] false
D [1, 2, 3] [[99, 2], [3, 4]] false
Question page & discussion
Question 7 Medium

What is the output of the following Java program?

Main.java
class Point implements Cloneable {
    int x, y;
    int[] history = new int[2];

    @Override public Point clone() {
        try {
            Point p = (Point) super.clone();
            p.history = history.clone();          // deep-copy the array
            return p;
        } catch (CloneNotSupportedException e) { throw new AssertionError(e); }
    }
}

public class Main {
    public static void main(String[] args) {
        Point a = new Point();
        a.x = 3; a.history[0] = 7;
        Point b = a.clone();
        b.x = 5; b.history[0] = 9;
        System.out.println(a.x + " " + a.history[0] + " " + b.x + " " + b.history[0] + " " + (a != b) + " " + (a.getClass() == b.getClass()));
    }
}
A 3 7 5 9 true true
B 5 9 5 9 true true
C 3 9 5 9 true true
D 3 7 5 9 false true
Question page & discussion
Question 8 Medium

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        int max = Integer.MAX_VALUE;
        long safe = max + 1L;
        System.out.println((max + 1) + " " + safe + " " + (Integer.MIN_VALUE - 1) + " " + Math.abs(Integer.MIN_VALUE));
        try {
            Math.addExact(max, 1);
        } catch (ArithmeticException e) {
            System.out.println(e.getMessage());
        }
    }
}
A -2147483648 2147483648 2147483647 -2147483648 integer overflow
B 2147483648 2147483648 -2147483649 2147483648 integer overflow
C -2147483648 -2147483648 2147483647 2147483648 overflow
D Compilation error
Question page & discussion
Question 9 Medium

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.toBinaryString(13) + " " + Integer.toHexString(4095) + " " + Integer.parseInt("777", 8) + " " + Integer.parseInt("1111", 2) + " " + Integer.toString(255, 36) + " " + Long.toHexString(-1L).length());
    }
}
A 1101 fff 511 15 73 16
B 1011 fff 511 15 73 16
C 1101 FFF 511 15 ff 16
D 1101 fff 777 15 73 8
Question page & discussion
Question 10 Hard

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        double nan = Double.NaN;
        Double boxedNan = nan;
        System.out.println((nan == nan) + " " + boxedNan.equals(Double.NaN) + " " + Double.isNaN(0.0 / 0.0) + " " + (0.0 == -0.0) + " " + Double.valueOf(0.0).equals(-0.0) + " " + Double.compare(0.0, -0.0));
    }
}
A false true true true false 1
B true true true true true 0
C false false true true false 0
D false true false false false 1
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links