Java - Java.lang Class Questions and Answers

Practice Mode
Showing 10 of 24 questions
Q1
What is the value of "d" after this line of code has been executed? double d = Math.round ( 2.5 + Math.random( ));
  • A 2
  • B 3
  • C 4
  • D 2.5
Answer: Option B
Q2
Which of the following would compile without error?
  • A int a = Math.abs(-5);
  • B int b = Math.abs(5.0);
  • C int c = Math.abs(5.5F);
  • D int d = Math.abs(5L);
Answer: Option A
Q3
 Which of the following are valid calls to Math.max? Math.max(1,4) Math.max(2.3, 5)     Math.max(1, 3, 5, 7)     Math.max(-1.5, -2.8f)
  • A 1, 2 and 4
  • B 2, 3 and 4
  • C 1, 2 and 3
  • D 2, 3 and 4
Answer: Option A
Q4
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
  • A java Myfile 222
  • B java Myfile 1 2 2 3 4
  • C java Myfile 1 3 2 2
  • D java Myfile 0 1 2 3
Answer: Option C
Q5
Which of the following is the root class for all Java classes?
  • A Object
  • B Class
  • C Main
  • D Root
Answer: Option A
Explanation: Object class is the root of the class hierarchy in Java
Q6
What method is used to get the string representation of an object?
  • A toString()
  • B getString()
  • C stringValue()
  • D convertString()
Answer: Option A
Explanation: toString() method returns string representation of object
Q7
Which method is called when an object is created?
  • A Constructor
  • B init()
  • C start()
  • D new()
Answer: Option A
Explanation: Constructor is called when object is instantiated
Q8
What is the return type of the hashCode() method?
  • A int
  • B String
  • C Object
  • D boolean
Answer: Option A
Explanation: hashCode() returns an integer value
Q9
Which class provides the System.out.println() method?
  • A System
  • B PrintStream
  • C Object
  • D String
Answer: Option A
Explanation: System class contains out static field of type PrintStream
Q10
What is the purpose of the finalize() method?
  • A Cleanup before garbage collection
  • B Initialize object
  • C Compare objects
  • D Create object copy
Answer: Option A
Explanation: finalize() is called by garbage collector before object destruction
Questions and Answers for Competitive Exams Various Entrance Test