What happens when you attempt to compile and run the following code?
#include
using namespace std;
int main()
{
int i=5;
switch(i)
{
case 1:
cout<<"Hello";
break;
case 2:
cout<<"world";
break;
case 3:
break;
default:
cout<<"End";
}
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
32 comments · newest first
GL
Glenn
11 months ago
Nah, it definitely prints "End".
VI
Vicente
11 months ago
I thought it would print something for case 3!
EF
Effie
11 months ago
Wait, why doesn't it print anything else?
DA
Dannette
11 months ago
Totally agree, the switch doesn't match any case.
TH
Thersa
11 months ago
It prints: End.
ES
Esteban
12 months ago
I thought the default case only runs if no other cases match, so yeah, I guess it should print "End" in this situation.
DA
Dante
12 months ago
I'm not entirely sure, but I feel like it might print something else if there's a fall-through, but in this case, it seems like it won't.
HU
Huey
12 months ago
I remember practicing a similar question, and I believe it should print "End" because none of the cases match.
TY
Tyisha
1 year ago
I think the switch statement will just skip all cases since `i` is 5, so it might go to the default case.
TI
Titus
1 year ago
Alright, I've got this. The variable 'i' is set to 5, and the switch statement checks the value of 'i'. Since 5 doesn't match any of the case values, the default case will be executed, which prints "End". I'm confident that's the correct answer.
AR
Ardella
1 year ago
Hmm, I'm not entirely sure about this one. The switch statement looks straightforward, but I'm wondering if there's some edge case or nuance I'm missing. I'll need to carefully re-read the code and think it through before answering.
ET
Ethan
1 year ago
Okay, let me break this down step-by-step. The variable 'i' is set to 5, and the switch statement checks the value of 'i'. Since 5 doesn't match any of the case values (1, 2, or 3), the default case will be executed, which prints "End". I think I've got it now.
KA
Kattie
1 year ago
I'm a bit confused here. The switch statement has cases for 1, 2, and 3, but the variable 'i' is set to 5. Does that mean the default case will be executed, or is there something I'm missing?
LI
Linette
1 year ago
Hmm, let me think this through. The variable 'i' is initialized to 5, so the default case should be executed, which prints "End". I'm pretty confident that's the correct answer.
BI
Billye
2 years ago
The correct answer is C. The code will print 'End' because the value of 'i' is 5, which doesn't match any of the case statements, so it will execute the default case.
JE
Jerilyn
2 years ago
D) It prints: Helloworld
GI
Gilma
2 years ago
C) It prints: End
AU
Audry
2 years ago
B) It prints: world
PU
Pura
2 years ago
A) It prints: Hello
DO
Donte
2 years ago
I believe it will print: End as well, since the default case will be executed when no other case matches.
AN
Annamae
2 years ago
Haha, I bet the answer is D. It will print 'Helloworld' because the code is clearly broken and will do something unexpected. That's what happens when you write spaghetti code!
IN
Inocencia
2 years ago
I think the answer is B. The code will print 'world' because the value of 'i' is 5, which matches the case 2 statement.
LE
Leota
2 years ago
Because the value of 'i' is 5, which matches the case 2 statement.
KA
Katheryn
2 years ago
The code will print 'world'.
NA
Na
2 years ago
I think the answer is B.
AN
Antonio
2 years ago
I agree with Tamala, because the value of i is 5 and there is no case for 5 in the switch statement.
TA
Tamala
2 years ago
I think it will print: End.
AR
Arlyne
2 years ago
The answer is C. The code will print 'End' because the value of 'i' is 5, which doesn't match any of the case statements, so it will execute the default case.
AB
Abraham
2 years ago
It's a common mistake, but the default case will be executed in this code.
DO
Dortha
2 years ago
I would have guessed 'Hello' at first.
FR
Franchesca
2 years ago
Yes, you're right. The code will print 'End'.
CA
Carri
2 years ago
I think the answer is C.