Java While Do While Loop Quiz Online Test

Java While Do while loop quiz contains 20 single and multiple choice questions. While Do While loop quiz questions are designed in such a way that it will help you understand how while and do while loop works in Java. At the end of the quiz, result will be displayed along with your score and Java while do while loop quiz answers.

There is no time limit to complete the quiz. Click Start Quiz button to start the Java While Do While quiz online.

  1. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		
    		int a = 0;
    		while(a < 10){
    			System.out.println(a++);
    		}
    	}	
    }
    
  2. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		
    		int a = 0, b = 5;
    		while(b--){
    			System.out.println(a++);
    		}
    	}	
    }
    
  3. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		
    		int a = 0;
    		while(a < = 10){
    			a++;
    		}
    		System.out.println(a);
    	}	
    }
    
  4. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		
    		int a = 0;
    		while(a < 10){
    			System.out.println(++a);
    		}
    	}	
    }
    
  5. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		
    		int a = 0, b = 10;
    		while( a + 2 < b){
    			System.out.println(a);
    		}
    	}	
    }
    
  6. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		
    		int a = 0, b = 5;
    		while( b-- > a++)
    			System.out.println(a);
    		
    	}	
    }
    
  7. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		
    		int a = 0, b = 10;
    		while( --b > a )
    			System.out.println(a);
    			a++;
    	}	
    }
    
  8. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int a = 100;
    		while(int b = 10){
    			a -= 10;
    			b += 10;
    			System.out.println(a);
    		}
    	}
    	
    }
    
  9. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int a = 0;
    		while(a + 2 < 10){
    			a += 2;
    			int b = 10;
    			while(b - 2 > 0){
    				b -= 2;
    			}				
    		}
    		System.out.println(a + " " + b);
    	}
    	
    }
    
  10. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int a = 10, b = 0;
    		while( a + 2 < 10 && b - 2 > 0){
    			System.out.println(a);
    		}
    	}
    	
    }
    
  11. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int a = 0, b = 10;
    		while(b - 2 > 0){
    			b -= 2;
    			while(a + 2 < 10){
    				a += 2;
    				if(a == b)
    					continue;
    				
    				System.out.print(a + " " + b + ", ");
    			}
    		}
    	}
    	
    }
    
  12. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int a = 3, b = 0;
    		while( a >= 0 ){
    			a -= 1;
    			while(b< =3 ){
    				b += 1;
    				if(a < b)
    					break;
    				
    				System.out.print(a + " " + b + ", ");
    			}
    		}
    	}
    	
    }
    
  13. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int i = 0;
    		outer: while(i < 10){
    			i+=2;
    			int j = 10;
    			inner: while(j > 0){
    				j -=2;
    				if(i == j)
    					break outer;
    				System.out.print(i + " " + j + ", ");
    			}
    		}
    		
    	}
    	
    }
    
  14. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int a = 4, b = 4;
    		while(a-- > 0){
    			while(b-- > 0){
    				if(a == b)
    					continue;
    				System.out.print(a + " " + b + ", ");
    			}
    		}
    	}
    	
    }
    
  15. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int a = 4, b = 4;
    		outer: while(a-- > 0){
    			while(b-- > 0){
    				if(a == b)
    					continue outer;
    				System.out.print(a + " " + b + ", ");
    			}
    		}
    	}
    	
    }
    
  16. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		while(1){
    			System.out.println("Hello");			
    		}
    	}
    	
    }
    
  17. Requirement is to read the user input line from the console using loop and write the input to a file line by line. If the user enters “END”, it should be written to the file and the program should exit. Which loop would you use?

  18. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int i = 0;
    		do{
    			System.out.println("Loop");
    		}while(i != 0);		
    		
    	}
    	
    }
    
  19. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int i = 0;
    		do{
    			i++;
    			System.out.println(i);
    		}while(i < 5)		
    		
    	}
    	
    }
    
  20. Regardless of the boolean condition of the do while loop, there is always at least one iteration of the loop.