MCQs on Java Object-Oriented Programming (OOP) Concepts
Object-Oriented Programming (OOP) in Java is foundational for building modular and reusable software. These MCQs cover key concepts such as classes, objects, inheritance, polymorphism, abstraction, and encapsulation.
Classes and Objects
What is the main purpose of a class in Java?
a) To store variables
b) To define methods and variables
c) To instantiate objects
d) To perform calculations
How do you create an object of a class in Java?
a) new ClassName();
b) ClassName object = new ClassName();
c) object = ClassName();
d) object = new ClassName;
What is the default value of an integer object in Java?
a) 0
b) null
c) 1
d) Undefined
Which of the following is a valid class declaration in Java?
a) class MyClass { }
b) public MyClass { }
c) class public MyClass { }
d) MyClass class { }
What is the role of a constructor in Java?
a) To initialize the values of an object
b) To perform arithmetic operations
c) To declare methods
d) To destroy an object
Encapsulation and Abstraction
What is encapsulation in Java?
a) Binding data and methods together
b) Hiding the implementation details
c) Creating inheritance relationships
d) Using multiple classes
How do you achieve encapsulation in Java?
a) By using public access modifiers for all fields
b) By using private fields and public getter and setter methods
c) By declaring all variables static
d) By using abstract classes
Which of the following is an example of abstraction in Java?
a) A class that defines only methods without implementation
b) A class that hides implementation details from the user
c) A method that performs calculations
d) A constructor that initializes objects
What is the main benefit of encapsulation in Java?
a) Increased speed of execution
b) Better control over data and behavior
c) Reduced program size
d) Faster program compilation
Which keyword is used to hide the implementation details of a class in Java?
a) abstract
b) private
c) public
d) final
Inheritance and Polymorphism
What is inheritance in Java?
a) A way to extend the functionality of a class
b) A method to initialize object values
c) A process to remove objects from memory
d) A way to protect data from external access
Which keyword is used to inherit a class in Java?
a) extends
b) implements
c) inherits
d) super
What is polymorphism in Java?
a) The ability of an object to take many forms
b) The process of hiding data
c) The ability to create multiple constructors
d) The inheritance of class methods
Which of the following is an example of polymorphism in Java?
a) A class having multiple constructors
b) A method that has different implementations for different data types
c) A class inheriting from another class
d) A class having only one method
What does the super keyword refer to in Java?
a) The current instance of a class
b) The parent class of the current class
c) The constructor of the current class
d) The methods of the current class
Constructors and Destructors
What is the primary role of a constructor in Java?
a) To initialize an object’s state
b) To perform cleanup tasks
c) To assign default values to variables
d) To declare variables
Which of the following is true about constructors in Java?
a) They have a return type
b) They can be overloaded
c) They cannot accept parameters
d) They are always private
What is the default constructor in Java?
a) A constructor that takes no arguments
b) A constructor that performs cleanup tasks
c) A constructor with parameters
d) A constructor that initializes variables to null
Does Java have destructors like C++?
a) Yes, they are used for resource management
b) No, Java uses garbage collection for memory management
c) Yes, but they are called manually
d) No, constructors handle memory management
What happens if a class does not define a constructor in Java?
a) The compiler automatically creates a default constructor
b) The class will not compile
c) The object will not be created
d) The constructor is inherited from the parent class
Combining OOP Concepts
What does the term “method overriding” refer to in Java?
a) Redefining a method in a subclass that already exists in the superclass
b) Creating a new method in a subclass
c) Modifying the signature of a method
d) Declaring methods in interfaces
What is the difference between abstract class and interface in Java?
a) An abstract class can have abstract methods, while an interface can only have concrete methods
b) An abstract class can have both abstract and concrete methods, while an interface can have only abstract methods
c) An abstract class can be instantiated, while an interface cannot
d) There is no difference between them
What is the role of the this keyword in Java?
a) To refer to the current object
b) To refer to the parent class
c) To refer to static variables
d) To initialize variables in a constructor
How can a subclass call the constructor of its superclass?
a) Using super()
b) Using this()
c) By calling parent()
d) By directly invoking the constructor name
Which of the following is NOT a feature of Object-Oriented Programming in Java?
a) Inheritance
b) Encapsulation
c) Garbage collection
d) Modularity
Object-Oriented Principles
Which of the following is an example of encapsulation in action?
a) A class with private fields and public getter/setter methods
b) A class that uses inheritance to extend another class
c) A method that is abstract
d) A class that implements an interface
What is method overloading in Java?
a) Having multiple methods with the same name but different parameters
b) Changing the implementation of a method in a subclass
c) Providing a method with no implementation
d) Creating methods with different names but similar behavior
Which of the following best describes inheritance in Java?
a) A subclass inherits methods and attributes from a superclass
b) A class can implement multiple interfaces
c) A class has the ability to be reused across projects
d) A class can only have one constructor
What is the output of the following code snippet in Java?csharpCopy codeclass Parent { void display() { System.out.println("Parent display"); } } class Child extends Parent { void display() { System.out.println("Child display"); } } public class Main { public static void main(String[] args) { Parent obj = new Child(); obj.display(); } }
a) Parent display
b) Child display
c) Error
d) No output
What is the purpose of super() in a constructor in Java?
a) To call the superclass constructor
b) To assign values to the fields
c) To return values from the constructor
d) To initialize static fields
Answer Key:
Qno
Answer
1
b) To define methods and variables
2
b) ClassName object = new ClassName();
3
b) null
4
a) class MyClass { }
5
a) To initialize the values of an object
6
a) Binding data and methods together
7
b) By using private fields and public getter and setter methods
8
b) A class that hides implementation details from the user
9
b) Better control over data and behavior
10
a) abstract
11
a) A way to extend the functionality of a class
12
a) extends
13
a) The ability of an object to take many forms
14
b) A method that has different implementations for different data types
15
b) The parent class of the current class
16
a) To initialize an object’s state
17
b) They can be overloaded
18
a) A constructor that takes no arguments
19
b) No, Java uses garbage collection for memory management
20
a) The compiler automatically creates a default constructor
21
a) Redefining a method in a subclass that already exists in the superclass
22
b) An abstract class can have both abstract and concrete methods, while an interface can have only abstract methods
23
a) To refer to the current object
24
a) Using super()
25
c) Garbage collection
26
a) A class with private fields and public getter/setter methods
27
a) Having multiple methods with the same name but different parameters
28
a) A subclass inherits methods and attributes from a superclass