Bird
Raised Fist0
C++programming~10 mins

Why abstraction is required in C++ - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an abstract class with a pure virtual function.

C++
class Shape {
public:
    virtual void draw() = [1];
};
Drag options to blanks, or click blank then click option'
A0
B1
Ctrue
Dvoid
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 1 instead of 0 for pure virtual function
Omitting the virtual keyword
Trying to define the function body here
2fill in blank
medium

Complete the code to create a pointer to the abstract class and assign it to a derived class object.

C++
Shape* shape = new [1]();
Drag options to blanks, or click blank then click option'
AShape
BObject
CRectangle
DCircle
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Trying to instantiate the abstract class directly
Using a class that does not implement all pure virtual functions
3fill in blank
hard

Fix the error in the code by completing the missing keyword to enforce abstraction.

C++
[1] Shape {
public:
    virtual void display() = 0;
};
Drag options to blanks, or click blank then click option'
Aclass
BAbstract
CInterface
DConcrete
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using 'abstract' or 'interface' keywords which do not exist in C++
Omitting the 'class' keyword
4fill in blank
hard

Fill both blanks to define a derived class that implements the pure virtual function.

C++
class Circle : public Shape {
public:
    void [1]() override {
        std::cout << "Drawing Circle" << std::endl;
    }
};
Drag options to blanks, or click blank then click option'
Apaint
Bdisplay
Cdraw
Dshow
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Using a different function name than the base class
Omitting the override keyword
5fill in blank
hard

Fill all three blanks to create an abstract class and a derived class that implements its pure virtual function.

C++
class [1] {
public:
    virtual void [2]() = 0;
};

class [3] : public [1] {
public:
    void [2]() override {
        std::cout << "Implemented" << std::endl;
    }
};
Drag options to blanks, or click blank then click option'
AShape
Bdraw
CCircle
DObject
Attempts:
3 left
πŸ’‘ Hint
Common Mistakes
Mismatching class or function names
Not implementing the pure virtual function in the derived class

Practice

(1/5)
1. Why is abstraction important in C++ programming?
easy
A. It hides complex details and shows only necessary information.
B. It makes the program run faster by using less memory.
C. It allows direct access to hardware components.
D. It removes the need for functions and classes.

Solution

  1. Step 1: Understand the purpose of abstraction

    Abstraction hides unnecessary details and shows only what is needed to the user.
  2. Step 2: Compare options with the definition

    Only It hides complex details and shows only necessary information. correctly describes abstraction's role in simplifying complexity.
  3. Final Answer:

    It hides complex details and shows only necessary information. -> Option A
  4. Quick Check:

    Abstraction = hiding complexity [OK]
Hint: Abstraction means hiding details, not speeding up code [OK]
Common Mistakes:
  • Confusing abstraction with performance optimization
  • Thinking abstraction removes functions or classes
  • Believing abstraction gives hardware access
2. Which of the following is the correct way to declare an abstract class in C++?
easy
A. class Abstract { virtual void func(); };
B. abstract class Abstract { void func(); };
C. class Abstract { void func() {} };
D. class Abstract { virtual void func() = 0; };

Solution

  1. Step 1: Identify syntax for abstract class in C++

    An abstract class has at least one pure virtual function declared with '= 0'.
  2. Step 2: Check each option

    class Abstract { virtual void func() = 0; }; correctly declares a pure virtual function, making the class abstract.
  3. Final Answer:

    class Abstract { virtual void func() = 0; }; -> Option D
  4. Quick Check:

    Pure virtual function = '= 0' [OK]
Hint: Look for '= 0' to spot pure virtual functions [OK]
Common Mistakes:
  • Using 'abstract' keyword which doesn't exist in C++
  • Missing '= 0' for pure virtual functions
  • Defining function body in abstract class
3. What will be the output of the following C++ code?
class Shape {
public:
  virtual void draw() = 0;
};
class Circle : public Shape {
public:
  void draw() { std::cout << "Circle drawn"; }
};
int main() {
  Circle c;
  c.draw();
  return 0;
}
medium
A. Compilation error due to abstract class
B. Shape drawn
C. Circle drawn
D. Runtime error

Solution

  1. Step 1: Understand class hierarchy and method overriding

    Shape is abstract with pure virtual draw(). Circle overrides draw() with implementation.
  2. Step 2: Check main function behavior

    Circle object c calls draw(), which prints "Circle drawn".
  3. Final Answer:

    Circle drawn -> Option C
  4. Quick Check:

    Overridden draw() prints output [OK]
Hint: Concrete class must implement pure virtual functions [OK]
Common Mistakes:
  • Thinking abstract class can be instantiated
  • Expecting base class method output
  • Confusing compilation and runtime errors
4. Identify the error in the following C++ code related to abstraction:
class Animal {
public:
  virtual void sound() = 0;
};
class Dog : public Animal {
};
int main() {
  Dog d;
  d.sound();
  return 0;
}
medium
A. Dog class must implement the pure virtual function sound()
B. Animal class should not have pure virtual functions
C. Dog object cannot be created because Animal is abstract
D. No error, code runs fine

Solution

  1. Step 1: Check pure virtual function implementation

    Dog inherits from Animal but does not implement sound(), so Dog remains abstract.
  2. Step 2: Understand object creation rules

    Cannot create object of abstract class Dog; must implement all pure virtual functions.
  3. Final Answer:

    Dog class must implement the pure virtual function sound() -> Option A
  4. Quick Check:

    Pure virtual functions require implementation [OK]
Hint: Derived class must implement all pure virtual functions [OK]
Common Mistakes:
  • Assuming derived class auto-implements pure virtual functions
  • Trying to instantiate abstract classes
  • Ignoring pure virtual function requirements
5. You want to design a C++ program where different vehicle types share a common interface for starting the engine, but each vehicle starts differently. How does abstraction help here?
hard
A. By writing all start logic in the main function and calling it directly.
B. By defining an abstract class Vehicle with a pure virtual startEngine() method, forcing each vehicle type to implement its own start logic.
C. By creating separate unrelated classes for each vehicle without any common interface.
D. By using global variables to control engine start for all vehicles.

Solution

  1. Step 1: Identify the need for a common interface

    All vehicles share the concept of starting engine but differ in implementation.
  2. Step 2: Use abstraction to enforce implementation

    Abstract class Vehicle with pure virtual startEngine() ensures each subclass defines its own start method.
  3. Final Answer:

    By defining an abstract class Vehicle with a pure virtual startEngine() method, forcing each vehicle type to implement its own start logic. -> Option B
  4. Quick Check:

    Abstraction enforces interface, hides implementation [OK]
Hint: Use abstract class to enforce method implementation [OK]
Common Mistakes:
  • Putting all logic in main instead of classes
  • Not using a common interface for related classes
  • Using global variables instead of abstraction
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard