Java Multiple-Choice Quiz
Read short Java snippets about types, strings, constructors, and loops. The final questions cover overloaded methods, list operations, and what happens when two variables refer to the same object.
Ages 13+ · beginning Java learners and classroom reviewWhich primitive type belongs in the blank in ___ ready = true?
Ready to play?
Open at Step 3 with every question loaded. Play solo, host live, or customize your game.
Play & Customize GameQuestions by ladder level
Which primitive type belongs in the blank in ___ ready = true?
- boolean
- int
- char
- double
Show answer and explanation
Correct answer: A. boolean
The literal true has type boolean. Java uses this type for true and false values.
The declaration long stars = 5_000_000_000L uses an L after the number. What does the L indicate?
- The literal has type double
- The variable can be assigned only once
- The literal has type long
- The value is rounded to fit an int
Show answer and explanation
Correct answer: C. The literal has type long
An L suffix gives the integer literal type long. Underscores separate groups of digits for readability.
What value does the Java expression -17 / 5 produce?
- -4
- -3
- 3
- -3.4
Show answer and explanation
Correct answer: B. -3
Both operands have integer type. Java integer division rounds the quotient toward zero, so the result is -3.
What text does the Java expression "Tray " + 2 + 3 produce?
- 23 Tray
- Tray 5
- 5 Tray
- Tray 23
Show answer and explanation
Correct answer: D. Tray 23
The additions group from left to right. The first + joins the string and 2 to make Tray 2, then the second appends 3.
An int[] named sizes contains {4, 7, 9} in that order. Which value does sizes[1] read?
- 4
- 7
- 9
- 1
Show answer and explanation
Correct answer: B. 7
Java gives the first array element index 0. Index 1 reads the second element, which is 7.
What string does "planet".substring(1, 4) return?
- lan
- plane
- lane
- pla
Show answer and explanation
Correct answer: A. lan
The substring begins at index 1 and ends before index 4. Those positions contain l, a, and n.
Basket has a field int count and a constructor Basket(int count) { this.count = count; }. What value does new Basket(6).count read?
- Undefined
- 0
- 1
- 6
Show answer and explanation
Correct answer: D. 6
The right-hand count is the constructor parameter. The assignment stores its value, 6, in the new object's field this.count.
Both value and total are ints, starting at 5 and 0. The program runs do { total += value; value--; } while (value < 3); What is total afterward?
- 4
- 0
- 5
- 9
Show answer and explanation
Correct answer: C. 5
The body runs once, adding 5 and reducing value to 4. Java then tests 4 < 3, so the loop ends.
Both a and b are initialized by their own new String("mint") expression. In order, what are the results of a.equals(b) and a == b?
- true, false
- true, true
- false, true
- false, false
Show answer and explanation
Correct answer: A. true, false
The two strings contain the same text, so equals returns true. The new expressions create separate objects, so the reference comparison returns false.
What is total after this Java snippet? int total = 0; for (int i = 1; i < 5; i++) { if (i == 2) continue; total += i; }
- 6
- 10
- 1
- 8
Show answer and explanation
Correct answer: D. 8
At i = 2, continue skips the addition and the loop proceeds with its update. The values added are 1, 3, and 4, giving 8.
The UTF-16 code unit for 'A' has the numeric value 65. What is the type and value of the Java expression 'A' + 1?
- A String with the value A1
- A char with the value 'B'
- An int with the value 66
- An int with the value 65
Show answer and explanation
Correct answer: C. An int with the value 66
For this numeric addition, Java promotes the char operand to int. It adds 65 + 1 and produces the int value 66.
One class declares pick(int n), which returns "int", and pick(double n), which returns "double". What does the call pick(3) return?
- double
- int
- 3
- 3.0
Show answer and explanation
Correct answer: B. int
The literal 3 has type int. The int overload is the more specific applicable choice for that argument.
An ArrayList<Integer> named values contains [7, 3, 1]. What is in the list after values.remove(1)?
- [7, 3, 1]
- [7, 3]
- [3, 1]
- [7, 1]
Show answer and explanation
Correct answer: D. [7, 1]
The argument 1 is an int, so Java calls the overload that removes by index. Index 1 holds 3. Removing it leaves [7, 1].
Label declares an instance method text() that returns "plain". BrightLabel extends Label and overrides text() to return "bright". Given Label tag = new BrightLabel(), what does tag.text() return?
- bright
- plain
- BrightLabel
- null
Show answer and explanation
Correct answer: A. bright
Java looks up the overriding instance method using the actual object's class. The object is a BrightLabel, so its text method returns bright.
The method adjust(int[] values) first sets values[0] = 9 and then sets values = new int[]{4}. The caller starts with int[] boxes = {1, 2} and calls adjust(boxes). What does boxes contain afterward?
- [1, 2]
- [4]
- [9, 2]
- [9]
Show answer and explanation
Correct answer: C. [9, 2]
The parameter initially holds a copy of the reference to boxes, so the first assignment changes that array. Assigning a new array changes only the method's local parameter.