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

Given:

and the code fragment:

Which two lines cause compilation errors? (Choose two.)

    Correct Answer: A, E

    Lines 6 and 7 cause compilation errors. The 'var' keyword in Java is used to infer the type of a local variable at compile time. However, 'var' cannot be used for member variables (fields) in a class, method parameters, or return types. In the code fragment provided, 'var' is incorrectly used for the field 'employee' in line 6 and as a method return type in line 7, both of which are not allowed.

Discussion
ASPushkinOptions: AE

AE var can be used for declaring local variables, including index variables of for-loops and resource variables of the try-with-resources statement. var cannot be used for fields, method parameters, and method return types. From Java 11 you can use var in lambda expressions

d7bb0b2

yes +1

d7bb0b2Options: AE

var cannot be used for fields instance, method parameters, and method return types.