Control Structures (College Board AP® Computer Science Principles): Exam Questions

18 mins18 questions
1
1 point

Consider the following code segment.

temp ← 18
IF(temp > 25)
{
   DISPLAY("Hot")
}

What is displayed?

  • Hot

  • Nothing is displayed

  • 18

  • An error occurs

2
1 point

Consider the following code segment.

age ← 20
IF (age ≥ 18)
{
   DISPLAY("Adult")
}

What is displayed when this code segment is executed?

  • Adult

  • Nothing is displayed

  • 20

  • An error occurs

3
1 point

Consider the following code segment.

num ← -4
IF (num ≥ 0)
{
   DISPLAY("Non-negative")
}
ELSE
{
   DISPLAY("Negative")
}

What is displayed when this code segment is executed?

  • Non-negative

  • 0

  • Nothing is displayed

  • Negative

4
1 point

Consider the following code segment.

day ← "Sun"
IF (day = "Sat" OR day = "Sun")
{
   DISPLAY("Weekend")
}

What is displayed when this code segment is executed?

  • Nothing is displayed

  • Sun

  • Weekend

  • An error occurs

5
1 point

Consider the following code segment.

total ← 0
REPEAT 4 TIMES
{
   total ← total + 3
}
DISPLAY(total)

What is displayed when this code segment is executed?

  • 3

  • 12

  • 4

  • 7

6
1 point

Consider the following code segment.

value ← 1
REPEAT 5 TIMES
{
   value ← value * 2
}
DISPLAY(value)

What is displayed when this code segment is executed?

  • 10

  • 32

  • 16

  • 64

1
1 point

Consider the following code segment.

n ← 7
IF(n MOD 2 = 0)
{
   DISPLAY("Even")
}
ELSE
{
   IF(n > 5)
   {
      DISPLAY("Odd and large")
   }
   ELSE
   {
      DISPLAY("Odd and small")
   }
}

What is displayed?

  • Even

  • Odd and large

  • Odd and small

  • Nothing is displayed

2
1 point

Consider the following code segment.

sum ← 0
i ← 1
REPEAT UNTIL(i > 4)
{
   sum ← sum + i
   i ← i + 1
}
DISPLAY(sum)

What is displayed?

  • 6

  • 10

  • 15

  • The loop runs infinitely

3
1 point

Which of the following code segments would result in an infinite loop?

  • k ← 0
    
    REPEAT UNTIL(k ≥ 3)
    
    {
    
       k ← k + 1
    
    }
  • k ← 0
    
    REPEAT UNTIL(k = 5)
    
    {
    
       k ← k + 2
    
    }
  • REPEAT 5 TIMES
    
    {
    
       DISPLAY("Hi")
    
    }
  • k ← 10
    
    REPEAT UNTIL(k ≤ 0)
    
    {
    
       k ← k - 1
    
    }
4
1 point

Consider the following code segment.

result ← ""
REPEAT 3 TIMES
{
   result ← result + "ab"
}
DISPLAY(LENGTH(result))

What is displayed?

  • 2

  • 3

  • 6

  • 9

5
1 point

Consider the following code segment.

score ← 63
IF (score ≥ 60)
{
   IF (score ≥ 80)
   {
      DISPLAY("Distinction")
   }
   ELSE
   {
      DISPLAY("Merit")
   }
}
ELSE
{
   DISPLAY("Fail")
}

What is displayed when this code segment is executed?

  • Distinction

  • Merit

  • Fail

  • Nothing is displayed

6
1 point

Consider the following code segment.

x ← 12
IF (x > 5)
{
   DISPLAY("P")
}
IF (x > 10)
{
   DISPLAY("Q")
}
IF (x > 15)
{
   DISPLAY("R")
}

What is displayed when this code segment is executed?

  • P and Q

  • P only

  • Q only

  • P, Q, and R

7
1 point

Consider the following code segment.

n ← 10
REPEAT UNTIL(n < 4)
{
   DISPLAY(n)
   n ← n - 3
}

What is displayed when this code segment is executed?

  • 10 7 4 1

  • 10 7

  • 10 7 4

  • 7 4 1

8
1 point

Consider the following code segment.

x ← 5
REPEAT UNTIL(x = 0)
{
   DISPLAY(x)
   x ← x + 1
}

What is the result of executing this code segment?

  • The loop runs infinitely

  • 5

  • 5 4 3 2 1

  • Nothing is displayed

9
1 point

Consider the following code segment.

nums ← [4, 7, 2, 9]
count ← 0
FOR EACH n IN nums
{
   IF (n > 3)
   {
      count ← count + 1
   }
}
DISPLAY(count)

What is displayed when this code segment is executed?

  • 1

  • 2

  • 3

  • 4

1
1 point

The code segment is intended to display the largest of three distinct values a, b, and c, but it does not always work.

IF(a > b)
{
   DISPLAY(a)
}
ELSE
{
   DISPLAY(b)
}

Which set of initial values shows that the code does not work as intended?

  • a = 1, b = 2, c = 3

  • a = 3, b = 2, c = 1

  • a = 2, b = 1, c = 0

  • a = 5, b = 4, c = 1

2
1 point

Consider the following code segment, where n is a positive integer.

IF (n MOD 3 = 0)
{
   IF (n MOD 2 = 0)
   {
      DISPLAY("Both")
   }
   ELSE
   {
      DISPLAY("Three only")
   }
}
ELSE
{
   DISPLAY("Neither")
}

For which value of n will the code segment display "Three only"?

  • 12

  • 8

  • 6

  • 9

3
1 point

Consider the following code segment.

count ← 8
REPEAT UNTIL(count > 5)
{
   DISPLAY(count)
   count ← count + 1
}
DISPLAY("Done")

What is displayed when this code segment is executed?

  • 8 Done

  • The loop runs infinitely

  • 8

  • Done