Search Header Logo

Java Decision Making Quiz

Authored by Swati Kashyap

English

University

Used 3+ times

Java Decision Making Quiz
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

10 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following correctly describes the behavior of an `if-else` statement in Java when the condition evaluates to `false`?

The `if` block executes and the `else` block is skipped

Both the `if` block and the `else` block execute

The `else` block executes and the `if` block is skipped

Neither block executes

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What will be the output of the following Java code snippet? ```java int x = 10; if (x > 5) { System.out.println("A"); } else if (x > 8) { System.out.println("B"); } else { System.out.println("C"); } ```

A

B

A and B

C

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which of the following is the correct syntax for a `switch` statement in Java?

`switch (expression) { case value: // code break; }`

`switch [expression] { case value => // code break; }`

`switch (expression) { value: // code break; }`

`switch {expression} ( case value: // code break; )`

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

In Java, what happens if a `break` statement is omitted in a `switch` case?

The program throws a compile-time error

Only the matching case executes and the switch exits automatically

Execution falls through to the next case and continues until a `break` or end of switch

The program skips all remaining cases and exits the switch

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Which data types are valid for use as a `switch` expression in Java (Java 7 and later)?

`int`, `float`, `String`, `char`

`int`, `char`, `String`, `enum`

`int`, `double`, `String`, `enum`

`byte`, `float`, `String`, `boolean`

6.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Consider the following Java code. What will be printed? ```java int score = 75; String grade; if (score >= 90) { grade = "A"; } else if (score >= 80) { grade = "B"; } else if (score >= 70) { grade = "C"; } else { grade = "F"; } System.out.println(grade); ```

A

B

C

F

7.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

A student writes the following Java code intending to check if a number is positive, negative, or zero. Identify the logical error and explain the corrected behavior: ```java int num = -5; if (num > 0) { System.out.println("Positive"); } if (num < 0) { System.out.println("Negative"); } if (num == 0) { System.out.println("Zero"); } ```

There is no error; the code works correctly for all cases

The code uses separate `if` statements instead of `else if`, meaning all three conditions are evaluated independently, but for `num = -5` it still prints "Negative" correctly

The code will print both "Negative" and "Zero" for `num = -5` because of missing `else`

The code will throw a runtime exception for negative numbers

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?