What is the output of this procedural C++ code that calculates the area of a rectangle?
#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; }
Multiply width by height to get the area.
The function multiplies 5 (width) by 3 (height), resulting in 15.
What is the output of this C++ code using a class to calculate the area of a rectangle?
#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; }
The area method multiplies width and height stored in the object.
The Rectangle object has width 5 and height 3, so area() returns 15.
Which statement best describes how data is handled differently in procedural programming compared to object-oriented programming?
Think about how classes group data and behavior.
Procedural code treats data and functions separately; OOP groups them inside objects (classes).
What error will this C++ code produce?
#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; }
Consider what value radius has before assignment.
The radius variable is uninitialized, so area() uses an undefined value, causing garbage output.
What is the output of this C++ code that mixes procedural and OOP styles?
#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; }
Each call to incrementCounter calls the object's increment method once.
The Counter object starts at 0, incremented twice, so count is 2.