facebook twitter pinterest google plus Youtube
www.TestsTestsTests.com

Java Decision Control Structures Test
Free Online Java Programming Tests
Java Language Programming

Test yourself on Java Programming Decision Control Structures - Creating a program using different Decision Control Structures like if, if-else, if-else-if, and switch statements.

10 Question Multiple Choice Quiz with Answers and Answer Explanations

1) What do you call a statement that executes a certain statement or statements if the condition is true, and executes a different statement or statements if the condition is false?
a) An if statement.
b) An if-else statement.
c) An if-else-if statement.
d) The switch statement.

2) What is a part of a switch statement that will execute if there are no values that matched the given choices?
a) case
b) break
c) default
d) None of the above

3) Which of the following is a correct syntax of if statement?
a)

if (boolean_expression)
statement;

b)

if (boolean_expression) {
   statement1;
   statement2;
}

c) Both a and b.
d) None of the above.

4) Which of the following is a correct if-else statement?

a)

if (num>0)
 System.out.println(“Positive”);
else
System.out.println(“Negative”);

b)

if (num>0)
 System.out.println(“Positive”);
 System.out.println(“%d”,num);
else
System.out.println(“Negative”);

c)

if (num>0)
 System.out.println(“Positive”);
 System.out.println(“%d”,num);
else
System.out.println (“Negative”);
System.out.println(“%d”,num);


d) All of the above.

5) What is the data type that can be used in a switch statement?
a) The double data type.
b) The float data type.
c) The char data type.
d) The String data type.

6) In the given switch statement, what will be displayed if the value of var is 5?

switch(var)
{
case 1: System.out.println(“Hello\n”); break;
case 2: System.out.println(“World!\n”); break;
case 3: System.out.println(“Happy\n”);
case 4: System.out.println(“Programming\n”);
case 5: System.out.println(“Thanks!\n”); break;
default : System.out.println(“Congratulations!\n”); break;
}

a)

Thanks!
Congratulations!


b)

Congratulations!


c)

Hello


d)

Thanks!


7) In the given switch statement, what will be displayed if the value of var is 3?

switch(var)
{
case 1: System.out.println(“Hello\n”); break;
case 2: System.out.println(“World!\n”); break;
case 3: System.out.println(“Happy\n”);
case 4: System.out.println(“Programming\n”);
case 5: System.out.println(“Thanks!\n”); break;
default : System.out.println(“Congratulations!\n”); break;
}


a)

Happy


b)

Happy
Programming


c)

Happy
Programming
Thanks!


d) None of the above.

8) In the given switch statement, what will be displayed if the value of var is 7?

switch(var)
{
case 1: System.out.println(“Hello\n”); break;
case 2: System.out.println(“World!\n”); break;
case 3: System.out.println(“Happy\n”);
case 4: System.out.println(“Programming\n”);
case 5: System.out.println(“Thanks!\n”); break;
default : System.out.println(“Congratulations!\n”); break;
}


a)

Programming
Thanks!


b)

Thanks!


c)

Congratulations!

d) None of the above.

9) What is a java keyword used in switch statements that causes immediate exit or that terminates the switch statement?
a) The case keyword.
b) The switch keyword.
c) The default keyword.
d) The break keyword.


10) From the given if-else statement, what will be displayed if the value of num is 6?

if (num>0)
 System.out.println(“Positive Number\n”);
else
 System.out.println(“Negative Number\n”);
 System.out.println(“The number is %d”,num);
a)

Positive Number

b)

Positive Number
The number is 6

c)

The number is 6

d)

Negative Number