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

Exercises: Variables, Expressions, and Strings

Practice★☆☆
Trace this program: What is displayed, and what is the final value of b?

Trace this program:

a ← 5

b ← a + 2

a ← b * 2

DISPLAY(a)

What is displayed, and what is the final value of b?

Solve
Practice★☆☆
The MOD operator gives the remainder after division. Evaluate: a) 17 MOD 5 b) 20 MOD 4 c) 3 MOD 8

The MOD operator gives the remainder after division. Evaluate:

  1. a) 17 MOD 5
  1. b) 20 MOD 4
  1. c) 3 MOD 8
Solve
Practice★☆☆
Assume string positions start at 1. SUBSTRING(str, start, length) returns length characters of str beginning at position start, and concatenation joins two strings…

Assume string positions start at 1. SUBSTRING(str, start, length) returns length characters of str beginning at position start, and concatenation joins two strings together.

  1. a) What is the result of concatenating "cyber" and "space"?
  1. b) What is SUBSTRING("computer", 1, 3)?
Solve
Practice★★☆
Consider this program, which is meant to swap the values of x and y: a) What are the final values of x and y? b) Explain why the program does not swap the values. c)…

Consider this program, which is meant to swap the values of x and y:

x ← 3

y ← 8

x ← y

y ← x
  1. a) What are the final values of x and y?
  1. b) Explain why the program does not swap the values.
  1. c) Rewrite the program using a third variable temp so that it correctly swaps them.
Solve
Practice★★☆
Evaluate each statement and give the final values. What are a, m, and h? Explain in one sentence what h and m represent.

Evaluate each statement and give the final values.

a ← (17 MOD 5) * 3 + 20 / 4

minutes ← 200

m ← minutes MOD 60

h ← (minutes - m) / 60

What are a, m, and h? Explain in one sentence what h and m represent.

Solve
Practice★★☆
A 12-hour clock shows hours 0 through 11. Ethan uses (start + hours) MOD 12 to find the time after waiting. a) If the clock shows 9 and he waits 20 hours, what hour does…

A 12-hour clock shows hours 0 through 11. Ethan uses (start + hours) MOD 12 to find the time after waiting.

  1. a) If the clock shows 9 and he waits 20 hours, what hour does the clock show?
  1. b) What does the expression give when start + hours is exactly 24, and why?
  1. c) How can you use MOD 2 to test whether a number is even?
Solve
Practice★★★
Assume CONCAT(a, b) joins two strings and automatically turns a number into text. SUBSTRING(str, start, length) starts at position 1. a) What is displayed? b) How long…

Assume CONCAT(a, b) joins two strings and automatically turns a number into text. SUBSTRING(str, start, length) starts at position 1.

name1 ← "Jayden"

name2 ← "Brooks"

user ← CONCAT(SUBSTRING(name1, 1, 1), name2)

num ← 2026 MOD 100

user ← CONCAT(user, num)

DISPLAY(user)
  1. a) What is displayed?
  1. b) How long (in characters) would the username be if num were 2026 instead of 2026 MOD 100?
  1. c) Write one line that builds the username for Priya Shah in the same style, using variables first and last.
Solve
Practice★★★
Trace this program. In the expression a + (b MOD 4), the parentheses mean the MOD is computed first. a) Make a table showing the values of a and b after each repetition.…

Trace this program. In the expression a + (b MOD 4), the parentheses mean the MOD is computed first.

a ← 2

b ← 10

REPEAT 3 TIMES

{

  a ← a + (b MOD 4)

  b ← b - a

}
  1. a) Make a table showing the values of a and b after each repetition.
  1. b) What are the final values of a and b?
  1. c) Explain why you cannot predict the final result without keeping track of the order of the two assignments inside the loop.
Solve