Question 11
Back to Exercise
What is the output of the following C++ program?
#include <iostream>
class StockCounter {
unsigned count_ = 0;
public:
void add(unsigned n) { count_ += n; }
bool remove(unsigned n) {
if (n > count_) return false;
count_ -= n;
return true;
}
unsigned count() const { return count_; }
};
int main() {
StockCounter s;
s.add(5);
bool a = s.remove(3), b = s.remove(4), c = s.remove(2);
std::cout << a << b << c << ' ' << s.count() << '\n';
return 0;
}
- A 101 0 Correct
- B 111 0
- C 100 2
- D 101 4294967295
Correct Answer: Option A
Removing 4 from 2 is refused, so the unsigned counter never wraps around to a huge value. The final count is 0.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!