Pancyprian Computer Science - independent foundations study materials
Pancyprian Computer Science - independent foundations study resources

Pancyprian Computer Science - independent foundations practice questions

One question at a time. Build your Pancyprian Computer Science - independent foundations understanding.

First 50 of 190 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.

Continue in the practice tool above to load more questions, 50 at a time.

Question 1Algorithms and computational thinking

Which of the following best describes an algorithm?

  1. A programming language used to solve computational problems
  2. A finite sequence of well-defined, unambiguous steps that solves a problem in finite time Correct answer.
  3. A computer program that has been compiled into machine code
  4. A diagram showing the hardware components of a computer system

Explanation

An algorithm is a finite, ordered set of precise steps that solves a problem or performs a task. It exists independently of any programming language; a program is only one possible implementation of an algorithm.

Question 2Algorithms and computational thinking

Which of the following is NOT a required property of an algorithm?

  1. Finiteness: it must terminate after a finite number of steps
  2. Definiteness: each step must be precisely and unambiguously defined
  3. It must be written in the C++ programming language Correct answer.
  4. Effectiveness: each step must be basic enough to be carried out exactly

Explanation

Algorithms are language-independent: the same algorithm can be expressed in pseudocode, a flowchart, or any programming language. Finiteness, definiteness, and effectiveness are classical required properties of algorithms.

Question 3Algorithms and computational thinking

Trace the pseudocode: x <- 5; y <- 3; x <- x + 2*y; y <- x - y. What is the final value of y?

  1. 8 Correct answer.
  2. 5
  3. 11
  4. 3

Explanation

First x becomes 5 + 2*3 = 11. Then y becomes x - y = 11 - 3 = 8. Note that the updated value of x (11), not the original 5, is used in the last assignment.

Question 4Algorithms and computational thinking

In a flowchart, what does a diamond-shaped symbol represent?

  1. An input or output operation
  2. The start or end of the algorithm
  3. A decision point with a conditional test (e.g. yes/no branches) Correct answer.
  4. A processing step such as a calculation

Explanation

In standard flowchart notation, the diamond denotes a decision: a condition is tested and the flow splits into branches (typically yes/no or true/false).

Question 5Algorithms and computational thinking

What precondition must hold for binary search to work correctly on an array?

  1. The data must be sorted Correct answer.
  2. The data must contain no duplicate values
  3. The data must consist of numbers only
  4. The array must contain an even number of elements

Explanation

Binary search works by comparing the target with the middle element and discarding half of the remaining range. This halving logic is only valid when the data are ordered.

Question 6Algorithms and computational thinking

A sorted array contains 1000 elements. Using binary search, what is the maximum number of comparisons needed to find a target value (or conclude it is absent)?

  1. 500
  2. 100
  3. 10 Correct answer.
  4. 1000

Explanation

Each comparison halves the search range. Since 2^10 = 1024 >= 1000, at most 10 comparisons are needed (ceiling of log2(1000)). This illustrates the logarithmic efficiency of binary search.

Question 7Algorithms and computational thinking

In the worst case, how many comparisons does linear (sequential) search need on an array of n elements?

  1. 1
  2. n/2
  3. log2 n
  4. n Correct answer.

Explanation

Linear search checks elements one by one from the start. In the worst case (target absent or in the last position) all n elements are examined, so n comparisons are made.

Question 8Algorithms and computational thinking

Trace the pseudocode: s <- 0; for i <- 1 to 5 do s <- s + i. What is the final value of s?

  1. 10
  2. 15 Correct answer.
  3. 20
  4. 5

Explanation

The loop accumulates 1 + 2 + 3 + 4 + 5 = 15. This is the classic accumulator pattern: a variable initialized to zero that gathers a running total.

Question 9Algorithms and computational thinking

Trace the pseudocode: n <- 1; c <- 0; while n < 100 do { n <- n * 2; c <- c + 1 }. What is the final value of c?

  1. 6
  2. 7 Correct answer.
  3. 8
  4. 100

Explanation

The values of n at each test are 1, 2, 4, 8, 16, 32, 64 (loop runs), then 128 (loop stops). The body executed 7 times, so c = 7.

Question 10Algorithms and computational thinking

