WorksheetsOOPS MCQ
Total questions: 78
Worksheet time: 44mins
What is the main purpose of an abstract class in Java?
To create a class that cannot be instantiated
To provide a template for subclasses
To implement multiple inheritance
To allow the use of inner classes
Which of the following keywords is used to define an abstract class?
final
abstract
static
virtual
In Java, which statement about inheritance is true?
A) A class can extend multiple classes
B) A subclass inherits methods from its superclass
C) Inheritance can only be achieved through interfaces
D) Constructors are inherited by subclasses
What is the purpose of the super keyword?
To call the parent class constructor
To reference the current class instance
To define an abstract method
To access private members of a superclass
Which of the following is NOT a feature of an inner class?
It can access members of its outer class
It can have static members
It can be private, public, or protected
It can extend another class
What is the result of using the equals() method in Java?
It compares object references
It compares object content
It must be overridden in every class
It compares class types
Why is it important to override the hashCode() method when overriding equals()?
To ensure unique object identity
To maintain the general contract for hash tables
To optimize memory usage
To allow method overloading
Which of the following is true about the Object class in Java?
All classes inherit from Object
Object class cannot be subclassed
Object class provides no methods
Object class has no constructors
In Java, what is the visibility of a protected attribute?
Visible to subclasses and classes in the same package
Visible only to classes in the same package
Visible to all classes
Visible only to subclasses
What is the visibility of private attributes in Java?
Accessible from anywhere
Accessible only within the same class
Accessible from subclasses
Accessible within its own package
Which of the following statements is true about multiple inheritance in Java?
A class can implement multiple interfaces
A class can extend multiple classes
Multiple inheritance is allowed for classes and interfaces
A class can inherit from multiple abstract classes
What is the purpose of the instanceof operator in Java?
To check the type of an object
To create an instance of a class
To cast an object to a different type
To compare two objects
Which of the following keywords allows a class to inherit from an interface?
extends
inherits
implements
includes
How can you achieve polymorphism in Java?
By method overloading only
By method overriding only
By both method overloading and overriding
By using final classes
What does the final keyword signify when used with a class?
The class can be subclassed
The class cannot be subclassed
The class can only have one constructor
The class is abstract
Which of the following is a feature of Java’s garbage collection?
Manual memory management
Automatic memory management
Explicit memory allocation
None of the above
In Java, what is the result of attempting to instantiate an abstract class?
A compile-time error occurs.
The abstract class is instantiated successfully.
A runtime exception is thrown.
The program ignores the instantiation.
Which method can be used to create a deep copy of an object?
Cloneable interface
Serialization
Copy constructor
All of the above
When a class inherits from an interface, which of the following must be implemented?
Only abstract methods
Only non-abstract methods
Both abstract and non-abstract methods
Only static methods
What happens if a method in a subclass has the same name and parameters as a method in its superclass?
The superclass method is executed
The subclass method is executed
A compile-time error occurs
The program fails to run
A Java interface is correctly described as:
A blueprint for a class that can contain abstract methods and constants.
A class that can be instantiated directly.
A class that contains only static methods.
A class that can have private constructors.
In Java, how can a subclass access the private members of its superclass?
Directly
Through public methods of the superclass
By overriding the superclass method
By using reflection
What is the purpose of the Serializable interface in Java?
To create a deep copy of an object
To allow objects to be serialized
To enable thread safety
To implement cloning
How do you prevent a method from being overridden in a subclass?
Declare it as final
Declare it as private
Declare it as static
Declare it as abstract
Which of the following statements about multiple inheritance of interfaces is true?
It can have method implementations
It can contain instance variables
It can extend multiple classes
It can only contain abstract methods
Which of the following keywords is used to declare an inner class?
inner
static
public
class
What is the purpose of the clone() method?
To create a new instance of a class
To create a shallow copy of an object
To compare two objects
To serialize an object
What is the default visibility of a class member declared without any access modifier?
Public
Protected
Private
Package-private
Which method in Java is called when an object is deserialized?
writeObject()
readObject()
serialize()
deserialize()
Which of the following methods is used for object deserialization in Java?
finalize()
readObject()
deserialize()
init()
What is the result of attempting to cast an object to a class that it does not inherit from?
Compile-time error
Run-time exception
It will return null
It will create a new instance
In the context of object serialization, what is a serialVersionUID?
A unique identifier for each class
A method used for serialization
A variable that holds serialized data
A marker interface
What is the purpose of a marker interface in Java?
To provide methods for implementation
To mark a class for specific behavior
To prevent multiple inheritance
To enforce type safety
Which of the following statements about static members in Java is correct?
Static members are associated with the instance of a class
Static members can be accessed without creating an instance of a class
Static members cannot be inherited
Static members can only be private
What happens if you do not override the toString() method in a class?
The program will not compile
The default implementation from Object class will be used
It will throw a run-time exception
It will return null
What is an anonymous inner class in Java?
A class defined without a name
A class that does not inherit from any superclass
A class that cannot access outer class members
A class with multiple constructors
Which of the following methods should be overridden for correct serialization of an object?
finalize()
readObject() and writeObject()
clone()
toString()
When using super() in a constructor, what must be true?
The constructor must be inside a subclass.
The constructor must be static.
super() can only be used outside constructors.
super() must be called after all statements in the constructor.
What is true about the visibility of an inner class?
It can be private, protected, or public
It cannot have private members
It can only be public
It cannot be a static class
Which of the following is a correct way to declare a static inner class?
public class Outer { static class Inner {} }
public class Outer { Inner static {} }
static class Outer { class Inner {} }
class Outer { static class Inner {} }
Which of the following keywords can be used to prevent method overriding?
final
static
abstract
virtual
Which of the following statements about the hashCode() method is true?
How can you invoke a method of a superclass from a subclass?
Using the base keyword
Using the super keyword
Directly, by method name
Using the parent keyword
In the context of OOP, what is encapsulation?
Inheriting methods from a superclass
Hiding the internal state and requiring all interaction to be performed through an object's methods
Using multiple classes to implement a single function
Overriding methods in subclasses
What does the final keyword signify when used with a variable?
The variable can be changed
The variable can only be initialized once
The variable can be accessed from anywhere
The variable is a class constant
Which of the following is a valid way to implement an interface?
class MyClass implements MyInterface {}
class MyClass inherits MyInterface {}
class MyClass extends MyInterface {}
MyClass : MyInterface {}
In Java, what is method overloading?
Having the same method name with different parameters in the same class
Having the same method name with the same parameters in different classes
Changing the method implementation in subclasses
Using the same method name across different packages
What happens if you attempt to serialize an object without implementing Serializable?
The object will be serialized anyway
A NotSerializableException is thrown
The object will be partially serialized
The object will lose all its data
What is the primary reason for using interfaces in Java?
To implement multiple inheritance
To provide a way to define abstract methods
To allow different classes to communicate with each other
To achieve abstraction and polymorphism
Which of the following is NOT a feature of a constructor in Java?
It must have the same name as the class
It can be overloaded
It has a return type
It is called when an object is created
Which keyword is used to access members of the superclass in Java?
this
base
super
parent
What is the purpose of the instanceof operator?
To check if an object is an instance of a specific class or subclass
To cast an object to a specified class
To compare two objects
To clone an object
What does it mean when a class is declared as abstract?
The class cannot have constructors
The class cannot be instantiated
The class cannot have fields
The class cannot implement interfaces
Which of the following statements about final classes is true?
They can be subclassed
They can have constructors
They cannot have any methods
They cannot have any attributes
In Java, what is the visibility of a member marked as protected?
Accessible only within its own package
Accessible only within its own class
Accessible by subclasses and classes in the same package
Accessible by any class
Which of the following is true regarding the equals() method?
It must be overridden if the == operator is used
It checks for reference equality by default
It can return null
It must return a boolean value
What is an interface?
A class that cannot be instantiated
A contract that classes can implement
A special type of class with abstract methods
A way to define constants
The significance of overriding the hashCode() method is:
It ensures that objects that are equal have the same hash code, which is important for correct functioning in hash-based collections.
It allows objects to be sorted in a TreeSet.
It enables objects to be serialized.
It improves the performance of the garbage collector.
Which of the following statements about inner classes is true?
An inner class cannot access the outer class’s private members
An inner class can be static
An inner class can only be public
An inner class can be instantiated without an outer class instance
What happens when you call the finalize() method on an object?
The object is destroyed
The object is marked for garbage collection
The object’s resources are released
The object’s state is saved
What is the difference between == and equals() in Java?
== compares references, while equals() compares values
== compares values, while equals() compares references
They are interchangeable
== is used for primitives, while equals() is used for objects
Which of the following can an abstract class do?
Provide implementations for some methods
Only have abstract methods
Be instantiated directly
Inherit from a concrete class
Which of the following statements is true about interfaces in Java?
An interface can extend multiple classes
An interface can have method implementations
An interface can contain fields
An interface can extend multiple interfaces
In the context of inheritance, what does the term “subclass” refer to?
A class that inherits from another class
A class that cannot be instantiated
A class that contains inner classes
A class that implements interfaces
What does polymorphism allow in Java?
Multiple classes to be instantiated from a single class
Objects to be treated as instances of their parent class
Methods to be defined only in abstract classes
Variables to change type at runtime
Which of the following is NOT a type of inner class?
Which of the following is NOT a type of inner class in Java?
A) Member inner class
B) Local inner class
C) Anonymous inner class
D) External inner class
What keyword is used to define an interface in Java?
interface
abstract
class
implements
Which of the following statements about method overriding is true?
The overriding method must have the same name and parameters as the method in the superclass
The overriding method can have a different return type
The overriding method can be more restrictive in terms of access
All of the above
Which of the following is true about the Object class in Java?
It is the superclass of all classes
It cannot be extended
It contains only abstract methods
It is not part of the Java API
The purpose of the Serializable interface is:
To enable objects to be converted into a byte stream for storage or transmission.
To allow objects to be cloned.
To provide synchronization for objects.
To enable objects to be compared.
What does the protected access modifier allow?
Access only within the same package
Access only within subclasses
Access within the same package and subclasses
Access from any class
In Java, what will happen if you attempt to access a private member from outside its class?
It will compile successfully
It will result in a run-time error
It will lead to a compile-time error
It will print null
Which of the following is true about the final keyword?
It can be applied to classes, methods, and variables
It cannot be used with methods
It restricts the use of static members
It can be applied only to variables
To declare a variable of an interface type in Java, which of the following is correct?
InterfaceName variableName;
variableName InterfaceName;
interface variableName;
InterfaceName = variableName;
Which of the following is the correct way to declare an interface variable in Java?
A) Interface varName;
B) varName: Interface;
C) Interface varName();
D) Interface varName = new Interface();
What does the throws keyword indicate in a method declaration?
The method is guaranteed to throw an exception
The method may throw exceptions of specified types
The method handles exceptions internally
The method will not throw any exceptions
What is the default access level of class members in Java?
public
private
protected
package-private
