Search Placementyogi.com
 

If Else And Switch in Java Objective Questions and Answers

  :: Topics ::

1

What is the output of the following program?

class IfExample 
{
	public static void main(String[] args) 
	{
		int var1;
		if (var1)
		{
			System.out.println("Inside If Condition");
		}
		
	}
}
A.

Inside If Condition

B.

does not print anything

C.

Compilation Error

D.

Run Time Error

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

If condition only accepts boolean value in condition.

The compiler throws the following Error at compile time

C:\java_practice\src\control\IfExample.java:6: error: incompatible types

                if (var1)

                    ^

  required: boolean

  found:    int

1 error

 
2

What is the output of the following program?

class IfExample 
{
	public static void main(String[] args) 
	{
		int var1;
		if (var1==0)
		{
			System.out.println("Inside If Condition");
		}
		
	}
}
A.

Inside If Condition

B.

does not print anything

C.

Compilation Error

D.

Run Time Error

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

The variable has to be initialized before using in the condition.

C:\java_practice\src\control\IfExample.java:6: error: variable var1 might not have been initialized

                if (var1==0)

                    ^

1 error

 
3

What is the output of the following program?

class IfExample 
{
	public static void main(String[] args) 
	{
		int var1=10;
		if (var1 = 20)
		{
			System.out.println("Inside If Condition");
			System.out.println("var1= "+var1);
		}
		
	}
}
A.

Inside If Condition

var1= 20

B.

Inside If Condition

var1= 10

C.

Compilation Error

D.

Runtime Error

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

var1=20 evaluates to 20 which is an integer value , but the condition has to be a boolean value. So, we get Compilation Error:

C:\java_practice\src\control\IfExample.java:6: error: incompatible types

                if (var1 = 20)

                         ^

  required: boolean

  found:    int

1 error

 
4

What is the output of the following program?

class IfExample 
{
	public static void main(String[] args) 
	{
		int var1=0;
		if (var1 == 10)
			System.out.println("Inside If Condition");
			System.out.println("var1= "+var1);
	}
}
A.

Inside If Condition

var1= 10

B.

Inside If Condition

C.

var1= 0

D.

Compilation Error

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

Look Closely ! There are no braces in the if statement, The statement : "var1= "+var1 is outside the if condition, Hence it is always executed

 
5

What is the output of the following program?

class IfExample 
{
	public static void main(String[] args) 
	{
		boolean var1=true;
		if (var1 )
			System.out.println("Inside If Condition");
		else
			System.out.println("Inside Else Condition");
	}
}
A.

Inside If Condition

B.

Inside Else Condition

C.

Compilation Error

D.

Run Time Error

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

The variable var1 is boolean and the value is true. Hence, the condition evaluates to true and the statement inside if condition is executed.

 
 

Banner to LogicGuns.com

Hide Page Information

Java If Else And Switch Objective Questions and Answers for Written Round Interview

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

Why learn Java If Else And Switch?

If Else And Switch are an integral part of Java and many Objective Questions in the written round are asked from this topic.In this section you can learn and practice Java If Else And Switch (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 If Else And Switch Objective Questions and Answers with Explanation?

PlacementYogi provides you lots of fully solved Java If Else And Switch 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 If Else And Switch, We have quiz Objective Questions with answers to help you test yourself..

\n

Where can I get Java If Else And Switch Interview Objective Questions and Answers (objective type, multiple choice)?

Here you can find objective type Java If Else And Switch 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 If Else And Switch problems?

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