Examveda
Examveda

Which keyword is used to declare a variable that can hold a reference to an object of any class in Java?

A. object

B. class

C. var

D. generic

Answer: Option C

Solution(By Examveda Team)

The correct answer is Option C: var.

In Java, the var keyword is used to declare a variable that can hold a reference to an object of any class without specifying the exact data type explicitly. It was introduced in Java 10 to support local type inference.

Here's the breakdown:

Option A: object - There is no object keyword for declaring variables with generic reference types in Java.

Option B: class - The class keyword is used for defining and declaring classes in Java, not for declaring variables with generic reference types.

Option C: var - This is the correct answer. The var keyword is used for declaring variables with inferred types, allowing them to hold references to objects of any class.

Option D: generic - The term generic usually refers to generic types and classes in Java, but it is not a keyword used for declaring variables.

So, the keyword used to declare a variable that can hold a reference to an object of any class in Java is var.

This Question Belongs to Java Program >> Data Types And Variables

Join The Discussion

Related Questions on Data Types and Variables