id:

FILL OUT YOUR ID AT TOP

Test#2MC portion 30 questions- 2points each. Total 60 points. Take your time. Only first submission will be graded.

Reference classes here

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.
 
1)  Assume that count is 0, total is 20 and max is 1. The following statement will do which of the following?
if (count !
= 0 && total / count > max) max = total / count;
1)
  A)  The condition will not compile because it uses improper syntax
  B)  The condition short circuits and the assignment statement is not executed
  C)  The condition short circuits and the assignment statement is executed without problem
  D)  The condition short circuits so that there is no division by zero error when evaluating the condition, but the assignment statement causes a division by zero error
  E)  The condition does not short circuit causing a division by zero error

2)  What is wrong, logically, with the following code?
if (x
> 10) System.out.println("Large");
else if (x
> 6 && x <= 10) System.out.println("Medium");
else if (x
> 3 && x <= 6) System.out.println("Small");
else System.out.println("Very small");
2)
  A)  The logical error is that no matter what value x is, "Large" is always printed out
  B)  There is nothing wrong with the logic at all
  C)  There is no logical error, but there is no need to have (x > 6) in the second conditional or (x > 3) in the third conditional
  D)  There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional
  E)  The logical error is that no matter what value x is, "Very small" is always printed out

3)  3)
  A)  Only Condition 2 is true
  B)  Conditions 2, 3 and 4 are all true, Condition 1 is not
  C)  Condition 2 and Condition 4 are true only
  D)  All 4 Conditions are true
  E)  All 4 Conditions are false

4) 

How many times will the following nested loop print out?

for (int i=0;i<4;i++)  
{ 
    j=4;
    while (j>i)
    {
         System.out.println(j);
         j--;
    }
}
4)
  A)  16
  B)  12
  C)  10
  D)  6
  E)  none of the above, this is an infinite loop

5)  After the following code, what will be the value of y (assume EasyInt is a class that models ints)

public void update (EasyInt x)
{  
     x.setValue(x.getValue()+5);
     
}             
EasyInt y=new EasyInt(4);
update(y);



5)
  A)  4
  B)  5
  C)  8
  D)  9
  E)  this code wont compile because of null pointer exception..

6)  Which of the following is NOT an example of a static method? 6)
  A) random of the Math class
  B) getting the MAX_VALUE from the Integer class.
  C) length of the String class
  D) sqrt of the Math class
  E) the public static void main method in every class that is automatically run.

11)  To define a class that will represent a car, which of the following definitions is most appropriate? 11)
  A)  public class CAR
  B)  private class Car
  C)  private class car
  D)  public class Car
  E)  public class car

12)  If a method does not have a return statement, then 12)
  A)  it can not be called from outside the class that defined the method
  B)  it must be defined to be a public method
  C)  it must be an int, double, or String method
  D)  it must be a void method
  E)  it will produce a syntax error when compiled

13)  A class' constructor usually defines 13)
  A)  how an object is initialized
  B)  if the instance data are accessible outside of the object directly
  C)  the number of methods in the class
  D)  how an object is interfaced
  E)  the number of instance data in the class

14)  Having multiple class methods of the same name where each method has a different number of or type of parameters is known as 14)
  A)  encapsulation
  B)  information hiding
  C)  importing
  D)  method overloading
  E)  tokenizing

(disregard the decimal format info - it is irrelevant)


15)  Which of the following could be used to instantiate a new Student s1? 15)
  A)  s1 = new Student( );
  B)  Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33);
  C)  new Student s1 = ("Jane Doe", "Computer Science", 3.333, 33);
  D)  new Student(s1);
  E)  Student s1 = new Student( );

16)  16)
  A)  s1 = getClassRank( );
  B)  getClassRank(s1);
  C)  s1.getClassRank( );
  D)  s1.toString( );
  E)  s1.getHours( );

17)  Another method that might be desired is one that updates the Student's number of credit hours. This method will receive a number of credit hours and add these to the Student's current hours. Which of the following methods would accomplish this? 17)
  A)  public void updateHours( )
{
hours
++;
}
  B)  public int updateHours( )
{
return hours;
}
  C)  public updateHours(int moreHours)
{
hours
+= moreHours;
}
  D)  public void updateHours(int moreHours)
{
hours
+= moreHours;
}
  E)  public int updateHours(int moreHours)
{
return hours
+ moreHours;
}


