MCQ With Ans
MCQ With Ans
A.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 com- bining multiple
variables in the same declarations with optional default values.
B.Instance variables have a default value based on the type. For any
non-primitive, includ- ing 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.
1
4.Which of the following is not a valid variable name?
A. _blue
B. 2blue
C. blue$
D. Blue
C. 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.
2
7.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
B.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 unini- tialized
variable.
10.Of the types double, int, and short, how many could fill in the blank
to have this code output 0?
public static void main(String[] args)
{
3
defaultValue;
System.out.println(defaultValue);
}
A. None
B. One
C. Two
D. Three
C. Options A and D are incorrect because byte and short do not store
values with deci- mal points. Option B is tempting. However, 3.14 is
automatically a double. It requires casting to float or writing 3.14f
in order to be assigned to a float. Therefore, Option C is correct.
4
13.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.
14.Given the following code, fill in the blank to have the code print
bounce.
public class TennisBall
{
public TennisBall()
{
System.out.println("bounce");
}
public static void main(String[] slam)
{
}
}
A. TennisBall;
B. TennisBall();
C. new TennisBall;
D. new TennisBall();
5
III. String cat, dog = "animal";
IV. String cat, String dog = "animal";
A. I
B. I, II
C. I, III
D. I, II, III, IV
7
C. aab
D. None of the above
C. The main() method calls the constructor which outputs a. Then the
main method calls the run() method. The run() method calls the constructor
again, which outputs a again. Then the run() method calls the Sand()
method, which happens to have the same name as the constructor. This
outputs b. Therefore, Option C is correct.