Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 94

Which two var declarations are correct? (Choose two.)

    Correct Answer: A, D

    The correct declarations are 'var names = new ArrayList<>();' and 'var y = null;'. The first declaration is correct as 'var' can infer the type of 'ArrayList'. The second declaration is correct because 'var' can infer the type of a variable initialized to 'null' based on further assignments. The declaration 'var var = “hello”;' is incorrect because 'var' cannot be used as a variable name. Similarly, 'var _ = 100;' is incorrect because '_' is a reserved keyword and cannot be used as an identifier. The declaration 'var a;' is incorrect because 'var' requires an initializer to infer the type.

Discussion
ASPushkinOptions: AD

A. it works var will infer the type of list to be ArrayList<Object> --- DE var can only be used when there is an initializer. var person = null; // ERROR --- C Ok var isn't keyword ----- B. failed as of release 9, '_' is a keyword, and may not be used as an identifier

[Removed]

var identifier is allowed, '_' is not allowed. Only var y = (String) null; for example is allowed SO A & C are correct.

StavokOptions: AC

A and C are correct