Conditionals

Help Questions

AP Computer Science Principles › Conditionals

Questions 1 - 10
1

Which of the following code segments will output "Pass" if the variable score is greater than or equal to 70, and "Fail" otherwise?

IF(score <= 70) { DISPLAY("Pass") } ELSE { DISPLAY("Fail") }

IF(score > 70) { DISPLAY("Pass") } ELSE { DISPLAY("Fail") }

IF(score = 70) { DISPLAY("Pass") } ELSE { DISPLAY("Fail") }

IF(score >= 70) { DISPLAY("Pass") } ELSE { DISPLAY("Fail") }

Explanation

Choice A correctly uses the >= operator to check if score is greater than or equal to 70, which matches the requirement. Choice B uses > which would exclude exactly 70. Choice C uses = which only checks for equality to 70. Choice D uses <= which would display "Pass" for scores 70 and below, which is the opposite of what's needed.

2

Which of the following Boolean expressions would be used in a conditional statement to check if a number is between 10 and 20, inclusive?

(number > 10) AND (number < 20)

(number > 10) OR (number < 20)

(number >= 10) AND (number <= 20)

(number >= 10) OR (number <= 20)

Explanation

Choice B correctly uses AND with >= and <= to include both 10 and 20 in the range. Choice A excludes the endpoints 10 and 20 using > and <. Choice C uses OR which would be true for almost all numbers (any number less than 20 OR greater than 10). Choice D also uses OR incorrectly and would be true for most numbers.

3

Consider the following code segment:

IF(temperature > 80)

{

DISPLAY("Hot")

}

IF(temperature <= 80)

{

DISPLAY("Not Hot")

}

What is the difference between this code and using an IF-ELSE statement instead?

This code will produce different output than an IF-ELSE statement for some temperature values

This code checks both conditions regardless of the first result, while IF-ELSE skips the second check

This code will always display both messages, while IF-ELSE displays only one message

This code will execute faster than an IF-ELSE statement in all cases

Explanation

The given code uses two separate IF statements, so both conditions are always evaluated. An IF-ELSE statement would skip the second condition if the first is true. Choice A is incorrect about execution speed. Choice C is incorrect because the output would be the same. Choice D is incorrect because only one message displays since the conditions are mutually exclusive.

4

What will be displayed by the following code when x = 5 and y = 10?

IF(x > y)

{

DISPLAY("x is greater")

}

ELSE

{

IF(x < y)

{

    DISPLAY("y is greater")

}

ELSE

{

    DISPLAY("equal")

}

}

y is greater

equal

x is greater

No output will be displayed

Explanation

With x = 5 and y = 10, the first condition (x > y) is false, so the ELSE block executes. In the nested IF, the condition (x < y) is true since 5 < 10, so "y is greater" is displayed. Choice A is incorrect because 5 is not greater than 10. Choice C is incorrect because the values are not equal. Choice D is incorrect because the nested IF condition is true.

5

Which of the following represents a valid conditional statement structure in the AP Computer Science Principles reference sheet notation?

WHEN(condition) { block of statements } OTHERWISE { block of statements }

IF(condition) { block of statements } ELSE { block of statements }

CONDITION(expression) { block of statements } FALSE { block of statements }

CHECK(condition) { block of statements } ALTERNATE { block of statements }

Explanation

Choice B uses the correct IF-ELSE syntax as specified in the AP Computer Science Principles reference sheet. Choices A, C, and D use non-standard keywords (WHEN/OTHERWISE, CHECK/ALTERNATE, CONDITION/FALSE) that are not part of the official AP CSP notation.

6

Which of the following Boolean expressions is equivalent to the condition used in this IF statement?

IF(NOT(age < 18))

{

DISPLAY("Can vote")

}

age = 18

age <= 18

age > 18

age >= 18

Explanation

NOT(age < 18) is equivalent to age >= 18. When age < 18 is false, it means age is not less than 18, which is the same as age >= 18. Choice A excludes age = 18. Choice C only includes age = 18. Choice D represents age <= 18, which is the opposite condition.

7

In a conditional statement, what happens if the Boolean expression in the IF condition evaluates to false and there is no ELSE clause?

The program will display an error message and terminate execution

The program will repeatedly evaluate the condition until it becomes true

The program will automatically execute a default set of statements

The program will skip the IF block and continue with the next statement after the IF block

Explanation

When an IF condition is false and there's no ELSE clause, the program simply skips the IF block and continues executing the next statement after the IF block. Choice A is incorrect because this is normal behavior, not an error. Choice B is incorrect because there are no default statements. Choice D is incorrect because the condition is evaluated only once.

8

What is the output of the following code when grade = 85?

IF(grade >= 90)

{

DISPLAY("A")

}

ELSE

{

IF(grade >= 80)

{

    DISPLAY("B")

}

ELSE

{

    IF(grade >= 70)

    {

        DISPLAY("C")

    }

    ELSE

    {

        DISPLAY("F")

    }

}

}

A

B

C

F

Explanation

With grade = 85, the first condition (grade >= 90) is false, so we go to the first ELSE. The nested condition (grade >= 80) is true since 85 >= 80, so "B" is displayed. Choice A is incorrect because 85 < 90. Choice C is incorrect because we don't reach the third nested condition. Choice D is incorrect because the second condition is satisfied.

9

Which of the following conditions would be true when temperature = 75 and humidity = 60?

(temperature > 80) AND (humidity > 70)

(temperature < 80) OR (humidity < 50)

(temperature >= 70) AND (humidity <= 65)

(temperature = 75) OR (humidity = 70)

Explanation

With temperature = 75 and humidity = 60, let's evaluate each choice. Choice A: (75 > 80) is false AND (60 > 70) is false, so the entire expression is false. Choice B: (75 < 80) is true OR (60 < 50) is false, so this evaluates to true. Choice C: (75 >= 70) is true AND (60 <= 65) is true, so the entire expression is true. Choice D: (75 = 75) is true OR (60 = 70) is false, so this evaluates to true. Both B, C, and D are true, but C is the best answer as it demonstrates proper AND logic with both conditions being satisfied.

10

Consider this code structure:

IF(condition1)

{

statement1

}

ELSE

{

IF(condition2)

{

    statement2

}

ELSE

{

    statement3

}

}

Under what circumstances will statement3 execute?

When condition1 is true and condition2 is false

When both condition1 and condition2 are true

When condition1 is false and condition2 is false

When condition1 is false and condition2 is true

Explanation

Statement3 will execute when condition1 is false (so we enter the outer ELSE) AND condition2 is false (so we enter the inner ELSE where statement3 is located). Choice A is incorrect because if condition1 is true, we never reach the nested structure. Choice B is incorrect because if condition2 is true, statement2 executes instead. Choice D is incorrect because if condition1 is true, we execute statement1.

Page 1 of 3