18)  The instruction Die d = new Die(10); results in 18)
  A)  The Die d having numFaces = 6 and faceValue = 10
  B)  The Die d having numFaces = 10 and faceValue = 1
  C)  The Die d having numFaces = 6 and faceValue = 1
  D)  A syntax error
  E)  The Die d having numFaces = 10 and faceValue = 10

19)  The instruction Die d = new Die(10, 0); results in 19)
  A)  The Die d having numFaces = 6 and faceValue = 10
  B)  The Die d having numFaces = 6 and faceValue = 1
  C)  A syntax error
  D)  The Die d having numFaces = 10 and faceValue = 1
  E)  The Die d having numFaces = 10 and faceValue = 10


20)  If the instruction Swapper s = new Swapper(0, "hello", 0); is executed followed by s.toString( ); what value is returned from s.toString( )? 20)
  A)  "00"
  B)  "hello"
  C)  0
  D)  "hello00"
  E)  "0"

21)  Which of the following criticisms is valid about the Swapper class? 21)
  A)  The instance data y is visible outside of Swapper
  B)  The instance data x is visible outside of Swapper
  C)  The instance data z is visible outside of Swapper
  D)  All 3 instance data are visible outside of Swapper
  E)  none of the methods are visible outside of Swapper

22)  If we have Swapper r = new Swapper (5, "no", 10); then r.swap( ); returns which of the following? 22)
  A)  "no510"
  B)  "15"
  C)  "no"
  D)  nothing
  E)  "510"

23)  Consider a method defined with the header: public void foo(int a, int b). Which of the following method calls is legal? 23)
  A)  foo(0, 0.1);
  B)  foo(0 / 1, 2 * 3);
  C)  foo(0);
  D)  foo( );
  E)  foo(1 + 2, 3 * 0.1);

24) 

Consider a method defined with the header:

public void doublefoo(double x). Which of the following method calls is legal?

24)
  A)  doublefoo(0);
  B)  doublefoo(0.555);
  C)  doublefoo(0.1 + 0.2);
  D)  doublefoo(0.1, 0.2);
  E)  all of the above are legal except for D

For the question(s) below, consider a class that stores 2 int values. These values can be assigned int values with the messages set1(x) and set2(x) where x is an int, and these values can be accessed through get1( ) and get2( ). Assume that y and z are two objects of this class. The following instructions are executed:
y.set1(5);
y.set2(6);
z.set1(3);
z.set2(y.get1( ));
y
= z;
25)  The statement z.get2( ); will 25)
  A)  return 6
  B)  return 0
  C)  cause a run-time error
  D)  return 5
  E)  return 3

26)  If the instructions z.set2(5); and y.set1(10); are performed (after the previous instructions), which of the following is true? 26)
  A)  (y =  = z) is still true
  B)  (y.get1( ) =  = z.get1( )) but (y.get2( ) != z.get2( ))
  C)  (y.get1( ) =  = z.get2( )) and (y.get2( ) =  = z.get1( )) but (y != z)
  D)  this statement causes a run-time error
  E)  (y.get1( ) =  = z.get1( )) and (y.get2( ) =  = z.get2( )) but (y != z)


27)  What is the value of z after the third statement executes below?
StaticExample a
= new StaticExample(5);
StaticExample b
= new StaticExample(12);
int z
= a.incr( );
27)
  A)  12
  B)  6
  C)  13
  D)  5
  E)  none, the code is syntactically invalid because a and b are attempting to share an instance data

28)  If there are 4 objects of type StaticExample, how many different instances of x are there? 28)
  A)  0
  B)  1
  C)  3
  D)  4
  E)  There is no way to know since any of the objects might share x, but they do not necessarily share x

For the question(s) below, consider a class called Timer. This class would keep track of how much time has passed. Its constructor initializes a specific amount of time and will count down. It has methods getTime() and stopTime() which does much what it says.
29)  Which of the following method headers would properly define the method needed to make this class Comparable? 29)
  A)  public int comparable(Object cp)
  B)  public boolean comparable(Object cp)
  C)  public boolean compareTo(Object cp)
  D)  public int compareTo( )
  E)  public int compareTo(Object cp)

30)  Which of the following pieces of logic could be used in the method that implements Comparable? Assume that the method is passed Object o, which is really a Timer. (Take a guess, extra credit) 30)
  A)  return (time-this.getTime());
  B)  return (time-o.getTime());
  C)  return (time-(Timer)o.getTime());
  D)  return (time-((Timer)o).getTime());
  E)  All of the above would work.