Java Control (Looping) Statements Aptitude Questions and Answers.

This section contains Aptitude Questions and Answers on Java Control Statements (Looping) like for, do while, while and for each with explanation.

List of Control (Loop) Statements Aptitude Questions

1) What is the correct syntax of for each loop?
  1. for(variable:collection)
     {body;}
  2. for(data_type variable:collection)
     {body;}
  3. for(variable;collection)
     {body;}
  4. for(data_type variable;collection)
     {body;}



2) Is following code snippet is correct?
for(int i=1; i<=10; i++)
	System.out.println(i);
  1. Yes
  2. No

3) What will be the output of following program?
public class temp
{
	public static void main(String agrs[])
	{
		for(int i=1; i<=10; i++);
		System.out.print(i);
	}
}
  1. 12345678910
  2. 11
  3. Error
  4. 1 2 3 4 5 6 7 8 9 10

4) What will be the output of following program?
public class temp
{
	public static void main(String agrs[])
	{
		int i;
		for(i=1; i<=10; i++);
		System.out.print(i);
	}
}
  1. 12345678910
  2. 11
  3. Error
  4. 1 2 3 4 5 6 7 8 9 10

5) What will be the output of following program?
public class temp
{
	public static void main(String agrs[])
	{
		int x[]={1,2,3,4,5};
		for(int i=0; i<x.length;i++)
			System.out.print(x[i]);
	}
}
  1. Error – length is not a method of x.
  2. 6
  3. 1,2,3,4,5
  4. 12345