0
0
C++programming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Abstraction Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do we use abstraction in programming?
Which of the following best explains why abstraction is important in programming?
AIt hides complex details and shows only essential features to reduce complexity.
BIt makes the program run faster by skipping unnecessary code.
CIt allows the program to use more memory for better performance.
DIt forces the user to learn all the details of the program.
Attempts:
2 left
πŸ’‘ Hint
Think about how abstraction helps programmers focus on what matters.
❓ Predict Output
intermediate
2:00remaining
Output of code demonstrating abstraction
What is the output of this C++ code that uses abstraction with a class?
C++
#include <iostream>
using namespace std;

class Car {
private:
    int speed;
public:
    Car() { speed = 0; }
    void setSpeed(int s) { speed = s; }
    void showSpeed() { cout << "Speed: " << speed << " km/h" << endl; }
};

int main() {
    Car myCar;
    myCar.setSpeed(60);
    myCar.showSpeed();
    return 0;
}
AError: speed is private
BSpeed: 0 km/h
CSpeed: 60 km/h
DSpeed: 60
Attempts:
2 left
πŸ’‘ Hint
Look at how setSpeed changes the speed and showSpeed prints it.
πŸ”§ Debug
advanced
2:00remaining
Identify the abstraction violation in this code
What is wrong with this C++ code that breaks the idea of abstraction?
C++
#include <iostream>
using namespace std;

class BankAccount {
public:
    int balance;
    void deposit(int amount) { balance += amount; }
    void showBalance() { cout << "Balance: " << balance << endl; }
};

int main() {
    BankAccount acc;
    acc.balance = 1000;
    acc.deposit(500);
    acc.showBalance();
    return 0;
}
AshowBalance should return balance instead of printing.
Bmain function should not create BankAccount objects.
Cdeposit method should not add amount to balance.
Dbalance should be private to hide details; making it public breaks abstraction.
Attempts:
2 left
πŸ’‘ Hint
Check which parts of the class are visible to users.
πŸ“ Syntax
advanced
2:00remaining
Which code snippet correctly implements abstraction?
Choose the code snippet that correctly hides data and provides access through methods.
Aclass Person { public: string name; void setName(string n) { name = n; } string getName() { return name; } };
Bclass Person { private: string name; public: void setName(string n) { name = n; } string getName() { return name; } };
Cclass Person { private: string name; void setName(string n) { name = n; } string getName() { return name; } };
Dclass Person { string name; public: string getName() { return name; } };
Attempts:
2 left
πŸ’‘ Hint
Look for private data with public methods to access it.
πŸš€ Application
expert
3:00remaining
How does abstraction improve software maintenance?
Which statement best describes how abstraction helps when fixing bugs or adding features?
ABy hiding complex details, abstraction lets developers change internal code without affecting users.
BAbstraction forces all code to be rewritten when a small change is needed.
CAbstraction makes the program run slower, so maintenance is harder.
DAbstraction removes the need for comments and documentation.
Attempts:
2 left
πŸ’‘ Hint
Think about how hiding details protects other parts of the program.