OOP Concepts Questions with Answers

9 practice questions on OOP Concepts (C++) with answers and step-by-step solutions. Try 9 samples below, then practise all 9 free in the AptiNest app.

Sample questions

Question 1Easy

In C++, what is the default access level of members declared inside a class (before any access label)?

  1. public
  2. protected
  3. private
  4. friend

Answer: C. private

Explanation

Members of a class are private by default. In a struct, the default is public, which is the only difference between the two keywords for member access.

Step-by-step solution

  1. A class without an access label makes its members private.
  2. A struct makes its members public by default.
  3. So the answer for class is private.

Why the other options are wrong

  • A. public is the default for struct members, not class members.
  • B. protected is never the default; it is an explicit label.
  • D. friend is a grant of access to another class or function, not a member access level.

Tip: class = private by default; struct = public by default.

Question 2Medium

How to access the object in the class?

  1. scope resolution operator
  2. ternary operator
  3. direct member access operator
  4. none of the mentioned

Answer: C. direct member access operator

Explanation

Objects in the method can be accessed using direct member access operator which is (.).

Step-by-step solution

  1. Objects in a class are accessed using the direct member access operator.
  2. This operator allows you to call methods or access properties directly on an object.
  3. The correct answer is option C, direct member access operator.

Why the other options are wrong

  • A. Scope resolution operator is used for accessing members in other classes.
  • B. Ternary operator is used for conditional expressions, not object access.
  • D. None of the mentioned is incorrect because direct member access is a valid method.

Tip: Remember: Use (. ) to access members of an object.

Question 3Medium

What is printed?

#include <iostream>
struct Counter {
    static int n;
    Counter() { ++n; }
};
int Counter::n = 0;
int main() {
    Counter a, b, c;
    std::cout << Counter::n;
}
  1. 1
  2. Compile error
  3. 0
  4. 3

Answer: D. 3

Explanation

n is a static member, so there is one shared copy for the whole class. Each of the three objects increments that shared n in its constructor, so it ends at 3.

Step-by-step solution

  1. n is static, so every Counter object shares one n.
  2. Creating a, b and c runs the constructor three times, adding 1 each time.
  3. Counter::n is therefore 3.

Why the other options are wrong

  • A. 1 would mean only one object was made; three constructors run, so n is 3.
  • B. The static member is defined and the code compiles.
  • C. 0 would be the value if the constructor never ran.

Tip: Static data members belong to the class, so every object changes the same variable.

Question 4Medium

What is printed?

#include <iostream>
struct Base {
    virtual void hi() { std::cout << "B"; }
    void call() { hi(); }
};
struct Der : Base {
    void hi() override { std::cout << "D"; }
};
int main() {
    Der d;
    d.call();
}
  1. B
  2. Compile error
  3. BD
  4. D

Answer: D. D

Explanation

hi() is virtual, so the call inside Base::call() uses the dynamic type of the object. The object is a Der, so Der::hi runs and prints D.

Step-by-step solution

  1. d.call() runs Base::call on a Der object.
  2. Inside call, hi() is a virtual call, so C++ looks at the real object type.
  3. The real type is Der, so Der::hi prints D.

Why the other options are wrong

  • A. Base::hi would run only if hi were not virtual or the object were a plain Base.
  • B. The override is valid and the code compiles.
  • C. Only one hi() runs, so B is not printed.

Tip: A virtual call inside a base class method still goes to the derived override.

Question 5Medium

Where does the object is created?

  1. class
  2. constructor
  3. destructor
  4. attributes

Answer: A. class

Explanation

An object is an instance of a class: the class is the blueprint it is created from.
The constructor only initialises the object once it exists, and the destructor cleans it up; attributes are data inside the class.

Step-by-step solution

  1. An object is an instance of a class, so an object is created from the class definition.
  2. The constructor runs when an object is created, but it initializes an object rather than defining the class.
  3. The destructor runs when an object is destroyed, not when it is created.
  4. The answer is class.

Why the other options are wrong

  • B. The constructor initializes an object that is created from the class.
  • C. The destructor cleans up an object at the end of its life.
  • D. Attributes are the data members that an object holds, not where it is created.

Tip: Objects are instances of a class.

Question 6Medium

What is the output of this program?

    #include <iostream>
    using namespace std;
    class sample
    {
        private:
        int var;
        public:
        void input()
        {
           cout << var;
        }
        void output()
        {
           cout << "Variable entered is ";
           cout << var << "";
        }
    };
    int main()
    {
        sample object;
        object.input();
        object.output();
        object.var();
        return 0;
    }
  1. Enter an integer 5 Variable entered is 5
  2. runtime error
  3. error
  4. none of the mentioned

Answer: C. error

Explanation

object.var(); is a compile-time error: var is a private data member, not a function, so it cannot be called (and it is not accessible outside the class anyway).

Step-by-step solution

  1. input() prints the private member var, which is never set, so it would show a garbage value.
  2. object.var() calls var as if it were a function, but var is a data member, not a method.
  3. Var is also private, so main cannot reach it at all.
  4. The compiler stops at object.var(), so the program produces no output and the answer is option C, error.

Why the other options are wrong

  • A. The code never prints 'Enter an integer', and the program does not compile, so no output is produced.
  • B. A runtime error needs the program to compile first; this fails at compile time.
  • D. Option C is correct, so 'none of the mentioned' is wrong.

Tip: A data member cannot be called with () as if it were a function, and a private member cannot be used from outside its class.

Question 7Medium

What is printed?

#include <iostream>
struct A {
    A() { std::cout << "A"; }
    ~A() { std::cout << "~A"; }
};
struct B : A {
    B() { std::cout << "B"; }
    ~B() { std::cout << "~B"; }
};
int main() {
    B b;
}
  1. AB~A~B
  2. AB~B~A
  3. BA~B~A
  4. A~AB~B

Answer: B. AB~B~A

Explanation

Constructors run from the base class outward, so A prints first, then B. Destructors run in the reverse order, so ~B prints before ~A.

Step-by-step solution

  1. Creating b runs the A constructor first, which prints A.
  2. Then the B constructor body runs and prints B.
  3. When b ends, ~B runs first, then the base destructor ~A.

Why the other options are wrong

  • A. Destructors run in reverse order of construction, so ~B comes before ~A.
  • C. The base constructor always runs before the derived one.
  • D. The base destructor runs after the derived one, and the text does not match the object's life.

Tip: Construction goes base first; destruction goes derived first.

Question 8Medium

How many objects can present in a single class?

  1. 1
  2. 2
  3. 3
  4. As many as possible

Answer: D. As many as possible

Explanation

Because a class may contain any number of objects according to its compliance.

Step-by-step solution

  1. The question asks how many objects a single class can contain.
  2. A class can have any number of member objects of other classes, limited only by memory and design.
  3. So the answer is 'as many as possible', which is option D.

Why the other options are wrong

  • A. A class is not limited to one object member.
  • B. A class is not limited to two object members.
  • C. A class is not limited to three object members.

Tip: A class can hold any number of member objects, so a fixed number is never the right answer.

Question 9Hard

What is printed?

#include <iostream>
struct Base {
    int v = 1;
    virtual int get() const { return v; }
};
struct Der : Base {
    int w = 2;
    int get() const override { return w; }
};
int main() {
    Der d;
    Base b = d;
    std::cout << b.get();
}
  1. 0
  2. 1
  3. 2
  4. Compile error

Answer: B. 1

Explanation

Base b = d copies only the Base part of d (object slicing). b is a Base object, so b.get() calls Base::get and returns v, which is 1.

Step-by-step solution

  1. Base b = d copies the Base subobject into a new Base object.
  2. The Der part, including w, is sliced away, so b has no Der behaviour.
  3. b.get() runs Base::get, which returns v = 1.

Why the other options are wrong

  • A. v was set to 1 by the Base member initialiser; it is not zero.
  • C. 2 is w, which is lost when the object is sliced.
  • D. The code compiles; a derived object can be copied to a base object.

Tip: Copying a derived object into a base object by value slices off the derived part, so virtual calls no longer reach the override.

Practise all 9 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C++ topics · All topics