Loops In Java: Trivia Exam Quiz!

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Pavan
P
Pavan
Community Contributor
Quizzes Created: 1 | Total Attempts: 2,635
| Attempts: 2,635 | Questions: 45
Please wait...
Question 1 / 45
0 %
0/100
Score 0/100
1. Is following code snippet is correct?
1 2 for(int i=1; i<=10; i++) System.out.println(i);

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.

Submit
Please wait...
About This Quiz
Loops In Java: Trivia Exam Quiz! - Quiz

Loops in Java: Trivia Exam Quiz tests understanding of Java loop constructs. It covers syntax, correct implementations, and typical outputs through code snippets. Ideal for learners aiming to... see moreenhance their Java programming skills. see less

2. Which of the following is used with the switch statement?

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.

Submit
3. 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;

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.

Submit
4. Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?

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.

Submit
5. What is true about do statement?

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.

Submit
6. 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]); } }

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.

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

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.

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

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.

Submit
9. From where break statement causes an exit?

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.

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

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.

Submit
11. Which of these are selection statements in Java?

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.

Submit
12. 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; }

Explanation

The code snippet contains a while loop that will continue running as long as the condition x
After the first iteration, x becomes 4 and y becomes 6. After the second iteration, x becomes 6 and y becomes 4. After the third iteration, x becomes 8 and y becomes 2.

At this point, the condition x

Submit
13. 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.     }

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.

Submit
14.  Which of these have highest precedence?

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.

Submit
15. 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.     }

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.

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

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.

Submit
17.  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 ");

Explanation

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

Submit
18.  Which of the following is not a decision making statement?

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.

Submit
19.  What is true about a break?

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.

Submit
20. 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.     }

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.

Submit
21. 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); } }

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.

Submit
22. 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);

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.

Submit
23. 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.     }

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

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

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".

Submit
25. What is the correct syntax for each loop?

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.

Submit
26. Which of the following is not a valid jump statement?

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.

Submit
27.  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.     }

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.

Submit
28. 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.     }

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

Submit
29. 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); } }

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.

Submit
30. 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.     }

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.

Submit
31. Which of these selection statements test only for equality?

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.

Submit
32. Which of this statement is incorrect?

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.

Submit
33. 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.     }

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.

Submit
34. Which of these statements are incorrect?

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.

Submit
35. 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.     
}

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.

Submit
36. 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); } }

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.

Submit
37.  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 + " ");         }     } }

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

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

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.

Submit
39.  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.     }

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.

Submit
40.  What is the order of precedence (highest to lowest) of following operators?     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.

Submit
41. 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. }

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.

Submit
42.  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.         }
}

Explanation

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

Submit
43.  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. }

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.

Submit
44. 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); } }

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.

Submit
45. 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); } }

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.

Submit
View My Results

Quiz Review Timeline (Updated): Oct 15, 2024 +

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
Cancel
  • All
    All (45)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Is following code snippet is correct? ...
Which of the following is used with the switch statement?
What is the value stored in x in the following lines of Java code? ...
Which of the following loops will execute the body of loop even when...
What is true about do statement?
What will be the output of the following program? ...
What should be expression1 evaluate to in using ternary operator as in...
 The while loop repeats a set of code while the condition is not...
From where break statement causes an exit?
 Which of the following is not a valid flow control statement?
Which of these are selection statements in Java?
How many times "Hello world!" will be printed? (Consider the...
What will be the output of the following Java program? ...
 Which of these have highest precedence?
What is the output of this program? ...
Which of these jump statements can skip processing the remainder of...
 What would be the output of the following code snippet if...
 Which of the following is not a decision making statement?
 What is true about a break?
What is the output of this program? ...
What will be the output of the following program? ...
Consider the given code snippet and select the ANS. ...
What will be the output of the following Java code? ...
 What is the valid data type for variable "a" to print "Hello...
What is the correct syntax for each loop?
Which of the following is not a valid jump statement?
 What will be the output of the following Java program? ...
What will be the output of the following Java code? ...
What will be the output of the following program? ...
What will be the output of the following Java program? ...
Which of these selection statements test only for equality?
Which of this statement is incorrect?
What is the output of this program? ...
Which of these statements are incorrect?
What is the output of this program? ...
What will be the output of the following program? ...
 What is the output of this program? ...
 Which of these lines of Java code will give better performance?...
 What will be the output of the following Java program? ...
 What is the order of precedence (highest to lowest) of following...
What will be the output of the following Java code? ...
 What will be the output of the following Java program? ...
 What will be the output of the following Java program? ...
What will be the output of the following program? ...
What will be the output of the following program? ...
Alert!

Advertisement