1 Suppose x 3 and y 2 show the output if any of the follow

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

}

Solution

1) condition 1 - x=3,y=2:

The code written will successfully execute but will not return any output. Assigned x value is greater than 2 when the \'first if condition\' is checked, so it goes on to check whether the assigned y value which is 2 is greater than 2 for the \'second if condition\'. When the machine realises it is false the \'second if condition\' is passed, likewise the \'else condition\' too is passed resulting in no output.

condition 2 - x=3, y=4:

This condition will return an output \'z is 7\'. When the \'first if condition\' checks with the assigned x value it turns out to be true and for the \'second if condition\' too the assigned y value is also true so the codes in the \'second if condition\' block are executed. z will have an addition value of x and y which is printed with the subsequent code of the block.

condition 2 - x=2, y=2:

This condition will return an output \'x is 2\'. While compiling the \'first if condition\' the assigned x value is not greater than the condition required so the whole block passes. On which the \'else block\' becomes operational where the printing of x value is done.

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&g
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&g

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site