Java Programming MCQs — Programming
3 practice MCQs
The correct answer is marked on every question, with its full explanation.
ProgrammingJava Programming PPSC FPSC NTSID: NWF-036
What is the output of this Java code? int[] arr = {1, 2, 3}; System.out.println(arr.length);
- A3 ✓ Correct answer
- B2
- C4
- D0
✓ Correct answer: 3
Full explanation
The output is 3. The array arr holds three elements, 1, 2 and 3, so arr.length is 3. Why the others are wrong: 2 — the array has three elements, not two. 4 — the array has three elements, not four. 0 — the array is not empty.
ProgrammingJava Programming PPSC FPSC NTSID: PROG-004
What is the output of this Java code? System.out.println(5 + 3 * 2);
- A11 ✓ Correct answer
- B16
- C10
- D8
✓ Correct answer: 11
Full explanation
Multiplication is done before addition, so 3 * 2 = 6 and 5 + 6 = 11, so the program prints 11. Why the others are wrong: 16 — 16 comes from (5 + 3) * 2, which is wrong because multiplication is done first. 10 — 10 is 5 times 2, which is not the calculation here. 8 — 8 is 5 plus 3, which ignores the multiplication.
ProgrammingJava Programming PPSC FPSC NTSID: PROG-009
What is the output of this Java code? System.out.println(10 - 4);
- A2
- B14
- C6 ✓ Correct answer
- D40
✓ Correct answer: 6
Full explanation
6. The code subtracts 4 from 10, so 10 - 4 = 6. Why the others are wrong: '14' — this is 10 + 4, but the code subtracts. '40' — this is 10 * 4, but the code subtracts. '2' — this is 10 divided by 4 rounded down, but the code subtracts.
Practise these MCQs interactively →
← Programming MCQs