Computer Knowledge
Object-Oriented Programming
2,686 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
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.
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.
A
Correct answer
Explanation
Only inherited methods can be overridden. Private methods are not inherited by subclasses, so they cannot be overridden. Similarly, final methods cannot be overridden even though they are inherited.
-
NoClassDefFoundError
-
ClassCastException
-
ArthmeticException
-
IllegalArgumentException