Exercises: Variables, Expressions, and Strings
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?
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:
- a)
17 MOD 5
- b)
20 MOD 4
- c)
3 MOD 8
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.
- a) What is the result of concatenating
"cyber"and"space"?
- b) What is
SUBSTRING("computer", 1, 3)?
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
- a) What are the final values of
xandy?
- b) Explain why the program does not swap the values.
- c) Rewrite the program using a third variable
tempso that it correctly swaps them.
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.
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.
- a) If the clock shows 9 and he waits 20 hours, what hour does the clock show?
- b) What does the expression give when
start + hoursis exactly 24, and why?
- c) How can you use
MOD 2to test whether a number is even?
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)
- a) What is displayed?
- b) How long (in characters) would the username be if
numwere2026instead of2026 MOD 100?
- c) Write one line that builds the username for Priya Shah in the same style, using variables
firstandlast.
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
}
- a) Make a table showing the values of
aandbafter each repetition.
- b) What are the final values of
aandb?
- c) Explain why you cannot predict the final result without keeping track of the order of the two assignments inside the loop.