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;
}
Suggested Answer: C
Get Premium CPA-21-02 Questions
as Interactive Web-Based Practice Test or PDF
Real Exam Format
Detailed Explanations
Instant Access
Get Your Practice Test
23 comments · newest first
LA
Latia
10 months ago
It's using integer division, so 1.0/2 is 0 in this case.
AL
Altha
10 months ago
Wait, how does it not print 0.5?
AL
Alana
10 months ago
It prints: 1
JA
Jamie
10 months ago
Definitely prints 1, no doubt!
BE
Beckie
11 months ago
I thought it would be 0.5 too, but I guess not?
MA
Matilda
11 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.
JE
Jeffrey
11 months ago
I practiced a similar question where the output was affected by type casting. I wonder if that applies here too.
CO
Cory
11 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.
AL
Alaine
11 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.
PH
Phung
12 months ago
I'm feeling pretty confident about this one. The expression looks straightforward, and I know integer division will truncate the result.
OR
Oretha
12 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.
CO
Colby
12 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.
JE
Jesusa
1 year ago
Hmm, the expression seems a bit convoluted. I'll need to pay close attention to the order of operations and integer division.
SH
Shantell
1 year ago
This looks like a tricky one. I'll need to carefully work through the expression step-by-step to figure out the result.
KE
Kerrie
1 year ago
Haha, the code looks like it's performing some sort of cosmic dance with the numbers. I wonder what the output will be?
BU
Buddy
1 year ago
Ooh, this looks like a trick question. I'm going to go with D just to be safe.
MA
Marylyn
1 year ago
Hmm, let me think about this... Doesn't integer division always truncate the result? I'm going with B!
PE
Pearly
1 year ago
Wait, are we supposed to use floating-point arithmetic or integer arithmetic? I'm so confused!
LO
Lorriane
1 year ago
That makes sense, I can see how the calculations would lead to that result
WI
Willodean
1 year ago
I think the correct answer is C) It prints: 2 because of the arithmetic operations in the code
DO
Dortha
1 year ago
But doesn't the code involve some calculations that might result in a different output?
LO
Lorriane
1 year ago
I believe the answer is B) It prints: 1
DO
Dortha
1 year ago
I think the answer is A) It prints: 0