Trace the pseudocode: a <- 4; b <- 7; temp <- a; a <- b; b <- temp. What are the final values?

  1. a = 4, b = 7
  2. a = 7, b = 7
  3. a = 7, b = 4 Correct answer.
  4. a = 4, b = 4

Explanation

The temporary variable preserves the original value of a (4). After a <- b, a is 7; then b <- temp restores the original a into b, giving a = 7 and b = 4. This is the standard swap idiom.

Question 11Algorithms and computational thinking

An ascending bubble sort performs one full pass over the array [6, 3, 8, 2], swapping adjacent elements whenever they are out of order. What does the array look like after this first pass?

  1. [2, 3, 6, 8]
  2. [3, 6, 2, 8] Correct answer.
  3. [3, 6, 8, 2]
  4. [6, 3, 2, 8]

Explanation

Compare 6 and 3: swap, giving [3, 6, 8, 2]. Compare 6 and 8: no swap. Compare 8 and 2: swap, giving [3, 6, 2, 8]. After one pass the largest element (8) has 'bubbled' to the last position.

Question 12Algorithms and computational thinking

Approximately how many comparisons does binary search need in the worst case on a sorted telephone directory of 1,000,000 names?

  1. About 500,000
  2. About 1,000
  3. About 1,000,000
  4. About 20 Correct answer.

Explanation

Binary search halves the range each step, and 2^20 = 1,048,576 >= 1,000,000, so about 20 comparisons suffice. This logarithmic behaviour is why binary search is preferred for large sorted datasets.

Question 13Algorithms and computational thinking

In computational thinking, what is 'decomposition'?

  1. Breaking a complex problem down into smaller, more manageable sub-problems Correct answer.
  2. Removing all unnecessary detail from a problem to focus on the essentials
  3. Looking for similarities and recurring structures within problems
  4. Writing a step-by-step solution that a computer can execute

Explanation

Decomposition means splitting a large problem into smaller parts that can be solved (and programmed) separately, then combined. It is one of the four pillars of computational thinking.

Question 14Algorithms and computational thinking

Trace the pseudocode: x <- 12; if x MOD 2 = 0 then y <- x / 2 else y <- 3*x + 1. What is the final value of y?

  1. 37
  2. 6 Correct answer.
  3. 12
  4. 24

Explanation

12 MOD 2 = 0, so the condition is true and the then-branch executes: y = 12 / 2 = 6. The else-branch (3*x + 1) is skipped entirely.

Question 15Algorithms and computational thinking

Trace the pseudocode: c <- 0; for i <- 1 to 3 do { for j <- 1 to 4 do c <- c + 1 }. What is the final value of c?

  1. 7
  2. 12 Correct answer.
  3. 16
  4. 64

Explanation

The inner loop runs 4 times for each of the 3 outer iterations, so the body executes 3 x 4 = 12 times. Nested-loop costs multiply rather than add.

Question 16Algorithms and computational thinking

An algorithm must read numbers one at a time and stop as soon as a negative number is entered, then output how many non-negative numbers were read. Which control structure is most appropriate for the reading step?

  1. A counted for-loop from 1 to 100
  2. A while-loop that continues as long as the number entered is non-negative Correct answer.
  3. A nested pair of for-loops
  4. A single if-else statement with no loop

Explanation

The number of inputs is unknown in advance; the repetition must be controlled by a condition (input >= 0). A while-loop with a sentinel value is the natural choice for condition-controlled repetition.

Question 17Algorithms and computational thinking

Given a <- 10 and b <- 3, what are the values of q <- a DIV b (integer quotient) and r <- a MOD b (remainder)?

  1. q = 3, r = 1 Correct answer.
  2. q = 3, r = 3
  3. q = 3.33, r = 0
  4. q = 1, r = 3

Explanation

DIV gives the integer quotient: 10 DIV 3 = 3. MOD gives the remainder: 10 MOD 3 = 1. They satisfy a = q*b + r, i.e. 10 = 3*3 + 1.

Question 18Algorithms and computational thinking

Trace the pseudocode: n <- 153; s <- 0; while n > 0 do { d <- n MOD 10; s <- s + d*d*d; n <- n DIV 10 }. What is the final value of s?

  1. 27
  2. 9
  3. 153 Correct answer.
  4. 126

