- Home
- Programming
- OCA Java SE8
46.
What is the first line in the following code to not compile?
public static void main(String[] args) {
int Integer = 0; // k1
Integer int = 0; // k2
Integer ++; // k3
int++; // k4
}
public static void main(String[] args) {
int Integer = 0; // k1
Integer int = 0; // k2
Integer ++; // k3
int++; // k4
}
- A.k1
- B.k2
- C.k3
- D.k4
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
Integer is the name of a class in Java. While it is bad practice to use the name of a class as your local variable name, this is legal. Therefore, k1 does compile. It is not legal to use a reserved word as a variable name. All of the primitives including int are reserved words. Therefore, k2 does not compile, and Option B is the answer. Line k4 doesn’t compile either, but the question asks about the first line to not compile. |
47.
Suppose foo is a reference to an instance of a class. Which of the following is not true
about foo.bar?
- A.bar is an instance variable.
- B.bar is a local variable.
- C.It can be used to read from bar.
- D.It can be used to write to bar.
- Answer & Explanation
- Report
Answer : [B]
Explanation :
Explanation :
Dot notation is used for both reading and writing instance variables, assuming they are in scope. It cannot be used for referencing local variables, making Option B the correct answer. |
48.
Which of the following is not a valid class declaration?
- A.class building {}
- B.class Cost$ {}
- C.class 5MainSt {}
- D.class _Outside {}
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
Class names follow the same requirements as other identifiers. Underscores and dollar signs are allowed. Numbers are allowed, but not as the first character of an identifier. Therefore, Option C is correct. Note that class names begin with an uppercase letter by convention, but this is not a requirement. |
49.
Which of the following can fill in the blanks to make this code compile?
_______d = new_______ (1_000_000_.00);
_______d = new_______ (1_000_000_.00);
- A.double, double
- B.double, Double
- C.Double, double
- D.None of the above
- Answer & Explanation
- Report
Answer : [D]
Explanation :
Explanation :
This question is tricky as it appears to be about primitive vs. wrapper classes. Looking closely, there is an underscore right before the decimal point. This is illegal as the underscore in a numeric literal can only appear between two digits. |
50.
Which is correct about a local variable of type String?
- A.It defaults to an empty string.
- B.It defaults to null.
- C.It does not have a default value.
- D.It will not compile without initializing on the declaration line.
- Answer & Explanation
- Report
Answer : [C]
Explanation :
Explanation :
Local variables do not have a default initialization value. If they are referenced before being set to a value, the code does not compile. Therefore, Option C is correct. If the variable was an instance variable, Option B would be correct. Option D is tricky. A local variable will compile without an initialization if it isn’t referenced anywhere or it is assigned a value before it is referenced. |