New Year Sale 2026! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute CPA-21-02 Exam - Topic 2 Question 42 Discussion

Actual exam question for C++ Institute's CPA-21-02 exam
Question #: 42
Topic #: 2
[All CPA-21-02 Questions]

What happens when you attempt to compile and run the following code?

#include

using namespace std;

int main(){

int *i;

i = new int;

*i = 1.0 / 2 * 2 / 1 * 2 / 4 * 4;

cout << *i;

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: C

Contribute your Thoughts:

0/2000 characters
Latia
2 months ago
It's using integer division, so 1.0/2 is 0 in this case.
upvoted 0 times
...
Altha
2 months ago
Wait, how does it not print 0.5?
upvoted 0 times
...
Alana
2 months ago
It prints: 1
upvoted 0 times
...
Jamie
3 months ago
Definitely prints 1, no doubt!
upvoted 0 times
...
Beckie
3 months ago
I thought it would be 0.5 too, but I guess not?
upvoted 0 times
...
Matilda
3 months ago
I feel like it should print 1 because of how the operations are ordered, but I'm not completely confident about the precedence rules.
upvoted 0 times
...
Jeffrey
3 months ago
I practiced a similar question where the output was affected by type casting. I wonder if that applies here too.
upvoted 0 times
...
Cory
4 months ago
I remember something about integer division in C++. If I recall correctly, 1.0 / 2 would be treated as a double, but the result gets assigned to an int pointer.
upvoted 0 times
...
Alaine
4 months ago
I think the expression evaluates to an integer, so it might print 0 or 1. I'm not entirely sure how the division works here.
upvoted 0 times
...
Phung
4 months ago
I'm feeling pretty confident about this one. The expression looks straightforward, and I know integer division will truncate the result.
upvoted 0 times
...
Oretha
4 months ago
I'm a bit confused by the use of the pointer here. I'll need to make sure I understand how the dereference operator works in this context.
upvoted 0 times
...
Colby
4 months ago
Okay, I think I've got this. The key is that the expression is using integer division, so I expect the result to be an integer.
upvoted 0 times
...
Jesusa
5 months ago
Hmm, the expression seems a bit convoluted. I'll need to pay close attention to the order of operations and integer division.
upvoted 0 times
...
Shantell
5 months ago
This looks like a tricky one. I'll need to carefully work through the expression step-by-step to figure out the result.
upvoted 0 times
...
Kerrie
6 months ago
Haha, the code looks like it's performing some sort of cosmic dance with the numbers. I wonder what the output will be?
upvoted 0 times
...
Buddy
6 months ago
Ooh, this looks like a trick question. I'm going to go with D just to be safe.
upvoted 0 times
...
Marylyn
6 months ago
Hmm, let me think about this... Doesn't integer division always truncate the result? I'm going with B!
upvoted 0 times
...
Pearly
6 months ago
Wait, are we supposed to use floating-point arithmetic or integer arithmetic? I'm so confused!
upvoted 0 times
...
Lorriane
7 months ago
That makes sense, I can see how the calculations would lead to that result
upvoted 0 times
...
Willodean
7 months ago
I think the correct answer is C) It prints: 2 because of the arithmetic operations in the code
upvoted 0 times
...
Dortha
7 months ago
But doesn't the code involve some calculations that might result in a different output?
upvoted 0 times
...
Lorriane
7 months ago
I believe the answer is B) It prints: 1
upvoted 0 times
...
Dortha
7 months ago
I think the answer is A) It prints: 0
upvoted 0 times
...

Save Cancel