Search Placementyogi.com
 

Loops in Java Objective Questions with Answers

  :: Topics ::

1

What is the output of the following program?

public class ForLoop
{
	public static void main(String[] args)
	{
		for(int i=0;i<6;i++)
		{
			System.out.println("i="+i);
			i++;
		}
	}
}
A.

i=0

i=1

i=2

i=3

i=4

i=5

B.

i=0

i=2

i=4

i=6

C.

i=0

i=2

i=4 

D.

Compilation Error

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

i is being incremented twice, once inside the body and once in the iteration part of the for statement, Hence,

When i is 0 initially, i=0 is printed and i++ increments it to 1, then iteration part of the for executes and again increments i to 2,

The control then enters the body again and prints 2. similarly the next iteration prints 4.

In the next iteration, i becomes 6 and the condition i<6 is false. So, the loop terminates and the program ends.

 
2

What is the output of the following program?

public class InfiniteLoop
{
	public static void main(String[] args)
	{
		while(1)
			System.out.println("Inside Infinite Loop");
	}
}
A.

Results in an Infinite Loop

B.

Compilation Error

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

The condition can only be a boolean expression, Hence compiler throws the following error:

C:\java_practice\src\control\InfiniteLoop.java:5: error: incompatible types

                while(1)

                      ^

  required: boolean

  found:    int

1 error

 
3

What is the output of the following program?

public class ForLoop
{
	public static void main(String[] args)
	{
		for(int i=0,int j=0;i<4;i++,j--)
		{
			System.out.println("i="+i+", j="+j);
		}
	}
}
A.

i=0, j=0

i=1, j=-1

i=2, j=-2

i=3, j=-3

B.

i=0, j=0

i=1, j=0

i=2, j=0

i=3, j=0

C.

Compilation Error

D.

None of the Above

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

The statement int i=0,int j=0 is an invalid form of declaration as we are using the data type(int) twice to declare i and j.

 
4

What is the output of the following program?

public class ForLoop
{
	public static void main(String[] args)
	{
		for(int i=0, j=4;i<4;i++,j--)
		{
			System.out.println("i="+i+", j="+j);
		}
	}
}
A.

i=0, j=3

i=1, j=2

i=2, j=1

i=3, j=0

B.

i=0, j=4

i=1, j=3

i=2, j=2

i=3, j=1

C.

Compilation Error

D.

None of the Above

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

j starts with 4 and i starts with 0. At the end when i is 3 j is 1. Hence the following output.

i=0, j=4

i=1, j=3

i=2, j=2

i=3, j=1

 
5

What is the output of the following program?

public class ForLoop
{
	public static void main(String[] args)
	{
		for(int i=0, j=4;i<4,j>2;i++,j--)
		{
			System.out.println("i="+i+", j="+j);
		}
	}
}
A.

i=0, j=4

i=1, j=3

B.

i=0, j=4

i=1, j=3

i=2, j=2

i=3, j=1

C.

Compilation Error

D.

None of the Above

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

We can not use comma operator in the condition part of the for statement./p>

 
 

Banner to LogicGuns.com

Hide Page Information

Loops Multiple Choice Questions and Answers in Java

Loops are very important for job interviews and written round.

Objective Questions on Loops in Java are asked in campus placement

Where Can I find Loops Multiple Choice Objective Questions in Java?

Placementyogi.com has a lot of Questions from SCJP, OCJP , and campus written round that are explained in detail with answers. Practicing Loops Objective Questions will help in placement exams and interview as well as this will build your concepts.

Good Questions on Loops in Java Objective Questions as well as Multiple Choice Questions.

Tricky and tough objective Questions are available here with answers and explanation