What happens when you attempt to compile and run the following code?
#include
using namespace std;
int fun(int x) {
return x<<2;
}
int main(){
int i;
i = fun(1) / 2;
cout << i;
return 0;
}
Suggested Answer: B
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
30 comments · newest first
ST
Stefania
1 year ago
Fun fact: bit shifting is cool!
RE
Regenia
1 year ago
Wait, really? I thought it would be 4!
LA
Lashon
1 year ago
I think it actually prints 2.
LA
Lauryn
1 year ago
No way, it should print 1!
WA
Walton
1 year ago
It prints: 0
CO
Coral
1 year ago
I’m confused about the division part. If it returns 4, then dividing by 2 should give us 2, right?
CH
Chuck
1 year ago
I practiced a similar question last week, and I believe it should print 2 as well. The left shift doubles the number.
EL
Elfriede
1 year ago
I'm not entirely sure, but I remember something about bitwise operations. Does that mean it would print 2 after dividing by 2?
AN
Annmarie
1 year ago
I think the function shifts the number left by 2 bits, so it should return 4 when I pass 1 to it.
AR
Arlean
1 year ago
Hmm, I'm not entirely sure about this one. I'll need to review my notes on bitwise operators and integer division to make sure I'm understanding it correctly.
FL
Fletcher
1 year ago
I think I've got it! The left shift by 2 bits is equivalent to multiplying by 4, so `fun(1)` returns 4. Then, dividing that by 2 gives us the final result of 2. I'm confident in this answer.
CH
Charlene
1 year ago
Wait, I'm a bit confused. Isn't the left shift operator `<<` supposed to multiply the value by 2 raised to the power of the shift amount? I'll have to double-check that.
RA
Raylene
1 year ago
Okay, let me see here. The function `fun(1)` returns 4, and then we divide that by 2. So the output should be 2, right?
KE
Kent
1 year ago
Hmm, this looks like a bit of a tricky one. I'll need to carefully step through the code and think about the bitwise shift operator.
MA
Mary
1 year ago
I'm a little confused by the wording of the question. Do I need to merge the contacts first, or can I just focus on the accounts? I'll have to re-read it carefully.
GE
Genevive
2 years ago
Haha, this is a classic bit-shift question! The answer is definitely B) It prints: 1. I remember learning about this in my coding class, where the instructor said, 'Bit-shifting is like magic, but it's also like a secret handshake for programmers.'
JE
Jeniffer
1 year ago
Oh, I see! So the result is 4, but then divided by 2 to get 2, which is printed out.
DO
Dorsey
2 years ago
Actually, it prints: 1 because the function fun(1) shifts the binary representation of 1 two positions to the left.
SH
Shonda
2 years ago
I think the answer is D) It prints: 4.
LE
Lelia
2 years ago
Hmm, this is a tricky one. I'm going to go with D) It prints: 4. The bit-shift operation must be doing something weird, so let's just go with the largest number in the options.
AU
Audra
1 year ago
User3: Yeah, let's go with D) It prints: 4
MA
Mariann
1 year ago
User2: I agree, the bit-shift operation is probably causing that
CA
Carmen
1 year ago
User1: I think it prints: 4
CH
Chaya
2 years ago
I think the answer is C) It prints: 2. The bit-shift operation `x<<2` effectively multiplies `x` by 4, so `fun(1)` returns 4, and when divided by 2, the result is 2.
YE
Yen
2 years ago
That makes sense, the bit-shift operation is multiplying by 4.
WI
Wilson
2 years ago
I agree, the answer is C) It prints: 2.
VE
Veronica
2 years ago
The correct answer is B) It prints: 1. The function `fun(1)` returns 4, and when divided by 2, the result is 2.
LE
Levi
2 years ago
I'm not sure, but I think the answer might be C.
LA
Laurene
2 years ago
I agree with Daniel, because the function fun() shifts the bits of the input left by 2 positions.
DA
Daniel
2 years ago
I think the answer is D.