Explanation

The loop extracts digits 3, 5, 1 and cubes them: 27 + 125 + 1 = 153. Because s equals the original number, 153 is an Armstrong (narcissistic) number.

Question 19Data representation and relational databases

What is the decimal value of the binary number 1011?

  1. 13
  2. 11 Correct answer.
  3. 9
  4. 1011

Explanation

1011 binary = 1*8 + 0*4 + 1*2 + 1*1 = 8 + 2 + 1 = 11. Each position is a power of two, starting from 2^0 on the right.

Question 20Data representation and relational databases

What is the binary representation of the decimal number 25?

  1. 10101
  2. 11100
  3. 11001 Correct answer.
  4. 10011

Explanation

25 = 16 + 8 + 1, so bits 4, 3 and 0 are set: 11001. Repeated division by 2 gives the same result: remainders 1, 0, 0, 1, 1 read bottom-up.

Question 21Data representation and relational databases

What is the decimal value of the hexadecimal number 2F?

  1. 47 Correct answer.
  2. 215
  3. 43
  4. 35

Explanation

In hex, F = 15. So 2F = 2*16 + 15 = 32 + 15 = 47.

Question 22Data representation and relational databases

What is the hexadecimal representation of the binary number 10110101?

  1. B5 Correct answer.
  2. A5
  3. B4
  4. 265

Explanation

Group the bits into nibbles: 1011 0101. 1011 = 11 = B, and 0101 = 5. Hence 10110101 binary = B5 hex.

Question 23Data representation and relational databases

What is the decimal value of the octal number 17?

  1. 17
  2. 15 Correct answer.
  3. 23
  4. 111

Explanation

Octal is base 8, so 17 octal = 1*8 + 7 = 15 in decimal.

Question 24Data representation and relational databases

What range of decimal integers can be represented with 8 bits using two's complement?

  1. 0 to 255
  2. -127 to 128
  3. -128 to 127 Correct answer.
  4. -255 to 255

Explanation

With n bits in two's complement, the range is -2^(n-1) to 2^(n-1) - 1. For 8 bits that is -128 to 127. The range is asymmetric because one pattern (10000000) represents -128 and there is no +128.

Question 25Data representation and relational databases

How is the decimal number -5 represented in 8-bit two's complement?

  1. 11111011 Correct answer.
  2. 10000101
  3. 11111010
  4. 00000101

Explanation

Start with +5 = 00000101. Invert all bits: 11111010. Add 1: 11111011. This is the standard invert-and-add-one procedure for negating in two's complement.

Question 26Data representation and relational databases

How many bytes are there in 4 KB (kilobytes) of memory, using the binary convention 1 KB = 1024 bytes?

  1. 4000
  2. 4096 Correct answer.
  3. 1024
  4. 400

Explanation

1 KB = 1024 bytes, so 4 KB = 4 x 1024 = 4096 bytes. The binary convention uses powers of two because memory is addressed in binary.

Question 27Data representation and relational databases

What is the minimum number of bits needed to represent 256 different values?

  1. 256
  2. 16
  3. 2
  4. 8 Correct answer.

Explanation

With n bits you can represent 2^n distinct values. Since 2^8 = 256, exactly 8 bits are needed.

Question 28Data representation and relational databases

In the ASCII encoding, the character 'A' has code 65. What is the ASCII code of the character 'F'?

  1. 69
  2. 70 Correct answer.
  3. 71
  4. 66

Explanation

Uppercase letters occupy consecutive ASCII codes. 'F' is 5 positions after 'A', so its code is 65 + 5 = 70.

Question 29Data representation and relational databases

A colour image uses 8 bits for each of the red, green and blue channels per pixel. How many bits are used to store the colour of one pixel?

  1. 8
  2. 16
  3. 24 Correct answer.
  4. 32

Explanation

Three channels x 8 bits = 24 bits per pixel. This 'true colour' scheme allows 2^24 (about 16.7 million) distinct colours.

Question 30Data representation and relational databases

What is the largest decimal value that can be stored in an unsigned 8-bit integer?

  1. 255 Correct answer.
  2. 256
  3. 127
  4. 128

Explanation

Unsigned 8-bit values run from 0 to 2^8 - 1 = 255. The count of distinct values is 256, but because it starts at 0, the maximum is 255.

Question 31Data representation and relational databases

What is the result of the binary addition 1011 + 110?

  1. 1111
  2. 10010
  3. 10001 Correct answer.
  4. 1101

Explanation

Aligning by the right: 1011 + 0110. Units: 1+0=1; twos: 1+1=10, write 0 carry 1; fours: 0+1+carry=10, write 0 carry 1; eights: 1+0+carry=10, write 0 carry 1; final carry gives 1. Result: 10001 (= 11 + 6 = 17 in decimal).

Question 32Data representation and relational databases

Why is hexadecimal notation widely used in computing?

  1. Because computers perform calculations directly in base 16
  2. Because it is a compact, human-friendly shorthand for binary patterns Correct answer.
  3. Because it allows negative numbers to be stored without a sign bit
  4. Because it doubles the storage capacity of memory chips

Explanation

One hex digit corresponds exactly to four bits, so long binary patterns (memory addresses, colour codes, machine code) can be written and read compactly with easy mental conversion.

Question 33Data representation and relational databases

A mono sound is recorded for 10 seconds at 1000 samples per second with 8 bits (1 byte) per sample, without compression. How many bytes of storage does the recording require?

  1. 80,000
  2. 10,000 Correct answer.
  3. 1,000
  4. 800

Explanation

File size = duration x sampling rate x bytes per sample = 10 s x 1000 samples/s x 1 byte = 10,000 bytes.

Question 34Data representation and relational databases

The 8-bit pattern 11111111 represents which decimal value when interpreted as a two's complement signed integer?

  1. 255
  2. -255
  3. -1 Correct answer.
  4. 0

Explanation

In two's complement, 11111111 = -1: inverting gives 00000000 and adding 1 gives 00000001 (=1), so the pattern is the negative of 1. The leading 1 signals a negative number.

Question 35Programming and C++ foundations

In programming, what is a variable?

  1. A named memory location that stores a value which can change during program execution Correct answer.
  2. A fixed value that can never be altered after compilation
  3. A reserved word that starts a loop
  4. A hardware component inside the CPU

Explanation

A variable is a named reference to a memory location whose contents the program can read and modify. Its name, type, and value are its key attributes.

Question 36Programming and C++ foundations

In C++, which statement correctly reads an integer from the keyboard into the variable x?

  1. cout << x;
  2. cin >> x; Correct answer.
  3. read x;
  4. x = input;

Explanation

cin is the standard input stream and >> is the extraction operator, so cin >> x; reads a value into x. cout with << does the opposite (output).

Question 37Programming and C++ foundations

What does the following C++ code output? int a = 7, b = 2; cout << a / b << ' ' << a % b;

  1. 3.5 1
  2. 3 1 Correct answer.
  3. 3 0
  4. 3.5 0

Explanation

Both operands are int, so a / b performs integer division: 7 / 2 = 3. The % operator gives the remainder: 7 % 2 = 1. Output: '3 1'.

Question 38Programming and C++ foundations

In C++, what value is stored in r after: double r = 7 / 2;

  1. 3.5
  2. 4.0
  3. 3.0 Correct answer.
  4. A compilation error occurs

Explanation

The expression 7 / 2 is evaluated first using integer division, giving 3; only then is it converted to double, so r = 3.0. To get 3.5 you must write 7.0 / 2 or 7 / 2.0.

Question 39Programming and C++ foundations

What does the following C++ code print? int x = 10; if (x > 5) x = x - 3; else x = x + 3; cout << x;

  1. 13
  2. 10
  3. 3
  4. 7 Correct answer.

Explanation

Since 10 > 5 is true, the if-branch executes: x = 10 - 3 = 7. The else-branch is skipped.

Question 40Programming and C++ foundations

What is the value of s after this C++ loop? int s = 0; for (int i = 1; i <= 4; i++) s += i * i;

  1. 24
  2. 30 Correct answer.
  3. 10
  4. 100

Explanation

