JavaScript Math Object & Random Numbers Quiz

JavaScript
0 Passed
0% acceptance

Explore 50 JavaScript questions covering Math constants, rounding methods, min/max, exponentiation, roots, absolute values, sign detection, trig/log functions, random generation, range helpers, conceptual seeding, randomization patterns, and performance tips for numerical code.

50 Questions
~100 minutes
1

Question 1

What does the Math object represent in JavaScript?

A
A namespace containing static math methods and constants
B
A constructor for creating Math instances
C
A DOM API for SVG graphics
D
A JSON parser
2

Question 2

Which property exposes the mathematical constant π?

A
Math.PI
B
Math.PIE
C
Math.TAU
D
Math.CIRCLE
3

Question 3

Which property represents Euler’s number?

A
Math.E
B
Math.LOGE
C
Math.EULER
D
Math.LN2
4

Question 4

What is Math.LN10?

A
The natural logarithm of 10
B
The base-10 logarithm of e
C
The log base 10 function
D
A rounding constant
5

Question 5

Which Math property stores the natural logarithm of 2?

A
Math.LN2
B
Math.LOG2E
C
Math.LOG10E
D
Math.SQRT2
6

Question 6

Which method rounds 4.6 to the nearest integer?

A
Math.round(4.6)
B
Math.floor(4.6)
C
Math.ceil(4.6)
D
Math.trunc(4.6)
7

Question 7

What value does Math.floor(-3.2) return?

A
-4
B
-3
C
3
D
-3.2
8

Question 8

What is the difference between Math.ceil and Math.trunc for positive decimals?

A
ceil rounds up, trunc drops the fractional part
B
Both round down
C
Both round up
D
ceil and trunc are identical
9

Question 9

Determine the result:

javascript
Math.trunc(9.99)
A
9
B
10
C
0
D
-9
10

Question 10

When should Math.fround be used?

A
When you need a number rounded to 32-bit float precision
B
When you need integer rounding only
C
When generating random booleans
D
When converting to strings
11

Question 11

What does Math.min() return when no arguments are provided?

A
Infinity
B
-Infinity
C
0
D
undefined
12

Question 12

Compute the output:

javascript
Math.max(4, 7, -2, 9)
A
9
B
7
C
-2
D
4
13

Question 13

Which expression equals Math.pow(2, 5)?

A
2 ** 5
B
2 ^ 5
C
2 * 5
D
5 ** 2
14

Question 14

What is Math.sqrt(81)?

A
9
B
8
C
81
D
sqrt is undefined
15

Question 15

What does Math.cbrt(27) compute?

A
The cube root, returning 3
B
The square root, returning ~5.19
C
The absolute value
D
A random number
16

Question 16

What does Math.abs(-12) return?

A
12
B
-12
C
NaN
D
undefined
17

Question 17

What is Math.sign(0)?

A
0
B
1
C
-1
D
Throws error
18

Question 18

What does the following evaluate to?

javascript
Math.sign(-0)
A
-0
B
0
C
1
D
-1
19

Question 19

Which Math method converts degrees to radians?

A
No built-in; multiply degrees by Math.PI / 180
B
Math.deg
C
Math.rad
D
Math.atan2
20

Question 20

Compute the sine of π/2:

javascript
Math.sin(Math.PI / 2)
A
Close to 1
B
Close to 0
C
-1
D
NaN
21

Question 21

What does Math.cos(0) evaluate to?

A
1
B
0
C
-1
D
undefined
22

Question 22

Which function returns the hyperbolic tangent?

A
Math.tanh
B
Math.hypot
C
Math.tan
D
Math.atan
23

Question 23

What does Math.log10(1000) return?

A
3
B
2
C
1
D
0
24

Question 24

What is Math.log1p(x) useful for?

A
Computing Math.log(1 + x) accurately for small x
B
Summing logarithms
C
Calculating base-2 logs
D
Rounding decimals
25

Question 25

What range does Math.random() generate numbers in?

A
[0, 1)
B
(0, 1]
C
[0, 1]
D
(-1, 1)
26

Question 26

Which pattern creates an integer between 0 and 5 inclusive?

javascript
const value = Math.floor(Math.random() * 6)
A
value is 0..5
B
value is 1..6
C
value is 0..6 inclusive
D
value is 0..4
27

Question 27

How do you generate an integer between min and max inclusive?

A
Math.floor(Math.random() * (max - min + 1)) + min
B
Math.random() + min + max
C
Math.ceil(Math.random() * (max - min))
D
Math.abs(Math.random() - max)
28

Question 28

How do you simulate a dice roll (1-6)?

javascript
const roll = Math.floor(Math.random() * 6) + 1
A
Returns integers from 1 to 6
B
Returns decimals only
C
Returns 0 to 5
D
Throws RangeError
29

Question 29

How can you create a random boolean?

