2/26/12 Quiz Bowl.

Slides:



Advertisements
Similar presentations
CSCI 160 Midterm Review Rasanjalee DM.
Advertisements

Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Final Review.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Quiz 1 What is this? (explain the use of the reserved word “this” in a class method). Answer each question briefly. – What is a Constructor? –Under what.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 1 Class 14 - Review: Sorting & searching r What are sorting and searching? r Simple sorting algorithms.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Inheritance and Polymorphism
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
100e Hosted by vhs © Don Link, Indian Creek School, 2004 Jeopardy.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 5:Interfaces and Abstract Classes
Lecture 18: Nested Loops and Two-Dimensional Arrays
Sorting and "Big Oh" ASFA AP Computer Science A SortingBigOh.
Sorting Mr. Jacobs.
The need for Programming Languages
Sixth Lecture ArrayList Abstract Class and Interface
Chapter Goals To be able to declare and use interface types
Objects as a programming concept
Interface, Subclass, and Abstract Class Review
Inheritance and Polymorphism
Specifications What? Not how!.
Interfaces.
CSC 113 Tutorial QUIZ I.
Java Programming Language
Polymorphism.
Lecture 5: complexity reading:
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
Lecture 13: Two-Dimensional Arrays
Code Refresher Test #1 Topics:
CMSC 202 Generics.
Chapter 9 Carrano Chapter 10 Small Java
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism Part 2
Two-Dimensional Arrays
First Semester Review.
A type is a collection of values
Warmup Which of the 4 sorting algorithms uses recursion?
Presentation transcript:

2/26/12 Quiz Bowl

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

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

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.

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

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.

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.

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

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.

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

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

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

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

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

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

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)

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

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

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

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

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.

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