Java If Else Quiz contains 10 single and multiple choice questions. If else quiz questions are designed in such a way that it will help you understand how if else statement works in Java. At the end of the quiz, result will be displayed along with your score and if else quiz answers.
There is no time limit to complete the quiz. Click Start Quiz button to start the Java If Else quiz online.
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Result
0 out of 10 questions were answered correctly.
Time has elapsed
Your score is 0 out of 0, (0)
Average score
Your score
Category Score
If Else0%
Review Answers
1
2
3
4
5
6
7
8
9
10
Answered
Review
Question 1 of 10
1. Question
What will happen when you compile and run the following code?
public class Test {
public static void main(String[] args){
String state = "on";
if(state = "on")
System.out.println("On");
else
System.out.println("Off");
}
}
Correct answer.
Option 3 is the correct choice. Program will give compilation error at line number 7. “=” is an assignment operator and cannot be used for comparison. “==” should be used when you want to compare in Java. Result of comparison done using == is either true or false.
Incorrect answer.
Option 3 is the correct choice. Program will give compilation error at line number 7. “=” is an assignment operator and cannot be used for comparison. “==” should be used when you want to compare in Java. Result of comparison done using == is either true or false.
Question 2 of 10
2. Question
What will happen when you compile and run the following code?
public class Test {
public static void main(String[] args){
int temperature = 33;
if(temperature < 0)
System.out.println("Freezing");
else if(temperature < 30)
System.out.println("Pleasant");
else if(temperature < 50)
System.out.println("Hot");
else
System.out.println("Boiling");
}
}
Correct answer.
Option 4 is correct choice. Program will output “Hot” when run. Since the value of temperature variable is 33, first “if” and “else if” conditions will be false. Temperature is less than 50, so second “else if” condition is true and Hot will be printed. Since one of the conditions was true, else block will not be executed.
Incorrect answer.
Option 4 is correct choice. Program will output “Hot” when run. Since the value of temperature variable is 33, first “if” and “else if” conditions will be false. Temperature is less than 50, so second “else if” condition is true and Hot will be printed. Since one of the conditions was true, else block will not be executed.
Question 3 of 10
3. Question
What will happen when you compile and run the following code?
public class Test {
public static void main(String[] args){
int i = 51;
if(i > 50)
System.out.println("Greater than 50");
else
System.out.println("Less than 50");
System.out.println("Done");
}
}
Correct answer.
Option 4 is the correct choice. Program will not give any compilation errors. Since value of i variable is 51, if condition will be true and “Greater than 50” will be printed. However, since there are no curly braces used after else, only the first System.out.println statement is considered part of it. Hence, the second System.out.println will also be executed and it will print “Done”.
Incorrect answer.
Option 4 is the correct choice. Program will not give any compilation errors. Since value of i variable is 51, if condition will be true and “Greater than 50” will be printed. However, since there are no curly braces used after else, only the first System.out.println statement is considered part of it. Hence, the second System.out.println will also be executed and it will print “Done”.
Question 4 of 10
4. Question
What will happen when you compile and run the following code?
public class Test {
public static void main(String[] args){
int i = 49;
if(i > 50)
System.out.println("Greater than 50");
System.out.println("Great");
else
System.out.println("Less than 50");
}
}
Correct answer.
Option 2 is the correct choice. Curly braces are not used after if statement so only the first System.out.println is considered as part of it. There is another System.out.println statement between if and else. So when you compile the program, it will give compilation error “else without if”. Always use curly braces as a good practice to avoid such errors.
Incorrect answer.
Option 2 is the correct choice. Curly braces are not used after if statement so only the first System.out.println is considered as part of it. There is another System.out.println statement between if and else. So when you compile the program, it will give compilation error “else without if”. Always use curly braces as a good practice to avoid such errors.
Question 5 of 10
5. Question
What will happen when you compile and run the following code?
public class Test {
public static void main(String[] args){
int i = 49;
if(i < 50){
System.out.println("Greater than 50");
System.out.println("Great");
}else
System.out.println("Less than 50");
}
}
Correct answer.
Option 1 is correct choice. Specifying curly braces after if or else is not mandatory. You can even use curly braces after if and skip using it for else. Since i is 49 if will be true and Grater than 50 will be printed followed by Great.
Incorrect answer.
Option 1 is correct choice. Specifying curly braces after if or else is not mandatory. You can even use curly braces after if and skip using it for else. Since i is 49 if will be true and Grater than 50 will be printed followed by Great.
Question 6 of 10
6. Question
What will happen when you compile and run the following code?
public class Test {
public static void main(String[] args){
boolean a = true, b = false;
if(a && !b) System.out.println("true");
else System.out.println("false");
}
}
Correct answer.
Option 1 is correct choice. Program will not give any compilation error. && is used as short circuit and operator. When we use short circuit and operator (&&), if the first part of the condition is false, second part will not checked and it will return false. Since ( true && !(false) ) will return true, code for if condition will be executed and it will print true.
Incorrect answer.
Option 1 is correct choice. Program will not give any compilation error. && is used as short circuit and operator. When we use short circuit and operator (&&), if the first part of the condition is false, second part will not checked and it will return false. Since ( true && !(false) ) will return true, code for if condition will be executed and it will print true.
Question 7 of 10
7. Question
What will happen when you compile and run the following code?
public class Test {
public static void main(String[] args){
int a = 25;
if(a < 25)
System.out.println("1");
if(a > 10)
System.out.println("2");
else
System.out.println("3");
}
}
Correct answer.
Option 2 is correct choice. Program will not give any compilation errors. Since value of variable a is not less than 25, first if condition will be false and 1 will not be printed. However, since there are no curly braces used, second if condition will not be considered as a part of first if condition. Since a is greater than 10, 2 will be printed.
Do not get fooled by the indentation. The else statement is considered for second if condition in absence of the curly braces, not for first if condition.
Incorrect answer.
Option 2 is correct choice. Program will not give any compilation errors. Since value of variable a is not less than 25, first if condition will be false and 1 will not be printed. However, since there are no curly braces used, second if condition will not be considered as a part of first if condition. Since a is greater than 10, 2 will be printed.
Do not get fooled by the indentation. The else statement is considered for second if condition in absence of the curly braces, not for first if condition.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
public class Test {
public static void main(String[] args){
int a = 23, b = 30;
if(a > 20 && b > 25)
System.out.println("1");
if(a > 30 && b < 35)
System.out.println("2");
else
System.out.println("3");
}
}
Correct answer.
Option 4 is correct choice. Program will not give any compilation error. Since a is greater than 20 and b is greater than 25, 1 will be printed. Second if statement will not be true since a is not greater than 30 so else block will be executed and it will print 3. So output will be 1 followed by 3. Pay close attention to if and else if statements. If there was an else if statement instead of second if, output would have been only 1.
Incorrect answer.
Option 4 is correct choice. Program will not give any compilation error. Since a is greater than 20 and b is greater than 25, 1 will be printed. Second if statement will not be true since a is not greater than 30 so else block will be executed and it will print 3. So output will be 1 followed by 3. Pay close attention to if and else if statements. If there was an else if statement instead of second if, output would have been only 1.
Question 9 of 10
9. Question
Select all true statements from the following statements
Correct answer.
Only option 3 is the correct choice. else statement after if or else if statement is not mandatory. Hence option 1 and 2 are incorrect. However, there must be an if statement for every else statement. Curly brackets are not mandatory for if, else if or else statements.
Incorrect answer.
Only option 3 is the correct choice. else statement after if or else if statement is not mandatory. Hence option 1 and 2 are incorrect. However, there must be an if statement for every else statement. Curly brackets are not mandatory for if, else if or else statements.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
public class Test {
public static void main(String[] args){
int a = 2 * 22 + 13 - 21;
int b = 15 * 3 + 17 - 12;
if(a > b);
System.out.println("Greater");
else
System.out.println("Less");
}
}
Correct answer.
Option 3 is the correct choice. There is a semicolon (;) after if statement that marks the end of the if statement. When you try to compile the program, it will report else without if error.
Do not do hurry to calculate the values of a and b, instead first check if there are any compilation errors in the program. These types of problems are often asked in exams to create confusions.
Incorrect answer.
Option 3 is the correct choice. There is a semicolon (;) after if statement that marks the end of the if statement. When you try to compile the program, it will report else without if error.
Do not do hurry to calculate the values of a and b, instead first check if there are any compilation errors in the program. These types of problems are often asked in exams to create confusions.