What is Modulo operation?

An operation that gives the remainder of a division, written mod in the pseudo-language and % in many languages. x mod 10 gives the ones digit, x mod 2 tells whether x is even or odd, and i mod n + 1 gives an index that cycles through 1 to n. The sign of the remainder for negative numbers differs between languages.

Japanese name: 剰余演算

Modulo operation appears in 15 practice questions across 5 topics, including C Programming Basics, Programming Basics (Pseudocode Tracing), Data Structures and Algorithms.

Field: Programming Basics (Pseudocode Tracing)

Try it: questions about Modulo operation

Keys 10, 17, 24 and 3 are stored in this order in a hash table of size 7 (positions 0 to 6) with the hash function h(k) = k mod 7. Collisions are resolved by linear probing, which moves forward one position at a time (wrapping from 6 to 0) to find a free slot. At which position is key 3 stored?

  1. APosition 3
  2. BPosition 4
  3. CPosition 6
Show answer and explanation

Answer: C. Position 6

All four keys leave a remainder of 3 when divided by 7, so each one first tries position 3. Key 10 goes into position 3; 17 finds position 3 taken and goes into position 4; 24 finds positions 3 and 4 taken and goes into position 5. Key 3 finds positions 3, 4 and 5 taken and is stored at position 6. As this shows, linear probing makes occupied positions bunch together (clustering), so keys that arrive later need more probes.

A function f(x, y) is defined as “if y is 0, return x; otherwise return f(y, x mod y)”. When f(84, 36) is called, what is the return value, and how many times is f called, including the first call?

  1. AReturns 12; called 3 times
  2. BReturns 12; called 4 times
  3. CReturns 6; called 3 times
Show answer and explanation

Answer: A. Returns 12; called 3 times

f(84, 36) calls f(36, 12) because 84 mod 36 = 12; that calls f(12, 0) because 36 mod 12 = 0; and since y is 0, it returns 12. The calls are f(84, 36), f(36, 12) and f(12, 0), three in total, and this is the Euclidean algorithm for the greatest common divisor. Because the function returns the result of its recursive call directly (tail recursion), a compiler that performs tail call optimization can turn it into a loop, so it does not keep using stack space however deep the recursion goes.

Modulo operation in the explanations

Questions that mention Modulo operation (15)

  1. Keys 10, 17, 24 and 3 are stored in this order in a hash table of size 7 (positions 0 to 6) with the hash function h(k) = k mod 7. Collisions are resolved by linear probing, which moves forward one position at a time (wrapping from 6 to 0) to find a free slot. At which position is key 3 stored?
  2. A function f(x, y) is defined as “if y is 0, return x; otherwise return f(y, x mod y)”. When f(84, 36) is called, what is the return value, and how many times is f called, including the first call?
  3. A queue uses the 4-element string array q as a ring. head and tail start at 1. enq(v) does q[tail] ← v and then tail ← tail mod 4 + 1; deq() returns the value of q[head] and then does head ← head mod 4 + 1. When the following program runs, in which element of q is "E" stored?
  4. In C, what is the result of subtracting 1 from 0 in a 32-bit unsigned integer (unsigned int)?
  5. In a cache with 64-byte lines and 64 sets, which set holds the data at address 0x1234?
  6. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. What does the following function f return when called as f(9087)?
  7. ÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. What does the following function toBin return when called as toBin(12)?
  8. The following program subtracts the two integers a and b from each other until they are equal. What is the value of k after it runs?
  9. The following function isLeap returns true if the year y is a leap year. and is evaluated before or. For which y does isLeap(y) return true?
  10. In embedded programs, variables that hold the value of a 32-bit peripheral register are often declared with uint32_t from stdint.h rather than unsigned int. Which of the following is the main reason?
  11. uint8_t is an 8-bit unsigned integer type. Which of the following is the output when the code above, which increments x ten times, is executed?
  12. unsigned char is 8 bits and int is 32 bits. Which of the following is the output when the code above is executed?
  13. int is 32 bits (maximum 2147483647). Which of the following is the largest n for which the function fact(n) above returns the factorial of n correctly, without overflow?
  14. Which of the following is the output when the code above, which stores 9 in a 3-bit bit-field, is executed?
  15. int and unsigned int are 32 bits. Which of the following is undefined behavior according to the C standard?

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Variable Undefined behavior Argument Hash table Hash function Return value Recursion Stack Queue Array

Back to the glossary