Constructors & Lang Classes  Answers




1)  Pick the incorrect statement. Constructors are unlike methods in that they have

     a) no return type
     b) are named after the class
     c) use round braces for arguments
     d) cannot be abstract                                                            ( d )

//  You might have needed to  deduce ( d ) .
// Constructors cannot be defined like abstract methods



2) If used inside one of the constructors of a class called Cat which
    of the following would the constructor this( 1.1, 2.2 ) call?

a ) Cat (double x, double y ) { /* */ }
b)  Cat( int j, int k ) { /* */ }
c)  Cat(  ) { /* */ }
d)  Cat  ( float u, int p ) { /* */ }                                                ( a )


3) What will the following print out? Select the correct answer.

String x1 = "X";
String x2 = "X";
if (x1.equals(x2))
System.out.println( "Positive")
else
System.out.println("Negative")     

a ) Positive
b ) Negative                                                                                  ( a )


// A deep comparison is executed where the same value is stored in both

// strings the if statement evaluates to true and 'Positive' is printed


4) What will the following print out? Select the correct answer.

String x1 = "X";

String x2 = "X";
 

if (x1==x2)
System.out.println( "Positive")
else
System.out.println("Negative")

a )
b)                                                                                                       ( a )


// Positive is printed because the shallow comparison compares addresses and in

// this case the addresses are for the same string literal in the string literal pool

3) One of the following is not a valid wrapper class. Which is it?

a) Float
b) Int
c) Double
d) Long                                                                                             ( b )

// Not in long form


4)  Which of the following statements is not correct?

a) The math class itself is final so can only be extended once
b) Math's constructor is private so it cannot be instantiated.
c) Math methods and constants are static
d) Math methods are only accessible through the Math class name     ( a )

// The math class is final so cannot be extended at all
 

5) Math.floor(44.4) returns
a) 45
b) 45.0
c) 44
d) 44.0                                                                                                                       ( c )       


6) Which of the following is not in the java lang package

a) Vector
b) Boolean
c) Math
d) String                                                                                                                   ( a )

// Vector needs to be imported

7 ) With respect to Vector class, which of the following statements is not correct?

a) The number of objects added may exceed the capacity.                      
b) The no-args Vector constructor defaults to an initial capacity set to 5.
c) To return an element to it's original type typically requires a cast.
d) Vector can throw an exception is an element index is addressed
    higher than the number of elements that have been added to the
    Vector.                                                                                                                  ( b )

// Picky question. Initial capacity is set to 10.