A
Math.random() < 0.5
B
Math.sign(Math.random())
C
Math.abs(Math.random())
D
Math.random() && true
30

Question 30

What is a common approach for shuffling an array with Math.random?

A
Use the Fisher-Yates algorithm and swap elements using random indices
B
Sort with Math.random() - 0.5 for perfect randomness
C
Reverse the array
D
Use Math.max to pick elements
31

Question 31

What limitation does Math.random have regarding seeding?

A
It cannot be seeded directly; results are not reproducible by default
B
It always returns the same number
C
It throws if used in strict mode
D
It requires a seed parameter
32

Question 32

How can you conceptually seed random values in JavaScript?

A
Use a PRNG library (e.g., seedrandom) that derives values from a seed
B
Call Math.random(seed)
C
Set Math.seed property
D
Change the browser clock manually
33

Question 33

What does Math.random() * (max - min) + min accomplish?

A
Creates a floating-point number between min (inclusive) and max (exclusive)
B
Creates an integer range
C
Returns min regardless of random
D
Produces negative values only
34

Question 34

Which snippet selects a random item from an array?

javascript
const choice = items[Math.floor(Math.random() * items.length)]
A
choice becomes a random array element
B
choice is always the first element
C
choice is an index only
D
Throws RangeError if array is empty
35

Question 35

What is a common randomization pattern for selecting weighted outcomes?

A
Generate a random number, walk through cumulative weights, and pick the first threshold exceeded
B
Use Math.max on weights
C
Shuffle weights and pick last
D
Pick the smallest weight always
36

Question 36

How can caching Math.PI improve performance in tight loops?

A
Store const PI = Math.PI and reuse it instead of repeated property access
B
Use Math.PI() as a function
C
Replace Math.PI with 3.1
D
No benefit; property access is always free
37

Question 37

Why might you avoid Math.pow when exponent is small constant?

A
Using multiplication or ** can be faster and more readable for simple powers
B
Math.pow is deprecated
C
Math.pow cannot handle integers
D
Math.pow changes the prototype
38

Question 38

What is a potential issue with using Math.random inside a hot loop?

A
It can become a performance bottleneck when called millions of times
B
It always throws exceptions
C
It modifies global state
D
It produces negative values
39

Question 39

Which approach keeps rounding consistent across different locales?

A
Rely on Math methods rather than string-based rounding
B
Use toLocaleString without options
C
Use parseInt on strings
D
Rely on implicit type coercion
40

Question 40

What is Math.imul useful for?

A
Performing 32-bit integer multiplication with correct overflow semantics
B
Generating random numbers
C
Calculating logs
D
Formatting decimals
41

Question 41

Why might Math.hypot be preferred for Euclidean distances?

A
It avoids overflow/underflow by scaling values internally
B
It is faster than addition
C
It returns angles
D
It only works for integers
42

Question 42

What does Math.expm1(x) compute?

A
Math.exp(x) - 1 with better precision for small x
B
Natural log of x
C
1 / Math.exp(x)
D
Rounding to nearest integer
43

Question 43

What is Math.clz32 used for?

A
Counting leading zero bits in a 32-bit integer
B
Clamping values between 0 and 32
C
Generating random bits
D
Rounding decimals
44

Question 44

What benefit does Math.log2 provide over Math.log?

A
Direct base-2 logarithms without manual conversions
B
It is faster for all computations
C
It returns degrees
D
It rounds to integers
45

Question 45

Why should you avoid Math.pow for simple squaring in tight loops?

A
Multiplying x * x is often faster and easier to read
B
Math.pow returns incorrect values
C
Math.pow only works for integers
D
Math.pow is deprecated
46

Question 46

What is a potential drawback of repeatedly calling Math.random within Array.sort comparator?

A
It produces biased results and is not a reliable shuffle
B
It sorts faster
C
It always throws an exception
D
It reverses the array
47

Question 47

Which Math method helps calculate the hypotenuse of a right triangle?

javascript
Math.hypot(3, 4)
A
Returns 5, the hypotenuse length
B
Returns 7
C
Throws error
D
Returns 12
48

Question 48

What is Math.random() * Number.MAX_SAFE_INTEGER often used for?

A
Generating a large pseudo-random integer (though not cryptographically secure)
B
Generating cryptographically secure IDs
C
Measuring performance
D
Converting numbers to strings
49

Question 49

Why might you replace Math.random with crypto.getRandomValues in some cases?

A
crypto.getRandomValues provides cryptographically secure random numbers
B
crypto is faster
C
Math.random is deprecated
D
Math.random cannot run in workers
50

Question 50

What practice can reduce overhead when calling Math methods frequently?

A
Destructure required methods once (const { sin, cos } = Math) and reuse references
B
Eval Math expressions
C
Disable strict mode
D
Wrap Math in proxies

QUIZZES IN JavaScript