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

Multiple choice technology programming languages
  1. Fields

  2. Links

  3. Groups

  4. Frames

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. A final method in class X can be abstract if and only if X is abstract.

  2. A private static method can be called only within other static methods in class X.

  3. A protected method in class X can be overridden by any subclass of X.

  4. A non-static public final method in class X can be overridden in any subclass of X.

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. Automatic

  2. Global

  3. Static

  4. External

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. Operator overloading

  2. Virtual functions

  3. Virtual base class

  4. Function overloading

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. a only

  2. a and b

  3. b only

  4. none of the above

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. Both are essential templates for which we can create objects.

  2. Both structs and classes are reference types and are stored on heap .

  3. For both struct and class, we use 'new' keyword to declare an instance.

  4. All of the above are correct

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. Value types are stored in heap and reference types are stored in stack area.

  2. Boxing allows users to convert value types to reference types

  3. Unboxing allows users to convert value type to reference type.

  4. During runtime boxing creates a temporary reference type box for the object on the heap.

Reveal answer Fill a bubble to check yourself
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.

Multiple choice technology programming languages
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
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'.

Multiple choice technology programming languages
  1. Parent

  2. Child

  3. Both

  4. None of these

Reveal answer Fill a bubble to check yourself
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.