Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Modular Arithmetic Basics in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Modular Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Modular Addition
What is the output of the following JavaScript code that performs modular addition?
DSA Javascript
const a = 17;
const b = 23;
const mod = 10;
const result = (a + b) % mod;
console.log(result);
A9
B10
C1
D0
Attempts:
2 left
💡 Hint
Remember to add first, then apply the modulo operation.
❓ Predict Output
intermediate
2:00remaining
Modular Multiplication Result
What will be printed after running this JavaScript code that calculates modular multiplication?
DSA Javascript
const x = 7;
const y = 8;
const mod = 5;
const product = (x * y) % mod;
console.log(product);
A1
B6
C0
D2
Attempts:
2 left
💡 Hint
Multiply first, then take modulo.
❓ Predict Output
advanced
2:00remaining
Modular Exponentiation Output
What is the output of this code that calculates modular exponentiation using repeated multiplication?
DSA Javascript
let base = 3;
let exponent = 4;
let mod = 7;
let result = 1;
for(let i = 0; i < exponent; i++) {
  result = (result * base) % mod;
}
console.log(result);
A2
B4
C5
D1
Attempts:
2 left
💡 Hint
Calculate 3^4 mod 7 step by step.
🧠 Conceptual
advanced
2:00remaining
Properties of Modular Arithmetic
Which of the following statements about modular arithmetic is TRUE?
AModular addition is not associative.
BModular subtraction always produces a positive result.
CModular multiplication distributes over modular addition.
DModular division is always defined for any divisor.
Attempts:
2 left
💡 Hint
Think about how multiplication and addition behave under modulo.
🔧 Debug
expert
2:00remaining
Identify the Error in Modular Inverse Calculation
Consider this code snippet that attempts to find the modular inverse of a number. What error will it produce when run?
DSA Javascript
function modInverse(a, m) {
  for(let x = 1; x < m; x++) {
    if ((a * x) % m == 1) {
      return x;
    }
  }
  return null;
}
console.log(modInverse(6, 9));
AReturns null
BInfinite loop
CThrows a TypeError
DReturns 4
Attempts:
2 left
💡 Hint
Check if 6 has an inverse modulo 9.

Practice

(1/5)
1. What is the result of 7 % 3 in modular arithmetic?
easy
A. 2
B. 3
C. 1
D. 0

Solution

  1. Step 1: Understand the modulus operation

    The modulus operator (%) gives the remainder after division of the first number by the second.
  2. Step 2: Calculate 7 divided by 3

    7 divided by 3 is 2 with a remainder of 1.
  3. Final Answer:

    1 -> Option C
  4. Quick Check:

    7 % 3 = 1 [OK]
Hint: Modulus gives remainder after division [OK]
Common Mistakes:
  • Confusing modulus with division
  • Using 3 instead of remainder
  • Thinking modulus returns quotient
2. Which of the following is the correct syntax to compute the remainder of a divided by b in JavaScript?
easy
A. a % b
B. a mod b
C. mod(a, b)
D. a // b

Solution

  1. Step 1: Recall JavaScript modulus syntax

    In JavaScript, the modulus operator is the percent sign (%) placed between two numbers or variables.
  2. Step 2: Identify correct syntax

    Only a % b correctly computes the remainder of a divided by b.
  3. Final Answer:

    a % b -> Option A
  4. Quick Check:

    Use % for modulus in JavaScript [OK]
Hint: Use % operator for modulus in JavaScript [OK]
Common Mistakes:
  • Using 'mod' keyword
  • Using integer division operator //
  • Calling a non-existent mod() function
3. What is the output of the following JavaScript code?
const x = 15;
const y = 4;
console.log(x % y);
medium
A. 4
B. 3
C. 0
D. 1

Solution

  1. Step 1: Calculate 15 divided by 4

    15 divided by 4 is 3 with a remainder of 3.
  2. Step 2: Apply modulus operator

    The expression 15 % 4 returns the remainder, which is 3.
  3. Final Answer:

    3 -> Option B
  4. Quick Check:

    15 % 4 = 3 [OK]
Hint: Modulus returns remainder after division [OK]
Common Mistakes:
  • Confusing remainder with quotient
  • Expecting 4 instead of remainder
  • Mistaking modulus for division
4. Find the error in the following code snippet that tries to compute the modulus of two numbers:
let a = 10;
let b = 3;
let result = a % b();
console.log(result);
medium
A. Missing semicolon after console.log
B. Modulus operator should be replaced with division
C. Variables a and b should be strings
D. Using parentheses after b causes an error

Solution

  1. Step 1: Analyze the modulus expression

    The code uses a % b(), which treats b as a function call, but b is a number, not a function.
  2. Step 2: Identify the error

    Parentheses after b cause a runtime error because numbers cannot be called like functions.
  3. Final Answer:

    Using parentheses after b causes an error -> Option D
  4. Quick Check:

    Variables are not functions, so no () after b [OK]
Hint: Don't add () after variables unless they are functions [OK]
Common Mistakes:
  • Adding () after number variables
  • Confusing modulus with division
  • Ignoring runtime errors
5. You want to find the remainder when the sum of two numbers a = 123456789 and b = 987654321 is divided by 1000. Which expression correctly computes this using modular arithmetic properties?
hard
A. ((a % 1000) + (b % 1000)) % 1000
B. (a % 1000) + (b % 1000)
C. a + b % 1000
D. a % (b % 1000)

Solution

  1. Step 1: Recall modular addition property

    The remainder of a sum modulo m equals the sum of remainders modulo m: (a + b) % m = ((a % m) + (b % m)) % m.
  2. Step 2: Apply property to given numbers

    Compute (a % 1000) and (b % 1000), add them, then take modulo 1000 again to get correct remainder.
  3. Final Answer:

    ((a % 1000) + (b % 1000)) % 1000 -> Option A
  4. Quick Check:

    Use modular addition: ((a%m)+(b%m))%m [OK]
Hint: Add remainders then mod again for sum modulus [OK]
Common Mistakes:
  • Skipping final modulus after addition
  • Adding without modulus
  • Using wrong modulus order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard