Java Numbers & Math Class Quiz

Java
0 Passed
0% acceptance

35 deep-dive questions on Java's numerical capabilities, from Math utilities and rounding behavior to BigInteger/BigDecimal precision, number formatting, and the subtle dangers of primitive overflow.

35 Questions
~70 minutes
1

Question 1

What is the output of Math.abs(Integer.MIN_VALUE)?

A
Negative value (overflow)
B
2147483648
C
0
D
Throws ArithmeticException
2

Question 2

What is the result?

java
System.out.println(0.1 + 0.2 == 0.3);
A
false
B
true
C
Compilation error
D
Runtime exception
3

Question 3

Which Math method rounds 3.6 to the nearest integer toward positive infinity?

A
Math.ceil(3.6)
B
Math.round(3.6)
C
Math.floor(3.6)
D
Math.rint(3.6)
4

Question 4

What is printed?

java
BigDecimal a = new BigDecimal("0.1");
BigDecimal b = new BigDecimal("0.2");
System.out.println(a.add(b));
A
0.3
B
0.3000000000000000166533453694
C
0.1
D
Compilation error
5

Question 5

What is the output?

java
System.out.println(Math.floor(-3.2));
A
-4.0
B
-3.0
C
-3.2
D
-3
6

Question 6

Which class should be used for exact decimal arithmetic (e.g., money)?

A
BigDecimal
B
double
C
float
D
long
7

Question 7

What is the result?

java
long x = Integer.MAX_VALUE + 1L;
System.out.println(x);
A
2147483648
B
-2147483648
C
2147483647
D
Compilation error
8

Question 8

What is printed?

java
System.out.println(Math.pow(2, 3));
A
8.0
B
8
C
9.0
D
Compilation error
9

Question 9

What is the output?

java
BigInteger a = BigInteger.valueOf(1000);
BigInteger b = a.multiply(BigInteger.TEN);
System.out.println(b);
A
10000
B
1000
C
10000.0
D
Exception
10

Question 10

Which method returns a pseudorandom double greater than or equal to 0.0 and less than 1.0?

A
Math.random()
B
new Random().nextDouble()
C
Both A and B
D
Neither
11

Question 11

What is the result?

java
int x = 1000000;
int y = 1000000;
int z = x * y;
System.out.println(z);
A
Negative or unexpected value due to overflow
B
1000000000000
C
1410065408
D
Compilation error
12

Question 12

What is printed?

java
System.out.printf("%.2f", 123.45678);
A
123.46
B
123.45
C
123.457
D
123.45678
13

Question 13

Which of these is the safest way to detect potential overflow before multiplication?

A
Math.multiplyExact(a, b)
B
a * b (and hope)
C
(long)a * b
D
BigInteger
14

Question 14

What is the output?

java
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println(nf.format(1234.56));
A
Depends on default locale (e.g., $1,234.56 or €1.234,56)
B
1234.56
C
$1234.56
D
Compilation error
15

Question 15

What is the result?

java
BigDecimal value = BigDecimal.valueOf(0.1);
value = value.add(BigDecimal.valueOf(0.2));
System.out.println(value);
A
0.3
B
0.300000000000000016653345369377
C
0.1
D
Compilation error
16

Question 16

What does Math.nextUp(0.0) return?

A
The smallest positive double greater than 0.0
B
1.0
C
Double.MIN_VALUE
D
0.0
17

Question 17

What is printed?

java
System.out.println(Math.round(3.5));
System.out.println(Math.round(-3.5));
A
4\n-4
B
4\n-3
C
3\n-3
D
4\n-3
18

Question 18

Which pattern formats 1234.567 as '1,234.57'?

A
#,##0.##
B
0,000.00
C
#,###.##
D
Both A and C
19

Question 19

What is the safest way to convert String to BigDecimal?

A
new BigDecimal(string)
B
BigDecimal.valueOf(Double.parseDouble(string))
C
new BigDecimal(Double.parseDouble(string))
D
DecimalFormat.parse()
20

Question 20

What is printed?

java
Random r = new Random(12345);
System.out.println(r.nextInt(10) == r.nextInt(10));
A
Probably false (depends on seed)
B
Always true
C
Always false
D
Compilation error
21

Question 21

Which of these values can be exactly represented in double?

A
0.5
B
0.1
C
0.3
D
1.0000000000000001
22

Question 22

What is the result of Math.addExact on overflow?

A
Throws ArithmeticException
B
Wraps around silently
C
Returns Long.MAX_VALUE
D
Returns null
23

Question 23

What is printed?

java
DecimalFormat df = new DecimalFormat("0000.00");
System.out.println(df.format(123.4));
A
0123.40
B
123.40
C
0123.4
D
123.4
24

Question 24

Why is 2,147,483,647 + 1 equal to -2,147,483,648 in int arithmetic?

A
Two’s complement wraparound overflow
B
Compiler optimization
C
Floating-point conversion
D
Bug in JVM
25

Question 25

What is the output?

java
System.out.println(Math.min(-0.0, 0.0) == -0.0);
A
true
B
false
C
Compilation error
D
NaN
26

Question 26

Which method should be used to generate cryptographically secure random numbers?

A
SecureRandom
B
Math.random()
C
Random
D
ThreadLocalRandom
27

Question 27

What is printed?

java
BigDecimal bd = new BigDecimal("10.0");
bd = bd.stripTrailingZeros();
System.out.println(bd.toPlainString());
A
10
B
10.0
C
10.00
D
1E+1
28

Question 28

What is the result of Integer.parseInt("123456789012345")?

A
NumberFormatException
B
123456789012345
C
Silent truncation
D
Compilation error
29

Question 29

What is printed?

java
System.out.println(Math.toDegrees(Math.PI));
A
180.0
B
3.141592653589793
C
180
D
Approximately 180
30

Question 30

Which of these is NOT a method in java.lang.Math?

A
Math.sqrt()
B
Math.log10()
C
Math.average()
D
Math.sin()
31

Question 31

What is the most important lesson about numeric computation in Java?

A
Choose the right type for the problem — precision, range, and performance all matter
B
Always use double
C
Overflow never happens
D
BigDecimal is always slower so avoid it
32

Question 32

What is printed?

java
double d = 1.0 / 0.0;
System.out.println(d);
A
Infinity
B
NaN
C
ArithmeticException
D
0.0
33

Question 33

What is the result?

java
System.out.println(Double.isNaN(Math.sqrt(-1)));
A
true
B
false
C
Exception
D
0.0
34

Question 34

Why is ThreadLocalRandom preferred in multithreaded applications?

A
Better performance — avoids contention compared to shared Random
B
More secure
C
Larger range
D
Deterministic
35

Question 35

After completing this quiz, you now understand that:

A
Java provides multiple numeric types because no single type fits every use case perfectly
B
double is always safe
C
Overflow throws exceptions by default
D
All decimal numbers are exactly representable

QUIZZES IN Java