- Home
- Programming
- OCA Java SE8
31.
How many of the following methods compile?
public String convert(int value) {
return value.toString();
}p
ublic String convert(Integer value) {
return value.toString();
}p
ublic String convert(Object value) {
return value.toString();
}
public String convert(int value) {
return value.toString();
}p
ublic String convert(Integer value) {
return value.toString();
}p
ublic String convert(Object value) {
return value.toString();
}
- A.None
- B.One
- C.Two
- D.Three
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
Objects have instance methods while primitives do not. Since int is a primitive, you cannot call instance methods on it. Integer and String are both objects and have instance methods. Therefore, Option C is correct. |
32.
Which of the following does not compile?
- A.int num = 999;
- B.int num = 9_9_9;
- C.int num = _9_99;
- D.None of the above; they all compile.
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
Underscores are allowed between any two digits in a numeric literal. Underscores are not allowed at the beginning or end of the literal, making Option C the correct answer. |
33.
Which of the following is a wrapper class?
- A.int
- B.Int
- C.Integer
- D.Object
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
Option A is incorrect because int is a primitive. Option B is incorrect because it is not the name of a class in Java. While Option D is a class in Java, it is not a wrapper class because it does not map to a primitive. Therefore, Option C is correct. |
34.
What is the result of running this code?
public class Values {
integer a = Integer.valueOf("1");
public static void main(String[] nums) {
integer a = Integer.valueOf("2");
integer b = Integer.valueOf("3");
System.out.println(a + b);
}
}
public class Values {
integer a = Integer.valueOf("1");
public static void main(String[] nums) {
integer a = Integer.valueOf("2");
integer b = Integer.valueOf("3");
System.out.println(a + b);
}
}
- A.4
- B.5
- C.The code does not compile.
- D.The code compiles but throws an exception at runtime.
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
There is no class named integer. There is a primitive int and a class Integer. Therefore, the code does not compile, and Option C is correct. If the type was changed to Integer, Option B would be correct. |
35.
Which best describes what the new keyword does?
- A.Creates a copy of an existing object and treats it as a new one
- B.Creates a new primitive
- C.Instantiates a new object
- D.Switches an object reference to a new one
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
The new keyword is used to call the constructor for a class and instantiate an instance of the class. A primitive cannot be created using the new keyword. Dealing with references happens after the object created by new is returned. |