Presentation is loading. Please wait.

Presentation is loading. Please wait.

2/26/12 Quiz Bowl.

Similar presentations


Presentation on theme: "2/26/12 Quiz Bowl."— Presentation transcript:

1 2/26/12 Quiz Bowl

2 Assume that A is a nonempty rectangular array of ints
Assume that A is a nonempty rectangular array of ints. Consider the following code segment: for (int col=0; col<A[0].length; col++) { for (int row=0; row<A.length-1; row++) { if (A[row][col] > A[row+1][col]) return false; } } return true; Which of the following best describes when this code segment returns true? A. When every row of A is sorted in increasing order B. When no row of A contains duplicate values C. When every column of A is sorted in increasing order D. When no column of A contains duplicate values E. When neither diagonal of A contains duplicate values

3 Consider the following (incomplete) class definitions: public abstract class PricklyThing{ public PricklyThing() {...} public abstract void print(); } public class Hedgehog extends PricklyThing{ public Hedgehog() {...} public void print() { System.out.println(“Hedgehogs are prickly.”); } Which of the following statements does not cause a compile-time error? I. PricklyThing h = new Hedgehog(); II. PricklyThing h = new PricklyThing(); III. Hedgehog h = new PricklyThing(); A. I only B. II only C. III only D. I and II only E. II and III only

4 Assume that variable A is a nonempty ArrayList
Assume that variable A is a nonempty ArrayList. Consider the following statement: String s = (String)A.get(0); Which of the following statements about this use of a class cast is true? A. The statement would compile and execute without error whether or not the cast is used. B. The statement would compile without error whether or not the cast is used, but the use of the cast prevents a runtime error when the first item in A is not a String. C. The statement would compile without error whether or not the cast is used, but there will be a runtime error if the first item in A is not a String whether or not the cast is used. D. The statement would cause a compile-time error if the cast were not used, and the use of the cast also prevents a runtime error if the first item in A is not a String. E. The statement would cause a compile-time error if the cast were not used, and there will be a runtime error if the first item in A is not a String even though the cast is used.

5 Consider the following code: ArrayList menu = new ArrayList(); Coffee c; Beverage b = new Beverage(); menu.add(b); statement Assume that the Coffee class is a subclass of the Beverage class. Which of the following can be used to replace the placeholder statement so that the code will cause neither a compile-time nor a runtime error? A. b = (Coffee)(menu.get(0)); B. b = (Beverage)(menu.get(0)); C. c = menu.get(0); D. c = (Beverage)(menu.get(0)); E. c= (Coffee)(menu.get(0));

6 Assume that variable A is an array of ints of length N. 1
Assume that variable A is an array of ints of length N. 1. for (int k = 0; k<N; k++) { 2. for (int j = k+1; j<N; j++) { 3. if (A[j] < A[k]) { 4. swap(A, j, k); 5. } 6. System.out.println(k); 7. } 8.} Assume that swap correctly swaps the jth and kth values in array A. Which of the following assertions is true every time line 6 is executed? A. The values in array A are sorted from low to high. B. The values in A[k] through A[N] are sorted from low to high. C. The values in A[k] through A[N] are sorted from high to low. D. The values in A[0] through A[k] are sorted from low to high. E. The values in A[0] through A[k] are sorted from high to low.

7 Consider the following code: ArrayList<Integer> a = new ArrayList<Integer>(); a.add(3); a.add(4); addNumberA(a, 1); ArrayList<Integer> b = new ArrayList<Integer>(); b.add(3); b.add(4); addNumberB(b, 1); ... public ArrayList<Integer> addNumberB(ArrayList<Integer> start, int newInt){ start.add(new Integer(newInt)); return start; } public ArrayList<Integer> addNumberA(ArrayList<Integer> start, int newInt){ ArrayList<Integer> newList = new ArrayList<Integer>(); newList.extend(start); newList.add(new Integer(newInt)); return newList; } Which of the following describe the contents of a and b after the snippet is run? A. a: 3, 4, 1 b: 3, 4, 1 B. a: 3, 4 b: 3, 4, 1 C. a: 3, 4, 1 b: 3, 4 D. a: 3, 4 b: 3, 4 E. None of the above.

8 Consider the following code: public class SuperClass {     private int value = 0;  private void setValue(int newValue){         value = newValue;     }         public void changeValue(int newValue){         setValue(newValue);     }         public int getValue(){         return value;     }        public class SubClass extends SuperClass {     public void addValue(int moreValue){         ...     } Which of the following could replace “…” I   super.changeValue(super.getValue() + moreValue) II  changeValue(getValue() + moreValue) III super.setValue(super.getValue() +moreValue) A.  I only   B.  II only C.  III only D.  I and II only E.  I, II, and III

9 The code shown sorts array a[0]. a[a. length-1] in descending order
The code shown sorts array a[0]... a[a.length-1] in descending order. public static void sort(Comparable[] a){    for(int i = 0; i < a.length -1; i ++){        for(int j = 0; j < a.length -i -1; j++){             if(a[j].compareTo(a[j+1])>0){                 swap(a, j, j+1); //swap a[j] and a[j+1]             }        }    } } This is an example of (A)  selection sort. (B)  insertion sort. (C)  mergesort. (D)  quicksort. (E)  none of the above.

10 Assume that the following interface and class have been defined: public interface Dinosaur{ public String getName(); public int getNumberOfTeeth(); } public class Velociraptor implements Dinosaur{ /*** fields ***/ private String name; private int numberofteeth; /*** methods ***/ public Velociraptor(String n, int t) { //constructor name = n; numberofteeth = t; } public String getName() { return name; } public int getNumberOfTeeth() { return numberofteeth; } } Which of the following will cause a compile-time error? A. An attempt to create an instance of a Dinosaur B. An attempt to create an instance of a Velociraptor C. An attempt to define a method with a parameter of type Dinosaur D. An attempt to define a method with a parameter of type Velociraptor E. An attempt to define a subclass of the Dinosaur class

11 Consider the following code: public void mystery1(int[] a){ for(int i = 0; i < a.length; i++){ if(i%2 == 0){ a[i] = -1*a[i]; } } } Which of the following describes the relationship between mystery1 and mystery2? (A) mystery1 and mystery2 do different things (B) mystery1 and mystery2 do the same thing, and mystery1 runs slightly faster (C) mystery1 and mystery2 do the same thing, and mystery1 runs much faster (D) mystery1 and mystery2 do the same thing, and mystery2 runs slightly faster (E) mystery1 and mystery2 do the same thing, and mystery2 runs much faster public void mystery2(int[] a){    for(int i = 0; i < a.length; i++){        for(int j = i; j < a.length; j++){             a[j] = -1*a[j];        }    } }

12 Consider these class declarations: public class Dessert{ … } public class Pie extends Dessert{ … } Which is a true statement? I    Pie inherits the constructors of Dessert. II   Pie can add new methods and private instance variables. III  Pie can override existing private methods of Dessert. A.  I only B.  II only C.  III only D.  I and II only E.  II and III only

13 Assume that the variable a is an ArrayList that has been initialized to contained a list of ten strings. Which of the following statements correctly adds the string “the end” to the end of the list? a.add(“the end”); a.add(10, “the end”); a.set(10, “the end”); I only II only I and II I and III II and III

14 Assume that the variable a is an ArrayList that has been initialized to contained a list of ten strings. Which of the following code segments correctly replaces the first string in the list with the string “start”? a.set(0, “start”); a.get(0, “start”); a.add(“start”); a.add(0, “start”); a.remove(0);

15 public static void printArray(String[] A, int k) {
if (k < A.length) { printArray(A, k+1); System.out.print(A[k]); } } Assume that array A has been initialized to be of length 4 and to contain the values “a”, “b”, “c”, and “d” (with “a” in A[0], “b” in A[1], and so on). What is output as the result of the call printArray(A,0)? bcd dcb abcd dcba ddd

16 Consider writing code to simulate flipping a coin ten times
Consider writing code to simulate flipping a coin ten times. An outline of the code is given below: Random ran = new Random(); for (int k=1; k<=10; k++) { if (expression == 0) System.out.println(“heads”); else System.out.println(“tails”); } Which of the following is the best replacement for expression? A. ran.nextInt(0) B. ran.nextInt(1) C. ran.nextInt(2) D. ran.nextInt(10) E. ran.nextInt(k)

17 public interface Employee {
void raiseSalary(); } public class Test implements Employee, Musician { public void raiseSalary() { System.out.println(“raising”); public void Play() { System.out.println(“playing”); } } Which of the following statements about these definitions is true? The code will not compile because class Test tries to implement two interfaces at once. The code will not compile because class Test only implements interfaces; it does not extend any class. The code will not compile because class Test only implements the methods defined in the Employee and Musician classes; it does not define any new methods. The code will compile; however, if class Test did not include a definition of the Play method, the code would not compile. The code will compile; furthermore, even if class Test did not include a definition of the Play method, the code would compile. public interface Musician { void Play(); }

18 Which of the following operation scan be implemented more efficiently (in terms of worst-case time) on a sorted array of integers than on an unsorted array of integers? Searching for a given value in the array Adding a new value to the array Removing a value from the array Printing all values in the array Computing the sum of all values in the array

19 public static int compute(int x, int y) {
if (x > y) return x; else return(compute(x+2, y-2)); What is returned by the call compute(1,5)? 1 3 5 7 No value is returned because an infinite recursion occurs

20 public static int compute(int x, int y) {
if (x > y) return x; else return(compute(x+2, y-2)); Which of the following best characterizes the circumstances under which the call computer x,y leads to an infinite recursion? Never Whenever x = y Whenever x < y Whenever x > y Whenever both x and y are odd

21 x = !y; y = !x; Assume that x and y are initialized boolean variables. Which of the following statements is true? The final value of x is the same as the initial value of x The final value of x is the same as the initial value of y. The final value of y is the same as the initial value of y. The final value of y is the same as the initial value of x. It is not possible to say anything about the final values of x and y without knowing their initial values.

22 public static void printVals(int n) {
if (n > 0) { int x = readInt(); printVals(n-1); if (x > 0) System.out.print(x + “ “); } } Assume that the input file reads: What is printed as a result of the call printVals(3)? 10 20 20 10


Download ppt "2/26/12 Quiz Bowl."

Similar presentations


Ads by Google