Java MCQs – Classes and Objects

21.) What is a class in Java?

A) An instance of an object
B) A template for creating objects
C) A predefined method in Java
D) A data type for variables

Answer: Option B

Explanation: A class in Java defines a blueprint for creating objects. It encapsulates attributes (data) and methods (behavior) that define the characteristics and actions of objects.

22.) Which keyword is used to create an instance of a class in Java?

A) new
B) this
C) class
D) object

Answer: Option A

Explanation: The “new” keyword is used to allocate memory and create an instance of a class.

23.) What is an object reference in Java?

A) The memory address of an object
B) The name of a class
C) A keyword for creating objects
D) A variable that holds the memory location of an object

Answer: Option D

Explanation: An object reference is a variable that stores the memory address of an object, allowing you to access and manipulate the object.

24.) Which of the following is a valid way to access an instance variable of an object in Java?

A) ClassName.variableName
B) objectName.variableName
C) variableName.objectName
D) variableName.ClassName

Answer: Option B

Explanation: Instance variables are accessed using the object name followed by the dot operator.

25.) What is the “constructor” of a class in Java?

A) A method used for destroying objects
B) A reserved keyword indicating class creation
C) A method used to initialize objects
D) A method used for inheritance

Answer: Option C

Explanation: A constructor is a special method in a class used to initialize the attributes of an object when it is created.

26.) Which of the following is true about the default constructor in Java?

A) It takes parameters and initializes object attributes.
B) It is provided by the compiler if no constructor is defined.
C) It is used to destroy objects.
D) It can be overloaded with multiple constructors.

Answer: Option B

Explanation: If no constructor is explicitly defined in a class, the compiler provides a default constructor that initializes attributes to default values.

27.) What is the relationship between a method and an object in Java?

A) A method is an object.
B) An object is a method.
C) A method operates on an object’s data and behavior.
D) A method creates objects.

Answer: Option C

Explanation: Methods define the actions that an object can perform and operate on the object’s attributes (data) and methods (behavior).

28.) In Java, can a class have multiple methods with the same name but different parameters?

A) Yes, but it is discouraged.
B) No, it will result in a compilation error.
C) Yes, and it is called method overloading.
D) No, it violates encapsulation principles.

Answer: Option C

Explanation: Method overloading allows a class to have multiple methods with the same name but different parameter lists, providing flexibility and clarity.

29.) What is the purpose of the “static” keyword in Java class members (fields and methods)?

A) It indicates that the member is dynamically allocated.
B) It signifies that the member belongs to the object.
C) It indicates that the member is shared among all instances of the class.
D) It is used to override methods.

Answer: Option C

Explanation: The “static” keyword is used to define class-level variables (static fields) and methods (static methods) that are shared among all instances of the class.

30.) What is the access modifier used to specify that a class member can be accessed by any class in the same package?

A) private
B) protected
C) protected
D) default (package-private)

Answer: Option D

Explanation: The default access modifier (no modifier) allows class members to be accessible within the same package but not outside it.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework