Computer Knowledge
Object-Oriented Programming
2,239 Questions
Object-oriented programming questions test core computer science concepts like classes, inheritance, polymorphism, and encapsulation. The focus includes Java program structures, method overloading, and memory allocation for objects. This topic is essential for technical sections in various recruitment tests.
Java class definitionsMethod overriding rulesPolymorphism conceptsGeneric type parametersMemory allocation in objects
Object-Oriented Programming Questions
-
Implement a no-argument constructor
-
provide an identifier property
-
declare accessors for persistent fields
-
all the above
A
Correct answer
Explanation
Hibernate requires a no-argument constructor to instantiate objects via reflection when retrieving data from the database. While identifier properties and accessors are best practices, they are not mandatory for basic POJO functionality.
-
Table per Concrete class
-
Table per Sub class
-
Table per class Hierarchy
-
all of the above
D
Correct answer
Explanation
Hibernate supports three inheritance mapping strategies: Table per Concrete Class (each class gets its own table), Table per Subclass (shared parent table with separate subclass tables), and Table per Class Hierarchy (single table for entire hierarchy).
A
Correct answer
Explanation
When a subclass inherits from an abstract class, it must either provide concrete implementations for all inherited abstract methods or be declared as an abstract class itself. Leaving any abstract method unimplemented makes the subclass abstract.
B
Correct answer
Explanation
A constructor's first statement must be either a call to super() or this(), but if you don't write either, the compiler automatically inserts super() as the first statement. So while a super constructor is always invoked, it doesn't have to be explicitly written by you.
A
Correct answer
Explanation
Interface methods are implicitly public. When a class implements an interface method, it cannot reduce visibility - it must remain public. Making it private, protected, or package-private would violate the interface contract.
B
Correct answer
Explanation
Whether a class can be inherited depends on its access modifier. If the class is private or has no modifier (package-private), classes outside that package cannot inherit from it. If the class is public, it can be inherited by any class anywhere.
A
Correct answer
Explanation
A final class cannot be subclassed, so all its methods are effectively non-overridable. While 'implicitly final' in the practical sense (they cannot be overridden), the methods themselves don't have the final keyword applied to them individually.
B
Correct answer
Explanation
Abstract classes can (and often do) contain concrete methods alongside abstract ones. The abstract keyword means the class cannot be instantiated, not that every method must be abstract. Concrete methods provide shared implementation to subclasses.
A
Correct answer
Explanation
Overriding methods cannot be more restrictive than the methods they override. This maintains the Liskov Substitution Principle - if the parent method is public, the child must also be public (or more accessible, though 'more accessible than public' doesn't exist). Making it protected or private would break the contract.
A
Correct answer
Explanation
Static methods belong to the class, not to instances. While a subclass can declare a static method with the same signature, this is method hiding, not overriding. There's no polymorphism for static methods - the method called is determined at compile-time based on the reference type.
A
Correct answer
Explanation
When overriding a method in Java, the subclass method is permitted to have a less restrictive access modifier than the parent method, but it cannot be more restrictive. For example, a protected method can be overridden as public.
B
Correct answer
Explanation
In Java, an overriding method can change the return type only if it's a covariant return type (a subtype of the original return type). It cannot arbitrarily change to any return type regardless of the overridden method's return type.
B
Correct answer
Explanation
Method overriding requires that the overriding method in a subclass must have the exact same method signature, including the argument list, as the method in the parent class.
A
Correct answer
Explanation
Constructors can be overloaded by defining multiple constructors with different parameter lists. This allows objects to be initialized in different ways depending on which constructor is called.