Java MCQs on rounding functions in Java Programming Language.
1. Which of these class provides various types of rounding functions? Answer: a 2. Which of these methods return a smallest whole number greater than or equal to variable X? Answer: a 3. Which of these method returns a largest whole number less than or equal to variable X? Answer: b 4. Which of function return absolute value of a variable? Answer: a 5. What will be the output of the following Java code? a) 1 2 0 0 6. What will be the output of the following Java code? a) 0 7. What will be the output of the following Java code? a) 0 8. What will be the output of the following Java code? a) 0
a) Math
b) Process
c) System
d) Object
Clarification: None.
a) double ceil(double X)
b) double floor(double X)
c) double max(double X)
d) double min(double X)
Clarification: ceil(double X) returns the smallest whole number greater than or equal to variable X.
a) double ceil(double X)
b) double floor(double X)
c) double max(double X)
d) double min(double X)
Clarification: double floor(double X) returns a largest whole number less than or equal to variable X.
a) abs()
b) absolute()
c) absolutevariable()
d) none of the mentioned
Clarification: abs() returns the absolute value of a variable.
class A
{
int x;
int y;
void display()
{
System.out.print(x + " " + y);
}
}
class Output
{
public static void main(String args[])
{
A obj1 = new A();
A obj2 = new A();
obj1.x = 1;
obj1.y = 2;
obj2 = obj1.clone();
obj1.display();
obj2.display();
}
}
b) 1 2 1 2
c) 0 0 0 0
d) System Dependent
Clarification: clone() method of object class is used to generate duplicate copy of the object on which it is called. Copy of obj1 is generated and stored in obj2.
Output:
$ javac Output.java
$ java Output
1 2 1 2
class Output
{
public static void main(String args[])
{
double x = 3.14;
int y = (int) Math.abs(x);
System.out.print(y);
}
}
b) 3
c) 3.0
d) 3.1
Clarification: None.
Output:
$ javac Output.java
$ java Output
3
class Output
{
public static void main(String args[])
{
double x = 3.14;
int y = (int) Math.ceil(x);
System.out.print(y);
}
}
b) 3
c) 3.0
d) 4
Clarification: ciel(double X) returns the smallest whole number greater than or equal to variable x.
Output:
$ javac Output.java
$ java Output
4
class Output
{
public static void main(String args[])
{
double x = 3.14;
int y = (int) Math.floor(x);
System.out.print(y);
}
}
b) 3
c) 3.0
d) 4
Clarification: double floor(double X) returns a largest whole number less than or equal to variable X. Here the smallest whole number less than 3.14 is 3.
Output:
$ javac Output.java
$ java Output
3