nerdexam
Oracle

1Z0-811 · Question #33

Which statement is valid?

The correct answer is A. int 2 totalScore = 0. There appears to be an error in the provided answer key - A is not correct. int 2 totalScore = 0; is invalid because variable names cannot begin with a digit, and it contains a space, making it unparseable. The actual correct answers are B and C (both are valid declarations)…

Java Basics

Question

Which statement is valid?

Options

  • Aint 2 totalScore = 0;
  • Bint totalScore = 0;
  • Cint totalScore2 = 0;
  • Dint total score = 0;

How the community answered

(45 responses)
  • A
    91% (41)
  • B
    4% (2)
  • C
    2% (1)
  • D
    2% (1)

Explanation

There appears to be an error in the provided answer key - A is not correct. int 2 totalScore = 0; is invalid because variable names cannot begin with a digit, and it contains a space, making it unparseable. The actual correct answers are B and C (both are valid declarations). Here's the proper breakdown:

B (int totalScore = 0;) and C (int totalScore2 = 0;) are both valid - identifiers can contain digits, they just cannot start with one.

A (int 2 totalScore = 0;) is invalid for two reasons: it starts with a digit (2) and contains a space, neither of which is allowed in an identifier.

D (int total score = 0;) is invalid because the space makes total score two separate tokens - the compiler sees total as the variable name and score as an unexpected symbol.

Memory tip: Think of variable names like usernames - they can have letters and numbers, but must start with a letter (or _/$), and spaces are never allowed.

Note for the exam: If this is a single-answer question and you must choose one, B is the safer pick as it's the most canonical example. But you should flag this with your instructor, as C is equally valid.

Topics

#variable naming rules#identifier syntax#variable declaration#Java syntax

Community Discussion

No community discussion yet for this question.

Full 1Z0-811 Practice