Java OCA OCP Practice Question 217

Question

Which is correct about a local variable of type String?

  • A. It defaults to an empty string.
  • B. It defaults to null.
  • C. It does not have a default value.
  • D. It will not compile without initializing on the declaration line.


C.

Note

Local variables do not have a default initialization value.

If they are referenced before being set to a value, the code does not compile.

Option C is correct.

If the variable was an instance variable, Option B would be correct.

Option D is tricky.

A local variable will compile without an initialization if it isn't referenced anywhere or it is assigned a value before it is referenced.




PreviousNext

Related