Object Oriented Programming in C++

Last Updated :
Discuss
Comments

Question 1

What happens if the base and derived class contains a definition of a function with the same prototype?

  • Compiler reports an error on compilation.

  • Only base class function will get called irrespective of object.

  • Only derived class function will get called irrespective of object.

  • Base class object will call base class function and derived class object will call derived class function.

Question 2

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 3

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 4

Which among the following is the correct output of the below program.

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

class Base {
public:
    Base() { std::cout << "Base Constructor\n"; }
    virtual ~Base() { std::cout << "Base Destructor\n"; }
};
class Derived : public Base {
public:
    Derived() { std::cout << "Derived Constructor\n"; }
    ~Derived() { std::cout << "Derived Destructor\n"; }
};
void createAndDestroy() {
    Base* b = new Derived();
    delete b;
}
int main() {
    createAndDestroy();
    return 0;
}
  • Base Constructor
    Derived Constructor
    Base Destructor

  • Base Constructor
    Derived Constructor
    Derived Destructor
    Base Destructor

  • Derived Constructor
    Base Constructor
    Derived Destructor
    Base Destructor

  • Derived Constructor
    Base Constructor
    Base Destructor
    Derived Destructor

Question 5

Predict the output of following C++ progran CPP
#include <iostream>
using namespace std;
 
int i;
 
class A
{
public:
    ~A()
    {
        i=10;
    }
};
 
int foo()
{
    i=3;
    A ob;
    return i;
}
 
int main()
{
    cout << foo() << endl;
    return 0;
}
  • 0
  • 3
  • 10
  • None of the above

Question 6

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 7

Predict the output the of following program. 

C
#include <iostream>
using namespace std;

class B;
class A {
    int a;
public:
    A():a(0) { }
    void show(A& x, B& y);
};

class B {
private:
    int b;
public:
    B():b(0) { }
    friend void A::show(A& x, B& y);
};

void A::show(A& x, B& y) {
    x.a = 10;
    cout << "A::a=" << x.a << " B::b=" << y.b;
}

int main() {
    A a;
    B b;
    a.show(a,b);
    return 0;
}
  • Compiler Error
  • A::a=10 B::b=0
  • A::a=0 B::b=0

Question 8

Which access specifier provides the highest level of protection?

  • private

  •  protected

  • public

  • None of the above

Question 9

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


  • Faster execution


  •  Better UI design


  • Data security and abstraction

  • Simplified syntax

Question 10

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

There are 17 questions to complete.

Take a part in the ongoing discussion