through Objects, 8th edition by Gaddis
1. What does the `main` method header look like?
A) `public void main(String args)`
B) `public static void main(String[] args)`
C) `static public main(String[] args)`
D) `public main(String[] args)`
Answer: B
Rationale: The JVM calls `public static void main(String[] args)` as the entry point.
2. Which data type stores a single character?
A) `String`
B) `char`
C) `boolean`
D) `int`
Answer: B
Rationale: `char` holds one Unicode character; `String` holds a sequence.
3. What is the result of `` in Java?
A) 3.5
B) 3
,C) 4
D) 3.0
Answer: B
Rationale: Integer division truncates the decimal; both operands are `int`.
4. Which operator gives the remainder?
A) `/`
B) `%`
C) `*`
D) `+`
Answer: B
Rationale: `%` is the modulus operator; it returns the remainder.
5. What is the value of `x` after `int x = 5; x += 3;`?
A) 5
B) 3
C) 8
D) 2
Answer: C
Rationale: `+=` adds the right operand to the variable; 5 + 3 = 8.
6. Which of these is a valid `if` statement?
2
,A) `if x > 5 then`
B) `if (x > 5)`
C) `if x > 5`
D) `if (x > 5) then`
Answer: B
Rationale: Java requires parentheses around the condition.
7. What is the output of `System.out.println("Hello" + 5 + 3);`?
A) `Hello53`
B) `Hello8`
C) `Hello 5 3`
D) `8Hello`
Answer: A
Rationale: Left-to-right: `"Hello" + 5` → `"Hello5"`; then `+ 3` → `"Hello53"`.
8. Which loop always executes at least once?
A) `for`
B) `while`
C) `do-while`
D) `foreach`
Answer: C
Rationale: `do-while` checks the condition after the body.
3
, 9. What is the correct way to declare an array of 10 ints?
A) `int[] arr = new int[10];`
B) `int arr = new int[10];`
C) `int arr[10];`
D) `array int arr[10];`
Answer: A
Rationale: Java syntax: `type[] name = new type[size];`.
10. What does `arr.length` return?
A) Last index
B) Number of elements
C) Size in bytes
D) First element
Answer: B
Rationale: `.length` gives the array’s capacity, not the last index.
11. Which method compares two strings for equality?
A) `==`
B) `equals()`
C) `compare()`
D) `equalsIgnoreCase()`
Answer: B
4