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
-
internal
-
implicit
-
explicit
-
new
D
Correct answer
Explanation
In C#, the 'new' keyword explicitly hides a member inherited from a base class. When a derived class declares a member with the same name as a base class member, 'new' indicates intentional hiding rather than overriding.
-
Fields
-
Links
-
Groups
-
Frames
D
Correct answer
Explanation
Frames are used in reporting tools to group objects together, ensuring they maintain their relative positions during printing. Fields hold data values, links connect objects, and groups organize data but don't control print positioning.
-
A final method in class X can be abstract if and only if X is abstract.
-
A private static method can be called only within other static methods in class X.
-
A protected method in class X can be overridden by any subclass of X.
-
A non-static public final method in class X can be overridden in any subclass of X.
C
Correct answer
Explanation
Protected methods in Java are accessible within the same package and to subclasses (even in different packages), and can be overridden by any subclass unless marked final. Option C is correct. Option A is wrong because final methods cannot be abstract. Option B is wrong because private static methods can be called from non-static methods too. Option D is wrong because final methods cannot be overridden.
-
Automatic
-
Global
-
Static
-
External
A,C,D
Correct answer
Explanation
Storage classes in C are auto (default for local variables, stored on stack), static (retains value between calls, stored in data segment), extern (variables defined in another file), and register (stored in CPU registers). 'Global' is not a storage class - global variables use extern or static storage classes.
-
Static
-
Virtual
-
Void
-
Friend
D
Correct answer
Explanation
Friend functions are granted special access to private and protected members of a class. They're not member functions themselves but can access non-public data. This provides controlled way to expose private data to specific external functions without making it public.
B
Correct answer
Explanation
Static member functions belong to the class rather than individual objects, so they don't have a 'this' pointer. They can only access static members of the class. The 'this' pointer only exists for non-static member functions that operate on specific object instances.
-
Operator overloading
-
Virtual functions
-
Virtual base class
-
Function overloading
A,B,D
Correct answer
Explanation
Polymorphism in C++ is achieved through compile-time methods (function overloading, operator overloading) and runtime methods (virtual functions). Virtual base class is used to solve the diamond problem in multiple inheritance but is not itself a type of polymorphism - it's an inheritance-related concept.
A
Correct answer
Explanation
Encapsulation is a fundamental object-oriented programming concept that binds together data and the functions that manipulate them into a single unit called a class.
-
a only
-
a and b
-
b only
-
none of the above
C
Correct answer
Explanation
Statement (a) is false - struct members are public by default in C++ (class members are private by default). Statement (b) is true - new and delete operators can be overloaded. Statement (c) is technically incorrect because the subscript operator [] and assignment operator = must be non-static member functions, not friend functions.
-
Both are essential templates for which we can create objects.
-
Both structs and classes are reference types and are stored on heap .
-
For both struct and class, we use 'new' keyword to declare an instance.
-
All of the above are correct
A,C
Correct answer
Explanation
Both structs and classes serve as templates for object creation (option A), and both use the 'new' keyword to instantiate (option C). However, option B is incorrect because structs are value types stored on the stack (or inline), while classes are reference types stored on the heap.
-
Value types are stored in heap and reference types are stored in stack area.
-
Boxing allows users to convert value types to reference types
-
Unboxing allows users to convert value type to reference type.
-
During runtime boxing creates a temporary reference type box for the object on the heap.
B,D
Correct answer
Explanation
Boxing converts value types to reference types by creating an object on the heap and copying the value type value there (Option B and D are correct). Option A is wrong because value types are stored on stack, not heap. Option C is incorrect because unboxing converts reference types back to value types, not value to reference.
B
Correct answer
Explanation
In MIDP, the Canvas class is a direct subclass of Displayable, not Screen. Both Canvas and Screen inherit from Displayable at the same level, making the statement false.
A
Correct answer
Explanation
Java is fundamentally object-oriented - everything must be part of a class. Even the simplest 'Hello World' program requires a class definition. This is a core design principle of Java, distinguishing it from languages that allow standalone functions or procedures.
B
Correct answer
Explanation
Non-static inner classes in Java cannot have static members because they are associated with an instance of the enclosing class. Static members belong to the class itself, not an instance, creating a fundamental conflict. However, static nested classes (declared with 'static' keyword) can have static members. The question is technically correct but should specify 'non-static inner class'.
-
Parent
-
Child
-
Both
-
None of these
A
Correct answer
Explanation
Static methods are bound at compile-time based on the reference type, not the actual object type. When you have a parent class reference and assign a child object, calling a static method invokes the parent class version, not the child's.