NEW
Font size
WorksheetsOOPS MCQ
Total questions: 73
Worksheet time: 37mins
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
Which of the following statements about inheritance in Java is correct?
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?
Accessible only within its own package
Accessible only within subclasses
Accessible within its own package and subclasses
Accessible from anywhere
The visibility of private attributes in Java is:
Only within the same class
Within the same package
Accessible by subclasses
Accessible from anywhere
Which of the following best describes the accessibility 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) A class can implement multiple interfaces
B) A class can extend multiple classes
C) Multiple inheritance is allowed for classes and interfaces
D) 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
implements
inherits
uses
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?
Compile-time error
Run-time error
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
Which of the following correctly describes a Java interface?
It can have method implementations
It can contain instance variables
It can extend multiple classes
It can only contain abstract methods
In Java, a subclass can access the private members of its superclass by:
Using public or protected getter and setter methods in the superclass
Directly accessing them using the 'super' keyword
Declaring the subclass in the same package
Using the 'private' keyword in the subclass
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
Which of the following statements about multiple inheritance of interfaces is true?
It is allowed in Java
It is not allowed in Java
It leads to ambiguity
Only one interface can be inherited
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?
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
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 is true?
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?
It must be the first statement in the constructor
It must be called after any initialization
It can only be called in the constructor of a subclass
It cannot be used in abstract classes
Which of the following is true about 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?
It can return negative values
It is mandatory to override hashCode() when equals() is overridden
It is called automatically during object comparison
It returns the memory address of the object
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 the correct syntax for implementing an interface in Java?
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
If a class does not implement the Serializable interface, what will happen when you attempt to serialize an instance of that class?
The object will be serialized without any issues
A NotSerializableException will be 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
Which of the following statements about abstract classes is true?
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?
A) Accessible only within its own package
B) Accessible only within its own class
C) Accessible by subclasses and classes in the same package
D) 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
Why is it important to override the hashCode() method when overriding equals()?
To change the default hashing behavior
To ensure consistent behavior with equals()
To improve performance
To enable object cloning
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
59. When you call the finalize() method on an object, which of the following occurs?
The object's cleanup actions are performed before garbage collection.
The object is immediately deleted from memory.
The object's constructor is called again.
The object becomes immutable.
What happens when the finalize() method is called on an object in Java?
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?
Member inner class
Local inner class
Anonymous inner class
External inner class
What keyword is used to define an interface in Java?
interface
abstract
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
What is the purpose of the Serializable interface?
To mark a class for serialization
To prevent class modification
To create constant values
To implement security features
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
