Wrapper Classes & Autoboxing Questions with Answers

23 practice questions on Wrapper Classes & Autoboxing (Core Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 23 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

public class Main {
    public static void main(String[] args) {
        String s = "42";
        int a = Integer.parseInt(s);
        Integer b = Integer.valueOf(s);
        System.out.println(a + b + s);
    }
}
  1. 8442
  2. 84
  3. 4242
  4. 424242

Answer: A. 8442

Explanation

a + b is integer addition (84), and then adding the String s concatenates "42", giving "8442".

Step-by-step solution

  1. S is "42". a is the int 42 from parseInt, and b is the Integer 42 from valueOf.
  2. A + b is numeric addition: 42 + 42 = 84.
  3. 84 + s joins the String "42" as text, giving "8442".
  4. The output is 8442, option A.

Why the other options are wrong

  • B. 84 is the sum, but the String "42" is then appended, so the output is not just 84.
  • C. The first addition is numeric, so it is 84, not 42 followed by 42.
  • D. A + b is 84, not 42 repeated three times.

Tip: Numbers add first when they come before a String. After a String, + joins text.

Question 2Easy

What is the output?

public class Main {
    public static void main(String[] args) {
        Integer a = 100, b = 100;
        Integer c = 200, d = 200;
        System.out.println((a == b) + " " + (c == d));
    }
}
  1. true false
  2. true true
  3. false false
  4. false true

Answer: A. true false

Explanation

Autoboxing uses Integer.valueOf, which caches values from -128 to 127. 100 comes from the cache; 200 creates two separate objects.

Step-by-step solution

  1. Autoboxing uses Integer.valueOf, which caches values from -128 to 127.
  2. A and b hold 100, which is cached, so both are the same object and a == b is true.
  3. C and d hold 200, which is outside the cache, so each boxing creates a separate object and c == d is false.
  4. The output is true false, option A.

Why the other options are wrong

  • B. 200 is outside the cache, so the two objects are different and == is false.
  • C. 100 is in the cache, so the first comparison is true.
  • D. 100 is cached, so the first value is true, and 200 is not cached, so the second is false.

Tip: == on Integer compares references. Values from -128 to 127 are cached, larger values are not.

Question 3Easy

Which method converts the String "15" to a primitive int?

  1. String.valueOf("15")
  2. Integer.toString("15")
  3. Integer.parseInt("15")
  4. Integer.valueOf("15").toString()

Answer: C. Integer.parseInt("15")

Explanation

parseInt returns a primitive int. valueOf returns an Integer object; String.valueOf and Integer.toString produce strings.

Step-by-step solution

  1. Integer.parseInt converts string to primitive int.
  2. It is the correct method for converting string to int.
  3. Other options return objects or strings, not primitives.
  4. So the answer is Integer.parseInt("15").
  5. Final answer: Integer.parseInt("15")

Why the other options are wrong

  • A. String.valueOf returns a String, not a primitive int, so it cannot convert 15 to an int.
  • B. Integer.toString takes an int argument and returns a String, so it does not give a primitive int.
  • D. Integer.valueOf gives an Integer object and toString returns a String, so no primitive int is produced.

Tip: Use parseInt for converting strings to primitive ints in Java.

Question 4Easy

What is the output?

public class Main {
    public static void main(String[] args) {
        byte b = (byte) 130;
        System.out.println(b);
    }
}
  1. 127
  2. -126
  3. 2
  4. 130

Answer: B. -126

Explanation

byte holds -128..127. Casting keeps the low 8 bits: 130 - 256 = -126.

Step-by-step solution

  1. A byte holds values from -128 to 127.
  2. 130 is outside that range, so the cast keeps only the low 8 bits.
  3. 130 - 256 = -126, which lies inside the byte range.
  4. The output is -126, option B.

Why the other options are wrong

  • A. 127 is the largest byte value; 130 wraps around, it is not capped.
  • C. The low 8 bits of 130 give -126, not 2.
  • D. A byte cannot hold 130, so the cast changes the value.

Tip: For a narrowing cast to byte, add or subtract 256 until the value lies between -128 and 127.

Question 5Medium

What is the output?

public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.parseInt("12abc"));
    }
}
  1. Compilation error
  2. 0
  3. 12
  4. NumberFormatException

Answer: D. NumberFormatException

Explanation

parseInt requires the whole string to be a valid integer. Trailing letters make it throw NumberFormatException.

Step-by-step solution

  1. parseInt needs the whole string to be a valid number.
  2. '12abc' has letters after 12.
  3. So it throws NumberFormatException at runtime.

Why the other options are wrong

  • A. The code compiles fine; the problem appears only when it runs.
  • B. parseInt never returns 0 for bad input; it throws.
  • C. It does not stop at the first invalid character.

Tip: Bad number text means NumberFormatException, not a partial result.

Question 6Medium

What is the output?

public class Main {
    public static void main(String[] args) {
        Integer n = null;
        int m = n;
        System.out.println(m);
    }
}
  1. NullPointerException
  2. Compilation error
  3. 0
  4. null

Answer: A. NullPointerException

Explanation

Assigning an Integer to an int unboxes it by calling n.intValue(), which throws NullPointerException when n is null.

Step-by-step solution

  1. Integer n = null is allowed, so n holds null.
  2. Int m = n unboxes n by calling intValue().
  3. Unboxing a null reference throws NullPointerException on this line, so println is never reached.
  4. The output is NullPointerException, option A.

