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. Private Funtion

  2. Method Function

  3. Member Function

  4. Class

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

In C++ terminology, a function contained within a class is called a 'Member Function'. Member functions are functions that are defined inside a class definition and operate on the class's data members. They're also commonly referred to as 'methods', but the formal C++ term is 'member function'. 'Private Function' describes an access level, not the function type itself. 'Method Function' is redundant terminology, and 'Class' refers to the containing structure, not the function.

Multiple choice technology programming languages
  1. giving new meaning to existing C++ operators

  2. making c++ operators works with objects

  3. both a and b

  4. either a or b

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Operator overloading in C++ serves two purposes: it gives new meaning to existing C++ operators (like redefining what '+' does for custom objects) AND it makes C++ operators work with user-defined objects (enabling expressions like obj1 + obj2). Option A captures the definitional aspect, while option B captures the functional capability. Therefore, operator overloading encompasses both - it's both giving new meanings to operators and enabling them to work with objects beyond primitive types.

Multiple choice technology programming languages
  1. a group function with the same name

  2. all have the same number and type of arguments

  3. functions with same name and same number and type of arguments

  4. none

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Function overloading in C++ allows multiple functions to share the same name within the same scope. The key requirement is that they must have different parameter lists - either different numbers of parameters or different types. Option A correctly captures this: it's 'a group of functions with the same name' (implying the group as a whole shares one name while individual functions are distinguished by parameters). Options B and C are incorrect because overloaded functions must NOT have the same number and type of arguments - that would be a redefinition error, not overloading.

Multiple choice technology architecture
  1. Command

  2. Limiter

  3. Strategy

  4. Singleton

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The Singleton pattern specifically restricts a class to a single instance. Option D is correct - Singleton ensures only one instance exists and provides global access. Options A, B, and C are incorrect because Command, Limiter (not a standard GoF pattern), and Strategy don't limit instance count - they're behavioral patterns focused on execution, encapsulation, and algorithm variation respectively.

Multiple choice technology architecture
  1. Dynamic

  2. Collection

  3. Singleton

  4. Small

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Iterators provide a way to access elements of a collection sequentially without exposing its underlying representation. They are specifically designed for collection classes where you need to traverse elements uniformly regardless of the collection's internal structure.

Multiple choice technology architecture
  1. Abstract Factory

  2. Wrapper

  3. Memento

  4. Factory Method

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Factory Method is known as Virtual Constructor because it defers object instantiation to subclasses, allowing the client code to work with product interfaces without knowing the concrete classes being instantiated. Abstract Factory creates families of related objects, not individual instances via virtual construction.

Multiple choice technology architecture
  1. Wrapper

  2. Adapter

  3. Composite

  4. Strategy

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Decorator is known as Wrapper because it wraps an object to add new behavior dynamically without altering its structure, stacking multiple decorators for combined effects. Adapter also wraps but for interface compatibility, not behavior extension.

Multiple choice technology testing
  1. encapsulation

  2. inheritance

  3. polymorphism

  4. both b and c

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Testing object-oriented class operations is complicated by inheritance and polymorphism. Inheritance creates complex relationships where a test for a parent class may need to cover child classes. Polymorphism means the same operation can behave differently depending on object type, requiring more comprehensive test coverage.

Multiple choice technology web technology
  1. Static

  2. InitOnly

  3. Virtual

  4. Abstract

  5. Sealed

Reveal answer Fill a bubble to check yourself
E Correct answer
Explanation

Value types are implicitly sealed because they cannot be inherited from - they're either primitives or structs. This is a fundamental characteristic of value types in the CLR type system, not an explicit modifier you apply. The other modifiers (Static, InitOnly, Virtual, Abstract) are member modifiers, not type-level modifiers.

Multiple choice technology programming languages
  1. 4 bytes

  2. 6 bytes

  3. 8 bytes

  4. 12 bytes

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The class my_class contains two int data members (x and y). Given that int occupies 2 bytes, the total size is 2 + 2 = 4 bytes. Member functions (add, display) do not contribute to object size as they are stored once in the code segment, not per object instance. Options B, C, and D incorrectly add extra bytes for member functions or padding.

Multiple choice technology programming languages
  1. A class which can not be inherited

  2. A class which can have only single object

  3. A class which can not have any objects

  4. A class which can not inherit other classes

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

An abstract class in C++ is a class that contains at least one pure virtual function and cannot be instantiated directly - meaning you cannot create objects of the abstract class type itself. However, you CAN create pointers/references to it, and you MUST derive from it to create objects of the derived classes. Option A is incorrect because abstract classes are designed to be inherited. Option D is wrong because abstract classes can inherit from other classes.

Multiple choice technology programming languages
  1. Using virtual functions

  2. Using method overloading

  3. Using operator overloading

  4. Using both method & operator overloading

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Dynamic binding (also called runtime polymorphism) in C++ is achieved through virtual functions. When a function is declared virtual, the compiler creates a vtable (virtual table) for the class, and function calls are resolved at runtime based on the actual object type rather than the pointer/reference type. Method overloading and operator overloading are compile-time polymorphism (static binding), not dynamic.

Multiple choice technology programming languages
  1. constructors can be virtual

  2. destructors can be virtual

  3. constructors can be overloaded

  4. virtual functions are fast in execution

Reveal answer Fill a bubble to check yourself
B,C Correct answer
Explanation

Constructors CANNOT be virtual because they initialize objects and virtual dispatch mechanisms (vtables) aren't available until after construction completes. Destructors CAN and SHOULD be virtual when a class is designed for polymorphism to ensure proper cleanup of derived class resources. Constructors CAN be overloaded (multiple constructors with different parameters). Virtual functions have slight performance overhead due to vtable lookup, so they're slower than direct function calls.

Multiple choice technology programming languages
  1. constructors are executed only(once) when the first object is instantiated

  2. destructor is executed only at the end of main() execution

  3. constructors are executed on every object instantiation

  4. destructor is executed when all the objects of the class moves out of scope

  5. constructors can not take parameters

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Constructors run automatically every time a new object of that class type is instantiated. In C++, destructors run when each individual object goes out of scope, not merely at the end of the entire program, and constructors can accept parameters.

Multiple choice technology programming languages
  1. (1)

  2. (2)

  3. (3)

  4. (4)

Reveal answer Fill a bubble to check yourself
B,C Correct answer
Explanation

A copy constructor in C++ takes a reference to an object of the same class. Declaration (3) 'C(C& t);' is the correct form - it accepts a reference to a C object. Declaration (2) 'C(C t);' creates a copy constructor that takes by value, which would cause infinite recursion (copying the argument requires calling the copy constructor), but C++ treats this as a valid declaration despite being problematic. The standard form is (3), and (2) is technically a copy constructor declaration though ill-formed.