/
2/26/12  Quiz Bowl Assume 2/26/12  Quiz Bowl Assume

2/26/12 Quiz Bowl Assume - PowerPoint Presentation

olivia-moreira
olivia-moreira . @olivia-moreira
Follow
346 views
Uploaded On 2018-11-21

2/26/12 Quiz Bowl Assume - PPT Presentation

that A is a nonempty rectangular array of ints  Consider the following code segment for int col 0 col ltA0length col     for int row0 rowltAlength1 ID: 732169

int public code class public int class code compile arraylist void string add error values integer return length cast

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "2/26/12 Quiz Bowl Assume" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

2/26/12 Quiz BowlSlide2

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 orderB.    When no row of A contains duplicate valuesC.    When every column of A is sorted in increasing orderD.    When no column of A contains duplicate valuesE.    When neither diagonal of A contains duplicate valuesSlide3

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 onlyB.    II onlyC.    III onlyD.    I and II onlyE.    II and III onlySlide4

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.Slide5

Consider

the following code:

ArrayList menu = new ArrayList();Coffee c;Beverage b = new Beverage();menu.add(b);statementAssume 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));Slide6

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.Slide7

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, 1B.  a:  3, 4           b:  3, 4, 1C.  a:  3, 4, 1       b:  3, 4D.  a:  3, 4           b:  3, 4E.  None of the above.Slide8

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;    }       

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 onlyC.  III onlyD.  I and II onlyE.  I, II, and IIIpublic class

SubClass

extends

SuperClass

{

    public void

addValue

(

int

moreValue

){

        ...

    }Slide9

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.Slide10

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 classSlide11

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];       }   }}Slide12

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 onlyB.  II onlyC.  III onlyD.  I and II onlyE.  II and III onlySlide13

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 onlyII onlyI and III and IIIII and IIISlide14

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); a.add(“start”);Slide15

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)?bcddcbabcddcbadddSlide16

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)Slide17

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();}Slide18

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 arrayRemoving a value from the arrayPrinting all values in the arrayComputing the sum of all values in the arraySlide19

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)?1357No value is returned because an infinite recursion occursSlide20

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?NeverWhenever x = yWhenever x < yWhenever x > yWhenever both x and y are oddSlide21

x = !y;

y = !x;

x = !y;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 xThe 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.Slide22

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: 10 -10 20 -20 30 -30What is printed as a result of the call printVals(3)?10 2020 1010 20 3010 -10 2020 -10 10