nerdexam
Oracle

1Z0-819 · Question #160

Which of these initialization statements are correct? (Choose three.)

The correct answer is A. int x = 12_34; B. short sh = (short) 'A'; F. int[][] x = {{1,1},{2,2}}. Options A, B, and F are valid Java initialization statements: A (int x = 12_34) correctly uses the underscore-in-numeric-literals feature from Java 7, where underscores between digits are legal and the value simply equals 1234; B (short sh = (short) 'A') performs a valid…

Working with Java Data Types

Question

Which of these initialization statements are correct? (Choose three.)

Options

  • Aint x = 12_34;
  • Bshort sh = (short) 'A';
  • CString name = new String("1+2")+(999 (232)");
  • DBoolean true = (4 == 4);
  • Efloat f = 1.6f;
  • Fint[][] x = {{1,1},{2,2}};
  • Gbyte b = 10;char c = b;

How the community answered

(46 responses)
  • A
    85% (39)
  • C
    4% (2)
  • D
    2% (1)
  • E
    9% (4)

Explanation

Options A, B, and F are valid Java initialization statements: A (int x = 12_34) correctly uses the underscore-in-numeric-literals feature from Java 7, where underscores between digits are legal and the value simply equals 1234; B (short sh = (short) 'A') performs a valid explicit narrowing cast from char (Unicode value 65) to short, which fits within the short range; and F (int[][] x = {{1,1},{2,2}}) uses correct 2D array initializer shorthand syntax.

The distractors fail for distinct reasons: C has a syntax error with malformed string/parenthesis structure; D uses true as a variable name, which is illegal because true is a reserved literal in Java; G tries to implicitly assign a byte to a char without a cast - illegal because byte is signed and char is unsigned, requiring char c = (char) b;; and E (float f = 1.6f) is worth flagging - it is actually syntactically valid Java, so if your exam marks it wrong, that may be an exam error.

Memory tip: When scanning initialization questions, hunt for three traps in order: reserved words used as identifiers (true, false, null), implicit narrowing conversions missing a cast (especially bytechar), and broken literal syntax - these cover the majority of trick answers.

Topics

#type casting#primitive types#numeric literals#array initialization

Community Discussion

No community discussion yet for this question.

Full 1Z0-819 Practice