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
-
To avoid having to declare variables
-
To refer to a class without using prefixes
-
To avoid calling methods
-
To import the images you want to use
B
Correct answer
Explanation
Import statements allow you to reference classes by their simple names instead of using fully qualified names with package prefixes. This improves code readability and reduces verbosity - instead of writing 'java.util.ArrayList' repeatedly, you can import it once and use 'ArrayList' throughout your code.
B
Correct answer
Explanation
Wicket does NOT require objects referenced by CompoundPropertyModel to be Serializable. While serialization can be useful for clustering or session replication, it is not mandatory for basic CompoundPropertyModel functionality.
B
Correct answer
Explanation
Objects referenced by CompoundPropertyModel should ideally be Serializable to support session replication, clustering, and other scenarios where the session may be serialized. However, Wicket does not strictly enforce this for basic operation, so the answer is False - it's recommended but not required.
B
Correct answer
Explanation
Wicket supports model inheritance - if a component's parent has an inheritable model, the child component can use that model even without explicitly providing one. This is a key feature of Wicket's model hierarchy, so the statement is False.
-
The keyword extends is used to specify that an interface inherits from another interface.
-
The keyword extends is used to specify that a class inherits from an interface.
-
The keyword implements is used to specify that an interface inherits from another interface.
-
The keyword implements is used to specify that a class inherits from an interface.
-
The keyword implements is used to specify that a class inherits from another class.
A,D
Correct answer
Explanation
In Java, 'extends' is used for interface inheritance (one interface extending another) and class inheritance (subclass extending superclass). 'implements' is used when a class implements an interface. Options B and E are incorrect because classes don't inherit from interfaces using 'extends', and classes don't inherit from other classes using 'implements'. Option C is wrong because interfaces don't use 'implements'.
-
from base to the derived class
-
from derived to base
-
only the derived class
-
only the base class
A
Correct answer
Explanation
In Java, constructor execution flows from base class to derived class. The base class constructor completes first to initialize inherited fields, then derived class constructors run. B is backwards, C and D are incomplete.
-
A subclass must define all the methods from the superclass
-
It is possible for a subclass to define a method with the same name and parameters as a method defined by the superclass.
-
It is possible for a subclass to define a field with the same name as a field defined by the superclass.
-
It is possible for two classes to be the superclass of each other.
B,C
Correct answer
Explanation
B describes method overriding, which is legal in Java. C describes field hiding, also legal. A is false - abstract methods need implementation, but concrete methods can be inherited. D is false - circular inheritance creates a paradox and Java prohibits it.
-
The class will fail to compile, since the class Other has not yet been declared when referenced in class AClass.
-
The class will fail to compile, since import statements must never be at the very top of a file.
-
The class will fail to compile, since the package declaration can never occur after an import statement.
-
The class will fail to compile, since the class Other must be defined in a file called Other.java.
-
The class will fail to compile, since the class Other must be declared public.
C
Correct answer
Explanation
Java enforces strict file structure: package declaration (if present) must be first line, followed by imports, then class/interface declarations. Option C correctly identifies this ordering violation. A is wrong - forward references are allowed within a compilation unit. B is wrong - imports do come before classes but after package. D and E are wrong - a class can be defined in the same file without being public or matching the filename.
-
Under all circumstances
-
Unless A is abstract
-
A class that implements an interface cannot be abstract
-
none of the above
B
Correct answer
Explanation
A concrete class implementing an interface must provide implementations for all interface methods. However, if the implementing class is abstract, it can defer implementation to its subclasses. Option A is too absolute, C is false (abstract classes can implement interfaces), and D is incorrect because B is valid.
A
Correct answer
Explanation
Abstract classes can contain both abstract and concrete (non-abstract) methods. This is a key distinction from interfaces. Abstract classes provide partial implementation with abstract methods defining what subclasses must implement, and concrete methods providing shared behavior.
B
Correct answer
Explanation
An abstract class in Java cannot be declared as final because abstract classes are designed to be extended by subclasses, whereas final classes explicitly prevent inheritance. These two modifiers are mutually exclusive.
-
keyword
-
method
-
both
-
none of the above
A
Correct answer
Explanation
'super' is a reserved keyword in Java, not a method. It is used to access superclass members (super.method() or super.field) or to call a superclass constructor (super() as the first statement in a constructor). It cannot be used as a variable name or for any other purpose.
B
Correct answer
Explanation
While Java permits accessing static members through an object reference (obj.staticMethod()), this is misleading and discouraged. Static members belong to the class, not instances. The correct practice is ClassName.staticMethod(). The compiler actually uses the declared type of the reference, not the runtime object.
B
Correct answer
Explanation
Java only provides a default no-argument constructor if no other constructor is explicitly defined. Once you define any constructor (with or without arguments), the default constructor is not automatically generated. If you need both parameterized and no-arg constructors, you must explicitly define both.
B
Correct answer
Explanation
Private member variables in a superclass are not accessible to subclasses - this is a fundamental principle of encapsulation in object-oriented programming. Only the superclass itself can access its private members. Subclasses can only access public, protected, and package-private (if in same package) members.