Back to homeAP® Computer Science Principles · AP Computer Science Principles · Change subject
Start for free
Notes

Exercises: Boolean Expressions, Conditionals, and Iteration

Practice★☆☆
Let A be true and B be false. Evaluate each expression: a) A AND B b) A OR B c) NOT B d) NOT (A AND B)

Let A be true and B be false. Evaluate each expression:

  1. a) A AND B
  1. b) A OR B
  1. c) NOT B
  1. d) NOT (A AND B)
Solve
Practice★☆☆
Let x ← 7. Evaluate each as true or false: a) (x > 5) AND (x < 10) b) (x < 3) OR (x MOD 2 = 1) c) NOT (x = 7)

Let x ← 7. Evaluate each as true or false:

  1. a) (x > 5) AND (x < 10)
  1. b) (x < 3) OR (x MOD 2 = 1)
  1. c) NOT (x = 7)
Solve
Practice★☆☆
a) How many times is "Go" displayed by REPEAT 4 TIMES { DISPLAY("Go") }? b) What is the final value of n?
  1. a) How many times is "Go" displayed by REPEAT 4 TIMES { DISPLAY("Go") }?
  1. b) What is the final value of n?
n ← 0

REPEAT 5 TIMES

{

  n ← n + 2

}
Solve
Practice★★☆
a) What is displayed for score equal to 95, 85, 80, and 79? b) If the first condition were changed to score > 90, which score value would give a different result? What…
IF (score ≥ 90)

{

  DISPLAY("A")

}

ELSE

{

  IF (score ≥ 80)

  {

    DISPLAY("B")

  }

  ELSE

  {

    DISPLAY("C")

  }

}
  1. a) What is displayed for score equal to 95, 85, 80, and 79?
  1. b) If the first condition were changed to score > 90, which score value would give a different result? What would be displayed?
Solve
Practice★★☆
a) What is displayed? b) How many times does the loop body run? c) What would happen if the condition were n < 0 instead?
n ← 1

REPEAT UNTIL (n > 50)

{

  n ← n * 2

}

DISPLAY(n)
  1. a) What is displayed?
  1. b) How many times does the loop body run?
  1. c) What would happen if the condition were n < 0 instead?
Solve
Practice★★☆
A roller coaster rule says: a rider may board if height ≥ 48 AND (age ≥ 12 OR withAdult), where height is in inches and withAdult is true or false. Evaluate the rule for…

A roller coaster rule says: a rider may board if height ≥ 48 AND (age ≥ 12 OR withAdult), where height is in inches and withAdult is true or false.

Evaluate the rule for each rider:

Rider Height (in.) Age With adult
Chloe 50 10 false
Malik 52 13 false
Camila 45 15 true

Give true or false for each, and then write a Boolean expression that says when a rider is not allowed to board.

Solve
Practice★★★
a) What is displayed, and how many times does the loop body run? b) What would happen if the line i ← i + 1 were removed? Explain. c) Describe the change needed so that…
i ← 1

total ← 0

REPEAT UNTIL (i > 10)

{

  IF (i MOD 3 = 0)

  {

    total ← total + i

  }

  i ← i + 1

}

DISPLAY(total)
  1. a) What is displayed, and how many times does the loop body run?
  1. b) What would happen if the line i ← i + 1 were removed? Explain.
  1. c) Describe the change needed so that the program adds up the even numbers from 1 to 10 instead.
Solve
Practice★★★
Three programmers wrote loops that are supposed to display 1, 2, 3, 4, 5. Version A: Version B: Version C: a) For each version, give the output or say that it never…

Three programmers wrote loops that are supposed to display 1, 2, 3, 4, 5.

Version A:

i ← 1

REPEAT UNTIL (i = 5)

{

  DISPLAY(i)

  i ← i + 1

}

Version B:

i ← 1

REPEAT UNTIL (i = 6)

{

  DISPLAY(i)

  i ← i + 1

}

Version C:

i ← 1

REPEAT UNTIL (i = 4)

{

  DISPLAY(i)

  i ← i + 2

}
  1. a) For each version, give the output or say that it never stops.
  1. b) Explain what is wrong with Version A and how to fix it.
  1. c) Explain why Version C is an infinite loop, and describe a condition that would make it stop safely.
Solve