What is the output of the program given below?
#include
using namespace std;
int main (int argc, const char * argv[])
{
float f=?10.501;
cout<<(int)f;
}
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
26 comments · newest first
WI
Wilburn
1 year ago
I agree, it should be 10 after casting!
AR
Arthur
1 year ago
Wait, is the output really 11? That seems off.
DO
Dominga
1 year ago
Pretty sure it rounds down to 10.
LO
Lottie
1 year ago
I think it will output 11 when cast to int.
HA
Haley
1 year ago
It's definitely 10.501 as a float.
LI
Ligia
1 year ago
I’m leaning towards option D, but I’m confused about the initial float value. Did we cover how to handle floats with a leading question mark?
MA
Maryann
1 year ago
This seems similar to a practice question we did last week. I believe it should output 11 because of how rounding works in C++.
HO
Howard
1 year ago
I think the output should be the integer part of the float, so maybe it's 10? But I'm not completely confident.
ZE
Zena
1 year ago
I remember we discussed type casting in class, but I'm not sure how the float will convert to an int here.
RO
Roselle
1 year ago
I've got this! The float 10.501 will be truncated to 10 when cast to an int. The output should be 10.
GE
German
1 year ago
I'm a bit confused here. Does the type casting round the value or just truncate it? I'll have to double-check the behavior.
DA
Daisy
1 year ago
Okay, let me walk through this step-by-step. The float is initialized to 10.501, and then we cast it to an int. I think the output will be 10.
TI
Tijuana
1 year ago
Hmm, this looks like a tricky one. I'll need to carefully think through the type casting from float to int.
JE
Jennifer
2 years ago
This is a simple case of rounding. The float f is 10.501, and when we cast it to an integer, it should round up to 11. Option B is the correct answer, no doubt about it.
EM
Emilio
2 years ago
Haha, the question mark is just there to confuse us. It's a classic trick question! I'm going to go with option D, because who doesn't love a bit of chaos in their code?
CY
Cyndy
1 year ago
I agree with you, option D seems like the chaotic choice.
EM
Emily
1 year ago
I'm going with option B, the output will be 11.
HE
Helaine
1 year ago
I think it's option C, the output will be ?10.
LO
Louisa
2 years ago
Wait, why is the float initialized with a question mark? That's not valid syntax! I'm going to go with option C, which seems to make the most sense.
SH
Sherell
1 year ago
I agree with you, option C seems like the correct answer.
RH
Rhea
1 year ago
I'm not sure about that. I believe it will be 11.
CA
Candra
2 years ago
That's a tricky one. I think the output will be 10.
MA
Marva
2 years ago
I think the output will be 11. The float f is initialized to 10.501, and when we cast it to an integer, the decimal part gets truncated.
SH
Shawna
2 years ago
But since it's casting a float to an int, it will truncate the decimal part, so the output should be 10.
HA
Harris
2 years ago
I believe the output will be 11.
SH
Shawna
2 years ago
I think the output will be 10.