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
-
Cohesionn is the OO principle most closely associated with hiding implementation details
-
Cohesion is the OO principle most closely associated with making sure that classes know about other classes only through their APIs
-
None of these
-
Cohesion is the OO principle most closely associated with making sure that a class is designed witha single well focussed principle
D
Correct answer
Explanation
Cohesion is the OO principle that a class should have a single, well-focused purpose or responsibility. High cohesion means a class does one thing well. Hiding implementation details is encapsulation (information hiding), and knowing other classes only through APIs is also related to encapsulation and loose coupling, not cohesion.
-
Class D has low Cohesion
-
Class B has weak encapsulation
-
Class A has weak encapsulation
-
Class B has strong encapsulation
B
Correct answer
Explanation
If Class A methods use public methods in Class B, this indicates Class B has weak encapsulation because its internal methods are exposed and tightly coupled to other classes. Class A using variables in Class C violates encapsulation but the question asks what is 'most likely true', and Class B's weak encapsulation is directly stated. Class D has no relationship described, so its cohesion cannot be determined.
A
Correct answer
Explanation
In Java, static methods are associated with the class rather than instance objects and cannot be overridden. Attempting to override a static method with a non-static method in a subclass results in a compilation error.
B
Correct answer
Explanation
The terminology is: the overridden method is in the PARENT class (the original version being replaced), and the overriding method is in the CHILD class (the new version). The statement reverses this correctly, so False is the right answer. You're overriding the parent's method from the child.
B
Correct answer
Explanation
You CAN override a non-abstract method as abstract in a subclass. This makes the subclass abstract and forces subclasses of it to provide an implementation. It's a valid technique when you want to require subclasses to reimplement a method, even if the parent provided a concrete version.
A
Correct answer
Explanation
You cannot reduce visibility when overriding - private is more restrictive than public. Java's overriding rules require the subclass method to have at least the same access level as the parent. Changing public void doIt() to private void doIt() violates this, so the code is illegal.
-
static because it has to be constant and cannot be changed.
-
static because it has to be loaded by the JVM without instantiating it
-
public because other classes may also call the main method of a class
-
public because JVM has to have access to the main method in order to call it.
B,D
Correct answer
Explanation
The main method must be static so the JVM can invoke it without creating an instance of the class (no object exists at program startup). It must be public so the JVM, which is external to your class, can access it. Static methods belong to the class itself, not instances, and public allows unrestricted access from outside.
-
public is more restrictive than protected
-
package (default access modifier) is more restrictive that private
-
package is more restrictive that public
-
package is more restrictive that protected
-
protected is more restrictive that package
D
Correct answer
Explanation
In Java, access modifiers restrict visibility in this order from most to least restrictive: private (only same class), package/default (same package), protected (same package + subclasses), public (everywhere). Since package-level access allows access only within the same package while protected additionally allows subclasses outside the package to access the member, package is indeed more restrictive than protected.
-
Mapper classes are Java classes that contain SQL Mapping Annotations that avoid the need for XML
-
Mapper classes are Java classes that contain data Mapping Annotations that encourage the need for XML
-
Mapper classes are Java classes that contain SQL that avoid the need for XML
-
Mapper classes are O/R Mapping objects contain SQL Mapping objects that avoid the need for XML
A
Correct answer
Explanation
Mapper classes in iBATIS are Java classes that use SQL Mapping Annotations to define database operations. This annotation-based approach can replace XML configuration, providing a more concise and type-safe way to map SQL statements to Java methods.
-
public
-
final
-
static
-
all the above
-
there should not be any variable in an interface
D
Correct answer
Explanation
All variables declared in a Java interface are implicitly public, static, and final. They become compile-time constants that must be initialized and cannot be modified. This is a fundamental Java language rule for interfaces.
A
Correct answer
Explanation
In Java, all interface members are implicitly public. Conversely, an abstract class can declare members with any access modifier, including private and protected.
A
Correct answer
Explanation
Abstract classes in Java CAN have constructors. While they cannot be instantiated directly, their constructors are called when a subclass is instantiated using super(). This allows initialization of inherited fields and setup logic.
-
a class implements one or more interfaces
-
a class extends one or more interfaces
-
an interface extends one of more interfaces
-
Serializable is a marker Interface
B
Correct answer
Explanation
Classes IMPLEMENT interfaces, not extend them. Option B claims classes extend interfaces, which is false - you use 'implements' keyword for interfaces. Classes extend other classes, and interfaces extend other interfaces.
-
By reference
-
By object copy
-
By producing a serialized copy
-
None
B,C
Correct answer
Explanation
When passing objects to remote methods in Java RMI, objects are passed by serialized copy (deep copy). This serialization process creates an object copy on the receiving side. By reference is not possible across JVM boundaries.
A
Correct answer
Explanation
Java allows marker interfaces - interfaces with no methods. They are used to mark classes for special treatment by the JVM or compiler (e.g., Serializable, Cloneable). An empty interface declaration is syntactically valid and serves this purpose.