• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
[email protected]
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Java

Crack Java Math Problems

  • Categories Java, Operators, Math Library
Crack Java Math Problems
Before we start attempting the questions on Java Math class, lets do a quick revision on Java libraries and Math class in particular.

Click here -> Java Class Libraries

Lets Start!

  1. In which package is math class located in Java?
Ans: Java.lang package.
  1. What is the return type of following Java math functions
    1. round(3.14)
    2. pow()
    3. rint()
    4. sqrt()
Ans:
    1. long
    2. double
    3. double
    4. double
  1. What is the return type of following Java math functions
    1. sin()
    2. ceil()
    3. round((float)x);
    4. exp()
Ans:
    1. double
    2. double
    3. int
    4. double
  1. Answer the questions
    1. What is the range of value returned by random() function
    2. What is the return type of random() function.
Ans:
    1. greater than or equal to 0.0 and less than 1.0  or  random no >= 0 and random no < 1.
    2. double
  1. Answer the questions
    1. Write a Java statement to generate a random number from 1 – 10.
    2. Write a Java statement to generate a random number to simulate a dice.
Ans:
    1. (int)(Math.random() * 10) + 1;
    2. (int)(Math.random() * 6) + 1;
  1. Give the output of the following:

(i) Math.floor(-4.7) (ii) Math.ceil(3.4) + Math.pow(2, 3)

Ans.

i) -5.0 ii) 12.0     Explanation: 4.0 + 8.0

  1. What are the values stored in variables r1 and r2:
    1. double r1 = Math.abs(Math.min(-2.83,-5.83));
    2. double r2 = Math.sqrt(Math.floor(16.3));
Ans.
    1. double r1 = Math.abs(Math.min(-2.83,-5.83)); = Math.abs(-5.83) = 5.83
    1. double r2 = Math.sqrt(Math.floor(16.3)); = Math.sqrt(16) = 4.0
  1. What will be the output of the following function?

i) Math.pow(9,0.5)               ii) Math.min(-21,-12) iii) Math.ceil(5.2)                 iv) Math.floor(2.9)

Ans   i) 3.0             ii) -21               iii) 6.0                  iv) 2.0
  1. Give the output of the following Math functions:

(i) Math.ceil(4.2) (ii) Math.abs(-4)

Ans.

(i) 5.0 (and not 5, as ceil() returns a double) (ii) 4

  1. Give the result of the following functions:
    1. Math.floor(70.3)
    2. Math.ceil(-10.01)
    3. Math.sqrt(36)
    4. Math.abs(-9)
Ans.
    1. 70.0
    2. -10.0
    3. 6.0
    4. 9
  1. What is the value stored in x and y after execution of these statements?

double x = Math.ceil(8.3) + Math.floor(-3.2)+Math.rint(4.5); double y = Math.abs(Math.round(Math.max( -4.4, -9.6)));

Ans.

X = 9.0 Working: 9.0 + (-4.0) + 4.0 = 9.0 Y = 4.0 Working Math.abs(Math.round(-4.4)) = 4.0

  1. What is the output of following statements?

double d1 = -0.5d; System.out.println(“Ceil for dl= ” + Math.ceil( dl ); System.out.println(“Floor for dl= ” +Math.floor (d1 ));

Ans:

Ceil for d1= -0.0 Floor for d1= -1.0;

  1. What are the final values stored in variables x and y below?

double a = – 6.35; double b = 14.74; double x = Math.abs(Math.ceil(a)); double y = Math.rint(Math.max(a,b));

Ans.

x=6.0 and y=15.0

  1. What are the values stored in variables r1 and r2:

(i) double r1 = Math.abs(Math.min(-2.83,-5.83)); (ii) double r2 = Math.sqrt(Math.floor(16.3));

Ans.
    1. (i) r1=5.83 (ii) r2=4.0
  1. Give the output of the following:

a) Math.floor (-4.7) b) Math.ceil(3.4) + Math.pow(2, 3)

Ans.
    1. a) -5.0 b) 12.0
  1. Give the results of the following functions:

(i) Math.floor (81.2)

(ii) Math.ceil (-10.01)

(iii) Math.sqrt (25)

(iv) Math.cbrt (-27.0)

Ans:

(i)81.0       (ii) -10.0       (iii) 5.0        (iv) -3.0

  1. Give the output of the following:

i. Math.ceil(4.2) ii. Math.abs(-4)

Ans.

i. 5.0 ii. 4

  1. Give the output of the following:
    1. Math.max(-17, -19)
    2. Math.ceil(7.8)
Ans.
    1. -17
    2. 0
  1. Give the output of the following:
    1. Math.floor(15.36)
    2. Math.round(146.5)
Ans.
    1. 15.0
    2. 147
  1. Give the output of the following:
    1. Math.max(11,10)
    2. Math.ceil(-12.56)
Ans.
    1. 11
    2. -12.0
  1. Give the output of the following:
    1. Math.sqrt(625)
    2. Math.cbrt(125)
Ans.
    1. 25.0
    2. 5.0
  1. Give the output of the following:
    1. Math.min(-0.0,0.0)
    2. Math.pow(4,3)
Ans.
    1. -0.0
    2. 64.0
  1. Give the output of the following:
    1. Math.floor(-126.349)
    2. Math.max(45.6,17.3)
Ans.
    1. -127.0
    2. 45.6
  1. What are the final values stored in variables x and y below?

double a = – 6.35; double b = 14.74; double x = Math.abs(Math.ceil(a)); double y = Math.rint(Math.max(a,b));

Ans.

Math.ceil() gives the smallest of all those integers that are greater than the input. So, Math.ceil(-6.35) gives -6.0 (not -6 as ceil returns a double value). Math.abs() gives the absolute value of the number i.e. removes negative sign, if present. So, Math.abs(-6.0) results in 6.0. So, x = 6.0.

Math.max(-6.35, 14.74) returns 14.74. rint() returns the double value which is closest in value to the argument passed and is equal to an integer. So, Math.rint(14.74) returns 15.0. So, y = 15.0.

  1. Name the type of error (syntax, runtime or logical error) in each case given below:

(i) c = Math.sqrt (36-45);

(ii) int a;b;c; 

Ans.

(i) Runtime error

Math.sqrt(36-45) = Math.sqrt(-9) which cannot be computed as square root of a negative number is not defined. Therefore, a runtime error will be thrown.

(ii) Syntax error

Multiple variables can be defined in one of the following ways int a, b, c; int a; int b; int c;

  1. Name the type of error (syntax, runtime or logical error) in each case given below:

(i) a = 5 + (Math.random(10) * 2);

(ii)    int a= 5/6-3*2;          int b= 15/4+3*2-9;         System.out.println(a/b);

Ans.
  1. Syntax error

Math.random() is non-parameterise function. Correct way for Math.random() is as following:

a = 5 + (Math.random() * 2);

  1. logical error

(a= 15, b=0; a/b=> as any number divided by zero is logical error.)

  • Share:
author avatar
Simply Coding

Previous post

Java print/println exam questions
June 7, 2020

Next post

Java Operators and Operator precedence questions
June 10, 2020

You may also like

Java Operator
Java Operators
16 June, 2021
Java Class Librares
Java Class Libraries and Packages
16 June, 2021
Input-Output in Java
Input/Output in Java
17 January, 2021

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now