Algo 04.C
1. Suppose x = 3 and y = 2, show the output, if any, of the following code.What is the output if x = 3 and y = 4? What is the output if x = 2 and y = 2?
Answer:
Output dari x = 3 dan y = 2 tidak terdapat output yang ditampilkan:
Output dari x = 3 dan y = 4, maka output “z is 7″
Output dari x =2 dan y = 2, maka output “z is 2″
2. What is y after the following switch statement is executed?
x=3, y=2;
Answer :
y = 2
x=3, y=2;
switch (x+3) {
case 5: y=1;
case 6: y=2;
default: y-=1;
}
3. Use a switch statement to rewrite the following if statement:?
Answer :
if (a ==1)
x +=5;
else if (a == 2)
x +=10;
else if (a == 3)
x +=16;
else if (a == 4)
x += 34;
switch statement :
switch (a) {
case 1: x+=5;
break;
case 2: x+=10;
break;
case 3: x+=16;
break;
case 4: x+=34;
break;
}
4. Use a ternary operator to rewrite the following if statement :
Answer :
if(x>65)
System.out.println(“Passed”);
else
System.out.println(“Failed”);
The result :
System.out.println(x>60 ? “Passed” : “Failed” );
Binus Universty