Machine-translated by Google. Questions are authored in English.
Top

Java.lang Class - General Questions Questions and Answers

1.

Which of the following are valid calls to Math.max?

1.Math.max(1,4)

2.Math.max(2.3, 5)

3.Math.max(1, 3, 5, 7)

4.Math.max(-1.5, -2.8f)

Answer: A

(1), (2), and (4) are correct. The max() method is overloaded to take two arguments of type int, long, float, or double.

(3) is incorrect because the max() method only takes two arguments.

Please log in or register to report this question.

2.

What is the value of "d" after this line of code has been executed?

double d = Math.round ( 2.5 + Math.random() );

Answer: B

The Math.random() method returns a number greater than or equal to 0 and less than 1 . Since we can then be sure that the sum of that number and 2.5 will be greater than or equal to 2.5 and less than 3.5, we can be sure that Math.round() will round that number to 3. So Option B is the answer.

Please log in or register to report this question.

3.

public class Myfile 
{ 
    public static void main (String[] args) 
    {
        String biz = args[1]; 
        String baz = args[2]; 
        String rip = args[3]; 
        System.out.println("Arg is " + rip); 
    } 
}
Select how you would start the program to cause it to print: Arg is 2

 

Answer: C

Correct answer: java Myfile 1 3 2 2

The program prints args[3], and command line arguments are indexed from zero, so args[3] is the *fourth* one. With the arguments 1, 3, 2, 2 the fourth is 2, giving “Arg is 2”. The invocation with 0 1 2 3 would print 3, and the one with five arguments would also print 3 - both losing a place to the zero-based indexing.

Please log in or register to report this question.

Answer: A

The return value of the Math.abs() method is always the same as the type of the parameter passed into that method.

In the case of A, an integer is passed in and so the result is also an integer which is fine for assignment to "int a".

The values used in B, C & D respectively are a double, a float and a long. The compiler will complain about a possible loss of precision if we try to assign the results to an "int".

Please log in or register to report this question.

Loading…

Other Subjects

Tags: Java.lang Class - General Questions Questions and Answers || Java.lang Class - General Questions MCQ Questions and Answers || Java.lang Class - General Questions GK Questions and Answers || Java.lang Class - General Questions GK MCQ Questions || Java.lang Class - General Questions Multiple Choice Questions and Answers || Java.lang Class - General Questions GK || GK on Java.lang Class - General Questions || Java Programming Questions and Answers || Java Programming MCQ Questions and Answers || Java Programming GK Questions and Answers || GK on Java Programming