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

1. Suppose x = 3 and y = 2; show the output, if any, of the following code. What

ID: 3781566 • Letter: 1

Question

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 of x = 2 and y = 2?

if(x>2){

   if(y>2){

     z=x+y;

     System.out.println("z is "+z);

   }

}

else

System.out.println("x is "+x);

2. What is value of y after the following switch statement is executed?

x=3 ; y = 3;

swicth(x+3){

     case 6:y = 1;

     default:y +=1;

}

3. Use a switch statement to rewrite the following if statement

if(a==1)

   x +=5;

else if (a == 2)

   x +=10;

else if(a==3)

   x +=16;

else if(a==4)

   x +=34;

4. Use a ternary operator to rewrite the following if statement

if(x>65)

    System.out.println("Passed");

else

    System.out.println("Failed");

5. What are differences between while loop and do-while loop?

6. Convert this while loop form to do-while loop:

int i = 1;

while(i<10)

    if (i%2==0)

         System.out.println(i++);

7. Do these two coding have same output? Explain it!

for(int i = 0;i <10;++i){                              for(int i = 0;i <10;i++){

    sum += i;                                                   sum +=i;

}                                                             }

8. Is for loop equal to pre-test loop or post-test loop? Explain it!

9. Convert this for loop form to while loop:

for(inti = 1;i < 4; i++){

    for(int j = 1;j < 4; j++){

         if(i*j>2)

               continue;

         System.out.println( i * j );

    }

    System.out.println(i);

}

Explanation / Answer

1.X=3;y=2; No output

x=3;y=4   

output:z is 7

x=2;y=2

output :x is 2

2.x=3;y=3

goes with default y=4;

3.

switch(a)

{case 1:x+=5;break;

case 2: x+=10;break;

case 3:x+=16;break;

case 4: x+=34;break;

default: exit;

}

4.x > 65 ? "Passed " : "failed"

5.while loop executes only if the condition is satisfied

In do while loop the statement execute once before it checcks the condition

6.int i=1;

do{

if(i%10==0)

system.out.println(i++);

}while(i<10);

7.yes the output is same 45

but this uses the post and pre increament here the first value is 0 hence the output is same.

8.NO.

In for loop number of iteration is user defined where as in while loop it depends on the condition.

9.

i=1;

while(i<4)

{

j=1;

while(j<4)

{

if(i*j>2)

continue;

system.out.println(i*j);

j++;

}

system.out.println(i);

i++;

}