C++ Virtual Function Aptitude Questions and Answers

C++ Virtual Function Aptitude: This section contains C++ Virtual Function Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on March 06, 2021

1) There are the following statements that are given below, which of them are correct about virtual function in C++?

  1. The virtual function is the type of runtime polymorphism in C++.
  2. The virtual function is used in inheritance where a function is declared in the base and it is overridden in child class.
  3. For virtual function implementation we need did not require any special keyword.
  4. All of the above

Options:

  1. A
  2. A and B
  3. A, B, and C
  4. D

2) Which of the following types of binding used in C++?

  1. Static binding
  2. Dynamic binding
  3. Pointer binding
  4. Inheritance binding

Options:

  1. A and B
  2. C and D
  3. A, B, and C
  4. A, B, C, and D

3) The dynamic binding is also known as?

  1. Early binding
  2. Late binding
  3. Static binding
  4. None of the above

4) There are the following statements that are given below, which of them are correct about dynamic binding in C++?

  1. The dynamic binding is performed at runtime.
  2. In the dynamic binding, function calls are not resolved until runtime.
  3. Dynamic binding is a type of runtime polymorphism.
  4. All the above

Options:

  1. A and B
  2. A and C
  3. B and C
  4. D

5) There is a code snippet that is given below, which is the example of dynamic binding?

#include <iostream>
using namespace std;

class Sample {
public:
    void display(int X, int Y)
    {
        cout << X << " " << Y << " ";
    }
    void display(int X, int Y, int Z)
    {
        cout << X << " " << Y << " " << Z << " ";
    }
};

int main()
{
    Sample S;

    S.display(10, 20);
    S.display(10, 20, 30);
    return 0;
}

Options:

  1. Yes
  2. No

6) A virtual function can be static?

  1. Yes
  2. No

7) A virtual function can be a friend function of another class?

  1. Yes
  2. No

8) There are the following statements that are given below, which of them are correct about the virtual keyword in C++?

  1. The signature of a virtual function must be the same in both base and derived class.
  2. We can create a virtual constructor in a class.
  3. We can create virtual destructor in a class.
  4. All the above

Options:

  1. A and B
  2. A and C
  3. B and C
  4. D

9) What is the correct output of the given code snippet?

#include <iostream>
using namespace std;

class Base {
public:
    virtual void display()
    {
        cout << "Base class function";
    }
};

class Child : public Base {
public:
    virtual int display()
    {
        cout << "Child class function";
    }
};

int main()
{
    Child C;

    C.display();

    return 0;
}

Options:

  1. Base class function
  2. Child class function
  3. Syntax error
  4. Runtime error

10) What is the correct output of the given code snippet?

#include <iostream>
using namespace std;

class Base {
public:
    virtual void display()
    {
        cout << "Base class function";
    }
};

class Child : public Base {
public:
    virtual void display()
    {
        cout << "Child class function";
    }
};

int main()
{
    Child C;

    C.display();

    return 0;
}

Options:

  1. Base class function
  2. Child class function
  3. Syntax error
  4. Runtime error

11) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Base {
public:
    void display()
    {
        cout << "Base class function";
    }
};

class Child : public Base {
public:
    virtual int display()
    {
        cout << "Child class function";
    }
};

int main()
{
    Child C;

    C.display();

    return 0;
}

Options:

  1. Base class function
  2. Child class function
  3. Syntax error
  4. Runtime error

12) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Base {
public:
    virtual void display()
    {
        cout << "Base class function" << endl;
    }
};

class Child : public Base {
public:
    void display()
    {
        cout << "Child class function" << endl;
    }
};

int main()
{
    Base* bPtr;

    bPtr->display();

    return 0;
}

Options:

  1. Base class function
  2. Child class function
  3. Syntax error
  4. Runtime error

13) What is the correct output of given code snippets?

#include <iostream>
using namespace std;

class Base {
public:
    virtual void display()
    {
        cout << "Base class function" << endl;
    }
};

