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

  True

  False

 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 will print numbers from 1 to 10

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

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

  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

  TRUE

  FALSE

 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: 0

  Answer: 10

  Compilation Error

  Answer: 1

 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 + ", ");
		}	
	}	
}

  None of these answers are correct

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

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

  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

  1

  2

  4

 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 not compile

  None of these answers are correct

  Code will print all odd numbers between 0 to 10

  Code will print all even numbers between 0 to 10

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

  abcde

  1234

  Compilation Error

  No Output

 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

  None of these answers are correct

  Code will print abc

  Compilation Error

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

  True

  False