0
0
C++programming~20 mins

Procedural vs OOP approach in C++ - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Procedural vs OOP Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of procedural code for area calculation

What is the output of this procedural C++ code that calculates the area of a rectangle?

C++
#include <iostream>

int calculateArea(int width, int height) {
    return width * height;
}

int main() {
    int w = 5, h = 3;
    std::cout << calculateArea(w, h) << std::endl;
    return 0;
}
A53
B8
C15
DCompilation error
Attempts:
2 left
πŸ’‘ Hint

Multiply width by height to get the area.

❓ Predict Output
intermediate
2:00remaining
Output of OOP code for area calculation

What is the output of this C++ code using a class to calculate the area of a rectangle?

C++
#include <iostream>

class Rectangle {
public:
    int width, height;
    int area() {
        return width * height;
    }
};

int main() {
    Rectangle rect;
    rect.width = 5;
    rect.height = 3;
    std::cout << rect.area() << std::endl;
    return 0;
}
A15
BCompilation error
C8
D0
Attempts:
2 left
πŸ’‘ Hint

The area method multiplies width and height stored in the object.

🧠 Conceptual
advanced
2:00remaining
Difference in data handling between procedural and OOP

Which statement best describes how data is handled differently in procedural programming compared to object-oriented programming?

AProcedural programming bundles data and functions inside objects, while OOP keeps data and functions separate.
BProcedural programming organizes code around functions and data separately, while OOP bundles data and functions together inside objects.
CBoth procedural and OOP always keep data and functions separate without any bundling.
DOOP does not use functions, only data, while procedural programming uses only functions.
Attempts:
2 left
πŸ’‘ Hint

Think about how classes group data and behavior.

πŸ”§ Debug
advanced
2:00remaining
Identify error in OOP code

What error will this C++ code produce?

C++
#include <iostream>

class Circle {
public:
    double radius;
    double area() {
        return 3.14 * radius * radius;
    }
};

int main() {
    Circle c;
    std::cout << c.area() << std::endl;
    return 0;
}
AOutputs 0
BRuntime error: division by zero
CCompilation error: radius not initialized
DOutputs garbage value
Attempts:
2 left
πŸ’‘ Hint

Consider what value radius has before assignment.

❓ Predict Output
expert
2:00remaining
Output of mixed procedural and OOP code

What is the output of this C++ code that mixes procedural and OOP styles?

C++
#include <iostream>

class Counter {
public:
    int count = 0;
    void increment() { count++; }
};

void incrementCounter(Counter &c) {
    c.increment();
}

int main() {
    Counter c;
    incrementCounter(c);
    incrementCounter(c);
    std::cout << c.count << std::endl;
    return 0;
}
A2
B1
C0
DCompilation error
Attempts:
2 left
πŸ’‘ Hint

Each call to incrementCounter calls the object's increment method once.