Pages

Java.lang Package Interview Questions

Java.lang Package Interview Questions

1 what is the base class of all classes?
Ans: java.lang.Object

2 what do you think is the logic behind having a single base class for all classes?
Ans: 1. casting
2. Hierarchial and object oriented structure.
3 why most of the Thread functionality is specified in Object Class?
Ans: Basically for intertribal communication.
4 what is the importance of == and equals () method with respect to String object?
Ans: == is used to check whether the references are of the same object.
.equals () is used to check whether the contents of the objects are the same.
But with respect to strings, object refernce with same content
will refer to the same object.
String str1=”Hello”;
String str2=”Hello”;
(str1==str2) and str1.equals(str2) both will be true.
If you take the same example with Stringbuffer, the results would be different.
Stringbuffer str1=”Hello”;
Stringbuffer str2=”Hello”;
str1.equals(str2) will be true.
str1==str2 will be false.
5 Is String a Wrapper Class or not?
Ans: No. String is not a Wrapper class
6 How will you find length of a String object?
Ans: Using length () method of String class.
7 How many objects are in the memory after the exection of following code segment?
Ans:  String str1 = “ABC”;
String str2 = “XYZ”;
String str1 = str1 + str2;
There are 3 Objects.
8 what is the difference between an object and object reference?
Ans: An object is an instance of a class. Object reference is a pointer to the object. There can be many refernces to the same object.
9 what will trim () method of String class do?
Ans: The trim () eliminate spaces from both the ends of a string. ***
10 what is the use of java.lang.Class class?
Ans: The java.lang.Class class is used to represent the classes and interfaces that are loaded by a java program.
11 what is the possible runtime exception thrown by substring () method?
Ans: ArrayIndexOutOfBoundsException.
12 what is the difference between String and String Buffer?
Ans: Object’s of String class is immutable and object’s of String Buffer class is
mutable moreover String Buffer is faster in concatenation.
13 what is the use of Math class?
Ans: Math class provides methods for mathematical functions.
14 Can you instantiate Math class?
Ans: No. It cannot be instantiated. The class is final and its constructor is private. But all the methods are static, so we can use them without instantiating the Math class.
15 what will Math. abs () do?
Ans: It simply returns the absolute value of the value supplied to the method, i.e. gives you the same value. If you supply negative value it simply removes the sign.
16 what will Math. ceil () do?
Ans: This method returns always double, which is not less than the supplied value.  It returns next available whole number
17 what will Math. floor () do?
Ans: This method returns always double, which is not greater than the supplied value.
18 what will Math. ax () do?
Ans: The max () method returns greater value out of the supplied values.

19 what will Math. in() do?
Ans: The min () method returns smaller value out of the supplied values.

20 what will Math. Random () do?
Ans: The random () method returns random number between 0.0 and 1.0. It always returns double.
21) java.lang package is automatically imported into all programs.
True
False
Ans : a
22) What are the interfaces defined by java.lang?
Ans: Cloneable, Comparable and Runnable.
23) What is the purpose of the Runtime class?
Ans: The purpose of the Runtime class is to provide access to the Java runtime system.
24) What is the purpose of the System class?
Ans: The purpose of the System class is to provide access to system resources.
25) Which class is extended by all other classes?
Ans: Object class is extended by all other classes.
26) Which class can be used to obtain design information about an object?
Ans: The Class class can be used to obtain information about an object’s design.
27) Which method is used to calculate the absolute value of a number?
Ans : abs( ) method.
28) What are E and PI?
Ans : E is the base of the natural logarithm and PI is the mathematical value pi.
29) Which of the following classes is used to perform basic console I/O?

             a)    System
             b)    SecurityManager
             c)    Math
             d)    Runtime
Ans : a.
30) Which of the following are true?
              a)    The Class class is the superclass of the Object class.
              b)    The Object class is final.
              c)    The Class class can be used to load other classes.
              d)    The ClassLoader class can be used to load other classes.
Ans : c and d.
31) Which of the following methods are methods of the Math class?
           a)    absolute( )
           b)    log( )
           c)    cosine( )
           d)    sine( )
Ans : b.
32) Which of the following are true about the Error and Exception classes?
          a)    Both classes extend Throwable.
          b)    The Error class is final and the Exception class is not.
          c)    The Exception class is final and the Error is not.
          d)    Both classes implement Throwable.
Ans : a.
33) Which of the following are true?
           a)    The Void class extends the Class class.
           b)    The Float class extends the Double class.
           c)    The System class extends the Runtime class.
           d)    The Integer class extends the Number class.
Ans : d. 
34) Which of the following will output -4.0
         a)    System.out.println(Math.floor(-4.7));
         b)    System.out.println(Math.round(-4.7));
         c)    System.out.println(Math.ceil(-4.7));
         d)    System.out.println(Math.Min(-4.7));
Ans : c.
35) Which of the following are valid statements
a) public class MyCalc extends Math
b) Math.max(s);
c) Math.round(9.99,1);
d) Math.mod(4,10);
e) None of the above.

Ans : e.