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. virtual destructor

  2. virtual constructor

  3. Both A and B

  4. None of the Above

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

C++ supports virtual destructors but not virtual constructors. Virtual destructors ensure proper cleanup when deleting derived class objects through base class pointers. Virtual constructors don't exist because you cannot construct an object without knowing its concrete type at compile time - that contradicts the purpose of constructors.

Multiple choice technology programming languages
  1. Explicit Instantiation

  2. Implicit instantiation

  3. Both A and B

  4. None of the Above

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

C++ templates can be instantiated both explicitly (using template<> declaration) and implicitly (when the compiler deduces template arguments from usage). Both mechanisms are valid and commonly used in C++ programming, making option C the correct answer.

Multiple choice technology programming languages
  1. Extraction Operator

  2. Insertion Operator

  3. Scope resolution operator

  4. None of the Above

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

The scope resolution operator (::) is used to define member functions outside the class definition, specifying which class the function belongs to. The extraction (>>) and insertion (<<) operators are used for I/O streams, not for defining class members.

Multiple choice technology programming languages
  1. Virtual Function

  2. Namespaces

  3. copy constructor

  4. None of the Above

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

A copy constructor is a special constructor in C++ used to initialize a newly declared object using an existing object of the same class. Virtual functions enable polymorphism, and namespaces organize code to prevent name collisions.

Multiple choice technology programming languages
  1. ostream

  2. ifstream

  3. strstream

  4. None of the Above

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

The strstream class in C++ is specifically designed for reading and writing to an array in memory. It implements stream operations on character arrays stored in memory. Ostream is for output streams only, ifstream is for input file streams, so strstream is the correct answer for in-memory array I/O operations.

Multiple choice technology programming languages
  1. True

  2. False

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

A copy constructor must accept a reference to an object of the same class, not the object itself. If it accepted an object by value, it would need to call the copy constructor to make the copy, creating infinite recursion. Therefore, copy constructors always take a reference parameter.

Multiple choice technology programming languages
  1. Extraction Operator

  2. Insertion Operator

  3. Scope resolution operator

  4. None of the Above

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

The scope resolution operator (::) is used to define member functions outside the class definition. It connects the function name to its class scope, such as ClassName::memberFunction. The extraction operator (>>) is for input, insertion operator (<<) is for output - neither is used for defining methods outside classes.

Multiple choice technology programming languages
  1. Virtual Function

  2. Namespaces

  3. copy constructor

  4. None of the Above

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

A copy constructor is a special constructor that creates a new object as a copy of an existing object. Virtual functions are for runtime polymorphism, namespaces organize code, and this is specifically about initialization from existing variables.

Multiple choice technology programming languages
  1. Virtual Function

  2. Namespaces

  3. copy constructor

  4. None of the Above

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

A copy constructor is a special constructor that initializes a new object as a copy of an existing object of the same class. It takes a reference to an object of the same class as its parameter and creates a new object with identical values. Virtual functions and namespaces are unrelated to variable initialization.

Multiple choice technology programming languages
  1. virtual destructor

  2. virtual constructor

  3. Both A and B

  4. None of the Above

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

C++ supports virtual destructors but not virtual constructors. Virtual destructors ensure proper cleanup when deleting derived class objects through base class pointers. Virtual constructors don't exist because you cannot construct an object without knowing its exact type at compile time - construction requires the complete type information.

Multiple choice technology programming languages
  1. Explicit Instantiation

  2. Implicit instantiation

  3. Both A and B

  4. None of the Above

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

Templates can be instantiated both explicitly and implicitly. Implicit instantiation occurs when you use a template with specific types and the compiler generates the code automatically. Explicit instantiation happens when you explicitly tell the compiler to instantiate a template with specific types using template<> syntax.

Multiple choice technology programming languages
  1. Extraction Operator

  2. Insertion Operator

  3. Scope resolution operator

  4. None of the Above

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

The scope resolution operator (::) is used in C++ to define a member function outside of its class declaration. Insertion (&lt;&lt;) and extraction (&gt;&gt;) operators are used for stream I/O, not for specifying class scope.

Multiple choice technology
  1. (B), (C) & (E)

  2. (B), (C), (E) & (F)

  3. B), (E) & (F)

  4. (A), (B), (C), (E) & (F)

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

The java.lang package contains fundamental classes that are automatically imported. Object, Math, and String are in java.lang, as is StringBuffer. Stack, Vector, and Random are in other packages (java.util). Option B correctly identifies all four classes that belong to java.lang: (B) Object, (C) Math, (E) String, and (F) StringBuffer.