1. Question: Which is a valid declarations of a String?

    A
    String s1 = null;

    B
    String s2 = 'null';

    C
    String s3 = (String) 'abc';

    D
    String s4 = (String) '\ufeed';

    Note: Option A sets the String reference to null. Option B is wrong because null cannot be in single quotes. Option C is wrong because there are multiple characters between the single quotes ('abc'). Option D is wrong because you can't cast a char (primitive) to a String (object).
    1. Report
  2. Question: What is the numerical range of a char?

    A
    -128 to 127

    B
    -(215) to (215) - 1

    C
    0 to 32767

    D
    0 to 65535

    Note: A char is really a 16-bit integer behind the scenes, so it supports 216 (from 0 to 65535) values.
    1. Report
  3. Question: public void foo( boolean a, boolean b) { if( a ) { System.out.println("A"); /* Line 5 */ } else if(a && b) /* Line 7 */ { System.out.println( "A && B"); } else /* Line 11 */ { if ( !b ) { System.out.println( "notB") ; } else { System.out.println( "ELSE" ) ; } } }

    A
    If a is true and b is true then the output is "A && B"

    B
    If a is true and b is false then the output is "notB"

    C
    If a is false and b is true then the output is "ELSE"

    D
    If a is false and b is false then the output is "ELSE"

    Note: Option C is correct. The output is "ELSE". Only when a is false do the output lines after 11 get some chance of executing. Option A is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. The condition at line 7 will never be evaluated (when a is true it will always be trapped by the line 12 condition) therefore the output will never be "A && B". Option B is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. Option D is wrong. The output is "notB".
    1. Report
  4. Question: switch(x) { default: System.out.println("Hello"); } Which two are acceptable types for x? 1. byte 2. long 3. char 4. float 5. Short 6. Long

    A
    1 and 3

    B
    2 and 4

    C
    3 and 5

    D
    4 and 6

    Note: Switch statements are based on integer expressions and since both bytes and chars can implicitly be widened to an integer, these can also be used. Also shorts can be used. Short and Long are wrapper classes and reference types can not be used as variables.
    1. Report
  5. Question: public void test(int x) { int odd = 1; if(odd) /* Line 4 */ { System.out.println("odd"); } else { System.out.println("even"); } } Which statement is true?

    A
    Compilation fails.

    B
    "odd" will always be output.

    C
    "even" will always be output.

    D
    "odd" will be output for odd values of x, and "even" for even values.

    Note: The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer.
    1. Report
  6. Question: public class While { public void loop() { int x= 0; while ( 1 ) /* Line 6 */ { System.out.print("x plus one is " + (x + 1)); /* Line 8 */ } } } Which statement is true?

    A
    There is a syntax error on line 1.

    B
    There are syntax errors on lines 1 and 6.

    C
    There are syntax errors on lines 1, 6, and 8.

    D
    There is a syntax error on line 6.

    Note: Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java. A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown.
    1. Report
  7. Question: Which is true about an anonymous inner class?

    A
    It can extend exactly one class and implement exactly one interface.

    B
    It can extend exactly one class and can implement multiple interfaces.

    C
    It can extend exactly one class or implement exactly one interface.

    D
    It can implement multiple interfaces regardless of whether it also extends a class.

    Note: Option C is correct because the syntax of an anonymous inner class allows for only one named type after the new, and that type must be either a single interface (in which case the anonymous class implements that one interface) or a single class (in which case the anonymous class extends that one class). Option A, B, D, and E are all incorrect because they don't follow the syntax rules described in the response for answer Option C.
    1. Report
  8. Question: class Boo { Boo(String s) { } Boo() { } } class Bar extends Boo { Bar() { } Bar(String s) {super(s);} void zoo() { // insert code here } } which one create an anonymous inner class from within class Bar?

    A
    Boo f = new Boo(24) { };

    B
    Boo f = new Bar() { };

    C
    Bar f = new Boo(String s) { };

    D
    Boo f = new Boo.Bar(String s) { };

    Note: Option B is correct because anonymous inner classes are no different from any other class when it comes to polymorphism. That means you are always allowed to declare a reference variable of the superclass type and have that reference variable refer to an instance of a subclass type, which in this case is an anonymous subclass of Bar. Since Bar is a subclass of Boo, it all works. Option A is incorrect because it passes an int to the Boo constructor, and there is no matching constructor in the Boo class. Option C is incorrect because it violates the rules of polymorphism—you cannot refer to a superclass type using a reference variable declared as the subclass type. The superclass is not guaranteed to have everything the subclass has. Option D uses incorrect syntax.
    1. Report
  9. Question: Which is true about a method-local inner class?

    A
    It must be marked final.

    B
    It can be marked abstract.

    C
    It can be marked public.

    D
    It can be marked static.

    Note: Option B is correct because a method-local inner class can be abstract, although it means a subclass of the inner class must be created if the abstract class is to be used (so an abstract method-local inner class is probably not useful). Option A is incorrect because a method-local inner class does not have to be declared final (although it is legal to do so). C and D are incorrect because a method-local inner class cannot be made public (remember-you cannot mark any local variables as public), or static.
    1. Report
  10. Question: Which statement is true about a static nested class?

    A
    You must have a reference to an instance of the enclosing class in order to instantiate it.

    B
    It does not have access to nonstatic members of the enclosing class.

    C
    It's variables and methods must be static.

    D
    It must extend the enclosing class.

    Note: Option B is correct because a static nested class is not tied to an instance of the enclosing class, and thus can't access the nonstatic members of the class (just as a static method can't access nonstatic members of a class). Option A is incorrect because static nested classes do not need (and can't use) a reference to an instance of the enclosing class. Option C is incorrect because static nested classes can declare and define nonstatic members. Option D is wrong because it just is. There's no rule that says an inner or nested class has to extend anything.
    1. Report
Copyright © 2025. Powered by Intellect Software Ltd