QuestionQ59

Working with Java Data Types

Which three local-variable initialization statements are valid?

Choose two
  • A
  • B Float array[][] = {10.0F,20.23F,30.3F};
  • C Integer smallChange = 1d;
  • D Boolean isTrue = TRUE;
  • E var var = 9.99;
Explanation

A text block literal is valid Java syntax as long as the opening """ is immediately followed by a line terminator, so a String[] initializer that mixes a multi-line text block with an ordinary quoted string compiles without issue. Separately, the identifier var is only a reserved type name used for local-variable type inference in a type position; it is not a reserved word, so it remains legal as the name of a variable, meaning a declaration such as var var = 9.99; compiles and infers the type double for the variable named var. The other candidate statements fail to compile: a Float[][] requires each initializer element to itself be a Float[] rather than a scalar Float, a double-typed literal cannot be assigned to an Integer target without an explicit narrowing cast (the JLS constant-narrowing exception applies only to byte/short/char/int constants, not double), and TRUE is not a predefined identifier in Java (the boolean literal is lowercase true, and Boolean.TRUE requires the class qualifier or a static import).

Community Discussion

No comments yet. Be the first to start the discussion!