nerdexam
Oracle

1Z0-811 · Question #37

Given: 1. class Test { 2. / comment text 1 / 3. // comment text 2 // 4. / comment text 3 5. and comment text 4 // 6. / comment text 5 7. and comment text 6 */ 8. } At which line does a compilation…

The correct answer is B. line 2. Option B states line 2 (/ comment text 1 /) causes a compilation error, but this is actually a valid standalone block comment in Java - there is no error here, and this stated answer appears to be incorrect. To clarify the actual Java comment rules that this question tests…

Java Basics

Question

Given:
  1. class Test {
  2. /* comment text 1 */
  3. // comment text 2 //
  4. /* comment text 3
  5. and comment text 4 //
  6. /* comment text 5
  7. and comment text 6 */
  8. }
At which line does a compilation error occur?

Options

  • Aline 5
  • Bline 2
  • Cline 3
  • Dline 7

How the community answered

(40 responses)
  • A
    3% (1)
  • B
    90% (36)
  • C
    3% (1)
  • D
    5% (2)

Explanation

Option B states line 2 (/* comment text 1 */) causes a compilation error, but this is actually a valid standalone block comment in Java - there is no error here, and this stated answer appears to be incorrect.

To clarify the actual Java comment rules that this question tests: Java block comments (/* */) do not nest. The /* on line 6 is inside the block comment opened on line 4, so it is treated as plain comment text and ignored. The */ on line 7 correctly closes the block comment from line 4 - not a nested "inner" comment.

Lines 3, 5, and 7 (distractors A, C, D) are all valid: the // on line 3 is a legal single-line comment (trailing // is just comment text), the // on line 5 is inside a block comment and ignored, and */ on line 7 is the correct and expected closing of the block comment.

The entire code block compiles without errors. The intended trap is likely testing whether students mistakenly believe /* */ comments nest - if they did, */ on line 7 would only close the "inner" /* from line 6, leaving the outer comment from line 4 unclosed and } on line 8 swallowed. That would cause an error, but it is not how Java works.

Memory tip: Think of Java block comments as a light switch, not a stack - the first */ always turns off the light, no matter how many /* you saw in between.

Topics

#Java Comments#Comment Syntax#Compilation Errors#Block Comments

Community Discussion

No community discussion yet for this question.

Full 1Z0-811 Practice