D278 Practice Exam 3 Questions with Verified
Correct Answers
A program runs: x = 10; y = x; x = 20. What is the final value of y?
10
A program runs: a = 5; b = 3; a = b; b = a. What is the final value of b?
3
A program tracks the average GPA of a class. Which data type should be used?
Float
A program tracks the number of times a button has been clicked. Which data type should be
used?
Integer
A program needs to store whether a user is logged in. Which data type should be used?
Boolean
Given integer a = 9 and integer b = 4. What is the value of a / b?
2
Given integer a = 9 and integer b = 4. What is the value of a % b?
1
Given integer a = 9 and integer b = 4. What is the value of (a + b) / 2?
6
Given float p = 7.0 and integer q = 2. What is the value of p / q?
3.5
A program checks if userAge is exactly 18 OR exactly 21. Which expression captures this?
(userAge == 18) or (userAge == 21)
A program checks if temperature is below 32 OR above 100. Which expression captures this?
, (temp < 32) or (temp > 100)
Why should a programmer use a constant instead of a regular variable for the value of pi?
The value should not change during program execution
A program has logic: if score >= 90 Put "A"; elseif score >= 80 Put "B"; elseif score >= 70 Put
"C"; else Put "F". What is output if score = 85?
B
A program has logic: if x > 0 then y = 1; if x > 10 then y = 2; if x > 100 then y = 3. Given x = 50,
what is the final value of y?
2
A delivery service: if weight > 100 then fee = 50, else fee = 20. If weight is exactly 100, which
branch executes?
else (false) branch
A program runs: i = 10; while i > 0; Put i; i = i - 2. How many times does the loop iterate?
5
A program runs: i = 1; while i < 20; i = i * 2. How many times does the loop iterate?
5
A program runs: count = 0; while count < 3; Put "Hi"; count = count + 1. What is the output?
HiHiHi
A program runs: x = 5; do; Put x; x = x - 1; while x > 5. What is the output?
5
Why does a do-while loop always execute its body at least once?
The condition is checked after the body executes
A program needs to process exactly each item in a 20-element array. Which loop is preferred?
For loop
A scores array has 10 elements with valid indices 0 to 9. A programmer writes scores[10] = 95.
What happens?
Out-of-bounds error (index 10 is invalid)
Correct Answers
A program runs: x = 10; y = x; x = 20. What is the final value of y?
10
A program runs: a = 5; b = 3; a = b; b = a. What is the final value of b?
3
A program tracks the average GPA of a class. Which data type should be used?
Float
A program tracks the number of times a button has been clicked. Which data type should be
used?
Integer
A program needs to store whether a user is logged in. Which data type should be used?
Boolean
Given integer a = 9 and integer b = 4. What is the value of a / b?
2
Given integer a = 9 and integer b = 4. What is the value of a % b?
1
Given integer a = 9 and integer b = 4. What is the value of (a + b) / 2?
6
Given float p = 7.0 and integer q = 2. What is the value of p / q?
3.5
A program checks if userAge is exactly 18 OR exactly 21. Which expression captures this?
(userAge == 18) or (userAge == 21)
A program checks if temperature is below 32 OR above 100. Which expression captures this?
, (temp < 32) or (temp > 100)
Why should a programmer use a constant instead of a regular variable for the value of pi?
The value should not change during program execution
A program has logic: if score >= 90 Put "A"; elseif score >= 80 Put "B"; elseif score >= 70 Put
"C"; else Put "F". What is output if score = 85?
B
A program has logic: if x > 0 then y = 1; if x > 10 then y = 2; if x > 100 then y = 3. Given x = 50,
what is the final value of y?
2
A delivery service: if weight > 100 then fee = 50, else fee = 20. If weight is exactly 100, which
branch executes?
else (false) branch
A program runs: i = 10; while i > 0; Put i; i = i - 2. How many times does the loop iterate?
5
A program runs: i = 1; while i < 20; i = i * 2. How many times does the loop iterate?
5
A program runs: count = 0; while count < 3; Put "Hi"; count = count + 1. What is the output?
HiHiHi
A program runs: x = 5; do; Put x; x = x - 1; while x > 5. What is the output?
5
Why does a do-while loop always execute its body at least once?
The condition is checked after the body executes
A program needs to process exactly each item in a 20-element array. Which loop is preferred?
For loop
A scores array has 10 elements with valid indices 0 to 9. A programmer writes scores[10] = 95.
What happens?
Out-of-bounds error (index 10 is invalid)