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
-
This is the super class
-
This is the inherited class
-
Compile time error
-
Run time exception
C
Correct answer
Explanation
This code fails to compile because of incompatible exception declarations in method overriding. When overriding a method, the subclass method cannot throw a broader checked exception than the parent class method. Here, the parent method throws FileNotFoundException, but the child class method throws Exception, which is broader. This violates Java's overriding rules and results in a compile-time error.
-
variable
-
same as if in noraml object
-
constant
-
none
C
Correct answer
Explanation
In C++, when an object is declared as const, all its members become const. This means you can only call const member functions on the object and cannot modify any member variables. The const qualifier applies to the entire object state.
-
woof
-
woof woof
-
Nothing will be printed as output
-
None of the above
B
Correct answer
Explanation
Static methods are not polymorphic in Java - they are bound at compile-time based on the reference type, not the actual object type. Both woofer and nipper are declared as Dog references, so Dog.bark() is called twice, printing 'woof woof'. Static methods cannot be overridden; they are hidden.
B
Correct answer
Explanation
Destructors cannot be overloaded in C++. A class can have only one destructor, which takes no parameters and has no return type. The destructor's signature is fixed as ~ClassName(), making overloading impossible.
-
Class
-
Constructor
-
Object
-
None
C
Correct answer
Explanation
An object is a storage area with associated semantics - it combines data (storage area) with the behavior and meaning (semantics) of what that data represents. Classes define the structure, constructors initialize it, but objects are the actual storage areas with meaning.
-
private member
-
Constructor
-
Destructor
-
Overloaded assignment operator
B,C,D
Correct answer
Explanation
Constructors, destructors, and overloaded assignment operators cannot be inherited. Constructors and destructors handle initialization/cleanup specific to each class. The assignment operator must be defined per class to handle member-specific semantics. Private members are inherited but not accessible.
-
Singleton
-
Abstract Factory
-
Prototype
-
None of these
B
Correct answer
Explanation
The Abstract Factory pattern is a creational design pattern that provides an interface for creating families of related objects without specifying their concrete classes. It allows you to create instances of several related classes through a common interface.
-
Prototype
-
Builder
-
Singleton
-
None of these
C
Correct answer
Explanation
The Singleton pattern ensures that a class can have only one instance and provides a global point of access to that instance. It's used when exactly one object is needed to coordinate actions across a system.
-
Creational
-
Behavioral
-
Structural
-
None of these
B
Correct answer
Explanation
Behavioral design patterns specifically focus on communication between objects, how they interact, and how work is distributed among them. Creational patterns deal with object creation mechanisms, while Structural patterns deal with composition of classes and objects. Behavioral patterns like Observer, Strategy, and Command define patterns of interaction and responsibility allocation.
-
1
-
2
-
3
-
4
-
None of the above
B
Correct answer
Explanation
After line 6, two objects are eligible for GC. First: the object created in m1() that was returned to c2 but then replaced when c2=c3. Second: the object originally referenced by c1 was passed to m1(), but m1() doesn't modify it - it creates a new object instead, so the original becomes unreachable. The object created in m1() is referenced by c3 (via c2=c3), so it's NOT eligible.
-
compile time error
-
prints base and super
-
prints super and base
-
none of the above
A
Correct answer
Explanation
Compile-time error occurs because super(1) must be the FIRST statement in the constructor. The Super() constructor first calls super() implicitly (to base's no-arg constructor), then prints 'super', then attempts super(1) - which violates Java's rule that super() or this() must be the first line.
-
editor
-
variable
-
object
-
class
A
Correct answer
Explanation
vi (and vim) is a text editor in UNIX/Linux systems. It's a modal editor with different modes for inserting and commanding text. It's not a variable, object, or class - those are programming concepts.
A
Correct answer
Explanation
In C++, static data members must be declared inside the class but defined outside the class (in a .cpp file) to allocate storage. Java combines declaration and definition in one step inside the class. This is a key difference between the two languages' handling of static members.
-
initialization
-
instantiation
-
inheritance
-
insubordination
B
Correct answer
Explanation
Instantiation is the technical term for creating an object from a class. Initialization sets initial values, inheritance is acquiring properties from a parent class, and insubordination is an unrelated term.