class Child : public Base {
public:
    void display()
    {
        cout << "Child class function" << endl;
    }
};

int main()
{
    Base* bPtr;

    Base B;

    bPtr = B;

    bPtr->display();

    return 0;
}

Options:

  1. Base class function
  2. Child class function
  3. Syntax error
  4. Runtime error

14) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Base {
public:
    virtual void display()
    {
        cout << "Base class function" << endl;
    }
};

class Child : public Base {
public:
    void display()
    {
        cout << "Child class function" << endl;
    }
};

int main()
{
    Base* bPtr;

    Base B;

    bPtr = &B;

    bPtr->display();

    return 0;
}

Options:

  1. Base class function
  2. Child class function
  3. Syntax error
  4. Runtime error

15) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Base {
public:
    virtual void display()
    {
        cout << "Base class function" << endl;
    }
};

class Child : public Base {
public:
    void display()
    {
        cout << "Child class function" << endl;
    }
};

int main()
{
    Base* bPtr;

    Child C;

    bPtr = &C;

    bPtr->display();

    return 0;
}

Options:

  1. Base class function
  2. Child class function
  3. Syntax error
  4. Runtime error

16) There are the following statements that are given below, which of them are correct about the virtual table in C++?

  1. A virtual table is used to resolve virtual function calls to achieve dynamic binding.
  2. Every class has its own virtual table for virtual functions.
  3. A virtual table is also known as the dispatch table.
  4. A virtual table is also known as VTABLE.

Options:

  1. A and B
  2. A and C
  3. A, B, and C
  4. A, B, C, and D

17) There are the following statements that are given below, which of them are correct about virtual pointer in C++?

  1. A virtual pointer is used for virtual function calls.
  2. A virtual pointer is inserted as data member to point virtual function address in the virtual table.
  3. Every object of a class has its own virtual pointer.
  4. A virtual pointer is also known as VPTR.

Options:

  1. A and B
  2. A and C
  3. A, B, and D
  4. A, B, C, and D

18) There are the following statements that are given below, which of them are correct about pure virtual function in C++?

Options:

  1. A pure virtual function is a virtual function that is not defined in the class where it is declared.
  2. A pure virtual function is declared by assigning 0.
  3. Both of the above
  4. None of the above

19) To create pure virtual function we need to use a "pure" keyword?

  1. Yes
  2. No

20) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Base {
public:
    virtual void display() = 0
    {
    }
};

class Child : public Base {
public:
    void display()
    {
        cout << "Child class function" << endl;
    }
};

int main()
{
    Base* bPtr;

    Child C;

    bPtr = &C;

    bPtr->display();

    return 0;
}

Options:

  1. Child class function
  2. Base class function
  3. Syntax error
  4. Runtime error

21) What is the correct output of the given code snippets?

#include <iostream>
using namespace std;

class Base {
public:
    virtual void display() = 0;
};

class Child1 : public Base {
public:
    void display()
    {
        cout << "Child1 class function" << endl;
    }
};

class Child2 : public Base {
public:
    void display()
    {
        cout << "Child2 class function" << endl;
    }
};

int main()
{
    Base* bPtr = new Child1();

    bPtr->display();

    return 0;
}

Options:

  1. Child1 class function
  2. Child2 class function
  3. Syntax error
  4. Runtime error

22) A class contains pure virtual functions is known as?

  1. Abstract class
  2. Concrete class
  3. Pure virtual class
  4. Virtual base class

23) We cannot create the object of an abstract class in C++?

  1. Yes
  2. No

24) There are the following statements that are given below, which of them are correct about abstract class in C++?

  1. We can create a pointer of an abstract class, but we cannot create an object of an abstract class.
  2. An abstract class can contain variables, pure virtual functions, and other functions.
  3. Both of the above
  4. None of the above

25) The virtual base class is used to prevent the ambiguity of multiple inheritance?

  1. Yes
  2. No





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.