Object Oriented Programming in C++

Last Updated :
Discuss
Comments

Question 1

Which of the following best describes a constructor in C++?

  • A function that is called when an object is destroyed

  • A function that is called when an object is accessed

  • A function that is called when an object is created

  • A function that is called when an object is inherited

Question 2

Which of the following concepts of OOP means exposing only necessary information to the client?

  • Encapsulation

  • Abstraction

  • Data hiding

  • Data binding

Question 3

What is the main benefit of encapsulation in C++?

  • Faster execution

  •  Better UI design

  • Data security and abstraction

  • Simplified syntax

Question 4

Which of the following best describes abstraction?


  • Hiding implementation details and showing only relevant information to the users.

  • Binding both data members and member functions into a single unit.

  • Accessing data members and member functions of one class by other

  • All of the above

Question 5

Which access specifier provides the highest level of protection?

  • private

  •  protected

  • public

  • None of the above

Question 6

What is the main difference between overloading and overriding in C++?

  • Overloading occurs when two or more methods in the same class have the same name but different parameters; overriding occurs when a derived class has a definition for one of the member functions of the base class.

  • Overloading occurs in the same class, overriding occurs between a base class and a derived class

  • Overloading requires the use of the 'virtual' keyword, overriding does not.

  • Overloading and overriding are the same in C++.

Question 7

What will be the output of the following program?

C++
#include<iostream>
using namespace std;

class Base {
public:
    virtual void show() {
        std::cout << "Base\n";
    }
};
class Derived : public Base {
public:
    void show() override {
        std::cout << "Derived\n";
    }
};
int main() {
    Base* b;
    Derived d;
    b = &d;
    b->show();
    return 0;
}
  • Base

  • Derived

  • Compilation error

  • Runtime error

Question 8

C
#include <iostream>
using namespace std;

class Player
{
private:
    int id;
    static int next_id;
public:
    int getID() { return id; }
    Player()  {  id = next_id++; }
};
int Player::next_id = 1;

int main()
{
  Player p1;
  Player p2;
  Player p3;
  cout << p1.getID() << " ";
  cout << p2.getID() << " ";
  cout << p3.getID();
  return 0;
}
  • Compiler Error
  • 1 2 3
  • 1 1 1
  • 3 3 3
  • 0 0 0

Question 9

Which access specifier is commonly used to hide data in abstraction?

  • Public

  • Private

  • Protected

  • Any of these

Question 10

Which of the following operators should be preferred to overload as a global function rather than a member method?
  • Postfix ++
  • Comparison Operator
  • Insertion Operator <<
  • Prefix++

There are 11 questions to complete.

Take a part in the ongoing discussion