The loop adds the squares 1 + 4 + 9 + 16 = 30. Note the condition i <= 4 includes the iteration i = 4.

Question 41Programming and C++ foundations

What is the final value of i after this C++ code? int i = 10; while (i > 0) { i -= 3; }

  1. 0
  2. -2 Correct answer.
  3. 1
  4. -1

Explanation

The values are 10, 7, 4, 1, -2. When i = 1 the condition still holds, so the body runs once more giving -2; then the test fails. Loop variables need not land exactly on the boundary.

Question 42Programming and C++ foundations

What is the key difference between a do...while loop and a while loop in C++?

  1. A do...while loop always executes its body at least once, because the condition is checked after the body Correct answer.
  2. A do...while loop cannot contain if statements
  3. A while loop always executes at least once, but a do...while loop may not execute at all
  4. There is no difference; they are interchangeable in all cases

Explanation

In do...while the condition is evaluated after the body (post-test), guaranteeing one execution. In a while loop the condition is tested first, so the body may run zero times.

Question 43Programming and C++ foundations

What is the value of the C++ expression 2 + 3 * 4?

  1. 20
  2. 14 Correct answer.
  3. 24
  4. 9

Explanation

Multiplication has higher precedence than addition, so the expression is 2 + (3 x 4) = 2 + 12 = 14, not (2 + 3) x 4.

Question 44Programming and C++ foundations

Given int x = 5, y = 10; what is the value of the C++ expression (x > 3 && y < 5) || !(x == 5)?

  1. true
  2. false Correct answer.
  3. A compilation error occurs
  4. 5

Explanation

x > 3 is true but y < 5 is false, so the && gives false. !(x == 5) is !(true) = false. false || false = false.

Question 45Programming and C++ foundations

Which C++ data type is the appropriate choice for storing the value 3.14159?

  1. int
  2. double Correct answer.
  3. char
  4. bool

Explanation

double stores floating-point numbers with fractional parts. int would truncate 3.14159 to 3, char stores a single character, and bool stores only true/false.

Question 46Programming and C++ foundations

Given the declaration int a[5]; in C++, which of the following accesses is out of bounds?

  1. a[0]
  2. a[4]
  3. a[5] Correct answer.
  4. a[2 + 1]

Explanation

Array indices in C++ start at 0, so a[5] has valid indices 0 to 4. Accessing a[5] reads/writes memory outside the array and causes undefined behaviour.

Question 47Programming and C++ foundations

Given int a[5] = {2, 4, 6, 8, 10}; what does cout << a[1] + a[3]; print?

  1. 14
  2. 12 Correct answer.
  3. 10
  4. 8

Explanation

a[1] is the second element (4) and a[3] is the fourth element (8), so the sum is 12. Zero-based indexing is the key detail here.

Question 48Programming and C++ foundations

What does this C++ code print? int a[6] = {4, 9, 1, 7, 7, 2}; int best = 0; for (int i = 1; i < 6; i++) if (a[i] > a[best]) best = i; cout << best;

  1. 1 Correct answer.
  2. 9
  3. 3
  4. 4

Explanation

The loop finds the index of the maximum element. The maximum value is 9 at index 1, and since the comparison is strict (>), ties are not updated. It prints the index 1, not the value 9.

Question 49Programming and C++ foundations

In C++, what is the effect of declaring a function parameter by reference, e.g. void f(int &x)?

  1. The function receives a copy, so the caller's variable cannot change
  2. The function works on the caller's original variable, so assignments to x modify it Correct answer.
  3. The parameter becomes a constant that cannot be assigned
  4. The variable is deleted from memory when the function returns

Explanation

A reference parameter is an alias for the caller's variable, not a copy. Any modification inside the function directly affects the original, which is how swap functions are written.

Question 50Programming and C++ foundations

What does f(4) return? int f(int n) { if (n == 0) return 1; return n * f(n - 1); }

  1. 10
  2. 24 Correct answer.
  3. 16
  4. 4

Explanation

This recursive function computes factorial: f(4) = 4 x f(3) = 4 x 3 x f(2) = 4 x 3 x 2 x f(1) = 4 x 3 x 2 x 1 x f(0) = 24. The base case n == 0 stops the recursion.