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. This is the super class

  2. This is the inherited class

  3. Compile time error

  4. Run time exception

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

Multiple choice technology programming languages
  1. woof

  2. woof woof

  3. Nothing will be printed as output

  4. None of the above

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

Multiple choice technology programming languages
  1. True

  2. False

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

Multiple choice technology programming languages
  1. Class

  2. Constructor

  3. Object

  4. None

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

Multiple choice technology programming languages
  1. private member

  2. Constructor

  3. Destructor

  4. Overloaded assignment operator

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

Multiple choice technology architecture
  1. Singleton

  2. Abstract Factory

  3. Prototype

  4. None of these

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

Multiple choice technology architecture
  1. Creational

  2. Behavioral

  3. Structural

  4. None of these

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

Multiple choice technology programming languages
  1. 1

  2. 2

  3. 3

  4. 4

  5. None of the above

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

Multiple choice technology programming languages
  1. compile time error

  2. prints base and super

  3. prints super and base

  4. none of the above

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

Multiple choice technology operating systems
  1. editor

  2. variable

  3. object

  4. class

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

Multiple choice technology programming languages
  1. True

  2. False

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