Challenge - 5 Problems
Abstraction Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
π§ Conceptual
intermediateWhy do we use abstraction in programming?
Which of the following best explains why abstraction is important in programming?
Attempts:
2 left
π‘ Hint
Think about how abstraction helps programmers focus on what matters.
β Incorrect
Abstraction hides the complex parts of code and shows only what is necessary. This helps reduce complexity and makes programs easier to understand and maintain.
β Predict Output
intermediateOutput 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; }
Attempts:
2 left
π‘ Hint
Look at how setSpeed changes the speed and showSpeed prints it.
β Incorrect
The class hides the speed variable (private). The setSpeed method sets it to 60, and showSpeed prints it as "Speed: 60 km/h".
π§ Debug
advancedIdentify 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; }
Attempts:
2 left
π‘ Hint
Check which parts of the class are visible to users.
β Incorrect
Making balance public exposes internal details. Abstraction requires hiding such data by making it private.
π Syntax
advancedWhich code snippet correctly implements abstraction?
Choose the code snippet that correctly hides data and provides access through methods.
Attempts:
2 left
π‘ Hint
Look for private data with public methods to access it.
β Incorrect
Option B correctly makes name private and provides public methods to set and get it, following abstraction principles.
π Application
expertHow does abstraction improve software maintenance?
Which statement best describes how abstraction helps when fixing bugs or adding features?
Attempts:
2 left
π‘ Hint
Think about how hiding details protects other parts of the program.
β Incorrect
Abstraction hides internal details so developers can improve or fix code without breaking how users interact with it.
