Consider the following code snippet: int a = 5; switch(a*2) { case 4 : cout<<a;
ID: 3695418 • Letter: C
Question
Consider the following code snippet:
int a = 5;
switch(a*2) {
case 4 : cout<<a;
if(a == 5) {
cout<<a+1<<" ";
}
break;
case 8 : cout<<a+2;
if(a%3 == 0)
cout<<a+1<<" ";
else
cout<<a+3<<" ";
case 10: cout<<a+5<<" ";
cout<<a-1<<" ";
case 11 : cout<<a-4<<" ";
cout<<a-3<<" ";
default : cout<<a*2<<" ";
break;
}
14. What is the value under consideration in the switch statement?
a. 5
b. 7
c. 10
d. 0
15. What is the value of a inside the switch block?
a. 5
b. 10
c. 7
d. 0
16. What is the output of the given code snippet?
a. 10 10 1 4 2 1 6
b. 10 4 1 2 10
c. 4 1 2 12 3 10 2
d. 4 1 12 2 10 7
the answer is c a b
please explain the reason
thanks so much
Explanation / Answer
int a = 5; //a is assigned a value of 5.
switch(a*2) //switch will take a value of 10.
{
case 4 : cout<<a; //This case will fail, as the product is 10.
if(a == 5)
{
cout<<a+1<<" ";
}
break;
case 8 : cout<<a+2; //This case will also fail as product is 10.
if(a%3 == 0)
cout<<a+1<<" ";
else
cout<<a+3<<" ";
case 10: cout<<a+5<<" "; //This case will satisfy. So, executes all the statements in the block till a break is executed. So, prints 10 + 5 i.e., 15.
cout<<a-1<<" "; //Prints 5 - 1, i.e., 4.
case 11: cout<<a-4<<" "; //Prints 5 - 4, i.e., 1.
cout<<a-3<<" "; //Prints 5 - 3, i.e., 2.
default: cout<<a*2<<" "; //Prints 5 * 2, i.e., 10.
break; //Comes out of the switch.
}
//Therefore, the output of the code snipped is: 10 4 1 2 10
14. What is the value under consideration in the switch statement?
a. 5
b. 7
c. 10
d. 0
15. What is the value of a inside the switch block?
a. 5
b. 10
c. 7
d. 0
16. What is the output of the given code snippet?
a. 10 10 1 4 2 1 6
b. 10 4 1 2 10
c. 4 1 2 12 3 10 2
d. 4 1 12 2 10 7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.