id:

Test 4

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.
 
For the question(s) below, use the following partial class definitions:

public class A1
{
public int x;
private int y;
public int z;
...
}

public class A2 extends A1
{
public int a;
private int b;
...
}
public class A3 extends A2
{
private int q;
...
}
1)  Which of the following is true with respect to A1, A2 and A3? 1)
  A)  A3 is a subclass of A2 and A2 is a subclass of A1
  B)  A2 and A3 are both subclasses of A1
  C)  A1, A2 and A3 are all subclasses of the class A
  D)  A1 is a subclass of A2 and A2 is a subclass of A3
  E)  A1 and A2 are both subclasses of A3

2)  Which of the following lists of instance data are accessible in class A2? 2)
  A)  x, z, a, b
  B)  z, a, b
  C)  a, b
  D)  x, y, z, a, b
  E)  x, y, z, a

3)  Which of the following lists of instance data are accessible in A3? 3)
  A)  x, a, q
  B)  x, y, z, a, b, q
  C)  a, q
  D)  x, z, a, q
  E)  a, b, q

4)  The instruction super( ); does which of the following? 4)
  A)  calls the method super as defined in the current class' parent class
  B)  calls the method super as defined in the current class
  C)  calls the method super as defined in java.lang
  D)  calls the constructor as defined in the current class
  E)  calls the constructor as defined in the current class' parent class

For the question(s) below, consider the following class definition:

public class AClass
{
private int x;
private int y;
public AClass(int a, int b)
{
x
= a;
y
= b;
}

public int addEm( )
{
return x
+ y;
}
public void changeEm( )
{
x
++;
y
--;
}

public String toString( )
{
return ""
+ x + " " + y;
}
}
5)  Consider that you want to extend AClass to BClass. BClass will have a third int instance variable, z. Which of the following would best define BClass' constructor? 5)
  A)  public BClass(int a, int b, int c)
{
super(a, b, c);
}
  B)  public BClass(int a, int b, int c)
{
x
= a;
y
= b;
z
= c;
}
  C)  public BClass(int a, int b, int c)
{
z
= c;
}
  D)  public BClass(int a, int b, int c)
{
super(a, b);
z
= c;
}
  E)  public BClass(int a, int b, int c)
{
super( );
}

6)  You want addEm to now add all three values and return the sum and changeEm to change x and y, but leave z alone. Which should you do? 6)
  A)  Redefine changeEm to call super.changeEm( ) without doing anything to z, and redefine addEm to return super.addEm( )
  B)  Redefine addEm to return the value of z + super.addEm( ), but leave changeEm alone
  C)  Redefine changeEm to call super.changeEm( ) and then set z = x + y, but leave addEm alone
  D)  Redefine addEm to return the value of z + super.addEm( ) and redefine changeEm to call super.changeEm( ) and thenset z = x + y
  E)  Redefine addEm and changeEm without referencing super.addEm( ) or super.changeEm( )

7)  Which of the following would best redefine the toString method for BClass? 7)
  A)  public String toString(int z)
{
return " "
+ x + " " + y + " " + z;
}
  B)  public String toString( )
{
return super.toString( );
}
  C)  public String toString( )
{
return super.toString( )
+ " " + z;
}
  D)  public String toString( )
{
return " "
+ x + " + y + " " + z;
}
  E)  public String toString( )
{
return super.toString( )
+ " " x + " " + y + " " + z;
}

8)  A variable declared to be of one class can later reference an extended class of that class. This variable is known as 8)
  A)  polymorphic
  B)  public
  C)  derivable
  D)  cloneable
  E)  none of the above, a variable declared to be of one class can never reference any other type of class, even an extended class

9)  In order to determine the type that a polymorphic variable refers to, the decision is made 9)
  A)  by the compiler at compile time
  B)  by the user at run time
  C)  by the Java run-time environment at run time
  D)  by the programmer at the time the program is written
  E)  by the operating system when the program is loaded into memory

For the question(s) below, assume that Student, Employee and Retired are all subclasses of Person, and all four classes have different implementations of the method getMoney. Consider the following code where ... are the required parameters for the constructors:
Person p
= new Person(...);
int m1
= p.getMoney( ); // assignment 1
p
= new Student(...);
int m2
= p.getMoney( ); // assignment 2
if (m2
< 100000) p = new Employee(...);
else if (m1
> 50000) p = new Retired(...);
int m3
= p.getMoney( ); // assignment 3
10)  The reference to getMoney( ) in assignment 2 is to the class 10)
  A)  Person
  B)  Student
  C)  this cannot be determined by examining the code
  D)  Retired
  E)  Employee

11)  The reference to getMoney( ) in assignment 3 is to the class 11)
  A)  Person
  B)  Employee
  C)  Student
  D)  Retired
  E)  this cannot be determined by examining the code

12)  The relationship between a child class and a parent class is referred to as a(n) ________ relationship. 12)
  A)  was-a
  B)  has-a
  C)  instance-of
  D)  alias
  E)  is-a

For the question(s) below, use the following recursive method.

public int question1_2(int x, int y)
{
if (x
== y) return 0;
else return question1_2(x
-1, y) + 1;
}
13)  If the method is called as question1_2(8, 3), what is returned? 13)
  A)  24
  B)  3
  C)  8
  D)  5
  E)  11

14)  Calling this method will result in infinite recursion if which condition below is initially true? 14)
  A)  (x < y)
  B)  (x = = y)
  C)  (x != y)
  D)  (x > y)
  E)  (x = = 0 && y != 0)

For the question(s) below, refer to the following recursive factorial method.

public int factorial(int x)
{
if (x
> 1) return x * factorial (x - 1);
else return 1;
}
15)  What is returned if factorial(3) is called? 15)
  A)  9
  B)  1
  C)  0
  D)  6
  E)  3

16)  What does the following recursive method determine?
public boolean question16(int[ ]a, int[ ] b, int j)
{
if(j
= = a.length) return false;
else if (j
= = b.length) return true;
else return question16(a, b, j
+1);
}
16)
  A)  Returns true if a is larger than b, false otherwise
  B)  Returns the length of array a + length of array b
  C)  Returns true if b is larger than a, false otherwise
  D)  Returns true if a and b have no elements
  E)  Returns true if a and b are equal in size, false otherwise

17)  Define the magnitude of a number as the location of the decimal point from the left of the number (that is, if a number has 4 digits followed by the decimal point, it will have a magnitude of 4). 100 would then have a magnitude of 3 and 55,555.555 would have a magnitude of 5. A partial recursive method is given below to compute a positive int parameter's magnitude. Which answer below is needed to complete the method?
public int magnitude(double x)
{
if (x
< 1) return 0;
else return ________;
}
17)
  A)  magnitude(x - 1) + 10;
  B)  magnitude(x - 10) + 1;
  C)  magnitude(x / 10) * 2;
  D)  magnitude(x / 10) + 10;
  E)  magnitude(x / 10) + 1;
18) 

What would mystery(7) output?

18)
  A)  2
  B)  4
  C)  5
  D) 7
  E) infinite loop