Home
26.
Which of the following declarations does not compile?
  • A.
    double num1, int num2 = 0;
  • B.
    int num1, num2;
  • C.
    int num1, num2 = 0;
  • D.
    int num1 = 0, num2 = 0;
  • Answer & Explanation
  • Report
Answer : [A]
Explanation :
Option A does not compile because Java does not allow declaring different types as part of the same declaration. The other three options show various legal combinations of combining multiple variables in the same declarations with optional default values.
Report
Name Email  
27.
What is the output of the following?
public static void main(String... args) {
String chair, table = "metal";
chair = chair + table;
System.out.println(chair);
}
  • A.
    metal
  • B.
    metalmetal
  • C.
    nullmetal
  • D.
    The code does not compile.
  • Answer & Explanation
  • Report
Answer : [D]
Explanation :
The table variable is initialized to "metal". However, chair is not initialized. In Java, initialization is per variable and not for all the variables in a single declaration. Therefore, the second line tries to reference an uninitialized local variable and does not compile, which makes Option D correct.
Report
Name Email  
28.
Which is correct about an instance 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 : [B]
Explanation :
Instance variables have a default value based on the type. For any non-primitive, including String, that type is a reference to null. Therefore Option B is correct. If the variable was a local variable, Option C would be correct.
Report
Name Email  
29.
Which of the following is not a valid variable name?
  • A.
    _blue
  • B.
    2blue
  • C.
    blue$
  • D.
    Blue
  • Answer & Explanation
  • Report
Answer : [B]
Explanation :
An identifier name must begin with a letter, $, or _. Numbers are only permitted for subsequent characters. Therefore, Option B is not a valid variable name.
Report
Name Email  
30.
Which of these class names best follows standard Java naming conventions?
  • A.
    fooBar
  • B.
    FooBar
  • C.
    FOO_BAR
  • D.
    F_o_o_B_a_r
  • Answer & Explanation
  • Report
Answer : [B]
Explanation :
In Java, class names begin with an uppercase letter by convention. Then they use lowercase with the exception of new words. Option B follows this convention and is correct. Option A follows the convention for variable names. Option C follows the convention for constants. Option D doesn’t follow any Java conventions.
Report
Name Email