Final Exam Studying AP Computer Science

Ace your homework & exams now with Quizwiz!

Consider the following program segment: int array2D[ ] [ ] = {11,22,33}, {44,55,66}. {77.88.99}; int x = matrix.length; for (int col = 1; col < x; col++) for (int row = 0; row < x; row++) array2D[row][col] = array2D[row][col-1]; After the previous code executes, which of the following is true?

All of the values in each row are the same.

Consider the following code segment. int k = 3; while(k >= 0 && k % 2 == 1) { System.out.print(k + ", "); k = 2 * k - 3; } What is printed as a result of executing this code segment?

An unending sequence of 3's

Consider the following code fragment: int x = 30; int y = 12; int z = 10; System.out.print("Answer is: " + x + y + z); What would be printed?

Answer is: 301210

Consider the following statements: String s1 = "One"; String s2 = "Two"; s2 = s1; s1 = "Three"; After the code above executes, what will the value of the object referenced by s2?

"One"

The following Boolean expression: !((A < B) || (C > D)) is equivalent to which of the following expressions?

(A >= B) && (C <= D)

Consider the following code: public static void zebra(int x) { if (x >= 0) zebra(x-1); System.out.print(x + " "); } Which of the following is printed as a result of the call zebra(6)?

-1 0 1 2 3 4 5 6

What will be the result of the following assignment statement? Assume x = 4 and y = 10. int z = x * (-y + 2) / 2;

-16

Consider the following code segment: int z = 10 int y = 3 boolean b = true; for (int i = 0; i < 15; i += 5) { z = z + y; b = (z % y == 2) if (!b) { y++; i += 5; } } What is the value of z after the code segment executes?

17

Consider the following code segment: int a = 10; double b = 3.7; int c = 4; int x = (int) (a + b); double y = (double) a / c; double z = (double) (a / c); double w = x + y + z; System.out.println(w); What is printed as a result of evaluating the code above?

17.5

Consider the following method. public static int method99 (int x) { while ( x < 5) { int z = 1; while (z < 5) { z++; x += z; } } return x; } What value is returned as a result of the call method99 (4)?

18

Consider the following code segment: String words [] = {"avocado", "buffet", "carrot", "center", "meaning", "couch", "furniture", "sleep", "peccadillo", "friendly", "potatoes"}; int x = 0; for(int i = 0; i < words.length; i++) { if (words[i].substring(0,3).indexOf('o') >= 0) x++; } System.out.println(x); What is the output?

2

Consider the following program segment: int array2D[ ] [ ] = {1,2,3}, {4,5,6}. {7.8.9}; int x = matrix.length; for (int y = 1; y < x-1; y++) System.out.print(array2D[y][x—y-1] + " "); What will the output be when the program statement listed executes?

2 4

Consider the following method: public static int myMethod(int a, int b) { System.out.println(a + " " + b); if (a > b) return myMethod(a, Math.abs(b) + 1); else if (b > a) return myMethod(Math.abs(a) + 1, b); return 1; } What is output with the method call, myMethod(2, 5);?

2 5 3 5 4 5 5 5

Consider the following code segment: int[][] numarray = { { 2, 4, 2 }, { 0, 0, 0 } }; System.out.print(numarray[0][0]); System.out.print(numarray[1][0]); What is the output of the given code segment?

20

Consider the following two methods. public static void main(String args[]) { int x = 5; x = method22(x); System.out.println(x); } public static int method22(int y) { for (int z = 1; z <= 5; z ++) y += z; return y; } What is printed as a result of executing the main method?

20

Examine the following array: int[ ] numbers = new int{65, 42, 70, 61, 23, 54} Which of the following lists of numbers would accurately show the array after the second pass through the Selection Sort algorithm?

23, 42, 70, 61, 65, 54

e following method. public static int method99 (int z) { int count = 1; for (int y = z; y < 2*z; y++) count += count; return count; } What value is returned as a result of the call method99(x), if x > 0?

2^x

Consider the following code segment. final int[] theArray = {1, 2}; theArray[1] = 3; System.out.print(theArray[1]); What is printed as a result of executing the code segment?

3

Consider the following code segment. int x = <an integer greater than zero> ; int n = 0; if (x < 1000) { if (x > 1500) n = 200; else n = 300; } else { if ( x < 500) n = 400; else n = 300; } System.out.println (n); What is printed as a result of executing the code segment?

300

Consider the following code segment: int x = 0; for (int i = 0; i < 10; i++) { for (int k = 0; k <= 5; k++) { for (int z = 1; z <= 16; z = z *2) { x++; } } } What is the value of x after the code segment executes?

300

Consider the following code segment. int[][] array = new int[10][10]; int num = 1; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { array[i][j] = num; num++; } } System.out.println(array[3][3]); What is printed when this code segment is executed?

34

If nums is and int array with two elements and holds the values 14 and 26 respectively, what are their values after change(a) is called? The method change is defined as follows: public void change(int[] x) { x[0] = (int)(100.0 * x[0] / (x[0] + x[1])); x[1] = (int)(100.0 * x[1] / (x[0] + x[1])); }

35 and 42

Consider the array declaration that follows. int[] a = {1, 3, 5, 6, 7, 8, 9, 11, 13, 14, 15}; How many comparisons would it take if you were to search the array for 3 using a binary search?

4

Consider the following statement: System.out.println("I like to eat,\teat,\neat apples and\nbananas,\tI like to ate,\nate, ate ay-ples\t\\and ba-nay-nays"); How many lines of output will there be in the terminal window?

4

The expression 5 + 6 * 3 % 2 - 1 evaluates to

4

Examine the following array: int[ ] numbers = new int{65, 42, 70, 61, 23, 54} Which of the following lists of numbers would accurately show the array after the two pass through the Insertion Sort algorithm?

42, 65, 70, 61, 23, 54

Consider the following code segment. int answer = 2; for (int x = 0; x < 5; p++) { if (x % 2 !=0) answer—; else answer += 2; } System.out.println(count); What is the output as a result of executing this code segment?

6

Consider the following code segment. int k = 20, i = 0; while(k > 10) { i = 3; while(i < k) { i *= 2; if(i % k == 1) { i += k; } } k -= 3; } System.out.println(k + " " + i); What will be the output after execution of this code segment?

8 23

What is the output of the following code segment? int x = some int value; x = x % 20; if(x < 25) System.out.println("Less than 25"); else System.out.println("Greater or equal to 25");

Less than 25

Consider the following code segment: int list [] = /* missing code */; int val = /* missing code */; int n = -1; for (int i = 0; i < list.length; i++) { if (val == list[i]) { n = i; break; } } What algorithm is shown?

Linier Search

Which of the following is true regarding linear searches?

None of the answers are correct.

Consider the following code segment: int i = 1; int k = 1; while (i < 5) { k *= i; k++; } System.out.print(k); What is printed as a result of executing the code above?

Nothing is printed

Consider the following code segment: int a = 10; double b = 10.7 ; double c = a + b; int d = a + c; System.out.println(c + " " + d); What will be output as a result of executing the code segment?

Nothing will be printed because of a compilation error

What is the output of the following program segment? int bonusPoints = 350; int score = 8000; if(score >=6600) bonusPoints += 300; System.out.println("Bonus: " + bonusPoints);

Bonus: 650

What is printed as a result of executing the following code segment? ArrayList<String> list = new ArrayList<String>(); list.add ("ice cream"); list.add ("candy"); list.add ("popcorn"); list.add ("peanuts"); list.add ("gum"); for (String s: list) if (s.length() > 2 && s.length() <= 5) System.out.print(s.toUpperCase() +" ");

CANDY

What does the String method compareTo() do?

Compares this String with a second String for greater than, equal to, or less than.

You are designing a software solution for a tool rental company. You have decided that the following nouns apply to the requirements: Tool, Customer, Address, Rental Contract, Condition, Rental Date, Daily Rate, Total. Which of these should be represented as instance variables?

Condition, Rental Date, Daily Rate.

Consider the following two classes. class alpha { public alpha() { System.out.println("Executing the alpha constructor"); } } class bravo { private alpha rocket; public bravo() { System.out.println("Executing the bravo constructor"); rocket = new alpha(); } } Assume that the following code segment appears in a client program. What is printed by the code segment? bravo y = new bravo(); alpha z = new alpha();

Executing the bravo constructor Executing the alpha constructor Executing the alpha constructor

Consider the following code segment. The code below is created to display grades as follows: 0 - 74 F 75 - 83 C 84 - 92 B 93 - 100 A int average = <int value such that 0 <=average <=100> ; if (average > 92) System.out.println ("Your grade is A") ; else if (average > 83) System.out.println ("Your grade is B") ; else if ( average > 74) System.out.println ("Your grade is C") ; else System.out.println ("Your grade is F") ; For which values of average does the code segment execute correctly?

For all values in the [0..100] range

The change method is defined as follows: public void change(String[] x) { String temp = x[0].substring(0, 1); x[0] = x[1].substring(0, 1) + w[0].substring(1); x[1] = temp + x[1].substring(1); } What is the output after the following code segment is executed? String[] words = {"Cover", "Hovers"}; change(words): System.out.println(words[0] + " " + words[1]);

Hover Covers

Which of the following correctly adds a new element to the list array? I. ArrayList list = new ArrayList(); list.add(new String("Java")); II. ArrayList list = new ArrayList(); list.add(new Integer(1000)); III. ArrayList list = new ArrayList(); list.add("1000");

I, II and III

Consider the following. ArrayList names = new ArrayList(); names.add("chase"); names.add("steve"); names.add("mary"); names.add("ellen"); for (int k = 1; k <= names.size(); k++) System.out.print(names.get(k) + " "); What's printed?

IndexOutOfBoundsExeption

Consider the following code segment The code below is created to display grades as follows: 0 - 69 F 70 - 79 C 80 - 89 B 90 - 100 A int average = <int value such that 0 <= average <= 100> ; if (average < 100) System.out.println ("Your grade is A") ; else if (average < 90) System.out.println ("Your grade is B") ; else if (average < 80) System.out.println ("Your grade is C") ; else if (average < 70) System.out.println("Your grade is F") ; For which values of average does te code segment execute correctly?

Only for values in the [90..99] range

A class, Purse, is considered to have a dependency on another class, Wallet, under which of the following conditions?

Purse uses objects of Wallet.

The following code segment is intended to sum the first 10 even integers. int sum = 0; for(int k = 2; k <= 10; k++) { sum += 2 * k + 2 } System.out.println(sum); Which of the follow best describes the error, if any in this code?

The segment leaves out the first two even integer and includes the eleventh even integer in the sum.

You have written a program to create a shopping list. As each item is placed into your cart you call method called removeItem and it should remove the item from your list. public static void removeItem(ArrayList<String> list, String remove) { for (String s: list) if (s.equals(remove)) list.remove(s); }

There will most likely be an error because the loop goes past the end of the ArrayList.

You are designing a software solution for a tool rental company. You have decided that the following nouns apply to the requirements: Tool, Customer, Address, Rental Contract, Condition, Rental Date, Daily Rate, Total. Which of these should be represented as classes?

Tool, Customer, Address, Rental Contract.

What is the output of the following code segment? int a = 32, b = 23; int c = 0, d = 0,e = 0; if (a < b) { c = a; } else { c = b; } if (a < b + 10) { d = a; } else if (a < b + 20) { e = a; } System.out.println("a = " + a + ", b = " + b + ", c = " + c + ", d = " + d + ", e = " + e);

a = 32, b = 23, c = 23, d = 32, e = 0

What will be the result when the following assignment statement is executed? int x= 50 / 10.00;

a run-time error arises because x is an int and 50 / 10.00 is not an int

Of the following data types, which one cannot store a numeric value?

char

Consider the following. ArrayList names = new ArrayList(); names.add("chase"); names.add("steve"); names.add("mary"); names.add("ellen"); for (int p = 0; p < names.size(); p++) System.out.print(names.set(p, "sarah") + " "); System.out.println(); for (int p = 0; p < names.size(); p++) System.out.print(names.get(p) + " "); What is printed?

chase steve mary ellen sarah sarah sarah sarah

Consider the following code fragment: double divisor = 21 / 5; System.out.println("21 / 5 = " + divisor); The terminal window displayed: 21 / 5 = 4.0 The programmer wanted the output to be: 21 / 5 = 4.2 Which of the following will NOT correct the error?

double divisor = (double)(21 / 5);

Consider the following program segment. String s1 = new String("ButterFly"); String s2 = s1.substring(4,5); System.out.println(s2); What will be output when the program segment executes?

e

Consider the following program segment. String s1 = new String("ButterFly"); int start = s1.indexOf("F"); String s2 = s1.substring(start); System.out.println(s2); What will be output when the program segment executes?

fly

Consider the following output: 0 1 0 2 4 0 3 6 9 0 4 8 12 16 Which of the following code segments will produce this output?

for (int x = 1; x < 5; x++) { for (int z = 0; z <= x; z++) { System.out.print(x * z + " "); } System.out.println(" "); }

Methods that change the values of the instance variables are called ____ methods.

mutator

If num1 = 5 and num2 = 5.0, all of the following are legal except which assignment statement?

num1= num2;

Consider the following. ArrayList names = new ArrayList(); names.add("chase"); names.add("steve"); names.add("mary"); names.add("ellen"); System.out.print(names.set(1, "phil"); for (int k = 0; k < names.size(); k++) System.out.print(names.get(k) + " "); What is displayed?

phil chase phil mary ellen

A well-designed class uses ____ instance variables, accessor methods, and (if needed) mutator methods to implement the OOD principle of encapsulation.

private

In object-oriented programming, data is broken down into four main areas: public variables, public methods, private variables, and private methods. Encapsulation utilizes:

private methods and private variables

A(n) ____ method is accessible to a class's descendants, but not to any other classes in the hierarchy.

protected

Consider the following code segment: public abstract class Appliance { public abstract void setRPMs(); /* other possible abstract methods */ } You wish to create a subclass named SmallAppliance. Which of the following is the correct way to declare this subclass?

public class SmallAppliance extends Appliance { public void setRPMs() { /* missing code */ } }

Members of a class are classified into three categories, they are:

public, private, and protected

Consider the following class. class Apple { public int counter; public Apple(int x) { counter = x; } public String toString() { return "Apples " + counter; } } What type of inheritance concepts is demonstrated by the Aardvark class declaration?

redefining an inherited method

Consider the following code segment String string1 = "Hello"; String string2 = "World"; string2 = string1; string1 += "There!"; After the code is executed, what are the values of string1 and string2?

string1 = "HelloThere!", string2 = "Hello"

Consider the following code segment. int z = some integer value double a = z; double c = Math.pow(Math.sqrt(a), 2); return (a == c); Which of the following statements about the code segment is true?

true or false may be returned, depending on roundoff errors.

Consider the following: public static int echo(int x, int y) { if (y == 1) return x; else return x + echo(x,y-1); } Assume a precondition of x > 0 and y > 0. Which of the following expressions represents the result of calling method echo?

x * y

Assume that x and y are integer variables and have been properly initialized. !(x <= Y) && ( x * y > 0) will always evaluate to true if

x > y and y > 0

! ((x < y) && (w == z)) is the same as which boolean expression?

x >= y || w != z

Consider the following code: public static int echo(int x, int y) { if (y == 1) return x; else return x * echo(x, y-1); } Assume a precondition of x > 0 and y > 0. Which of the following expressions represents the result of calling method echo?

x^y

A method of a class that only retrieves the value(s) of the data member(s) is known as a(n) ____ method.

accessor

Consider the following declaration: double [ ] [ ] array; int r; //the row index on array int c; //the column index on array Which of the following statements stores the number of rows of array?

array.length

Consider the following declaration: double [ ] [ ] array; int r; //the row index on array int c; //the column index on array Which of the following statements stores the number of columns of array?

array[0].length

Suppose the abstract class Message is defined below public abstract class Message { private String value; public Message(String initial) { value = initial; } public String getMessage() { return value; } public abstract String autoCorrect(); } A child subclass of Message, TextMessage, is defined. Which methods must TextMessage define?

autoCorrect() only

Consider the following code segment: if (s.toLowerCase().equals( s.toUpperCase() )) System.out.println("*"); What does the code do?

Tests if the String s contains only non-letter characters.

Consider the following code segment: for (int x = 1; x < 100; x = x * 2) { if (x / 50 = = 0) System.out.print(x + " "); } What is printed as a result of executing the code segment?

1 2 4 8 16 32

Consider the following code. public class alpha { public static void main(String args[]) { for (int x = 1; x <=10; x++) System.out.print(beta(x) + " "); } public static int beta(int n) { int temp = 0; for (int y = 0; y < n; y++) for (int z = 0; z < n-y; z++) temp++; return temp; } } What is printed as a result of executing this program?

1 3 6 10 15 21 28 36 45 55

Consider the following program code. public class Delta { public static void main(String args[]) { int x = 5; int y = 10; System.out.println(x + " " + y); swap (x,y); System.out.println(x + " " + y); } public static void swap(int a, int b) { int temp = a; a = b; b = temp; } } What is printed as a result of executing the program?

10 20 10 20

Consider the following method. public static int method88 (int x) { int count = 0; for (int p = 1; p <= x; p++) for (int q = 0 ; q <=x/2; q++) count++; return count; } What value is returned as a result of the call method88 (5) ?

15

Consider the following Strings: String s1 = "gw"; String s2 = "GW"; String s3 = "George Washington"; Given that G comes before g in dictionary order, which of the following is true?

!( s1.equals(s2) && s3.compareTo(s2) < 0)

Consider the following code fragment: int x = 30; int y = 12; int z = 10; System.out.print( x / y / z); What would be printed?

0

Consider the following code segment: int x = 10; int y = x / 3; int z = x % 2; x++; System.out.println(z) What is printed as a result of executing the code segment above

0

Examine the following recursive method: public static void recur1(int n) { if(n > 0) recur1(n - 2 ); System.out.print(n + " "); } What is the output when the parameter 6 passed into the method?

0 2 4 6

Consider the following method: public void division6 (int[] a, int[] b, int[] c) { b[0] = a[0] / 6; c[0] = a[0] % 6; } What is the value of y[0] after the following statements are executed? int[] x = {25, 26, 27], y = new int[3]; division6 (x, y, y);

1

Consider the following program segment: int array2D[ ] [ ] = {11,22,33}, {44,55,66}. {77.88.99}; for (int[ ] row: matrix) { for (int number: row) System.out.print(number + " "); System.out.println(); What is printed?

1 1 1 1 2 2 2 2 3 3 3 3

Consider the following code segment. int list[] = {1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50}; for (int d = 1; d < list.length; k++) list[d] = list[d] / list[1]; for (int d = 0; d < list.length; k++) System.out.print(list[d] + " "); What will be output when the program segment executes?

1 1 10 15 20 25 30 35 40 45 50

Consider the following code segment and two methods. int square [ ] [ ] = new int [4] [4]; makeSquare(square); showSquare(square); public static void makeSquare(int[ ] [ ] square) { int size = square.length; for (int x = 0; x< size; x++) { int z = x; for (int y = x; y<size; y++) { square [x] [y] = z + y; } } } public static void showSquare(int[ ] [ ] square) { for (int x = 0; x< 5; x++) { for (int y = 0; y<5; c++) System.out.print(square [x] [y] + " "); System.out.prinln(); } } What is printed as a result of executing the code segment?

1 2 3 4 0 1 2 3 0 0 1 2 0 0 0 0

Consider the following code segment: int x = 10; int y = x / 3; int z = x % 2; x++; System.out.println(x) What is printed as a result of executing the code segment above

11

Consider the following two methods. public static void alpha() { int list[] = {111, 999, 222, 888, 333, 777, 444, 666, 555}; beta(list); for (int z = 0; z < list.length; k++) System.out.print(list[z] + " "); System.out.println(); } public static void beta(int list1[]) { int list2[] = list1; int max = list2.length; for (int y = 1; y < max-1; y++) for (int x = 0; x < max-y; x++) { if (list2[x] > list2[x+1]) { int temp = list2[x]; list2[x] = list2[x+1]; list2[x+1] = temp; } } } What is the output when method alpha is called?

111 222 333 444 555 666 777 888 999

An array of integers contains the following elements: 90, 41, -4, 14, 111, 75, 3. What would the array look like after the third pass of a Selection Sort, sorting in descending order?

111, 90, 75, 14, 41, -4, 75, 3

When performing a binary search on an ordered list of 4,050 integers, what is the MAXIMUM number of comparisons that the code must make in order to find a certain integer?

12

Consider the following program code. String b1 = "ABCD"; String b2 = ""; for (int x = 1; x < b1.length(); x++) b2 += b1.substring(0,x); System.out.println(b2); What will be the output when the program segment executes?

AABABC

Consider the following class declaration representing points in the xy-coordinate plane. public abstract class Point { private int myX; private int myY; public abstract void toOrigin(); public static int distance(int newX, int newY { /* method implementation not shown */ } } I. public class NewPoint extends Point { private int x,y; public NewPoint() { x=0; y=0; super(x,y); } public void toOrigin() { x=0; y=0; } } II. public class NewPoint extends Point { private int x,y; public NewPoint() { x=0; y=0; } public void toOrigin() { x=0; y=0; } public static int distance(int x, int y) { return super.distance(x,y); } } III. public class NewPoint extends Point { private int x,y; public NewPoint() { x=0; y=0; } public void toOrigin() { x=0; y=0; } }

II and III

Assuming all variables are declared correctly, which of the following code segments correctly swaps the values x and y? I. x = y; y = x; II. int tmp = x; y = x; x = tmp; III. int tmp = x; x = y; y = tmp;

III only

Consider the following method. public static int gazelle(ArrayList list) // precondition: list contains an Integer object // post condition: gazelle returns sum of the int values in list { int sum = 0; for (int q = 0; q < list.size(); q++) { /* this code is missing */ } return sum; } Which of the following can replace the missing code so the method works correctly? I. sum += list.get(q); II. sum += list.get(q).intValue(); III. Integer temp = (integer) list.get(q); sum += temp.intValue();

III only

Assume the following list of integers: 18, 45, 33, 65, 76, 32, 96, 12, 46, 68 After three iterations, the list looks like this: 12, 18, 32, 65, 76, 33, 96, 45, 46, 68 What type of sort was used?

Selection

Consider the following two methods. public static void garage(int numList[]) { int max = list .length-1; for (int z = 0; z < max; z++) if numList[z] > numList [z+1]) band(numList, z); } public static void method band(int numList[], int y) { int temp = numList[y]; numList[y] = numList[y+1]; numList[y+1] = temp; } Which of the following correctly describes the results of calling method garage?

The largest number is located in numList[max].

If you make a private member public, then anyone can access that member, violating ____.

encapsulation

Making a variable public, when it should be declared private, violates the concept of:

encapsulation

Consider the following two classes. class Person { private int age; public Person(int x) { age = x; } public String toString() { return "Age: " + age + "\n"; } } class Student extends Person { private double gpa; public Student(int x, double y) { super (x); gpa = y; } public String toString() { /* missing code */ } } Assume that the following code segment appears in a client program. Student barry = new Student(16,5.25); System.out.println(barry); Furthermore, assume that the following output should be the result of executing the code segment. Age: 16 GPA: 5.25 Which of the following can be used to replace the /* missing code */ in method toString so that the desired output is achieved?

return super.toString() + "GPA: " + gpa;


Related study sets

psych mind tap and book questions

View Set

Chapter 12: Drug Therapy: Immunizations

View Set

ULTIMATE Linear Algebra 33A Studyset

View Set

Real Property Characteristics, Legal Descriptions, and Property Use

View Set