Search Placementyogi.com
 

Java Arrays Difficult and Tricky Objective Questions and Answers

  :: Topics ::

1

What is the output of the following program?

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

i=0

i=1

i=2

i=3

i=4

i=10

B.

i=0

i=1

i=2

i=3

i=4

i=5

C.

Compilation Error

D.

None of the Above

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

You can not declare the same variable twice in Java!

C:\java_practice\src\control\ForLoop.java:6: error: variable i is already defined in method main(String[])

                for(int i=0;i<10;i++)

                        ^

 
2

What is the output of the following program?

public class ForLoop
{
	public static void main(String[] args)
	{
		for(int i=0;i>4?false:true;i++)
		{
			System.out.println("i="+i);
		}
	}
}
A.

i=0

i=1

i=2

i=3

i=4

B.

i=0

i=1

i=2

i=3

C.

Compilation Error

D.

prints nothing

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

Initially, i is 0, i>4 is false, So the conditional operator return true, and the body of the loop is executed.

In the next iteration,

i is 1 , i>4 is false, so body executes and i=1 is printed, similarly

when i is 4, i>4 is false, so i=4 is printed, next

i is 5, i>4 is true, so conditional operator returns false, and the loop terminates!

 

Hope this one troubled your mind a little :-)

 
 

Banner to LogicGuns.com

Hide Page Information

Java Loops difficult Objective Questions and Answers for Written Round Interview

Here you can find Java Loops difficult Objective Questions with answers and explanation. We have a vast collection of Objective Questions on Loops difficult in Java that will help you a lot!

Why learn Java Loops ?

Loops are an integral part of Java and many difficult and tricky Objective Questions in the written round are asked from this topic.In this section you can learn and practice Java Loops difficult (Objective Questions with Answers) to improve your skills in order to face the interview and clear campus and off-campus selection of TCS, Infosys, Wipro etc.

Where can I get Java Loops difficult Objective Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved Java Loops difficult Objective Questions and answers with explanation. Fully solved examples with detailed answer description, explanation are given and it would be easy to understand.Though we do not have pdf file for Java Loops difficult, We have quiz Objective Questions with answers to help you test yourself..

\n

Where can I get Java Loops difficult Interview Objective Questions and Answers (objective type, multiple choice)?

Here you can find objective type Java Loops difficult Objective Questions and answers for interview and entrance examination.Multiple Answers and true or false type Objective Questions are also provided.

How to solve Java Loops difficult problems?

You can easily solve all kind of Objective Questions based on Java Loops difficult by practicing the exercises (including shortcut Loops difficult to solve problems) given below.