Java Programming - Java.lang Class

Why should I learn to solve Java Programming questions and answers section on "Java.lang Class"?

Learn and practise solving Java Programming questions and answers section on "Java.lang Class" to enhance your skills so that you can clear interviews, competitive examinations, and various entrance tests (CAT, GATE, GRE, MAT, bank exams, railway exams, etc.) with full confidence.

Where can I get the Java Programming questions and answers section on "Java.lang Class"?

IndiaBIX provides you with numerous Java Programming questions and answers based on "Java.lang Class" along with fully solved examples and detailed explanations that will be easy to understand.

Where can I get the Java Programming section on "Java.lang Class" MCQ-type interview questions and answers (objective type, multiple choice)?

Here you can find multiple-choice Java Programming questions and answers based on "Java.lang Class" for your placement interviews and competitive exams. Objective-type and true-or-false-type questions are given too.

How do I download the Java Programming questions and answers section on "Java.lang Class" in PDF format?

You can download the Java Programming quiz questions and answers section on "Java.lang Class" as PDF files or eBooks.

How do I solve Java Programming quiz problems based on "Java.lang Class"?

You can easily solve Java Programming quiz problems based on "Java.lang Class" by practising the given exercises, including shortcuts and tricks.

Exercise : Java.lang Class - General Questions
1.

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

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

2
3
4
2.5
Answer: Option
Explanation:

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.


2.
Which of the following would compile without error?
int a = Math.abs(-5);
int b = Math.abs(5.0);
int c = Math.abs(5.5F);
int d = Math.abs(5L);
Answer: Option
Explanation:

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".


3.
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)
1, 2 and 4
2, 3 and 4
1, 2 and 3
3 and 4
Answer: Option
Explanation:

(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.


4.
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
java Myfile 222
java Myfile 1 2 2 3 4
java Myfile 1 3 2 2
java Myfile 0 1 2 3
Answer: Option
Explanation:

Arguments start at array element 0 so the fourth arguement must be 2 to produce the correct output.