Object Oriented Programming in C++

Last Updated :
Discuss
Comments

Question 1

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

  • A

    A function that is called when an object is destroyed

  • B

    A function that is called when an object is accessed

  • C

    A function that is called when an object is created

  • D

    A function that is called when an object is inherited

Question 2

Which OOP concept in C# is used to restrict direct access to data and allow it only through methods?

  • A

    Encapsulation

  • B

    Abstraction

  • C

    Data hiding

  • D

    Data binding

Question 3

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

  • A

    Faster execution

  • B

     Better UI design

  • C

    Data security and abstraction

  • D

    Simplified syntax

Question 4

Which of the following best describes abstraction?


  • A

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

  • B

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

  • C

    Accessing data members and member functions of one class by other

  • D

    All of the above

Question 5

Which access specifier provides the highest level of protection?

  • A

    private

  • B

     protected

  • C

    public

  • D

    None of the above

Question 6

Which of the following correctly defines function overloading and function overriding in C++?

  • A

    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.

  • B

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

  • C

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

  • D

    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;
}
  • A

    Base

  • B

    Derived

  • C

    Compilation error

  • D

    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;
}
  • A
    Compiler Error
  • B
    1 2 3
  • C
    1 1 1
  • D
    3 3 3
  • E
    0 0 0

Question 9

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

  • A

    Public

  • B

    Private

  • C

    Protected

  • D

    Any of these

Question 10

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

There are 11 questions to complete.

Take a part in the ongoing discussion