Why the other options are wrong

  • B. The code compiles, because assigning an Integer to an int is valid unboxing.
  • C. Unboxing does not turn null into 0; it throws an exception.
  • D. Println is never reached, and an int variable cannot hold null.

Tip: Unboxing null throws NullPointerException. Check for null before unboxing.

Question 7Medium

What is the output?

public class Main {
    public static void main(String[] args) {
        char c = 'a';
        c += 2;
        char d = 'a' + 1;
        System.out.println(c + "" + d + (char) (c + 1));
    }
}
  1. Compilation error
  2. cbd
  3. 99b100
  4. cb100

Answer: B. cbd

Explanation

Compound assignment includes an implicit cast, and 'a' + 1 is a constant that fits in char, so both compile. c is 'c', d is 'b', and (char)(c + 1) is 'd'.

Step-by-step solution

  1. C is 'a'. c += 2 adds 2 and casts back to char, so c is 'c'.
  2. 'a' + 1 is the constant 98, which fits in char, so d is 'b'.
  3. C + "" is "c", then + d gives "cb", then + (char) (c + 1) adds 'd', giving "cbd".
  4. The output is cbd, option B.

Why the other options are wrong

  • A. Both lines compile: += includes an implicit cast, and the constant 'a' + 1 fits in char.
  • C. The first + has a String, so c and d print as letters, not as numbers 99 and 98.
  • D. The last part is (char) (c + 1), which prints d, not 100.

Tip: Compound assignment casts back to the variable's type. A constant expression that fits in char is allowed without a cast.

Question 8Medium

What is the output?

public class Main {
    public static void main(String[] args) {
        System.out.println((Integer.MAX_VALUE + 1) + " " + (Math.abs(Integer.MIN_VALUE) < 0));
    }
}
  1. -2147483648 true
  2. 2147483648 false
  3. ArithmeticException
  4. -2147483648 false

Answer: A. -2147483648 true

Explanation

int arithmetic wraps on overflow, so MAX_VALUE + 1 is MIN_VALUE. MIN_VALUE has no positive int counterpart, so Math.abs returns it unchanged (negative).

Step-by-step solution

  1. Integer.MAX_VALUE + 1 overflows int and wraps around to Integer.MIN_VALUE, which is -2147483648.
  2. Math.abs(Integer.MIN_VALUE) has no positive int counterpart, so it returns MIN_VALUE, which is still negative.
  3. So the second check, that the value is less than 0, is true.
  4. The output is -2147483648 true, option A.

Why the other options are wrong

  • B. Int cannot hold 2147483648; the sum wraps to a negative number, and the abs check is true.
  • C. Integer overflow in Java wraps around silently and does not throw an exception.
  • D. Math.abs of MIN_VALUE stays negative, so the comparison with 0 is true, not false.

Tip: Int overflow wraps around silently, and Math.abs(Integer.MIN_VALUE) stays negative.

Question 9Hard

What is the output?

public class Main {
    static void m(long x) { System.out.print("long "); }
    static void m(Integer x) { System.out.print("Integer "); }
    static void m(int... x) { System.out.print("varargs "); }

    public static void main(String[] args) {
        byte b = 1;
        m(b);
        Integer i = 5;
        m(i);
        m();
    }
}
  1. long Integer varargs
  2. varargs Integer varargs
  3. Integer Integer varargs
  4. long long varargs

Answer: A. long Integer varargs

Explanation

byte widens to long (widening beats boxing). An Integer argument matches m(Integer) exactly. With no arguments only the varargs method applies.

Step-by-step solution

  1. m(b): the byte widens to long in the exact phase, so m(long) runs and prints long.
  2. m(i): the Integer i matches m(Integer) without unboxing, so it prints Integer.
  3. m(): with no arguments only the varargs method m(int...) applies, so it prints varargs.
  4. The output is long Integer varargs, option A.

Why the other options are wrong

  • B. Byte widens to long in the first phase, so m(long) is chosen, not the varargs method.
  • C. A byte does not match Integer without boxing, and widening to long wins, so the first call is long.
  • D. The Integer matches m(Integer) exactly, so it does not go to m(long) through unboxing.

Tip: Java picks overloads in phases: exact or widening first, then boxing, then varargs.

Question 10Hard

What is the output?

public class Main {
    public static void main(String[] args) {
        Double nan = Double.NaN;
        System.out.println((Double.NaN == Double.NaN) + " " + nan.equals(Double.NaN));
    }
}
  1. true true
  2. true false
  3. false true
  4. false false

Answer: C. false true

Explanation

By IEEE 754, NaN is not equal to itself with ==. Double.equals compares the bit patterns, so it treats NaN as equal to NaN.

Step-by-step solution

  1. Double.NaN == Double.NaN follows IEEE 754, where NaN is never equal to anything, so it is false.
  2. nan.equals(Double.NaN) uses Double.equals, which compares bit patterns, so it is true.
  3. The output is false true, option C.

Why the other options are wrong

  • A. Under IEEE 754, == with NaN is always false, so the first value is not true.
  • B. Double.NaN == Double.NaN is false, not true.
  • D. Double.equals treats NaN as equal to NaN, so the second value is true.

Tip: Test for NaN with Double.isNaN(x). The == operator never reports NaN as equal.

Practise all 23 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Core Java topics · All topics