- Home
- Programming
- OCA Java SE8
36.
Which is the first line to trigger a compiler error?
double d1 = 5f; // p1
double d2 = 5.0; // p2
float f1 = 5f; // p3
float f2 = 5.0; // p4
double d1 = 5f; // p1
double d2 = 5.0; // p2
float f1 = 5f; // p3
float f2 = 5.0; // p4
- A.p1
- B.p2
- C.p3
- D.p4/li>
- Answer & Explanation
- Report
Answer : [D]
Explanation :
Explanation :
Java uses the suffix f to indicate a number is a float. Java automatically widens a type, allowing a float to be assigned to either a float or a double. This makes both lines p1 and p3 compile. Line p2 does compile without a suffix. Line p4 does not compile without a suffix and therefore is the answer. |
37.
Which of the following lists of primitive types are presented in order from smallest to
largest data type?
- A.byte, char, float, double
- B.byte, char, double, float
- C.char, byte, float, double
- D.char, double, float, bigint
- Answer & Explanation
- Report
Answer : [A]
Explanation :
Explanation :
A byte is smaller than a char, making Option C incorrect. bigint is not a primitive, making Option D incorrect. A double uses twice as much memory as a float variable, therefore Option A is correct. |
38.
Which of the following is not a valid order for elements in a class?
- A.Constructor, instance variables, method names
- B.Instance variables, constructor, method names
- C.Method names, instance variables, constructor
- D.None of the above: all orders are valid.
- Answer & Explanation
- Report
Answer : [D]
Explanation :
Explanation :
The instance variables, constructor, and method names can appear in any order within a class declaration. |
39.
Which of the following lines contains a compiler error?
String title = "Weather"; // line x1
int hot, double cold; // line x2
System.out.println(hot + " " + title); // line x3
String title = "Weather"; // line x1
int hot, double cold; // line x2
System.out.println(hot + " " + title); // line x3
- A.x1
- B.x2
- C.x3
- D.None of the above
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
Java does not allow multiple Java data types to be declared in the same declaration, making Option B the correct answer. If double was removed, both hot and cold would be the same type. Then the compiler error would be on x3 because of a reference to an uninitialized variable. |
40.
How many instance initializers are in this code?
1: public class Bowling {
2: { System.out.println(); }
3: public Bowling () {
4: System.out.println();
5: }
6: static { System.out.println(); }
7: { System.out.println(); }
8: }
1: public class Bowling {
2: { System.out.println(); }
3: public Bowling () {
4: System.out.println();
5: }
6: static { System.out.println(); }
7: { System.out.println(); }
8: }
- A.None
- B.One
- C.Two
- D.Three
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
Lines 2 and 7 illustrate instance initializers. Line 6 is a static initializer. Lines 3–5 are a constructor. |