Вы находитесь на странице: 1из 13

Math

Question 1
class SRC102 {
public static void main (String[] args) {
int i1 = Math.round(0.5f);
int i2 = Math.round(1.5d);
System.out.print(i1 + "," + i2);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 0,1


b.  Prints: 0,2
c.  Prints: 1,1
d.  Prints: 1,2
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
There are two versions of the Math.round method. One accepts an
argument of type float; the return type is int. The other accepts an
Compile-
1 e  argument of type double; the return type is long. A compile-time
time error 
error is generated here due to the attempt to assign the long return
value to the int variable, i2.  

Question 2
class SRC103 {
public static void main (String[] args) {
int i1 = Math.round(0.5f);
int i2 = Math.round(1.5f);
System.out.print(i1 + "," + i2);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 0,1


b.  Prints: 0,2
c.  Prints: 1,1
d.  Prints: 1,2
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
The Math.round method returns the floor of the argument value plus 0.5.
Prints:
2 d  If the argument is of type float, then the return type is int. If the
1,2 
argument is of type double, then the return type is long.  

Question 3
class SRC104 {
public static void main (String[] args) {
System.out.print(Math.round(Float.NaN));
}}

What is the result of attempting to compile and run the program?

a.  Prints: NaN


b.  Prints: 0.0
c.  Prints: 0
d.  Compile-time error
e.  Run-time error
f.  None of the above

ANSWER
If NaN is the argument passed to Math.round, then the return value is
Prints:
3 c  zero. If the argument type is float, then the return type is int. If the

argument type is double, then the return type is long.  

Question 4
class SRC105 {
public static void main(String[] args) {
double d1 = Math.ceil(0.5);
double d2 = Math.ceil(1.5);
System.out.print(d1 + "," + d2);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 0.0,1.0


b.  Prints: 0.0,2.0
c.  Prints: 1.0,1.0
d.  Prints: 1.0,2.0
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
The Math.ceil method name is not overloaded. The Math.ceil method
Prints: accepts an argument of type double and returns a double. The returned
4 d 
1.0,2.0  value is the smallest whole number that is greater than or equal to the
argument.  

Question 5
class SRC106 {
public static void main(String[] args) {
double d1 = Math.floor(0.5);
double d2 = Math.floor(1.5);
System.out.print(d1 + "," + d2);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 0.0,1.0


b.  Prints: 0.0,2.0
c.  Prints: 1.0,1.0
d.  Prints: 1.0,2.0
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
The Math.floor method name is not overloaded. The Math.floor
Prints: method accepts an argument of type double and returns a double. The
5 a 
0.0,1.0  returned value is the largest whole number that is smaller than or equal
to the argument.  

Question 6
class SRC107 {
public static void main (String[] args) {
int a = -1; long b = -2;
float c = -3.0f; double d = -4.0;
a = Math.abs(a); b = Math.abs(b);
c = Math.abs(c); d = Math.abs(d);
System.out.print(a+b+c+d);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 10.0


b.  Compile-time error
c.  Run-time error
d.  None of the above

ANSWER
The Math class declares four versions of the abs method; each declares
one parameter. The parameter can be of type int, long, float or double.
Prints: The return type is the same as the argument type. At run-time, the
6 a 
10.0  argument might not match the parameter type; so the argument might
require an implicit conversion to an acceptable type. If the argument is of
type byte, short or char, then it will be promoted to type int.  

Question 7
class SRC109 {
public static void main (String[] args) {
short x3 = 0; short x4 = 1;
short b1 = Math.min(x3,x4); // 1
int c1 = Math.min(0,1); // 2
long d1 = Math.min(0L,+1L); // 3
System.out.print(b1+","+c1+","+d1);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 0,0,0


b.  Compile-time error at 1
c.  Compile-time error at 2
d.  Compile-time error at 3
e.  Run-time error
f.  None of the above

ANSWER
7 b  Compile- At line 1, the invocation of the Math.min method produces a return
time error at value of type int. The variable b1 is of type short; so the assignment
expression results in a compile-time error. The Math class declares
four versions of the min method; each declares a pair of parameters
of the same type. The parameter pair can be of type int, long, float
or double. The return type is the same as the argument types. At
run-time, the arguments might not match the declared parameter
types; so one argument or both might require an implicit conversion

to an acceptable type. If both arguments are of type byte, short or
char, then both will be promoted to type int. If only one argument is
of type byte, short or char, then it will be promoted to the type of the
other argument. If both arguments are of type int, long, float or
double but the types differ, then a primitive widening conversion
will be applied to one of the two arguments.  

Question 8
class SRC110 {
public static void main (String[] args) {
byte x1 = 0; byte x2 = 1;
byte a1 = Math.min(x1,x2); // 1
int c1 = Math.min(0,1); // 2
long d1 = Math.min(0L,+1L); // 3
System.out.print(a1+","+c1+","+d1);
}}

What is the result of attempting to compile and run the program?

a.  Prints: 0,0,0


b.  Compile-time error at 1
c.  Compile-time error at 2
d.  Compile-time error at 3
e.  Run-time error
f.  None of the above

ANSWER
8 b  Compile- At line 1, the invocation of the Math.min method produces a return
time error at value of type int. The variable a1 is of type byte; so the assignment
1  expression results in a compile-time error. The Math class declares
four versions of the min method; each declares a pair of parameters
of the same type. The parameter pair can be of type int, long, float
or double. The return type is the same as the argument types. At
run-time, the arguments might not match the declared parameter
types; so one argument or both might require an implicit conversion
to an acceptable type. If both arguments are of type byte, short or
char, then both will be promoted to type int. If only one argument is
of type byte, short or char, then it will be promoted to the type of the
other argument. If both arguments are of type int, long, float or
double but the types differ, then a primitive widening conversion
will be applied to one of the two arguments.  

Question 9
class SRC111 {
static String m(byte i) {return "byte";}
static String m(short i) {return "short";}
static String m(char i) {return "char";}
static String m(int i) {return "int";}
static String m(double i) {return "double";}
public static void main (String[] args) {
byte b = 0; short s = 0; char c = 0;
System.out.print(m(Math.min(b,b))+",");
System.out.print(m(Math.min(s,s))+",");
System.out.print(m(Math.min(c,c)));
}}

What is the result of attempting to compile and run the program?

a.  Prints: byte,byte,byte


b.  Prints: short,short,short
c.  Prints: int,int,int
d.  Prints: byte,short,int
e.  Prints: short,short,int
f.  Prints: byte,short,char
g.  Compile-time error
h.  Run-time error
i.  None of the above

ANSWER
The Math class declares four versions of the min method; each
declares a pair of parameters of the same type. The parameter pair
can be of type int, long, float or double. The return type is the same
as the argument types. At run-time, the arguments might not match
the declared parameter types; so one argument or both might require
Prints:
9 c  an implicit conversion to an acceptable type. If both arguments are of
int,int,int 
type byte, short or char, then both will be promoted to type int. If
only one argument is of type byte, short or char, then it will be
promoted to the type of the other argument. If both arguments are of
type int, long, float or double but the types differ, then a primitive
widening conversion will be applied to one of the two arguments.  
Question 10
class SRC112 {
static String m(byte i) {return "byte";}
static String m(int i) {return "int";}
static String m(long i) {return "long";}
static String m(double i) {return "double";}
public static void main (String[] args) {
byte b = 0;
System.out.print(m(Math.min(b,b))+",");
System.out.print(m(Math.min(b,1))+",");
System.out.print(m(Math.min(b,1L)));
}}

What is the result of attempting to compile and run the program?

a.  Prints: byte,byte,byte


b.  Prints: byte,int,long
c.  Prints: int,int,int
d.  Prints: int,int,long
e.  Prints: long,long,long
f.  Compile-time error
g.  Run-time error
h.  None of the above

ANSWER
The Math class declares four versions of the min method; each
declares a pair of parameters of the same type. The parameter pair
can be of type int, long, float or double. The return type is the
same as the argument types. At run-time, the arguments might not
match the declared parameter types; so one argument or both
Prints: might require an implicit conversion to an acceptable type. If both
10 d 
int,int,long  arguments are of type byte, short or char, then both will be
promoted to type int. If only one argument is of type byte, short or
char, then it will be promoted to the type of the other argument. If
both arguments are of type int, long, float or double but the types
differ, then a primitive widening conversion will be applied to one
of the two arguments.  

Question 11
class SRC113 {
static String m(byte i) {return "byte";}
static String m(short i) {return "short";}
static String m(char i) {return "char";}
static String m(int i) {return "int";}
static String m(long i) {return "long";}
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
byte b = 0; short s = 0; char c = 0;
System.out.print(m(Math.min(b,1L))+",");
System.out.print(m(Math.min(s,1.0f))+",");
System.out.print(m(Math.min(c,1.0)));
}}

What is the result of attempting to compile and run the program?

a.  Prints: int,int,int


b.  Prints: byte,short,char
c.  Prints: long,float,double
d.  Prints: double,double,double
e.  Prints: long,long,long
f.  Compile-time error
g.  Run-time error
h.  None of the above

ANSWER
The Math class declares four versions of the min method;
each declares a pair of parameters of the same type. The
parameter pair can be of type int, long, float or double. The
return type is the same as the argument types. At run-time,
the arguments might not match the declared parameter
types; so one argument or both might require an implicit
Prints:
11 c  conversion to an acceptable type. If both arguments are of
long,float,double 
type byte, short or char, then both will be promoted to type
int. If only one argument is of type byte, short or char, then
it will be promoted to the type of the other argument. If both
arguments are of type int, long, float or double but the types
differ, then a primitive widening conversion will be applied
to one of the two arguments.  

Question 12
class SRC114 {
static String m(int i) {return "int";}
static String m(long i) {return "long";}
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
System.out.print(m(Math.random()));
}}

What is the result of attempting to compile and run the program?

a.  Prints: int


b.  Prints: long
c.  Prints: float
d.  Prints: double
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
Prints: The Math.random method returns a value of type double. The range
12 d 
double  of values is greater than or equal to zero and less than one.  

Question 13
class SRC115 {
static String m(int i) {return "int";}
static String m(long i) {return "long";}
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
long seed = 1L;
System.out.print(m(Math.random(seed)));
}}

What is the result of attempting to compile and run the program?

a.  Prints: int


b.  Prints: long
c.  Prints: float
d.  Prints: double
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
Compile-time The Math.random method does not accept a seed value. If your
13 e 
error  application requires a seed, then use java.util.Random.  
Question 14
class SRC116 {
static String m(int i) {return "int";}
static String m(long i) {return "long";}
static String m(float i) {return "float";}
static String m(double i) {return "double";}
public static void main (String[] args) {
System.out.print(m(Math.sin(0.0f))+",");
System.out.print(m(Math.cos(0.0f))+",");
System.out.print(m(Math.tan(0.0f)));
}}

What is the result of attempting to compile and run the program?

a.  Prints: int,int,int


b.  Prints: long,long,long
c.  Prints: float,float,float
d.  Prints: double,double,double
e.  Compile-time error
f.  Run-time error
g.  None of the above

ANSWER
Prints: The Math.sin, Math.cos and Math.tan methods
14 d 
double,double,double  return a value of type double.  

Question 15
class SRC117 {
public static void main (String[] args) {
double d1 = Math.random();
boolean b1 = (d1 < 0.0), b2 = (d1 <= 0.0), b3 = (d1 == 0.0);
boolean b4 = (d1 >= 0.0), b5 = (d1 < 1.0), b6 = (d1 <= 1.0);
boolean b7 = (d1 == 1.0), b8 = (d1 >= 1.0), b9 = (d1 > 1.0);
}}

Which of the boolean variables will never be initialized to the value true?

a.  b1
b.  b2
c.  b3
d.  b4
e.  b5
f.  b6
g.  b7
h.  b8
i.  b9

ANSWER
a  g  b1  b7  b8  The Math.random method returns a value that is equal to or
15
h  i  b9  greater than zero and less than 1.0.  

Question 16
Which method of the java.lang.Math class is not static?

a.  abs
b.  ceil
c.  floor
d.  max
e.  min
f.  random
g.  round
h.  sin
i.  cos
j.  tan
k.  sqrt
l.  None of the above

ANSWER
16 l  None of the above  All methods of the java.lang.Math class are static.  

Question 17
Some of the following method names are overloaded and some are not. Which of the
following method names are overloaded such that for each of the following four primitive
types int, long, float and double there is at least one corresponding method that returns
that type?

a.  abs
b.  ceil
c.  floor
d.  max
e.  min
f.  random
g.  round
h.  sin
i.  cos
j.  tan
k.  sqrt

ANSWER
17 a  d  e  abs  max  min 

Question 18
class SRC118 {
static String m1(int i) {return "I";}
static String m1(long i) {return "L";}
static String m1(float i) {return "F";}
static String m1(double i) {return "D";}
public static void main (String[] args) {
System.out.print(m1(Math.abs(1.0)) + m1(Math.ceil(1.0f)));
System.out.print(m1(Math.max(1.0,1.0)) + m1(Math.round(1.0)));
System.out.print(m1(Math.sin(1.0)) + m1(Math.sqrt(1.0)));
}}

What is the result of attempting to compile and run the program?

a.  Prints: DDDDDD


b.  Prints: LLLLLL
c.  Prints: DLLLDD
d.  Prints: DIDLDD
e.  Prints: DLDLDD
f.  Prints: DDDLDD
g.  Prints: DIDIDD
h.  None of the above

ANSWER
The round method does not return either of the two floating point
primitive types, float or double. The ceil, sin and sqrt methods
Prints:
18 f  return only a value of type double. The abs and max methods are
DDDLDD 
able to return an int, long, float or double depending on the
argument type.  

Question 19
class SRC119 {
static String m1(int i) {return "I";}
static String m1(long i) {return "L";}
static String m1(float i) {return "F";}
static String m1(double i) {return "D";}
public static void main (String[] args) {
System.out.print(m1(Math.floor(1.0f)) + m1(Math.floor(1.0d)));
System.out.print(m1(Math.ceil(1.0f)) + m1(Math.ceil(1.0d)));
}}

What is the result of attempting to compile and run the program?

a.  Prints: IIII


b.  Prints: ILIL
c.  Prints: LLLL
d.  Prints: FDFD
e.  Prints: DDDD
f.  None of the above

ANSWER
The floor and ceil methods are not overloaded. There is only one
Prints:
19 e  version of each method. The parameter type is double, and the return
DDDD 
type is double.  

Вам также может понравиться