Programming Concepts
Practice questions
Which search method halves the range on each step?
- Linear search
- Binary search
- Random search
- Serial search
Answer and explanation
Answer: B
Binary search divides a sorted list in half at every comparison.
A class is best described as:
- A blueprint for objects
- A running program copy
- A name for a variable
- A kind of program loop
Answer and explanation
Answer: A
A class is a blueprint or template from which objects are made.
Which structure is first in, first out?
- Stack
- Array
- Queue
- Tree
Answer and explanation
Answer: C
A queue removes the item that arrived first.
A program runs to the end but prints a wrong answer. This is a
- Syntax error
- Runtime error
- Compile-time error
- Logical error
Answer and explanation
Answer: D
Wrong results from a program that runs normally indicate a logical error.
Which translator executes a program statement by statement?
- Compiler
- Assembler
- Interpreter
- Preprocessor
Answer and explanation
Answer: C
An interpreter translates and runs one statement at a time.
Which number is a palindrome?
- 1223
- 1231
- 1212
- 1221
Answer and explanation
Answer: D
1221 reads the same forwards and backwards.
The digit sum of 7305 is
- 12
- 15
- 10
- 20
Answer and explanation
Answer: B
7 + 3 + 0 + 5 = 15.
Which of these numbers is prime?
- 53
- 57
- 49
- 51
Answer and explanation
Answer: A
51 = 3 x 17, 57 = 3 x 19 and 49 = 7 x 7; 53 has no divisor from 2 to 7, so it is prime.
What is the factorial of 5?
- 25
- 210
- 120
- 240
Answer and explanation
Answer: C
1 x 2 x 3 x 4 x 5 = 120.
Which operator gives the last digit of a whole number n?
- n / 10
- n * 10
- n % 10
- n - 10
Answer and explanation
Answer: C
The remainder after dividing by 10 is the last digit.
x = 4; x = x * 3; x = x - 5. What is x?
- 7
- 12
- 5
- 9
Answer and explanation
Answer: A
x becomes 12 and then 12 - 5 = 7.
Evaluate (3 > 1) AND (2 > 5).
- True
- Error
- Unknown
- False
Answer and explanation
Answer: D
The second comparison is false, so True AND False is False.
Evaluate 4 + 18 / 3 % 4.
- 5
- 8
- 6
- 4
Answer and explanation
Answer: C
18 / 3 = 6, then 6 % 4 = 2, then 4 + 2 = 6.
What is 23 % 6?
- 4
- 5
- 3
- 2
Answer and explanation
Answer: B
23 = 6 x 3 + 5, so the remainder is 5.
Evaluate 12 / 4 * 3.
- 9
- 1
- 3
- 36
Answer and explanation
Answer: A
Work left to right: 12 / 4 = 3, then 3 * 3 = 9.
A loop is written with the test after the body. A body that prints 'Hi' with an initially false test prints it how many times?
- For ever
- Never
- Twice
- Once
Answer and explanation
Answer: D
A loop that tests after the body, such as DO-WHILE, always runs once.
n = 20; count = 0; WHILE n > 0: n = n - 6; count = count + 1. What is count?
- 3
- 4
- 6
- 5
Answer and explanation
Answer: B
n takes 14, 8, 2 and -4. That is 4 subtractions, so count = 4.
S = 0; FOR i = 1 TO 5: S = S + i * 2. What is S?
- 30
- 15
- 10
- 25
Answer and explanation
Answer: A
2 x (1 + 2 + 3 + 4 + 5) = 2 x 15 = 30.
x = 2; WHILE x < 50: x = x * 2. What is x when the loop ends?
- 32
- 64
- 50
- 100
Answer and explanation
Answer: B
x takes 2, 4, 8, 16, 32 and then 64. 64 is not less than 50, so the loop stops with x = 64.
How many times does FOR i = 3 TO 11 run its body?
- 8
- 10
- 9
- 11
Answer and explanation
Answer: C
The count is 11 - 3 + 1 = 9.