Preview

23 - Iteration - For Loops

 1. Analyse the code snippet below. Is the output shown correct?
Code Snippet
=============

class ForLoopExample {
    public static void main(String args[]){
         for(int i=10; i>1; i--){
              System.out.println("The value of i is: "+i);
         }
    }
}


Output
=======
The value of i is: 9
The value of i is: 8

  False

  True

 2. Which of the following statements is true of this code?
class ForLoopExample2 {
    public static void main(String args[]){
         for(int i=1; i>=1; i++){
              System.out.println("The value of i is: "+i);
         }
    }
}

  This is a loop that prints only the number 1 and then stops

  This will cause an error as it is not possible to initialise a number to 1

  This is a loop that will print numbers from 1 to 10

  This is an infinite loop as the condition would never return false.

 3. Analyse the code snippet below. Is the output shown correct?
Here we are iterating and displaying array elements using the for loop.

Code Snippet
=============
class ForLoopExample3 {
    public static void main(String args[]){
         int arr[]={2,11,45,9};
         //i starts with 0 as array index starts with 0 too
         for(int i=0; i

  FALSE

  TRUE

 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 = 0 ; i < 10; i++){			
		}		
		System.out.println(i);
		
	}
	
}

  Answer: 1

  Answer: 0

  Compilation Error

  Answer: 10

 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 = 100; i < = 0; i -= 10 ){
			System.out.print(i + ", ");
		}	
	}	
}

  100, 90, 80, 70, 60, 50, 40, 20, 10, 0,

  None of these answers are correct

  100, 90, 80, 70, 60, 50, 40, 20, 10,

  90, 80, 70, 60, 50, 40, 20, 10, 0,

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

  3

  4

  1

  2

 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 < 10; i++){
			if(i % 2 == 0)
				continue;
			System.out.println(i);
		}
	}	
}

  Code will print all even numbers between 0 to 10

  Code will print all odd numbers between 0 to 10

  None of these answers are correct

  Code will not compile

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

  Compilation Error

  abcde

  No Output

  1234

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

  Code will print 012

  Compilation Error

  None of these answers are correct

  Code will print abc

 10. This code will compile.
public class Test{
	public static void main(String[] args){
		for(;;){}
	}
}

  False

  True