var & Local Type Inference — Concept Check
Questions on var & Local Type Inference. Use var (Java 10+) to let the compiler infer local variable types — less boilerplate, same static safety.
Questions in this quiz
7 questions on var & Local Type Inference — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.In Java 10, what happens if you declare a local variable using 'var' without an initializer?
- AThe variable is inferred as null
- BThe variable type is inferred as Object
- CRuntime error occurs when the variable is used
- DCompile-time error due to missing initializer
2.You have a method `int calculate()`. What is the type of `result` when declared as `var result = calculate();`?
- ACompile-time error
- BInteger
- Cint
- DObject
3.In a for-each loop, you declare `var item`. What must be true for this declaration to work?
- AThe collection must be a Map
- BThe loop must iterate over a List
- CThe collection must be non-empty
- DThe collection must have a known element type
4.What is a valid use of 'var' in Java 10?
- ADeclaring class fields
- BDeclaring return types
- CDeclaring a local variable with an initializer
- DDeclaring method parameters
5.Why might using 'var' in a stream pipeline be advantageous?
- AIt enforces stronger type safety
- BIt reduces visual noise
- CIt improves runtime performance
- DIt allows dynamic typing
6.A developer uses 'var' for a variable initialized with `null`. What is the result?
- AThe variable type is inferred as Object
- BCompile-time error
- CRuntime error when the variable is used
- DThe variable is initialized as null
7.What happens if you use 'var' for a field in a Java class?
- ACompile-time error
- BField type is inferred as Object
- CField is initialized to null
- DRuntime error on field access