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 will cause an error as it is not possible to initialise a number to 1

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

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

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

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

  Compilation Error

  Answer: 0

  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

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

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

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

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

  2

  4

  3

  1

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

  No Output

  1234

  abcde

  Compilation Error

 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

  Code will print abc

  None of these answers are correct

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

  True

  False