Java MCQs – Classes and Objects

Java’s Classes and Objects with a curated set of Multiple Choice Questions (MCQs). These questions delve into the heart of Object-Oriented Programming (OOP) by examining the building blocks of Java applications – classes and objects. Test your knowledge of concepts like instantiation, encapsulation, constructors, methods, and instance variables. Whether you’re a beginner seeking a strong foundation or an experienced programmer aiming to reinforce your expertise, these MCQs offer a comprehensive assessment of your understanding. Get ready to unlock the secrets of Java’s Classes and Objects and elevate your programming prowess.

1.) What is the “this” keyword used in Java?

A) To create a new instance of a class
B) To refer to the current instance of the class
C) To access static members of the class
D) To define constructors

Answer: Option B

Explanation: The “this” keyword is used to refer to the current instance of the class and is often used to disambiguate between instance variables and method parameters with the same name.

2.) Which of the following is NOT a valid way to create an object in Java?

A) MyClass obj = new MyClass();
B) obj = new MyClass();
C) new obj = MyClass();
D) MyClass obj = new();

Answer: Option C

Explanation: Option c is not a valid syntax for creating an object. The correct syntax is MyClass obj = new MyClass();.

3.) What is a class constructor’s purpose in Java?

A) To define the attributes of a class
B) To create an instance of a class
C) To destroy an instance of a class
D) To hide the implementation of a class

Answer: Option B

Explanation: A class constructor is used to create an instance (object) of the class and initialize its attributes.

4.) Which keyword is used to prevent a class from being inherited by other classes?

A) inherit
B) sealed
C) prevent
D) final

Answer: Option D

Explanation: The “final” keyword is used to indicate that a class cannot be subclassed or inherited by other classes.

5.) What is the relationship between a class and an object in Java?

A) An object is an instance of a class.
B) A class is an instance of an object.
C) A class and an object are the same thing.
D) An object inherits from a class.

Answer: Option A

Explanation: An object is a specific instance created from a class’s blueprint, which defines its attributes and behaviors.

6.) What is the default value of an instance variable (non-static field) of an object in Java if not explicitly initialized?

A) 0
B) null
C) false
D) Depends on the data type

Answer: Option B

Explanation: The default value of an instance variable (object reference) is “null” if not explicitly initialized.

7.) Can a class have multiple constructors in Java?

A) No, a class can only have one constructor.
B) Yes, but only if they have the same parameter list.
C) Yes, a class can have multiple constructors with different parameter lists.
D) Yes, but only if they have the same method name.

Answer: Option C

Explanation: A class can have multiple constructors with different parameter lists, enabling different ways to initialize objects.

8.) Which access modifier allows a class member to be accessible within the same package and by subclasses, even if they are in different packages?

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

Answer: Option B

Explanation: The “protected” access modifier allows class members to be accessible within the same package and by subclasses, regardless of whether they are in the same or different packages.

9.) What is the purpose of the “static” keyword for a class method in Java?

A) To indicate that the method can only be called from a static context
B) To create a new instance of the class
C) To prevent the method from being overridden
D) To indicate that the method belongs to the class, not an instance

Answer: Option D

Explanation: The “static” keyword is used to define a class-level method that belongs to the class itself, not to any specific instance of the class.

10.) What is the purpose of the “instanceof” operator in Java?

A) To compare two instances of the same class
B) To check if an object is null
C) To test if an object is of a particular class type
D) To access instance variables of an object

Answer: Option C

Explanation: The “instanceof” operator is used to test whether an object is an instance of a specific class or a class that inherits from it.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework