AP Computer Science A Fall Final Review

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

for (int k = 0; k < 20; k = k + 2) { if (k % 3 == 1) { System.out.print(k + " "); } } What is printed as a result of executing the code segment? a) 4 16 b) 4 10 16 c) 0 6 12 18 d) 1 4 7 10 13 16 19 e) 0 2 4 6 8 10 12 14 16 18

b) 4 10 16

Consider the following Util class, which contains two methods. The completed sum1D method returns the sum of all the elements of the 1-dimensional array a. The incomplete sum2D method is intended to return the sum of all the elements of the 2-dimensional array m. public class Util { /** Returns the sum of the elements of the 1-dimensional array a */ public static int sum1D(int[] a) { /* implementation not shown */ } /** Returns the sum of the elements of the 2-dimensional array m */ public static int sum2D(int[][] m) { int sum = 0; /* missing code */ return sum; } } Assume that sum1D works correctly. Which of the following can replace /* missing code */ so that the sum2D method works correctly? I. for (int k = 0; k < m.length; k++) { sum += sum1D(m[k]); } II. for (int[] row : m) { sum += sum1D(row); } III. for (int[] row : m) { for (int v : row) { sum += v; } } a) I only b) II only c) I and II only d) II and III only e) I, II, and III

e) I, II, and III

Declaration

int x; Student Vinh; (No specific information, just declared) - There is no default value for local variables For instance variables: - Primitive data types (int, long, double) are automatically set to 0 or 0.0 - Reference variables (String, ArrayList) are set to null. You CAN have String x = null;

Dynamic Binding

making a run time decision about which instance method to call. (different parameters)

Accessor/get methods

public methods to access private instance variables. Has a return type, no parameters. e.g: public int getX(){ return x; }

Mutator/set methods

public methods to mutate private instance variables. Has void return type and parameters. e.g: public void setX(int x){ this.x = x; }

super keyword

refers to the superclass. e.g: super.execute() refers to the superclass' execute() method instead of the current class.

clear();

removes all items

remove(integer parameter); or remove(Object parameter)

removes item at int param or remove the *FIRST OCCURRENCE* of Object param and all elements are shifted to fill in gap

Arrays[integer n].length

returns the length of row n of the 2D array

Arrays.length

returns the length of the array if 1D. returns the number of ROWS of the array if 2D.

size();

returns the size of the arraylist

s1.replace(c1, c2);

A string consisting of all the character from s1, exempt that all occurrences of character c1 will be replace by character c2

s1.substring(i, j);

A string consisting of characters starting at i and ending at the character located at j‐1 (j exclusive) from s1.

Relationship between List, ArrayList, and Collections

ArrayList implements List, List extends Collections

Declaring an ArrayList

ArrayList<Integer> l = new ArrayList<Integer>();

string1.equals(string2)

Checks to see if the two strings are the same (don't use ==)

string1.equalsIgnoreCase(string2)

Checks to see if the two strings are the same, ignores case (don't use ==)

string1.compareTo(string2);

Compare s1 and s2 using their ASCII values. Returns an integer n. If n==0, the strings are equal. n>0 if string1 is greater than string2 and vice versa.

Is Doge a Pet or a Dog in this line of code? Pet Doge = new Dog();

Pet, you can't call methods that are in Dog but not in Pet on Doge

s1.length();

Determine the length of the string in s1.

Arrange the following from first to last in the ASCII chart: - Digits - Small Letters - Capital Letters

Digits, capital letters, small letters.

extend vs implement

Extend ONE, implement multiple.

Are constructors inherited?

NO, constructors are NOT inherited. Superclass MUST have a default constructor otherwise there will be an error.

set(int index,E element)

Replace element at index with element E in an ArrayList.

Math.random()

Returns a random number between 0 (inclusive) and 1 (exclusive)

s1.lastIndexOf(s2);

Returns an integer>=0 representing the location of the last occurrence of string s2 in string s1. If s2 is not contained in s1, lastIndexOf returns ‐1

s1.charAt(i);

Returns the character located at position i from string s1

s1.indexOf(s2)

Returns the location of String or char s2 in s1.

Collections.sort(List)

Sort ascending order (smallest to largest)

s1.substring(i);

String from index i to the end of string s1. Example: String s1 = "Vinh is cool", s1.substring(5) returns "is cool".

Polymorphism

The mechanism of selecting the appropriate method for a particular object in a class hierarchy. Dynamic/Late Binding or Static/Early Binding. Example 1: Student a = new Student("Vinh"); Student b = new Student("Vihn"); a.computeGrades({99,100,95}); b.computeGrades({50,76,81}); //This is dynamic binding because the parameters with which the .computeGrades() method runs is determined at run time. Example 2: String a = "AP Comp Sci A"; a.subString(3); a.subString(1,4); //This is static binding because the compiler chooses which method to run (overloading). public String subString(int a,int b){ /* implementation */ } public String subString(int a){ /* implementation */}

Scope of variables

WHAT EXIST WITHIN THE BRACES STAY IN THE BRACES.

Can subclasses override methods in their superclass?

Yes, subclasses can override methods in their superclass

Which of the following expressions is equivalent to !(c || d) ? a) (!c) && (!d) b) (c || d) c) (c && d) d) !(c && d) e) (!c) || (!d)

a) (!c) && (!d)

Consider the following declarations. public interface Shape { int isLargerThan(Shape other); // Other methods not shown } public class Circle implements Shape { // Other methods not shown } Which of the following method headings of isLargerThan can be added to the declaration of the Circle class so that it will satisfy the Shape interface? I. public int isLargerThan(Shape other) II. public int isLargerThan(Circle other) III. public boolean isLargerThan(Object other) a) I only b) II only c) III only d) I and II only e) I, II, and III

a) I only

public class TimeRecord { private int hours; private int minutes; // 0 < minutes < 60 /** Constructs a TimeRecord object. @param h the number of hours Precondition: h > 0 @param m the number of minutes Precondition: 0 < m < 60 */ public TimeRecord(int h, int m) { hours = h; minutes = m; } /** @return the number of hours */ public int getHours() { /* implementation not shown */ } /** @return the number of minutes Postcondition: 0 < minutes < 60 */ public int getMinutes() { /* implementation not shown */ } /** Adds h hours and m minutes to this TimeRecord. @param h the number of hours Precondition: h > 0 @param m the number of minutes Precondition: m > 0 */ public void advance(int h, int m) { hours = hours + h; minutes = minutes + m; /* missing code */ } // Other methods not shown -- Which of the following can be used to replace /* missing code */ so that advance will correctly update the time? a) minutes = minutes % 60; b) minutes = minutes + hours % 60; c) hours = hours + minutes / 60; minutes = minutes % 60; d) hours = hours + minutes % 60; minutes = minutes / 60; e) hours = hours + minutes / 60;

c) hours = hours + minutes / 60; minutes = minutes % 60;

public class Dog { public void act() { System.out.print("run "); eat(); } public void eat() { System.out.print("eat "); } } public class UnderDog extends Dog { public void act() { super.act(); System.out.print("sleep "); } public void eat() { super.eat(); System.out.print("bark "); } } Assume that the following declaration appears in a class other than Dog. Dog fido = new UnderDog(); What is printed as a result of the call fido.act() ? a) run eat b) run eat sleep c) run eat sleep bark d) run eat bark sleep e) Nothing is printed due to infinite recursion.

d) run eat bark sleep

int[][] mat = new int[3][4]; for (int row = 0; row < mat.length; row++) { for (int col = 0; col < mat[0].length; col++) { if (row < col) { mat[row][col] = 1; } else if (row == col) { mat[row][col] = 2; } else { mat[row][col] = 3; } } } What are the contents of mat after the code segment has been executed? a) { {2, 1, 1}, {3, 2, 1}, {3, 3, 2}, {3, 3, 3} } b) { {2, 3, 3}, {1, 2, 3}, {1, 1, 2}, {1, 1, 1} } c) { {2, 2, 3, 3}, {1, 2, 3, 3}, {1, 1, 2, 3} } d) { {2, 1, 1, 1}, {3, 2, 1, 1}, {3, 3, 2, 1} } e) { {1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3} }

d) { {2, 1, 1, 1}, {3, 2, 1, 1}, {3, 3, 2, 1} }

String a = "Hello World"; System.out.print(a.subString(3,3)); What is printed as a result of executing the code segment?

empty string. Java prioritizes 2nd index.

get(integer parameter);

gets the item at the specified index

Instantiation

int x = 5; Student Vinh = new Student(); (Specific information, can call methods on these).

Static Binding

making a compile-time decision about which instance method to call. (method overloading)

this keyword

refers to the class (instance variables). e.g: public void setX(int x){ this.x = x; //set the instance variable equals to the local x. }

\t

tab escape sequence

Encapsulation

the hiding of implementation details (setting instance variables/methods to private)

Inheritance

- Subclasses can override inherited methods. - Subclasses will always prioritize methods in the class itself over the superclass methods. example: public class Dog extends Pet{ } If Pet doesn't have fetch() method, this will be illegal: Pet a = new Dog(); a.fetch(); // <= a is a Pet, not a Dog, though it's treated as a Dog

Consider the following code segment. List<String> list 5 new ArrayList<String>(); list.add("P"); list.add("Q"); list.add("R"); list.set(2, "s"); list.add(2, "T"); list.add("u"); System.out.println(list); What is printed as a result of executing the code segment? (a) [P, Q, R, s, T] (b) [P, Q, s, T, u] (c) [P, Q, T, s, u] (d) [P, T, Q, s, u] (e) [P, T, s, R, u]

(c) [P, Q, T, s, u]

private List<Integer> nums; /** Precondition: nums.size > 0 */ public void numQuest() { int k = 0; Integer zero = new Integer(0); while (k < nums.size()) { if (nums.get(k).equals(zero)) nums.remove(k); k++; } } Assume that List nums initially contains the following Integer values: [0, 0, 4, 2, 5, 0, 3, 0] What will List nums contain as a result of executing numQuest ? (a) [0, 0, 4, 2, 5, 0, 3, 0] (b) [4, 2, 5, 3] (c) [0, 0, 0, 0, 4, 2, 5, 3] (d) [3, 5, 2, 4, 0, 0, 0, 0] (e) [0, 4, 2, 5, 3]

(e) [0, 4, 2, 5, 3]

Switch statement

- break; stops the case. - If no break statement then subsequent cases will execute. In this example, if x were to equal 2 then case 2 and case 3 would execute. switch( x ) { case 1: System.out.println("one"); break; case 2: x = 5; case 3: System.out.println("a value"); break; default: System.out.println(" occurs when x does not equal a case value"); } - The default case is selected when the x value does not equal any case. - You cannot have case values the same or an error will result.

Abstract class

- cannot be instantiated (can't create an object) - can extend or be extended by other abstract and concrete classes. Can extend only *ONE* other class - can contain both abstract and concrete methods - can implement interfaces - variable and methods can vary

Interface

- cannot be instantiated (can't create an object) - can extend or be extended by other interfaces. - *can extend MULTIPLE interfaces* (unlike [abstract] classes) - cannot implement other interfaces. - *can contain ONLY public static final variables* - methods can be static, public, default or protected. But can't be private (Java 8)

Random class

- should be instantiated as an instance variable - .nextInt(int a) returns a random integer from 0 (inclusive) to a (exclusive).

At a certain high school students receive letter grades based on the following scale: 93 or above is an A. 84 to 92 is a B. 75 to 83 is a C. below 75 is an F. Which of the following code segments will assign the correct string to grade for a given integer score? I. if (score >= 93) grade = "A"; if (score >= 84 && score <=92) grade = "B"; if (score >=75 && score <= 83) grade = "C"; if (score < 75) grade = "F"; II. if (score >= 93) grade = "A"; if (score >= 84) grade = "B"; if (score >=75) grade = "C"; if (score < 75) grade = "F"; III. if (score >= 93) grade = "A"; else if (score >= 84) grade = "B"; else if (score >=75) grade = "C"; else grade = "F"; a) I and III only b) II only c) III only d) I and II only e) I, II, and III

a) I and III only

A car dealership needs a program to store information about the cars for sale. For each car, they want to keep track of the following information: number of doors (2 or 4), whether the car has air-conditioning, and its average number of miles per gallon. Which of the following is the best object-oriented program design? (a) Use one class, Car, with three instance variables: int numDoors, boolean hasAir, and milesPerGallon. (b) Use four unrelated classes: Car, Doors, AirConditioning, and MilesPerGallon. (c) Use a class Car with three subclasses: Doors, AirConditioning, and MilesPerGallon. (d) Use a class Car, with a subclass Doors, with a subclass AirConditioning, with a subclass MilesPerGallon. (e) Use three classes: Doors, AirConditioning, and MilesPerGallon, each with a subclass Car.

a) Use one class, Car, with three instance variables: int numDoors, boolean hasAir, and milesPerGallon.

List<String> animals = new ArrayList<String>(); animals.add("dog"); animals.add("cat"); animals.add("snake"); animals.set(2, "lizard"); animals.add(1, "fish"); animals.remove(3); System.out.println(animals); What is printed as a result of executing the code segment? a) [dog, fish, cat] b) [dog, fish, lizard] c) [dog, lizard, fish] d) [fish, dog, cat] e) The code throws an ArrayIndexOutOfBoundsException exception.

a) [dog, fish, cat]

public void changer(String x, int y) { x = x + "peace"; y = y * 2; } public void test() { String s = "world"; int n = 6; changer(s, n); /* End of method */ } When the call test() is executed, what are the values of s and n at the point indicated by /* End of method */ ? a) s: "world", n: 6 b) s: "worldpeace", n: 6 c) s: "world", n: 12 d) s: "worldpeace", n: 12 e) s: "peace", n: 12

a) s: "world", n: 6

Which of the following is equivalent to the code segment below? (trick question) if (x > 0) x = -x; if (x < 0) x = 0; a) x = 0; b) if (x > 0) x = 0; c) if (x < 0) x = 0; d) if (x > 0) x = -x; else x = 0; e) if (x < 0) x = 0; else x = -1;

a) x = 0;

add(Object parameter)

adds item to arraylist

public

can be accessed anywhere

private

can only be accessed within the class

default

can only be accessed within the package

protected

can only be accessed within the package AND subclasses in other packages

Consider the following output. 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5 Which of the following code segments will produce this output? a) for (int j = 1; j <= 5; j++) { for (int k = 1; k <= 5; k++) { System.out.print(j + " "); } System.out.println(); } b) for (int j = 1; j <= 5; j++) { for (int k = 1; k <= j; k++) { System.out.print(j + " "); } System.out.println(); } c) for (int j = 1; j <= 5; j++) { for (int k = 5; k >= 1; k--) { System.out.print(j + " "); } System.out.println(); } d) for (int j = 1; j <= 5; j++) { for (int k = 5; k >= j; k--) { System.out.print(j + " "); } System.out.println(); } e) for (int j = 1; j <= 5; j++) { for (int k = j; k <= 5; k++) { System.out.print(k + " "); } System.out.println(); }

d) for (int j = 1; j <= 5; j++) { for (int k = 5; k >= j; k--) { System.out.print(j + " "); } System.out.println(); }

Consider the following declaration for a class that will be used to represent points in the xy-coordinate plane. public class Point { private int x; // x-coordinate of the point private int y; // y-coordinate of the point public Point() { x = 0; y = 0; } public Point(int a, int b) { x = a; y = b; } // Other methods not shown } The following incomplete class declaration is intended to extend the above class so that points can be named. public class NamedPoint extends Point { private String name; // name of point // Constructors go here // Other methods not shown } Consider the following proposed constructors for this class. I. public NamedPoint() { name = ""; } II. public NamedPoint(int d1, int d2, String pointName) { x = d1; y = d2; name = pointName; } III. public NamedPoint(int d1, int d2, String pointName) { super(d1, d2); name = pointName; } Which of these constructors would be legal for the NamedPoint class? a) I only b) II only c) III only d) I and III only e) II and III only

d) I and III only

private int[] array; /** Precondition: array.length > 0 */ public int checkArray() { int loc = array.length / 2; for (int k = 0; k < array.length; k++) { if (array[k] > array[loc]) { loc = k; } } return loc; } Which of the following is the best postcondition for checkArray ? a) Returns the index of the first element in array array whose value is greater than array[loc] b) Returns the index of the last element in array array whose value is greater than array[loc] c) Returns the largest value in array array d) Returns the index of the largest value in array array e) Returns the index of the largest value in the second half of array array

d) Returns the index of the largest value in array array

private int[] numbers; /** Precondition: numbers contains int values in no particular order. */ public int mystery(int num) { for (int k = numbers.length − 1; k >= 0; k−−) { if (numbers[k] < num) { return k; } } return -1; } Which of the following best describes the contents of numbers after the following statement has been executed? int m = mystery(n); a) All values in positions 0 through m are less than n. b) All values in positions m+1 through numbers.length-1 are less than n. c) All values in positions m+1 through numbers.length-1 are greater than or equal to n. d) The smallest value is at position m. e) The largest value that is smaller than n is at position m.

e) The largest value that is smaller than n is at position m.


Set pelajaran terkait

Chapter 10 Psychology for Exam 2

View Set

Lesson 26: Overview of Hypothesis Testing

View Set

AP Psych Unit 12 (Modules 65-69) Review, MC, FRQ

View Set

Peds Module 1 Practice Questions

View Set

Ch11 - The Diversity of Bacteria and Archaea

View Set

Nutrition- digrestion and absorbtion

View Set