Java Programming Multiple Choice Questions - Java Lang
This section focuses on the "Java Lang" in Java programming. These Multiple Choice Questions (MCQ) should be practiced to improve the Java programming skills required for various interviews (campus interviews, walk-in interviews, company interviews), placements and other competitive examinations.
1. Which of the following would compile without error?
A. int a = Math.abs(-5);
B. int b = Math.abs(5.0);
C. int d = Math.abs(5L);
D. int c = Math.abs(5.5F);
View Answer
Ans : A
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"".
2. Which of these classes encapsulate runtime state of an object?
A. Class
B. Runtime
C. System
D. Cache
View Answer
Ans : A
Explanation: Class encapsulate runtime state of an object.
3. Which of these classes is not included in java.lang?
A. Class
B. Integer
C. Array
D. Byte
View Answer
Ans : C
Explanation: Array class is a member of java.util..
4. Which of the following will produce an answer that is closest in value to a double, d, while not being greater than d?
A. (int)Math.abs(d);
B. (int)Math.max(d);
C. (int)Math.min(d);
D. (int)Math.floor(d);
View Answer
Ans : D
Explanation: The casting to an int is a smokescreen.
5. Which of these methods returns the class of an object?
A. getClass()
B. WhoseObject()
C. Class()
D. WhoseClass()
View Answer
Ans : A
Explanation: getClass() methods returns the class of an object.
6. Which of these class have only one field "TYPE"?
A. Void
B. Process
C. System
D. Runtime
View Answer
Ans : A
Explanation: The Void class has one field, TYPE, which holds a reference to the Class object for the type void.
I
7. Which of the following method of Process class can terminate a process?
A. void kill()
B. void destroy()
C. void terminate()
D. void exit()
View Answer
Ans : B
Explanation: Kills the subprocess. The subprocess represented by this Process object is forcibly terminated
8. Standard output variable "out" is defined in which class?
A. Void
B. Process
C. Runtime
D. System
View Answer
Ans : D
Explanation: Standard output variable "out" is defined in System class. out is usually used in print statement i:e System.out.print().
9. Which of these class can encapsulate an entire executing program?
A. Void
B. Process
C. Runtime
D. System
View Answer
Ans : B
Explanation: None.
10. Which of the following is method of System class is used to find how long a program takes to execute?
A. currenttime()
B. currentTime()
C. currentTimeMillis()
D. currenttimeMillis()
View Answer
Ans : C
Explanation: None.
11. Which of these is a process of converting a simple data type into a class?
A. type casting
B. type conversion
C. type wrapping
D. None of the Mentioned
View Answer
Ans : B
Explanation: type conversion is a process of converting a simple data type into a class
12. Which of the following is method of wrapper Float for converting the value of an object into byte?
A. Bytevalue()
B. byte bytevalue()
C. bytevalue()
D. Byte Bytevalue()
View Answer
Ans : B
Explanation: byte bytevalue() is method of wrapper Float for converting the value of an object into byte
13. Which of these methods is used to check for infinitely large and small values?
A. isInfinite()
B. Isinfinite()
C. isNaN()
D. IsNaN()
View Answer
Ans : A
Explanation: isinfinite() method returns true is the value being tested is infinitely large or small in magnitude.
14. Which of the following methods is a method of wrapper Integer for obtaining hash code for the invoking object?
A. Integer hashcode()
B. int hashcode()
C. int hashCode()
D. int hash()
View Answer
Ans : C
Explanation: int hashCode() methods is a method of wrapper Integer for obtaining hash code for the invoking object
15. Which of the following method of Process class can terminate a process??
A. void terminate()
B. void destroy()
C. void exit()
D. void kill()
View Answer
Ans : B
Explanation: Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.
Also check :
Discussion