Math Question 1

  • Uploaded by: rahul rastogi
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Math Question 1 as PDF for free.

More details

  • Words: 2,686
  • Pages: 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. b. c. d. e. f. g.

Prints: 0,1 Prints: 0,2 Prints: 1,1 Prints: 1,2 Compile-time error Run-time error 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 Compilean argument of type double; the return type is long. A compile1 e time error time 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. b. c. d. e.

Prints: 0,1 Prints: 0,2 Prints: 1,1 Prints: 1,2 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: If the argument is of type float, then the return type is int. If the 2 d 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. b. c. d. e. f.

Prints: NaN Prints: 0.0 Prints: 0 Compile-time error Run-time error None of the above ANSWER If NaN is the argument passed to Math.round, then the return value is Prints: zero. If the argument type is float, then the return type is int. If the 3 c 0 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. b. c. d.

Prints: 0.0,1.0 Prints: 0.0,2.0 Prints: 1.0,1.0 Prints: 1.0,2.0

e. Compile-time error f. Run-time error g. None of the above ANSWER 4 d

Prints: 1.0,2.0

The Math.ceil method name is not overloaded. The Math.ceil method accepts an argument of type double and returns a double. The returned 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. b. c. d. e. f. g.

Prints: 0.0,1.0 Prints: 0.0,2.0 Prints: 1.0,1.0 Prints: 1.0,2.0 Compile-time error Run-time error None of the above ANSWER 5 a

Prints: 0.0,1.0

The Math.floor method name is not overloaded. The Math.floor method accepts an argument of type double and returns a double. The 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. b. c. d.

Prints: 10.0 Compile-time error Run-time error 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 Prints: double. The return type is the same as the argument type. At run-time, 6 a 10.0 the 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. b. c. d. e. f.

Prints: 0,0,0 Compile-time error at 1 Compile-time error at 2 Compile-time error at 3 Run-time error 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 1 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. b. c. d. e. f.

Prints: 0,0,0 Compile-time error at 1 Compile-time error at 2 Compile-time error at 3 Run-time error None of the above ANSWER At line 1, the invocation of the Math.min method produces a return value of type int. The variable a1 is of type byte; 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 Compile- argument types. At run-time, the arguments might not match the 8 b time error at declared parameter types; so one argument or both might require an 1 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. b. c. d. e. f. g. h. i.

Prints: byte,byte,byte Prints: short,short,short Prints: int,int,int Prints: byte,short,int Prints: short,short,int Prints: byte,short,char Compile-time error Run-time error None of the above ANSWER

9 c

Prints: int,int,int

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 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. b. c. d. e. f. g. h.

Prints: byte,byte,byte Prints: byte,int,long Prints: int,int,int Prints: int,int,long Prints: long,long,long Compile-time error Run-time error None of the above ANSWER

10 d

Prints: int,int,long

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 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. b. c. d. e. f. g. h.

Prints: int,int,int Prints: byte,short,char Prints: long,float,double Prints: double,double,double Prints: long,long,long Compile-time error Run-time error None of the above ANSWER

11 c

Prints: long,float,double

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 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. d. e. f. g.

Prints: float Prints: double Compile-time error Run-time error None of the above ANSWER Prints: 12 d double

The Math.random method returns a value of type double. The range 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. b. c. d. e. f. g.

Prints: int Prints: long Prints: float Prints: double Compile-time error Run-time error 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. b. c. d. e. f. g.

Prints: int,int,int Prints: long,long,long Prints: float,float,float Prints: double,double,double Compile-time error Run-time error None of the above ANSWER Prints: 14 d double,double,double

The Math.sin, Math.cos and Math.tan methods 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. b. c. d. e. f. g. h. i.

b1 b2 b3 b4 b5 b6 b7 b8 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. b. c. d. e. f. g. h. i. j. k. l.

abs ceil floor max min random round sin cos tan sqrt 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. b. c. d. e. f. g. h. i. j. k.

abs ceil floor max min random round sin cos tan 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. b. c. d. e. f. g. h.

Prints: DDDDDD Prints: LLLLLL Prints: DLLLDD Prints: DIDLDD Prints: DLDLDD Prints: DDDLDD Prints: DIDIDD None of the above ANSWER

18 f

Prints: DDDLDD

The round method does not return either of the two floating point primitive types, float or double. The ceil, sin and sqrt methods return only a value of type double. The abs and max methods are 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. b. c. d.

Prints: IIII Prints: ILIL Prints: LLLL Prints: FDFD

e. Prints: DDDD f. None of the above ANSWER Prints: 19 e DDDD

The floor and ceil methods are not overloaded. There is only one version of each method. The parameter type is double, and the return type is double.

Related Documents

Math Question 1
June 2020 10
Math Question
May 2020 3
Math Question
October 2019 9
Some Math Question
May 2020 3
Math Question -one.docx
August 2019 23
Question 1
November 2019 23

More Documents from ""