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 b c d e
 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 b c d e
 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 b c d e
 A)  syntactically it is invalid to have more if clauses than else clauses B)  statement2 will never execute C)  statement2 will only execute if condition1 is false, it does not matter what condition2 is D)  statement2 will only execute if condition1 is false and condition2 is false E)  statement2 will only execute if condition1 is true and condition2 is false

 4) 4) a b c d e
 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

 5) 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--; } }``` 5) a b c d e
 A)  16 B)  12 C)  10 D)  6 E)  none of the above, this is an infinite loop

 6) 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); ``` 6) a b c d e
 A)  4 B)  5 C)  8 D)  9 E)  this code wont compile because of null pointer exception..

 7) Which of the following is NOT an example of a static method? 7) a b c d e
 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.

 8) Given a String, s, which is assumed to have at least one character in it, which of the following conditions would determine if the first character of the String is the same as the last character? 8) a b c d e
 A)  (s.charAt(1) == s.charAt(s.length( ))) B)  (s.charAt(0) == s.charAt(s.length( ) - 1)) C)  (s.charAt(0) == s.charAt(s.length( ) + 1)) D)  (s.charAt(0) == s.charAt(s.length( ))) E)  (s.charAt(0) == s.charAt(last))

 9) 9) a b c d e
 A)  it yields a run-time error because there is no character at s.charAt(j-1) for j = 0 B)  it prints s out backwards after skipping the last character C)  it prints s out forwards D)  it prints s out backwards but does not print the 0th character E)  it prints s out backwards

 10) 10) a b c d e
 A)  200 B)  100 C)  10,000 D)  1,000,000 E)  20,000

 11) To define a class that will represent a car, which of the following definitions is most appropriate? 11) a b c d e
 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 b c d e
 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 b c d e
 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 b c d e
 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 b c d e
 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 b c d e
 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 b c d e
 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 b c d e
 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 b c d e
 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 b c d e
 A)  "00" B)  "hello" C)  0 D)  "hello00" E)  "0"

 21) Which of the following criticisms is valid about the Swapper class? 21) a b c d e
 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 b c d e
 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 b c d e
 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 b c d e
 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 b c d e
 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 b c d e
 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 b c d e
 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 b c d e
 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 b c d e
 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 a, which is really a Timer. (Take a guess, extra credit) 30) a b c d e
 A)  return (time-this.getTime()); B)  return (time-a.getTime()); C)  return (time-(Timer)a.getTime()); D)  return (time-((Timer)a).getTime()); E)  All of the above would work.