Conditional Statements QuizS2C Home « Conditional Statements

The questions in this Java quiz are on the topics covered in the Java - Beginning Java section of the site. The table below lists the lesson used for each quiz, a description of the lesson content and the quiz number and questions associated with that lesson.

Beginning Java Quizzes Summary

Click on a lesson in the table to go to that lesson for a refresher on the topics for that quiz.

Click on a quiz in the table to go straight to the quiz for a particular lesson.

Beginning Java Lessons Description Quiz Info.
Lesson 2 - Code Structure & SyntaxFor this lesson we look at the code structure of a Java source file and look at some syntax.Quiz1
8 questions
Lesson 3 - Primitive VariablesThis lesson is about the primitive variable data types available in Java.Quiz2
8 questions
Lesson 4 - Method ScopeIn this lesson we look at Java method scope and what scope and its definition mean.Quiz3
8 questions
Lesson 5 - OperatorsThis lesson is the first of two about the symbols used in Java for mathematical and logical manipulation.Quiz6
6 questions
Lesson 6 - Bitwise OperatorsIn our second lesson on operators we look at the Java bitwise operators.Quiz5
6 questions
Lesson 7 - Conditional StatementsIn this lesson we look at the conditional statements available in Java.This quiz.
8 questions
Lesson 8 - Loop StatementsThis lesson is all about the loop statements available in Java.Quiz7
8 questions

Beginning Java Quiz 6

The quiz below tests your knowledge of the material learnt in Beginning Java - Lesson 7 - Conditional Statements.

Question 1 : What will be output from this code snippet?

boolean a = false;
if (a = true) {System.out.println("true");}
else {System.out.println("false");}
- The snippet will output true. This is because if ( a = true) actually assigns true to 'a'.<br>if ( a == true) would check 'a' for equality and return false.
Question 2 : When would we branch to else in an simple if...else construct?
- When the condition expression returns 'false' we branch to 'else'.
Question 3 : What will be output from this code snippet?

long intValue = 1;
switch (intValue) {
case 1:
System.out.print(" 1 matched");
case 2:
System.out.print(" 2 matched");
break;
default:
System.out.println(" Default as no case matches");
}
- The snippet will not compile as <code>long</code> is not a valid type for a <code>switch</code> expression.
Question 4 : What will be output from this code snippet?

int intValue = 3;
// A switch where a case matches.
switch (intValue) {
default:
System.out.print(" Default as no case matches");
case 1:
System.out.print(" 1 matched");
case 2:
System.out.print(" 2 matched");
}
- The <code>default</code> statement can appear anywhere.<br>In this case it appears first and as no <code>break</code> the rest of the <code>switch</code> is processed
Question 5 : What will be output from this code snippet?

int a = 20, b = 20; if (a & b) {System.out.println("Result was true");}
- This will not compile as the result of an <code>if</code> conditional expression must be a boolean and therefore requires use of a relation operator or logical operands. <br>In this case <code>(a & b)</code> returns an <code>int</code> from a bitwise operation.
Question 6 : What will be output from this code snippet?

char charValue = 'C';
switch (charValue) {
case A:
System.out.print(" A matched");
case B:
System.out.print(" B matched");
break;
default:
System.out.print(" Default as no case matches");
}
- This will not compile as the <code>case</code> checks need to be integers or enclosed in single quotes to compare against the <code>char</code> type.
Question 7 : How many operands does the ? : operator take?
- The <code>? :</code> operator is known as a <code>tenary</code> operator as it takes three operands.
Question 8 : What will be be the value of maxInt after this code snippet is run?

int int1 = 1234, int2 = 5678, maxInt; maxInt = (int1 > int2) ? 111 : 222;
- <code>maxInt</code> will hold the value 222 as the result of <code>(int1 > int2)</code> is <code>false</code> and therefore 222 gets assigned.
Quiz Progress Bar Please select an answer

What's Next?

The next quiz on Java is all about Loop Statements.