Skip to content

Java For Loop Quiz Online Test

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

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

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

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

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

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

    public class Test{	
    	
    	public static void main(String[] args){
    		int i = 0;
    		for(i = 100; i <= 0; i -= 10 ){
    			System.out.print(i + ", ");
    		}	
    	}	
    }
    
  5. What will happen when you compile and run the following code?

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

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

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

    public class Test{	
    	
    	public static void main(String[] args){
    		for(char c = 'a' ; c < 'd'; c++){
    			System.out.print(c);
    		}
    	}	
    }
    
  9. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		for(int i = 65; i < 68 ; i++){
    			System.out.print((char)i);
    		}
    	}	
    }
    
  10. What will happen when you compile and run the following code?

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

    public class Test{	
    	
    	public static void main(String[] args){
    		int i = 100;
    		for(; i > 50 ;){
    			i -= 10;
    			System.out.print(i + " ");
    		}
    	}	
    }
    
  12. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		for(int a = 0, b = 3; a < 3 && b > 0; a++, b--){
    			System.out.print(a + " " + b + ", ");
    		}
    	}
    	
    }
    
  13. Will this code compile?

    public class Test{
    	public static void main(String[] args){
    		for(;;){}
    	}
    }
    
  14. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int i = 10;
    		for(; i > 0;)
    			System.out.print(i + " ");
    			i--;
    	}
    	
    }
    
  15. What will happen when you compile and run the following code?

    public class Test{	
    	
    	public static void main(String[] args){
    		int i = 0, j = 3;
    		for(;i < 3 && j > 0;i++, j--);
    		{
    			System.out.print(i + " " + j + ", ");
    		}
    	}
    	
    }
    
  16. What will you write inside the for loop to make the code print numbers divisible by 3 between 1 and 10 (both inclusive)?

    public class Test{	
    	
    	public static void main(String[] args){
    		for( _____________ ){
    			if( i % 3 == 0 )
    				System.out.println(i);
    		}
    	}		
    }
    
  17. What will happen when you compile and run the following code?

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

    public class Test{	
    	
    	public static void main(String[] args){
    		for(int i = 0; i < 3; i++){
    			for(int j = 0; j < i; j++){
    				System.out.print(i + " " + j + ", ");
    			}
    		}
    	}		
    }
    
  19. What will happen when you compile and run the following code?

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

    public class Test{	
    	
    	public static void main(String[] args){
    		outer:
    		for(int i = 0; i < 3; i++){
    			inner:
    			for(int j = 0; j < 3; j++){
    				if(i == j)
    					continue outer;				
    				System.out.print(i + " " + j + ", ");
    			}
    		}
    	}		
    }