C++ Advanced Skill Assessment Practice Test

Last Updated :
Discuss
Comments

Question 1

Given the following class definition, which of the following statements is true regarding object construction and destruction?

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

class MyClass {
    MyClass() { cout << "Constructor\n"; }
    ~MyClass() { cout << "Destructor\n"; }
};

void createObject() {
    MyClass obj;
}

int main() {
    createObject();
    return 0;
}
  • A

    Constructor and Destructor will be called once.

  • B

    Constructor will be called twice, Destructor once.

  • C

    Constructor and Destructor will be called twice.

  • D

    Constructor will be called once, Destructor twice.

Question 2

How to implement a class in C++ to prevent it from being inherited?

  • A

    By making all constructors private.

  • B

    By declaring the class as sealed.

  • C

    By using the final keyword.

  • D

    By making all methods private.

Question 3

What will be the output of the following code?

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

class Base{
  public:
    Base(){
        cout << "Base Constructor\n";
    }
    virtual ~Base(){
        cout << "Base Destructor\n";
    }
};

class Derived : public Base{
  public:
    Derived(){
        cout << "Derived Constructor\n";
    }
    ~Derived(){
        cout << "Derived Destructor\n";
    }
};

int main(){
    Base *obj = new Derived();
    delete obj;
    return 0;
}
  • A

    Only Base Destructor is called

  • B

    Base Constructor, Derived Constructor, and Base Destructor are called.

  • C

    Base Constructor, Derived Constructor, Derived Destructor, and Base Destructor are called.

  • D

    Base Constructor, Derived Constructor, and Derived Destructor are called.

Question 4

What would be the outcome if the following setter method were used to modify a private member without validation?

C++
class Account {
private:
    double balance;
public:
    void setBalance(double b) { balance = b; }
};


  • A

    It prevents setting invalid values by default.

  • B

    It allows for any value to be set, including invalid states.

  • C

    It automatically rounds off the value to the nearest integer.

  • D

    It throws a runtime exception if the value is negative.

Question 5

Consider the following code. What will happen if the getter method is not declared as const?

C++
class MyClass {
private:
    int value;
public:
    int getValue() const { return value; }
};
  • A

    The method can modify the class's internal state.

  • B

    The method will automatically become a const method.

  • C

    The method will return a reference to the internal state.

  • D

    The method cannot be called on const instances of MyClass.

Question 6

What will be the output of the following code, considering the presence of slicing?

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

class Base {
public:
    virtual void print() const { cout << "Base\n"; }
};

class Derived : public Base {
public:
    void print() const override { cout << "Derived\n"; }
};

void show(Base b) {
    b.print();
}

int main() {
    Derived d;
    show(d);
    return 0;
}


  • A

    Base

  • B

    Derived

  • C

    Base followed by Derived

  • D

    Compilation error

Question 7

What will be the output of the following code?

C++
#include <iostream>
using namespace std;
class Base{
  public:
    Base(){
        cout << "Base Constructor\n";
    }
    virtual ~Base(){
        cout << "Base Destructor\n";
    }
};

class Derived : public Base{
  public:
    Derived(){
        cout << "Derived Constructor\n";
    }
    ~Derived(){
        cout << "Derived Destructor\n";
    }
};

int main(){
    Derived d;
    Base *b = &d;
    return 0;
}
  • A

    Base Constructor, Derived Constructor, Base Destructor

  • B

    Derived Constructor, Base Destructor, Derived Destructor

  • C

    Base Constructor, Derived Constructor, Derived Destructor, Base Destructor

  • D

    Compilation error

Question 8

What will be the output of the following code?

C++
#include <iostream>
using namespace std;
class Base{
  public:
    virtual void display() const{
        cout << "Base Display" << endl;
    }
};

class Derived : public Base{
  public:
    void display() const override{
        cout << "Derived Display" << endl;
    }
};

void show(const Base &b){
    b.display();
}

int main(){
    Derived d;
    show(d);
    return 0;
}


  • A

    Base Display

  • B

    Derived Display

  • C

    Base Display followed by Derived Display

  • D

    Compilation error

Question 9

Which among the following is the correct way to implement an abstract class in C++?

  • A

    By declaring a class as abstract

  • B

    By marking all member functions as virtual

  • C

    By inheriting from another abstract class

  • D

    By having at least one pure virtual function

Question 10

What will be the output of the following code?

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

template <typename T> T custom_max(T a, T b){
    return (a > b) ? a : b;
}

int main(){
    cout << custom_max<int>(3, 7) << endl;
    cout << custom_max(3.5, 2.1) << endl;
    return 0;
}
  • A

    7 and 2.1

  • B

    Compilation error

  • C

    7 and 3.5

  • D

    Undefined behavior

There are 37 questions to complete.

Take a part in the ongoing discussion