int myVal = 2; int price = 0; switch(myVal) { Case 1: price = 3; Break; Case 2:
ID: 3773280 • Letter: I
Question
int myVal = 2;
int price = 0;
switch(myVal)
{
Case 1: price = 3;
Break;
Case 2: price += 4;
Break;
Case 3: price % 5;
Break;
Default: price = 0;
Break;
}
3
4
5
0
int myVal = 2;
int price = 0;
switch(myVal)
{
Case 1: price = 3;
Break;
Case 2: price += 4;
Break;
Case 3: price % 5;
Break;
Default: price = 0;
Break;
}
Explanation / Answer
The value of a price after executing the code is 4
Explanation :
Given myVal=2
price=0
The switch case select the case 2 label as it match with myVal
The statement associated with case 2 is price +=4
The statement can be rewritten as price=price+4
price is set to 0
price=0
So price=price+4=0+4
Thne Break statement stops executing further statements in switch
block and come out of loop
Therefore , the price value after executing the switch code is 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.