Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

7. (TCO 4) What will be the value of the variable "price" after the following code executes?

            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;
            }

(Points : 5)

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