Loops In Java: Trivia Exam Quiz!

45 Questions | Total Attempts: 229

SettingsSettingsSettings
Please wait...
Loops In Java: Trivia Exam Quiz!

.


Questions and Answers
  • 1. 
    What is the correct syntax for each loop?
    • A. 

      For(variable:collection) {body;}

    • B. 

      For(data_type variable:collection) {body;}

    • C. 

      .for(variable;collection) {body;}

    • D. 

      For(data_type variable;collection) {body;}  

  • 2. 
    Is following code snippet is correct? 1 2 for(int i=1; i<=10; i++) System.out.println(i);
    • A. 

      Yes

    • B. 

      No

  • 3. 
    What will be the output of the following program? 1 2 3 4 5 6 7 8 public class temp { public static void main(String agrs[]) { for(int i=1; i<=10; i++); System.out.print(i); } }
    • A. 

      12345678910

    • B. 

      11

    • C. 

      Error

    • D. 

      1 2 3 4 5 6 7 8 9 10

  • 4. 
    What will be the output of the following program? 1 2 3 4 5 6 7 8 9 public class temp { public static void main(String agrs[]) { int i; for(i=1; i<=10; i++); System.out.print(i); } }
    • A. 

      12345678910

    • B. 

      11

    • C. 

      Error

    • D. 

      1 2 3 4 5 6 7 8 9 10

  • 5. 
    What will be the output of the following program? 1 2 3 4 5 6 7 8 9 public class temp { public static void main(String agrs[]) { int x[]={1,2,3,4,5}; for(int i=0; i<x.length;i++) System.out.print(x[i]); } }
    • A. 

      Error – length is not a method of x.

    • B. 

      6

    • C. 

      C.1,2,3,4,5

    • D. 

      12345

  • 6. 
    What will be the output of the following program? 1 2 3 4 5 6 7 8 public class temp { public static void main(String agrs[]) { for(int i=1, int j=1; i<5 ; i++, j++) System.out.print(i+""+j); } }
    • A. 

      1122334455

    • B. 

      12345

    • C. 

      11223344

    • D. 

      Error

  • 7. 
    What will be the output of the following program? 1 2 3 4 5 6 7 8 9 public class temp { public static void main(String agrs[]) { for(int i=1, j=1; i<5 ; i++, j++) System.out.print(i+""+j); } }
    • A. 

      1122334455

    • B. 

      12345

    • C. 

      11223344

    • D. 

      Error

  • 8. 
    Consider the given code snippet and select the ANS. 1 2 for(int i=1, j=1; i<5 ; i++, j++) System.out.print(i+j);
    • A. 

      2468

    • B. 

      12345

    • C. 

      11223344

    • D. 

      Error

  • 9. 
    How many times "Hello world!" will be printed? (Consider the following code snippet). 1 2 3 4 5 6 7 8 int x = 2; int y = 8; while(x<(y+5)) { System.out.println("Hello world!"); x+=2; y-=2; }
    • A. 

      3

    • B. 

      4

    • C. 

      5

    • D. 

      6

  • 10. 
    What will be the output of the following program? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class temp { public static void main(String agrs[]) { int loop; for(loop=1; loop<=10; loop++) { loop*=2; if(loop>12) break; } System.out.println(loop); } }
    • A. 

      11

    • B. 

      12

    • C. 

      13

    • D. 

      14

  • 11. 
     What would be the output of the following code snippet if variable a=10?
    1. if(a<=0)
    2. {
    3.    if(a==0)
    4.    {
    5.      System.out.println("1 ");
    6.    }
    7.    else
    8.    {
    9.       System.out.println("2 ");
    10.    }
    11. }
    12. System.out.println("3 ");
    • A. 

      1 2

    • B. 

      2 3

    • C. 

      1 3

    • D. 

      3

  • 12. 
     The while loop repeats a set of code while the condition is not met?
    • A. 

      True

    • B. 

      False

  • 13. 
     What is true about a break?
    • A. 

      Break stops the execution of entire program

    • B. 

      Break halts the execution and forces the control out of the loop

    • C. 

      Break forces the control out of the loop and starts the execution of next iteration

    • D. 

      Break halts the execution of the loop for certain time frame

  • 14. 
    What is true about do statement?
    • A. 

      Do statement executes the code of a loop at least once  

    • B. 

      Do statement does not get execute if condition is not matched in the first iteration

    • C. 

      Do statement checks the condition at the beginning of the loop

    • D. 

      Do statement executes the code more than once always

  • 15. 
    Which of the following is used with the switch statement?
    • A. 

      Continue

    • B. 

      Exit

    • C. 

      Break

    • D. 

      Do

  • 16. 
     What is the valid data type for variable “a” to print “Hello World”?
    1. switch(a)
    2. {
    3.    System.out.println("Hello World");
    4. }
    • A. 

      Int and float

    • B. 

      Byte and short

    • C. 

      Char and long

    • D. 

      Byte and char

  • 17. 
     Which of the following is not a decision making statement?
    • A. 

      If

    • B. 

      If-else

    • C. 

      Switch

    • D. 

      Do-while

  • 18. 
    Which of the following is not a valid jump statement?
    • A. 

       break

    • B. 

      Goto

    • C. 

      Continue

    • D. 

      Return

  • 19. 
    From where break statement causes an exit?
    • A. 

      Only from innermost loop

    • B. 

      Terminates a program

    • C. 

      Only from innermost switch

    • D. 

      From innermost loops or switches

  • 20. 
     Which of the following is not a valid flow control statement?
    • A. 

        exit()

    • B. 

      Break

    • C. 

      Continue

    • D. 

      Return

  • 21. 
    Which of these selection statements test only for equality?
    • A. 

      If

    • B. 

      Switch

    • C. 

      If & switch

    • D. 

      None of the mentioned

  • 22. 
    Which of these are selection statements in Java?
    • A. 

      If()

    • B. 

      For()

    • C. 

      Continue

    • D. 

       break

  • 23. 
    Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?
    • A. 

      Do-while

    • B. 

      While

    • C. 

      For

    • D. 

      None of the mentioned

  • 24. 
    Which of these jump statements can skip processing the remainder of the code in its body for a particular iteration?
    • A. 

      Break

    • B. 

      Return

    • C. 

      Exit

    • D. 

      Continue

  • 25. 
    Which of this statement is incorrect?
    • A. 

      Switch statement is more efficient than a set of nested ifs

    • B. 

      Two case constants in the same switch can have identical values

    • C. 

      Switch statement can only test for equality, whereas if statement can evaluate any type of boolean expression

    • D. 

      It is possible to create a nested switch statements

Back to Top Back to top