Loops In Java: Trivia Exam Quiz!

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Pavan
P
Pavan
Community Contributor
Quizzes Created: 1 | Total Attempts: 2,054
Questions: 45 | Attempts: 2,076

SettingsSettingsSettings
Loops In Java: Trivia Exam Quiz! - 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;}  

    Correct Answer
    B. For(data_type variable:collection) {body;}
    Explanation
    The correct syntax for each loop is "for(data_type variable:collection) {body;}". This syntax is commonly used in programming languages like Java and Python to iterate over elements in a collection, such as an array or a list. The "data_type" refers to the type of the elements in the collection, and "variable" is the name given to each element as it is being iterated over. The "body" represents the code block that will be executed for each iteration of the loop.

    Rate this question:

  • 2. 

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

    • A.

      Yes

    • B.

      No

    Correct Answer
    A. Yes
    Explanation
    The code snippet is correct because it initializes a variable i to 1, and then runs a loop that executes as long as i is less than or equal to 10. In each iteration of the loop, it prints the value of i. This will result in printing the numbers from 1 to 10.

    Rate this question:

  • 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

    Correct Answer
    C. Error
    Explanation
    The program will output "Error" because there is a syntax error in the for loop. The semicolon (;) after the for loop is causing the loop to terminate immediately, and the System.out.print(i) statement is outside of the loop. Therefore, the variable "i" is out of scope and cannot be accessed, resulting in an error.

    Rate this question:

  • 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

    Correct Answer
    B. 11
    Explanation
    The program will output 11. This is because the for loop is terminated by a semicolon after the condition, resulting in an empty loop body. As a result, the loop does not execute any statements and the value of i remains at 11, which is then printed.

    Rate this question:

  • 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

    Correct Answer
    D. 12345
    Explanation
    The program creates an array called "x" with elements 1, 2, 3, 4, and 5. It then uses a for loop to iterate over the elements of the array and print each element. Therefore, the output of the program will be the numbers 1, 2, 3, 4, and 5 printed on separate lines.

    Rate this question:

  • 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

    Correct Answer
    D. Error
    Explanation
    The program will give an error because in the for loop declaration, you cannot declare multiple variables of different types using a comma. In this case, the variables i and j are both declared as int, but they are separated by a comma instead of a semicolon. This is not valid syntax in Java, hence the program will not compile and give an error.

    Rate this question:

  • 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

    Correct Answer
    C. 11223344
    Explanation
    The program uses a for loop to iterate from 1 to 5. Inside the loop, it prints the values of both i and j concatenated together. Since i and j are both initialized to 1 and incremented by 1 in each iteration, the output will be 1122334455.

    Rate this question:

  • 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

    Correct Answer
    A. 2468
    Explanation
    The given code snippet is a for loop that initializes two variables i and j to 1. The loop runs as long as i is less than 5. In each iteration, it prints the sum of i and j. Since i and j both start at 1 and increment by 1 in each iteration, the output will be the sequence of numbers 2, 4, 6, and 8. Therefore, the correct answer is 2468.

    Rate this question:

  • 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

    Correct Answer
    A. 3
    Explanation
    The code snippet contains a while loop that will continue running as long as the condition x

    Rate this question:

  • 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

    Correct Answer
    D. 14
    Explanation
    The program starts with initializing the variable loop to 1. Then, it enters a for loop where the value of loop is multiplied by 2 in each iteration. If the value of loop becomes greater than 12, the loop is terminated using the break statement. In this case, the loop terminates when the value of loop is 16. Therefore, the output of the program is 16.

    Rate this question:

  • 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

    Correct Answer
    D. 3
    Explanation
    The output of the code snippet will be "3". This is because the condition "a

    Rate this question:

  • 12. 

     The while loop repeats a set of code while the condition is not met?

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    The while loop repeats a set of code while the condition is met, not while the condition is not met. Therefore, the correct answer is False.

    Rate this question:

  • 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

    Correct Answer
    B. Break halts the execution and forces the control out of the loop
    Explanation
    The statement "Break halts the execution and forces the control out of the loop" is true because when a break statement is encountered in a loop, it immediately terminates the loop and transfers control to the next statement after the loop. This allows the program to exit the loop prematurely based on a certain condition or criteria.

    Rate this question:

  • 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

    Correct Answer
    A. Do statement executes the code of a loop at least once  
    Explanation
    The do statement executes the code of a loop at least once, regardless of whether the condition is initially true or false. This is because the condition is checked at the end of the loop, after the code has been executed. Therefore, even if the condition is false from the beginning, the code will still be executed once before checking the condition again.

    Rate this question:

  • 15. 

    Which of the following is used with the switch statement?

    • A.

      Continue

    • B.

      Exit

    • C.

      Break

    • D.

      Do

    Correct Answer
    C. Break
    Explanation
    The correct answer is "break" because it is used with the switch statement to terminate the execution of the switch block. When a break statement is encountered, the control is transferred to the next statement after the switch block. Without the break statement, the code would continue to execute the following case statements until a break statement is encountered or the end of the switch block is reached.

    Rate this question:

  • 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

    Correct Answer
    D. Byte and char
    Explanation
    The switch statement in Java can only accept variables of type byte, char, short, or int as its argument. In this case, the variable "a" needs to be of type byte or char in order for the code to compile and print "Hello World".

    Rate this question:

  • 17. 

     Which of the following is not a decision making statement?

    • A.

      If

    • B.

      If-else

    • C.

      Switch

    • D.

      Do-while

    Correct Answer
    D. Do-while
    Explanation
    A do-while statement is not a decision-making statement because it is a looping statement that executes a block of code repeatedly until a certain condition is no longer true. It does not make a decision based on a condition like the other options. The if statement, if-else statement, and switch statement are all decision-making statements that determine the flow of the program based on a condition.

    Rate this question:

  • 18. 

    Which of the following is not a valid jump statement?

    • A.

       break

    • B.

      Goto

    • C.

      Continue

    • D.

      Return

    Correct Answer
    B. Goto
    Explanation
    The "goto" statement is not a valid jump statement in most modern programming languages. It allows the program to jump to a specific labeled line of code, which can lead to unstructured and hard-to-maintain code. The use of "goto" can make the program flow unpredictable and can cause difficulties in debugging and understanding the code. As a result, many programming languages have chosen to exclude the "goto" statement as a valid jump statement.

    Rate this question:

  • 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

    Correct Answer
    D. From innermost loops or switches
    Explanation
    The break statement causes an exit from the innermost loop or switch statement. This means that when a break statement is encountered within a loop or switch, the program will immediately exit that loop or switch and continue with the next statement outside of it. It will not terminate the entire program or exit from any other loops or switches that may be nested within.

    Rate this question:

  • 20. 

     Which of the following is not a valid flow control statement?

    • A.

        exit()

    • B.

      Break

    • C.

      Continue

    • D.

      Return

    Correct Answer
    A.   exit()
    Explanation
    The exit() function is not a valid flow control statement. It is a function used to terminate the program execution and exit the current process. Flow control statements are used to alter the flow of program execution based on certain conditions or criteria. The exit() function is typically used to indicate a successful termination or to handle exceptional cases, but it does not control the flow of program execution like break, continue, or return statements.

    Rate this question:

  • 21. 

    Which of these selection statements test only for equality?

    • A.

      If

    • B.

      Switch

    • C.

      If & switch

    • D.

      None of the mentioned

    Correct Answer
    B. Switch
    Explanation
    The switch statement is the only selection statement that tests only for equality. Unlike the if statement, which can test for various conditions using comparison operators, the switch statement evaluates a single expression and compares it to multiple case values to determine the appropriate block of code to execute. Therefore, the switch statement is the correct answer as it solely tests for equality.

    Rate this question:

  • 22. 

    Which of these are selection statements in Java?

    • A.

      If()

    • B.

      For()

    • C.

      Continue

    • D.

       break

    Correct Answer
    A. If()
    Explanation
    The if() statement is a selection statement in Java. It allows the program to make decisions based on a certain condition. If the condition is true, the code inside the if() block will be executed, otherwise, it will be skipped. This statement is commonly used to implement conditional branching in programs, where different actions are taken depending on the outcome of a condition.

    Rate this question:

  • 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

    Correct Answer
    A. Do-while
    Explanation
    The do-while loop is the correct answer because it is the only loop that guarantees the execution of the loop body at least once, even if the condition controlling the loop is initially false. This is because in a do-while loop, the condition is checked at the end of the loop iteration, so the loop body is always executed at least once before the condition is evaluated.

    Rate this question:

  • 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

    Correct Answer
    D. Continue
    Explanation
    The jump statement "continue" can skip processing the remainder of the code in its body for a particular iteration. When "continue" is encountered in a loop, it immediately jumps to the next iteration without executing the remaining code in the loop for that iteration. This allows for skipping certain iterations based on certain conditions, while still continuing the loop.

    Rate this question:

  • 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

    Correct Answer
    B. Two case constants in the same switch can have identical values
    Explanation
    The statement "two case constants in the same switch can have identical values" is incorrect. In a switch statement, each case constant must have a unique value. If two case constants have the same value, it will result in a compilation error.

    Rate this question:

  • 26. 

    What will be the output of the following Java program?
    1.     class selection_statements
    2.     {
    3.         public static void main(String args[])
    4.         {
    5.             int var1 = 5;
    6.             int var2 = 6;
    7.             if ((var2 = 1) == var1)
    8.                 System.out.print(var2);
    9.             else
    10.                 System.out.print(++var2);
    11.         }
    12.     }

    • A.

      1

    • B.

      2

    • C.

      3

    • D.

      4

    Correct Answer
    B. 2
    Explanation
    The output of the program will be 2. This is because the if statement is checking if the value of var2 after assigning 1 to it is equal to var1. Since the assignment is successful, the condition evaluates to true and the code inside the if block is executed. Therefore, the value of var2, which is 1, is printed.

    Rate this question:

  • 27. 

    What will be the output of the following Java program?
    1.     class comma_operator
    2.     {
    3.         public static void main(String args[])
    4.         {   
    5.              int sum = 0;
    6.              for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
    7.                  sum += i;
    8.        System.out.println(sum);
    9.         }
    10.     }

    • A.

      5

    • B.

      6

    • C.

      14

    • D.

      Compilation error

    Correct Answer
    B. 6
    Explanation
    The program initializes two variables, i and j, to 0. It then enters a for loop that continues as long as both i and j are less than 5. In each iteration of the loop, i is incremented by 1 and j is set to the value of i + 1. The sum variable is incremented by the value of i in each iteration. After the loop, the value of sum is printed, which is 6.

    Rate this question:

  • 28. 

     What will be the output of the following Java program?
    1.     class jump_statments
    2.     {
    3.         public static void main(String args[])
    4.         {       
    5.              int x = 2;
    6.              int y = 0;
    7.              for ( ; y < 10; ++y)
    8.              {
    9.                  if (y % x == 0)
    10.                      continue
    11.                  else if (y == 8)
    12.                       break;
    13.                  else
    14.                     System.out.print(y + " ");
    15.              }
    16.         }
    17.     }

    • A.

      1 3 5 7

    • B.

      2 4 6 8

    • C.

      1 3 5 7 9

    • D.

      1 2 3 4 5 6 7 8 9

    Correct Answer
    C. 1 3 5 7 9
    Explanation
    The program starts with initializing two variables, x = 2 and y = 0. Then, it enters a for loop that runs as long as y is less than 10. Inside the loop, it checks if y is divisible by x (2) using the condition y % x == 0. If it is true, the continue statement is executed, which skips the rest of the code inside the loop and moves to the next iteration. If y is equal to 8, the break statement is executed, which terminates the loop. Otherwise, if none of the conditions are met, the value of y is printed. In this case, the values printed are 1, 3, 5, 7, and 9.

    Rate this question:

  • 29. 

     What will be the output of the following Java program?
    1. class Output
    2. {
    3.         public static void main(String args[])
    4.         {   
    5.            final int a=10,b=20;
    6.           while(a<b)
    7.           {
    8.  
    9.           System.out.println("Hello");
    10.           }
    11.           System.out.println("World");
    12.  
    13.         }
    }

    • A.

      Hello

    • B.

      Run time error

    • C.

      Hello world

    • D.

      Compile time error

    Correct Answer
    D. Compile time error
    Explanation
    The given program will result in a compile time error. This is because the condition in the while loop, "a

    Rate this question:

  • 30. 

     What will be the output of the following Java program?
    1.     class Output
    2.     {
    3.         public static void main(String args[])
    4.         {   
    5.              int a = 5;
    6.              int b = 10;
    7.              first:
    8.              {
    9.                 second:
    10.                 {
    11.                    third:
    12.                    {
    13.                        if (a ==  b >> 1)
    14.                            break second;
    15.                    }
    16.                    System.out.println(A.;
    17.                 }
    18.                 System.out.println(B.;
    19.              }
    20.         }
    21.     }

    • A.

        5 10

    • B.

      10 5

    • C.

      5

    • D.

       10

    Correct Answer
    D.  10
    Explanation
    The output of the program will be "10". This is because the if statement checks if the value of variable "a" is equal to the value of variable "b" shifted right by 1 bit. Since the value of "a" is 5 and the value of "b" is 10, the condition is not true and the break statement is not executed. Therefore, the program continues to the next line which prints the value of "b", which is 10.

    Rate this question:

  • 31. 

     Which of these have highest precedence?

    • A.

      ()

    • B.

      ++

    • C.

      *

    • D.

      >>

    Correct Answer
    A. ()
    Explanation
    Parentheses have the highest precedence in this case. This means that any expression enclosed in parentheses should be evaluated first before any other operation is performed.

    Rate this question:

  • 32. 

    What should be expression1 evaluate to in using ternary operator as in this line?  expression1 ?  expression2  :  expression3

    • A.

      Integer

    • B.

      Floating – point numbers

    • C.

      Boolean

    • D.

      None of the mentioned

    Correct Answer
    C. Boolean
    Explanation
    The expression1 in the given line should evaluate to a boolean value. This is because the ternary operator requires the expression1 to be a condition that evaluates to either true or false. The value of expression2 will be returned if the condition is true, otherwise the value of expression3 will be returned.

    Rate this question:

  • 33. 

    What is the value stored in x in the following lines of Java code?    int x, y, z;     x = 0;     y = 1;     x = y = z = 8;

    • A.

      0

    • B.

      1

    • C.

      9

    • D.

      8

    Correct Answer
    D. 8
    Explanation
    The value stored in x is 8. In the given code, the variables x, y, and z are declared as integers. Initially, x is assigned a value of 0 and y is assigned a value of 1. Then, the statement "x = y = z = 8" is executed. This statement assigns the value 8 to z, then assigns the value of z (which is 8) to both y and x. Therefore, the final value stored in x is 8.

    Rate this question:

  • 34. 

     What is the order of precedence (highest to lowest) of following operators?     1. &        2. ^     3. ?:

    • A.

      1 -> 2 -> 3

    • B.

      2 -> 1 -> 3

    • C.

      3 -> 2 -> 1

    • D.

      2 -> 3 -> 1

    Correct Answer
    A. 1 -> 2 -> 3
    Explanation
    The order of precedence of operators is determined by the hierarchy of operations they perform. In this case, the ^ operator has the highest precedence, followed by the && operator, and finally the ?: operator. This means that the ^ operator will be evaluated first, then the && operator, and finally the ?: operator.

    Rate this question:

  • 35. 

    Which of these statements are incorrect?

    • A.

       Equal to operator has least precedence

    • B.

      Brackets () have highest precedence

    • C.

      Division operator, /, has higher precedence than multiplication operator

    • D.

        Addition operator, +, and subtraction operator have equal precedence

    Correct Answer
    C. Division operator, /, has higher precedence than multiplication operator
    Explanation
    The given statement is incorrect because in mathematics, multiplication and division have the same precedence. This means that they are performed from left to right in the order they appear in an expression. Therefore, neither the division operator nor the multiplication operator has higher precedence over the other.

    Rate this question:

  • 36. 

    What will be the output of the following Java code?
    1.     class operators
    2.     {
    3.         public static void main(String args[])
    4.         {
    5.             int var1 = 5;
    6.             int var2 = 6;
    7.             int var3;
    8.             var3 = ++ var2 * var1 / var2 + var2;
    9.             System.out.print(var3);
    10.         }
    11.     }

    • A.

      10

    • B.

      11

    • C.

      12

    • D.

      56

    Correct Answer
    C. 12
    Explanation
    Pre-Increment Operation: ++var2 increments var2 from 6 to 7 before it is used in the expression.
    Multiplication and Division: The expression var2 * var1 / var2 involves multiplying the incremented var2 (which is now 7) by var1 (which is 5), resulting in 35. This result is then divided by var2 (still 7), yielding 5.
    Addition: Finally, add the current value of var2 (which remains 7) to the result of the division, resulting in 5 + 7.
    Thus, the full expression evaluates as:
    7 * 5 / 7 + 7
    35 / 7 + 7
    5 + 7
    12

    Rate this question:

  • 37. 

    What will be the output of the following Java code?
    1.     class operators
    2.     {
    3.         public static void main(String args[])
    4.         {   
    5.              int x = 8;
    6.              System.out.println(++x * 3 + " " + x);
    7.         }
    8.     }

    • A.

      24 8

    • B.

      24 9

    • C.

      27 8

    • D.

      27 9  

    Correct Answer
    D. 27 9  
    Explanation
    The code first increments the value of x by 1 using the pre-increment operator (++x). Then, it multiplies the incremented value of x by 3 and concatenates it with a space character. Finally, it prints the result, which is "27" followed by a space and the value of x (which is 9).

    Rate this question:

  • 38. 

    What will be the output of the following Java code?
    1. class Output
    2. {
    3.         public static void main(String args[])
    4.         {   
    5.              int x=y=z=20;
    6.  
    7.         }
    8. }

    • A.

      Compile and runs fine

    • B.

      20

    • C.

      Run time error

    • D.

      Compile time error

    Correct Answer
    D. Compile time error
    Explanation
    The code will result in a compile time error because the variables x, y, and z are not declared before they are assigned the value of 20. Each variable should be declared separately before assigning a value to it.

    Rate this question:

  • 39. 

     Which of these lines of Java code will give better performance?    1. a | 4 + c >> b & 7;    2. (a | ((( 4 * c ) >> b ) & 7 ))

    • A.

      1 will give better performance as it has no parentheses

    • B.

      2 will give better performance as it has parentheses  

    • C.

      Both 1 & 2 will give equal performance

    • D.

      Dependent on the computer system

    Correct Answer
    C. Both 1 & 2 will give equal performance
    Explanation
    Both 1 and 2 will give equal performance. The presence or absence of parentheses in the code does not affect the performance of the code. The performance of the code depends on the operations being performed and the efficiency of the underlying Java compiler and runtime environment.

    Rate this question:

  • 40. 

     What will be the output of the following Java program?
    1. class Output
    2. {
    3.         public static void main(String args[])
    4.         {   
    5.              int a,b,c,d;
    6.              a=b=c=d=20
    7.             a+=b-=c*=d/=20
    8.            System.out.println(a+" "+b+" "+c+" "+d;
    9.  
    10.         }
    11. }

    • A.

      Compile time error

    • B.

      Runtime error

    • C.

      A=20 b=0 c=20 d=1

    • D.

      None of the mentioned

    Correct Answer
    C. A=20 b=0 c=20 d=1
    Explanation
    The given program will output a=20, b=0, c=20, and d=1. This is because of the order of operations in the expression a+=b-=c*=d/=20.

    First, the value of d is divided by 20, resulting in d=1. Then, c is multiplied by the updated value of d, which is 1, resulting in c=20. Next, b is subtracted by the updated value of c, which is 20, resulting in b=0. Finally, a is incremented by the updated value of b, which is 0, resulting in a=20.

    Rate this question:

  • 41. 

    What is the output of this program?
    1.     class selection_statements {
    2.         public static void main(String args[])
    3.         {
    4.             int var1 = 5;
    5.             int var2 = 6;
    6.             if ((var2 = 1) == var1)
    7.                 System.out.print(var2);
    8.             else
    9.                 System.out.print(++var2);
    10.         }
    11.     }

    • A.

      1

    • B.

      2

    • C.

      3

    • D.

      4

    Correct Answer
    B. 2
    Explanation
    The output of this program is 2. This is because in the if statement, the variable var2 is assigned the value 1 and then compared to var1. Since the assignment operation returns the value that was assigned, the condition becomes true and the code inside the if statement is executed, which prints the value of var2 (which is 1). However, in the else statement, the value of var2 is incremented by 1 before being printed, resulting in the final output of 2.

    Rate this question:

  • 42. 

    What is the output of this program?
    1.     class comma_operator {
    2.         public static void main(String args[])
    3.         {   
    4.              int sum = 0;
    5.              for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
    6.                  sum += i;
    7.        System.out.println(sum);
    8.         }
    9.     }

    • A.

      5

    • B.

      6

    • C.

      14

    • D.

      Compilation error

    Correct Answer
    B. 6
    Explanation
    The program initializes two variables, i and j, to 0. It then enters a for loop that increments i and sets j to i + 1 on each iteration. The loop continues as long as both i and j are less than 5. Inside the loop, the value of i is added to the variable sum. After the loop completes, the value of sum is printed, which is 6. This is because the loop runs for 5 iterations, with i taking the values 0, 1, 2, 3, and 4. The sum of these values is 10, but since j is always one step ahead of i, the final value of sum is 6, not 10.

    Rate this question:

  • 43. 

     What is the output of this program? class jump_statements {     public static void main(String args[]) {         int x = 2;         int y = 0;         for (; y < 10; ++y) {             if (y % x == 0)                 continue;             else if (y == 8)                 break;             else                 System.out.print(y + " ");         }     } }

    • A.

      1 3 5 7

    • B.

      2 4 6 8

    • C.

      1 3 5 7 9

    • D.

      1 2 3 4 5 6 7 8 9

    Correct Answer
    A. 1 3 5 7
    Explanation
    This Java program demonstrates the use of continue and break statements within a for loop.
    The continue statement skips the current iteration of the loop if the condition (y % x == 0) is true, meaning when y is even.
    The break statement terminates the loop completely when y is equal to 8.
    Therefore, the program prints only the odd numbers from 1 to 7, as 8 triggers the break statement, and the even numbers are skipped due to the continue statement.Sources and related content

    Rate this question:

  • 44. 

    What is the output of this program?
    1. class Output {
    2.         public static void main(String args[])
    3.         {   
    4.            final int a=10,b=20;
    5.           while(a<B.
    6.           {
    7.  
    8.           System.out.println("Hello");
    9.           }
    10.           System.out.println("World");
    11.  
    12.         }
    13.     }

    • A.

      Hello

    • B.

      Run time error

    • C.

      Hello world

    • D.

      Compile time error

    Correct Answer
    D. Compile time error
    Explanation
    The program will result in a compile time error. This is because there is a syntax error in the while loop condition. The variable "B" should be "b" to match the variable declaration.

    Rate this question:

  • 45. 

    What is the output of this program?
    1.     class Output {
    2.         public static void main(String args[])
    3.         {   
    4.              int a = 5;
    5.              int b = 10;
    6.              first: {
    7.                 second: {
    8.                    third: {
    9.                        if (a ==  b >> 1)
    10.                            break second;
    11.                           }
    12.                    System.out.println(A.;
    13.                 }
    14.                 System.out.println(B.;
    15.              }
    16.         }
    17.     
    }

    • A.

      5 10

    • B.

      10 5

    • C.

      5

    • D.

      10

    Correct Answer
    D. 10
    Explanation
    The output of the program is 10. This is because the if statement checks if the value of a is equal to the value of b shifted right by 1 (which is equivalent to dividing b by 2). Since b is 10 and 10 shifted right by 1 is 5, the condition is false and the code inside the if statement is not executed. Therefore, the program continues to the next line which is System.out.println(B.; and prints the value of b, which is 10.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Oct 15, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 20, 2019
    Quiz Created by
    Pavan
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.