• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar

JavaTutorOnline

1-on-1 Online Java Training by a Senior Software Engineer

  • Home
  • AP CSA
    • FRQ Practice
      • Arrays
      • ArrayList
      • Strings
      • 2D Arrays
    • MCQ Practice
      • Booleans
      • Iteration
      • Arrays
      • View all Units
  • Courses
  • Tutorials
    • Java
    • Servlets
    • Struts
    • Spring
    • Webservice
  • FAQ
  • Testimonials
  • Blog
  • CONTACT US

AP CSA Unit 1.11 MCQ Practice: Math Class

Home » AP CSA MCQ Practice » Unit 1: Using Objects and Methods

AP CSA Unit 1.11 MCQ Practice: Math Class

Topic 1.11 ⏱️ 10 Min Practice ✅ Current AP CSA Framework

The Math class in Java provides built-in static methods to perform common mathematical operations. For AP Computer Science A, the current Java Quick Reference includes the following Math methods: Math.abs(int), Math.abs(double), Math.pow(double, double), Math.sqrt(double), and Math.random().

Key Concepts to Remember Before You Start:

  • Return Types: Math.pow(), Math.sqrt(), and Math.random() always return a double. Even if you input integers, the result is a double.
  • Math.random() Range: This method generates a double greater than or equal to 0.0 and strictly less than 1.0.
  • The Casting Trap: In (int) Math.random() * 10, the cast applies to Math.random() before the multiplication, so the cast produces 0. Use parentheses around the multiplication: (int)(Math.random() * 10).

Test your knowledge with these 10 practice questions. Click an answer to see the explanation and understand the reasoning behind each result.

Topic 1.11 Practice

Question 1 of 10 | Score: 0
1. What is the guaranteed range of values returned by the Math.random() method?
Correct Answer: BMath.random() always returns a double that includes 0.0 but excludes 1.0. Mathematically, this is represented as [0.0, 1.0). It will never return exactly 1.0.
2. What is output by the following code?
System.out.println(Math.pow(3, 2));
Correct Answer: CMath.pow(base, exponent) calculates 3 to the power of 2, which is 9. Remember that Math.pow() always returns a double, even if both arguments are integers. Therefore, it prints 9.0.
3. Which of the following code segments simulates rolling a standard 6-sided die (returning a random integer from 1 to 6 inclusive)?
Correct Answer: CThe standard formula is (int)(Math.random() * totalOutcomes) + min. A die has 6 outcomes, starting at 1.Math.random() * 6 generates values from 0.0 inclusive to less than 6.0. Casting to (int) truncates the fractional part, leaving integers 0 through 5. Adding 1 shifts the range to 1 through 6.
4. Consider the following code segment. What will occur when it runs?
int result = (int) Math.random() * 10;
System.out.println(result);
Correct Answer: AThis is a common casting trap. Java casts the result of Math.random() to an int before the multiplication, truncating the value to 0. Then it multiplies 0 by 10. The result is always 0.You need parentheses: (int)(Math.random() * 10).
5. What is printed as a result of executing the following code segment?
System.out.println(Math.abs(Math.pow(-2, 3)));
Correct Answer: BEvaluate from the inside out.Math.pow(-2, 3) calculates (-2)³, which is -8.0 because Math.pow() returns a double.Then Math.abs(-8.0) takes the absolute value, resulting in 8.0.
6. Which of the following code segments will generate a random integer from 5 to 15, inclusive?
Correct Answer: CA standard formula to generate an integer between min and max, inclusive, is:

(int)(Math.random() * ((max - min) + 1)) + min

To generate values from 5 through 15:(15 - 5) + 1 = 11possible values.Therefore:(int)(Math.random() * 11) + 5.
7. What is the maximum possible value that can be assigned to the variable num in the following code?
int num = (int)(Math.random() * 4) + 2;
Correct Answer: BMath.random() * 4 generates a random decimal from 0.0 up to, but not including, 4.0.Casting to (int) truncates the decimal, leaving integers {0, 1, 2, 3}.Adding 2 produces:{2, 3, 4, 5}Therefore, the maximum possible value is 5.
8. What is printed as a result of executing the following code segment?
System.out.println(Math.sqrt(25));
Correct Answer: BMath.sqrt() calculates the square root of a number. Like Math.pow(), it returns a double.It accepts the integer 25, calculates its square root, and returns 5.0.
9. You want to randomly select an index from an array named arr. Which of the following correctly generates a valid random index?
Correct Answer: CArray indices go from 0 to arr.length - 1.Math.random() * arr.length produces a decimal from 0.0 up to, but strictly less than, the length of the array.When cast to an int, the decimal is truncated, resulting in the range 0 to length - 1.
10. What is output by the following code segment?
int a = Math.abs(-10);
double b = Math.abs(-10.5);
System.out.println(a + ” ” + b);
Correct Answer: BUnlike Math.pow() and Math.sqrt(), the Math.abs() method is overloaded.If you pass it an integer, it returns an integer. If you pass it a double, it returns a double.Therefore, a becomes the integer 10 and b becomes the double 10.5.

Topic 1.11 Practice Complete!

0 / 10

The Math.random() Formula

If you struggled with the random-number questions, use this formula to practice generating an integer in an inclusive range:

int x = (int)(Math.random() * ((MAX - MIN) + 1)) + MIN;

Continue Your AP CSA Practice

After practicing the Math class, continue your AP CSA Unit 1 review with the related object-reference concepts.

Continue your AP CSA Unit 1 review with Topics 1.12–1.14: Objects, References & Instance Methods , covering object creation, references, and instance-method calls.

← Back to Unit 1: Using Objects & Methods AP CSA MCQ Practice Hub
Next: Object References & Instance Methods →

Primary Sidebar

Mr Chinmay

Chinmay Patel
Book a Demo Class

Phone & Whatsapp +919853166385
javatution@gmail.com

Recent Posts

  • Learn Java in One Day: 10-Hour 1-on-1 Crash Course | JavaTutorOnline
  • Constructor in Java: Examples, Types & Constructor Overloading
  • Important Interview Questions on Java Multithreading
  • React Spring Boot Web Services React Integration
  • Spring Boot RESTful Web Services Example
  • Top Spring MVC Interview Questions and Answers for Developers
  • Top Spring Core Interview Questions and Answers for Developers
  • Host Java Web Apps for Free on Mobile with Tomcat and Termux
  • How to Deploy Java Web Application on Aws EC2 with Elastic IP
  • Simple Jsp Servlet Jdbc User Registration using Tomcat Mysql and Eclipse

Additional Resources

  • AP Computer Science A Summer Prep
Copyright © 2026 JavaTutorOnline