AP Computer Science A Final Exam

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

The expression !(A || B) is equivalent to which of the following the boolean expressions? --- A !(A && B) B !A || !B C !A && !B D !(!(A || B)) E !(!A || !B)

C !A && !B

Consider the following code segment. int x = 8; int y = 0; int z = 0; while (y <=x) { z+=y; y++; } --- What will be the new value of z after the while loop finishes? --- A 0 B 18 C 36 D 45 E 72

C 36

Consider the following code segment. int[][] heights = new int[3][6]; heights[0][0] = 10; heights[1][0] = 20; heights[2][0] = 30; double total = 0; int count = 0; for (int r=0; r < heights.length; r++) { for (int c=0; c < heights[r].length; c++) { total += heights[r][c]; count++; } } System.out.println((int) (total/count) + ", " + (int) (total/heights.length) + ", " + (int) (total/heights[0].length)); --- Which of the following is the output printed to the console after this segment of code is executed? --- A 20, 20, 10 B 3, 3, 3 C 20, 20, 20 D 10, 10, 10 E 3, 20, 10

E 3, 20, 10

Consider the following code segment. public static void sleek(String str) { if(str.length()>2) { sleek(str.substring(2)); System.out.print(str); } } --- Including this first call of sleek, how many times is the method sleek called when the following statement is executed? sleek("scare"); --- A 1 B 2 C 3 D 4 E 5

C 3

Consider the following code segment. ArrayList<Integer> list = new ArrayList<Integer>(); list.add(4); list.add(6); list.add(5); list.add(9); list.add(27); int c=0; for(int i=0; i<list.size(); i++) { if(list.get(i)%3==0) c+=list.get(i)+1; } System.out.println(c); --- What is printed as a result of executing the code segment? --- A 3 B 15 C 42 D 45 E 51

D 45

Consider the following method. // return the number of cities from NY public static int countNYCities(String[][] cities) { int count=0; for (int r=0; r<cities.length; r++) { for (int c=0; c<cities[r].length; c++) { if (cities[r][c].indexOf ("NY")>=0) count++; } } return count; } --- Which of the following is the BEST description of this method? --- A For each column in cities, the method would loop through all the rows. The total number of loops is cities[0].length-1 multiplied by cities.length-1 B For each row in cities, the method would loop through all the columns. The total number of loops is cities.length-1 multiplied by cities[0].length-1 C For each column in cities, the method would loop through all the rows. The total number of loops is cities[0].length multiplied by cities.length D For each row in cities, the method would loop through all the columns. The total number of loops is cities.length multiplied by cities[0].length E This algorithm will not loop through all the row/column combinations in the cities 2-dimensional array.

D For each row in cities, the method would loop through all the columns. The total number of loops is cities.length multiplied by cities[0].length

Which of the following segments of code will print 75.2 to the console? --- A double num = 75.0; String str = "" + num + 0.2; System.out.println(str); B int num = 75; String str = num + 0.2; System.out.println(str); C double num = 75.0; String str = ("" + num) + 0.2; System.out.println(str); D String num = "75.0"; String str = num + 0.2; System.out.println(str); E double num = 75.0; String str = "" + (num + 0.2); System.out.println(str);

E double num = 75.0; String str = "" + (num + 0.2); System.out.println(str);

Consider the following code. String[] people = {"Hank", "George", "Carol", "Johnsie"}; int d = 0; for (int i=1; i<people.length; i++) { d+=people.length; } System.out.println(d); --- What is printed as a result of executing this code? --- A 12 B 16 C 18 D 22 E A syntax error is encountered.

A 12

Consider the following method. public static int juggle(int a, int b) { if (a<b) return 1+juggle(a+1, b); else if (b<a) return 1+juggle(a, b+1); return 1; } --- Which of the following statements will result in calling juggle the least number of times? --- A juggle(22, 20); B juggle(-9, -5); C juggle(1, 4); D juggle(6, 10); E juggle(1, -2);

A juggle(22, 20);

Consider the following code segment. int num1=3, num2=5, num3=4; System.out.println( num1/num3-num2*num1/(num3+1)+10/4*2); --- What is printed to the console as a result of executing the code segment? --- A 1.75 B 4 C 1 D 2.75 E 7

C 1

Consider the following two declarations. int k=0; String[] teams = {"lions", "vikings", "bears", "titans", "packers"}; --- Which segment of code would print "snail" to the console? --- A for (int i=0; i<teams.length; i++) { System.out.print(teams[i].substring(teams[i].length()-k-1, teams[i].length()-k)); k++; } B for (int i=0; i<teams.length; i++) { System.out.print(teams[i].substring(teams[i].length()-k, teams[i].length())); k++; } C for (int i=teams.length-1; i>=0; i--) { System.out.print(teams[i].substring(teams[i].length()-k-1, teams[i].length()-k)); k++; } D for (int i=teams.length-1; i>=0; i--) { System.out.print(teams[i].substring(teams[i].length()-k, teams[i].length())); k++; } E for (int i=teams.length-1; i>=0; i--) { System.out.print(teams[i].substring(teams[i].length()-k-1)); k++; }

C for (int i=teams.length-1; i>=0; i--) { System.out.print(teams[i].substring(teams[i].length()-k-1, teams[i].length()-k)); k++; }

Consider the following. public static int countEvensI(int[ ] nums) { int count=0; for (int i=0; i<nums.length; i++) { _________________________ // fill in code here } return count; } --- Which of the following can be used to replace ________//fill in code here so that countEvensI(int[ ] nums) will determine the number of even numbers in the array? --- A count++; B if( nums[i]/2 == 0 ) count=count+1; C if( nums[i]%2 == 0 ) count+=1; D if( i/2 == 0 ) count++; E if( i%2 == 0 ) count+=1;

C if( nums[i]%2 == 0 ) count+=1;

Consider the following class. public class Z { public static int A(int x) { x *= -x; return(x); } public static int B(int x) { x += x; return(x); } public static int C(int x) { x/=2; return(x); } } --- Which of the following is returned as a result of calling Z.C(Z.A(Z.B(Z.C(5)))) executed from another class? --- A -8 B -2 C 0 D 2 E 8

A -8

Consider the following code segment. int[][] matrix = {{1,2,3,4,5,6,7,8,9}, {3,4,5,6,7,8,9,1,2}, {5,6,7,8,9,1,2,3,4}, {7,8,9,1,2,3,4,5,6}}; for (int c=0; c<matrix[0].length; c++) { for (int r=c; r<matrix.length; r++) { System.out.print(matrix[r][c]); } System.out.println(); } --- What is printed as a result of executing the code segment? --- A 1357 2468 3579 4681 5792 6813 7924 8135 9246 B 1357 468 79 1 C 123456789 45678912 7891234 123456 D 1234 456 78 1 E An ArrayIndexOutOfBoundsException is thrown.

B 1357 468 79 1

Consider the following method. public static ArrayList<String> rearrangeList(ArrayList<String> words) { ArrayList<String> outputList = new ArrayList<String>(); for (int i=0; i<words.size(); i++) { if (i%2==1) outputList.add(0, words.get(i)); else outputList.add(words.get(i)); } return outputList; } --- What is the output after running the following section of code? ArrayList<String> wordList = new ArrayList(); wordList.add("hard"); wordList.add("work"); wordList.add("pays"); wordList.add("off"); // rearrangeList called twice below System.out.println(rearrangeList(rearrangeList(wordList))); --- A [hard, work, pays, off] B [pays, work, off, hard] C [off, work, hard, pays] D [off, pays, work, hard] E [off, off, off, off]

B [pays, work, off, hard]

Consider the following method with missing code. public static int sumNums(int a, int b) { int c = 0; while (a <= b) { // missing code } return c; } --- Which of the following segments of code should replace // missing code in the sumNums method so that it returns the sum of the integers between a and b, both included? For example, if a is 1 and b is 3, then the method should return 6, since 1 + 2 + 3 = 6. --- A c+=b; a++; B c+=a; a++; C c+=a; b--; D c+=a; b++; E a+=c; c++;

B c+=a; a++;

Consider the following method. public static int mystery(int x) { if (x > 0) return x + mystery(x - 1); return 0; } --- Which of the following is returned when mystery(5) is called? --- A 0 B 14 C 15 D 16 E 21

C 15

Consider the following code segment. ArrayList<String> list = new ArrayList<String>(); String phrase = "We love AP Computer Science"; int space = phrase.indexOf(" "); while (space>=0) { list.add(phrase.substring(0,space-1)); phrase = phrase.substring(space+1); space = phrase.indexOf(" "); } System.out.println(list); --- What is printed as a result of executing the code segment? --- A [W, lov, A, Compute, Scienc] B [We, love, AP, Computer] C [W, lov, A, Compute] D [We, love, AP, Computer, Science] E "We love AP Computer Science"

C [W, lov, A, Compute]

Consider the following code segment . ArrayList<String> list = new ArrayList<String>(); String word = "flowerbed"; for (int i=0; i<word.length()-1; i++) { list.add(word.substring(i,i+2)); if (i%3==0) i++; } --- What does list contain after the segment of code is executed? --- A [f, o, w, r, b] B [fl, lo, ow, we, er, rb, be, ed] C [flo, wer, bed] D [fl, ow, we, rb, be] E [f, l, o, w, e, r, b, e, d]

D [fl, ow, we, rb, be]

Consider the following code segment. int j=0; String phrase="farmhouse", result=""; while(j<=4) { result += phrase.substring(0,j); j++; } System.out.println(result); --- What will print to the console when the code segment is executed? --- A farmh B ffafarfarmfarmh C farmhousearmhousermhousemhousehouse D ffafarfarm E farmfarfaf

D ffafarfarm

Consider the following code segment. double a=-2.0, b=3.0; double num = Math.sqrt(Math.pow(a, Math.abs(a))) * Math.sqrt(Math.abs(Math.pow(a,b))/2)*-3; System.out.println(num); --- What is output to the console when this code segment is executed? --- A 12.0 B 6.0 C -0.7 D -6.0 E -12.0

E -12.0

Consider the following code segment. String phrase = "Great players are really serious at practice"; System.out.println(phrase.indexOf(phrase.substring(15,17))); --- What is printed as a result of executing the code segment? --- A 1 B re really serious at practice C 15 D re E 18

A 1

Consider the following code segment. int[] arr = new int[30]; for (int i = 0; i < arr.length; i+=2) arr[i] = i*3; --- What is the value of arr[21] after the code segment has been executed? --- A 0 B 3 C 21 D 42 E 63

A 0

What does the following code segment print to the console? int c=0; while (c<6) { System.out.print(c + " + " + c); c++; } --- A 0 + 01 + 12 + 23 + 34 + 45 + 5 B 0 + 1 + 2 + 3 + 4 + 5 C 30 D 0 + 0 + 1 + 1 + 2 + 2 + 3 + 3 + 4 + 4 + 5 + 5 E 1 + 12 + 23 + 34 + 45 + 56 + 6

A 0 + 01 + 12 + 23 + 34 + 45 + 5

Consider the following code. int[][] matrix = {{1,2,3,4,5,6,7,8,9}, {2,3,4,5,6,7,8,9,1}, {3,4,5,6,7,8,9,1,2}, {4,5,6,7,8,9,1,2,3}}; for (int i=0; i<matrix.length; i++) { for (int j=i; j<matrix[0].length; j++) { System.out.print(matrix[i][j]); } System.out.println(); } --- What is printed to the console as a result of executing this code? --- A 123456789 34567891 5678912 789123 B 123456789 234567891 345678912 456789123 C 1 3 5 7 D 1234 345 56 7 E An array index out of bounds error is thrown

A 123456789 34567891 5678912 789123

Consider the following code segment. int[][] matrix = {{2, 5, 3, 1, 4, 6}, {1, 4, 3, 2, 5, 3}, {2, 4, 3, 1, 5, 1}, {5, 4, 3, 1, 2, 5}, {4, 1, 3, 5, 2, 2}}; for (int r=0; r<matrix.length; r++) { for (int c=0; c<matrix[r].length; c++) { if (matrix[r][matrix[r].length-1]>matrix[r][c]) System.out.print(matrix[r][c]); else System.out.print(0); } System.out.println(); } --- What is printed as a result of executing the code segment? --- A 253140 100200 000000 043120 010000 B 000100 000000 000101 043120 013022 C 203100 143203 243101 000100 010000 D 000000 100000 000000 000000 413022 E An error is thrown.

A 253140 100200 000000 043120 010000

Consider the following code. public static void main(String args[]) { int numArray[][]={{3,5,7},{2,4,6},{9,8,7},{6,5,4}}; if (numArray.length==4) { for (int i=0; i<numArray[0].length; i++) { System.out.print(numArray[i][2]); } } else { for(int i=2; i>-1; i--) { System.out.print(numArray[2][i]); } } } --- What is printed as a result of executing the code? --- A 767 B OutOfBoundsException C 548 D 987 E 246

A 767

Consider the following method. public int finder(String[] array, String s) { for(int i=array.length-1; i>=0; i--) { if (array[i].equals(s)) return i; } return -1; } --- Which of the following calls to this method would run the fastest (require the least number of passes through the loop)? --- A String[] a = {"cat", "tiger","zebra"}; int foundPos = finder(a, "zebra"); B String[] a = {"cat", "dog","frog","tiger","zebra"}; int foundPos = finder(a, "dog"); C String[] a = {"cat", "dog","frog","tiger","zebra"}; int foundPos = finder(a, "frog"); D String[] a = {"cat", "dog","frog","tiger"}; int foundPos = finder(a, "frog"); E String[] a = {"cat", "dog","frog","tiger","zebra"}; int foundPos = finder(a, "cat");

A String[] a = {"cat", "tiger","zebra"}; int foundPos = finder(a, "zebra");

Consider the following code. ArrayList<Integer> ratings = new ArrayList<Integer>(); ratings.add(29); ratings.add(0,12); ratings.add(1,4); ratings.add(0,36); ratings.add(2,123); ratings.add(1,54); System.out.println(ratings) --- ; What is printed to the console when the following code is executed? --- A [36, 54, 12, 123, 4, 29] B [36, 12, 54, 123, 4, 29] C [36, 54, 12, 29, 4, 123] D [12, 36, 4, 54, 29, 123] E [29, 12, 4, 36, 123, 54]

A [36, 54, 12, 123, 4, 29]

Which code segment will take an array list of dog types and modify the element to add"dog" to the end of the type if there is no "dog" in the dog type? For example, "collie" becomes "collie dog", and "hound dog" remains as "hound dog". ArrayList<String> dogs = // a list full of dog types --- A for (int i=0; i<dogs.size(); i++) { if (dogs.get(i).indexOf("dog") == -1) dogs.set(i, dogs.get(i) + " dog"); } B for (int i=0; i<dogs.size(); i++) { if (dogs[i].indexOf("dog") == -1) dogs[i] += " dog"); } C for (int i=0; i<dogs.size(); i++) { if (dogs.indexOf("dog") < 0) dogs.add(i, dogs.get(i) + " dog"); } D for (int i=0; i<dogs.size(); i++) { if (dogs.get(i).indexOf("dog") <= 0) dogs.set(i, dogs.get(i) + " dog"); } E for (int i=0; i<dogs.size(); i++) { if (dogs.get(i).contains("dog")) dogs.set(i, dogs.get(i) + " dog"); }

A for (int i=0; i<dogs.size(); i++) { if (dogs.get(i).indexOf("dog") == -1) dogs.set(i, dogs.get(i) + " dog"); }

The following method counts the number of times a value (represented by key) appears as an element in an ArrayList named nums. public static int search(ArrayList<Integer> nums, int key) { int j = 0; for ( /*missing code*/ ) if (key == x) j++; return j; } --- Which of the following should replace /*missing code*/? --- A int x : nums B int x : nums.get(i) C int x = 0; x < nums.length; x++ D int x = 0; x E int x = nums.size() - 1; x >= 0; x--

A int x : nums

Consider the following method. public static void print2DimArray(int[][] nums) { for (int i=0; i<nums.length; i++) { for (int j=0; j<=i; j++) { System.out.println(nums[i][j]); } } } --- Which of the following calls to print2DimArray will throw an error? --- A int[][] nums = {{15, 22}, {41, 78}, {91, 23}}; print2DimArray(nums); B int[][] nums = {{15, 22, 89}, {41, 78, 63}}; print2DimArray(nums); C int[][] nums = {{15, 22, 89}, {41, 78, 63}, {91, 23, 82}}; print2DimArray(nums); D int[][] nums = {}; print2DimArray(nums); E int[][] nums = new int[4][5]; print2DimArray(nums);

A int[][] nums = {{15, 22}, {41, 78}, {91, 23}}; print2DimArray(nums);

Consider the following method. public static void print2DimArray(int[][] nums) { for (int i=0; i<nums.length; i++) { for (int j=0; j<=i; j++) { System.out.println(nums[i][j]); } } } --- Which of the following calls to print2DimArray will throw an error? --- A int[][] nums = {{15, 22}, {41, 78}, {91, 23}}; print2DimArray(nums); B int[][] nums = {{15, 22, 89}, {41, 78, 63}}; print2DimArray(nums); C int[][] nums = {{15, 22, 89}, {41, 78, 63}, {91, 23, 82}}; print2DimArray(nums); D int[][] nums = {}; print2DimArray(nums); E int[][] nums = new int[4][5]; print2DimArray(nums);

A int[][] nums = {{15, 22}, {41, 78}, {91, 23}}; print2DimArray(nums);

Consider the following code segment. String[] liquids = {"milk", "soda", "water", "tea", "eggnog"}; String chars = ""; for (int i=0; i<liquids.length; i++) { int x = liquids[i].length()/2; chars += liquids[i].substring(x, x+1); } System.out.println(chars); --- What is printed as a result of executing the code segment? --- A ldten B lkdateeano C lkdatereanog D msaee E A StringIndexOutOfBoundsException is thrown

A ldten

Consider the following code segment. String[] liquids = {"milk", "soda", "water", "tea", "eggnog"}; String chars = ""; for (int i=0; i<liquids.length; i++) { int x = liquids[i].length()/2; chars += liquids[i].substring(x, x+1); } System.out.println(chars); --- What is printed as a result of executing the code segment? --- A ldten B lkdateeano C lkdatereanog D msaee E A StringIndexOutOfBoundsException is thrown

A ldten

Consider the following methods. public class testClass { public static String sSearch(int[] x, int key) { String y = ""; boolean z = false; for (int i = 0; i < x.length; i++) { if (x[i] == key) { y += i + " "; z = true; } } if (!z) { y = "key was not found"; return y; } } public static void testMethod() { int[] y = new int[12]; for (int i = 0; i < y.length; i++) y[i] = 5*(i/4); System.out.println(sSearch(y, 5)); } } --- Which of the following is printed to console when testMethod() is called? --- A 4 5 6 B 4 5 6 7 C 4 6 7 8 9 10 11 12 13 14 D 5 6 7 8 9 10 11 12 13 14 E key was not found

B 4 5 6 7

Consider the following method. public static int sumUntilGreaterThan(int max) { int sum=0, i=1; while(sum<=max) { sum+=i; i++; } return sum; } --- When calling sumUntilGreaterThan(12), how many times would it execute the body of the while loop? --- A 4 B 5 C 6 D 7 E 12

B 5

Consider the following method. public static int kooky(int a) { if (a>10) return 2; else if (a<5) return 5+kooky(a+2); return a+kooky(a+1); } --- How many times is the method kooky being called when the following statement is executed? kooky(1); --- A 10 B 9 C 8 D 7 E 6

B 9

Consider the following Equipment and GolfBag classes. public class Equipment { private String type; private boolean required; private double weight; public Equipment(String type, boolean required, double weight) { this.type = type; this.required = required; this.weight = weight; } // other methods not shown } public class GolfBag { private ArrayList<Equipment> equipment; private String golferName; public GolfBag(String name, ArrayList<Equipment> equip) { golferName = name; equipment = equip; } // other methods not shown } --- Which of the segments of code will create an ArrayList of two GolfBag objects, each which contains a putter, 5 iron, and driver as its equipment? --- A ArrayList bags; ArrayList equipment ; equipment.add(new Equipment("driver", true, 1)); equipment.add(new Equipment("5 iron", true, 1)); equipment.add(new Equipment("putter", true, 1)); bags.add(new GolfBag("Phil", equipment)); bags.add(new GolfBag("Sergio", equipment)); B ArrayList bags = new ArrayList(); ArrayList equipment = new ArrayList(); equipment.add(new Equipment("driver", true, 1)); equipment.add(new Equipment("5 iron", true, 1)); equipment.add(new Equipment("putter", true, 1)); bags.add(new GolfBag("Phil", equipment)); equipment = new ArrayList(); equipment.add(new Equipment("driver", true, 1)); equipment.add(new Equipment("5 iron", true, 1)); equipment.add(new Equipment("putter", true, 1)); bags.add(new GolfBag("Sergio", equipment)); C ArrayList bags = new ArrayList(); ArrayList equipment = new ArrayList(); bags.add(new GolfBag("Phil", new Equipment("driver", true, 1), new Equipment("5 iron", true, 1), new Equipment("putter", true, 1)); bags.add(new GolfBag("Sergio", new Equipment("driver", true, 1), new Equipment("5 iron", true, 1), new Equipment("putter", true, 1)); D ArrayList bags = new ArrayList(); ArrayList equipment = new ArrayList(); equipment.add(new Equipment("driver", true, 1)); equipment.add(new Equipment("5 iron", true, 1)); equipment.add(new Equipment("putter", true, 1)); bags.add(new GolfBag("Phil", equipment)); equipment.add(new Equipment("driver", true, 1)); equipment.add(new Equipment("5 iron", true, 1)); equipment.add(new Equipment("putter", true, 1)); bags.add(new GolfBag("Sergio", equipment)); E ArrayList bags = new ArrayList(); ArrayList equipment = new ArrayList(); equipment.add("driver", true, 1); equipment.add("5 iron", true, 1); equipment.add("putter", true, 1); bags.add("Phil", equipment); bags.add("Sergio", equipment);

B ArrayList bags = new ArrayList(); ArrayList equipment = new ArrayList(); equipment.add(new Equipment("driver", true, 1)); equipment.add(new Equipment("5 iron", true, 1)); equipment.add(new Equipment("putter", true, 1)); bags.add(new GolfBag("Phil", equipment)); equipment = new ArrayList(); equipment.add(new Equipment("driver", true, 1)); equipment.add(new Equipment("5 iron", true, 1)); equipment.add(new Equipment("putter", true, 1)); bags.add(new GolfBag("Sergio", equipment));

Consider the following classes: public class B { // methods not shown } public class C extends B { // methods not shown } public class D extends C { // methods not shown } --- Which of the following statements is correct concerning the relationships between these three classes? --- A C is a B and B is a C. B C is a B and D is a B. C B is a C and C is a D. D B is a C and B is a D. E B is a C and C is a B.

B C is a B and D is a B.

Consider the following declared variables. String firstName = "John"; String lastName = "Doe"; String street = "100 Main Street"; String city = "Charlotte"; String state = "NC"; int zip = 28205; --- Which of the following code segments will print the address in this format? John Doe 100 Main Street Charlotte, NC 28205 I. System.out.println(firstName + " " + lastName); System.out.println(street); System.out.println(city + ", " + state + " " + zip); II. System.out.print(firstName); System.out.print(" "); System.out.println(lastName); System.out.println(street); System.out.print(city); System.out.print(", "); System.out.print(state); System.out.print(" "); System.out.println(zip); III. System.out.print(firstName + " " + lastName); System.out.print(street); System.out.print(city + ", " + state + " " + zip); IV. System.out.print(firstName + " " + lastName + "\n" + street + "\n" + city + ", " + state + " " + zip); --- A I and II only. B I, II, and IV only. C II, III, and IV only. D II and III only. E I, II, III, and IV.

B I, II, and IV only.

Given that petList is an ArrayList of Strings that contains {"dog", "snake", "snake", "cat"}, which of the following fragments of code will result in an IndexOutOfBoundsException? I. for(int i=0; i<petList.size(); i++) { if(petList.get(i).equals("snake")) petList.remove(i); } II. for(int i=0; i<4; i++) { if(petList.get(i).equals("snake")) { petList.remove(i); i--; } } III. for(int i=0; i<petList.size(); i++) { if (petList.get(i).equals("snake")) { petList.remove(i); i--; } } --- A I only. B II only. C III only. D I and II only. E I and III only.

B II only.

Which of the following methods will accept an original string and a search string as parameters, and return the count of how many times the search string is found in the original string? For example: countString("mississippi", "i") will return 4 countString("mississippi", "is") will return 2 countString("peanuts", "to") will return 0 I. public static int countString(String str, String search) { int count = 0; for (int i=0; i<str.length(); i++) { if (str.substring(i, i+1).equals(search)) count++; } return count; } II. public static int countString(String str, String search) { int count = 0; for (int i=0; i<str.length()-search.length()+1; i++) { if (str.substring(i,i+search.length()).equals(search)) count++; } return count; } III. public static int countString(String str, String search) { int count = 0; for (int i=0; i<str.length(); i++) { if (str.indexOf(search)>=0) count++; } return count; } --- A I only. B II only. C III only. D I and II only. E II and III only.

B II only.

Consider the following class. public class Athlete { private String name; private String sport; private int ranking; public Athlete(String name, String sport, int ranking) { this.name = name; this.sport = sport; this.ranking = ranking; } public String getName() { return name; } public String getSport() { return sport; } public int getRank() { return ranking; } } --- Consider the following code. Athlete[] athletes = new Athlete[10]; athletes[0] = new Athlete("Steve", "football", 12); athletes[1] = new Athlete("Karen", "softball", 40); athletes[2] = new Athlete("Greg", "football", 55); athletes[3] = new Athlete("Ralph", "tennis", 5); athletes[4] = new Athlete("Sally", "golf", 5); printFootballInfo(athletes); --- Consider the following methods. I. public static void printFootballInfo (Athlete[] athletes) { for (int i=0; i<athletes.length; i++) { if (athletes[i].getSport().equals("football")) System.out.println(athletes[i].getName() + " " + athletes[i].getRank()); } } II. public static void printFootballInfo (Athlete[] athletes) { for (Athlete a : athletes) { if (a.getSport().equals("football") && a != null) System.out.println(a.getName() + " " + a.getRank()); } } III. public static void printFootballInfo (Athlete[] athletes) { for (int i=0; i<athletes.length; i++) { if (athletes[i] != null && athletes[i].getSport().equals("football")) System.out.println(athletes[i].getName() + " " + athletes[i].getRank()); } } --- Which of these methods would print football player information without throwing an error if this code were executed from the main method? --- A I only. B III only. C I and II only. D II and III only. E I, II, and III

B III only.

The code segment int[] x = {5,10,15,20,25}; is equivalent to which of the following? I. int[] x = new int[5]; for (int i = 0; i < x.length; i+=5) x[i] = i; II. int[] x = new int[5]; for (int i = x.length; i >= 0; i--) x[i] = i*5 + 5; III. int[] x = new int[5]; for (int i = x.length-1; i >= 0; i--) x[i] = 5*(i + 1); IV. int[] x = new int[5]; for (int i : x) { i*=5; i+=5; } --- A I only. B III only. C II and III only. D I, II, and III only. E I, II, III, and IV.

B III only.

Consider the following code. // Assume flag is defined as a boolean variable System.out.println( !flag && flag || !flag ); --- What will this statement print to the console? --- A It will always print the value assigned to flag. B It will always print the opposite of the value assigned to flag. C It will always print true. D It will always print false. E It will throw an error.

B It will always print the opposite of the value assigned to flag.

What is the output from the following code segment? ArrayList < String > list = new ArrayList < String > (); list.add("owl"); list.add("snake"); list.add("bear"); list.add("wolf"); for(int i=0; i < list.size(); i++) { System.out.print(list.get(i).substring(i,i+1) + " "); } --- A o w l B o n a f C o s b w D owl snake bear wolf E owl nake ar f

B o n a f

A Bowler class requires that the following data be stored: the bowler's high score, the bowler's average score (should not be rounded), and the bowler's last 10 scores. --- What would be an acceptable way to declare this data in the Bowler class? --- A private int highScore, double avgScore, int[] scores; B private int highScores, scores[]; private double avgScore; C private int highScores, double avgScore; private int[] scores; D private int highScores, avgScore; private int[] scores; E private int highScores, scores; private double avgScore;

B private int highScores, scores[]; private double avgScore;

Consider the following code segment. String goofy = 1234%10+(1234+"").length()+"goofy".length()+""; System.out.println(goofy); --- What is printed as a result of executing the code segment? --- A 4 B 445 C 13 D 1243 E An error occurs.

C 13

Consider the following method. public static int mystery(int x, int y, int z) { if (x > z) return x + (y%2) + mystery(x - z, y - 1, z + 1); else return x*y*z; } --- What is the returned value of mystery(9, 4, 2)? --- A 21 B 29 C 49 D 55 E 80

C 49

Consider the following method. public static ArrayList<String> removeBirds(ArrayList<String> animals) { for(int i=0; i<animals.size(); i++) { if(animals.get(i).indexOf("bird") >= 0) { animals.remove(i); } } return animals; } --- If the following array list were passed to this method, how many elements would it contain after the method completed execution? ["collie", "bluebird", "blackbird", "lion", "cardinal", "hummingbird", "snowbird"] --- A 3 B 4 C 5 D 6 E 7

C 5

Given that: double d = 10.0; int i = 20; --- Which of the following are valid statements which will compile successfully? I. int num = i / d; II. i = d; III. d = i / (int) d; --- A I only. B II only. C III only. D I, II, and III. E None of these choices is valid.

C III only.

Consider the following code. List list = new ArrayList(); list.add("carrot"); list.add("potato"); list.add("milk"); for (int i=0; i<list.size(); i++) { // put statement here } --- Which statement should be placed in the following for loop to print out the first character of each element stored in the ArrayList? --- A System.out.println(list.get(i).substring(0,1)); B System.out.println(list.substring(0,1)); C System.out.println(((String) list.get(i)).substring(0,1)); D System.out.println(list.get(i).substring(i,i+1)); E System.out.println(((String) list.get(i)).substring(i,i+1));

C System.out.println(((String) list.get(i)).substring(0,1));

Consider the following code. double randNum = Math.random(); double answer = (int)(Math.pow((int)randNum*10, 2)) - Math.sqrt(Math.pow(randNum,2)); --- Which of the following will be the BEST approximate value of answer after this segment of code is run? --- A The value stored in randNum. B The value stored in randNum squared. C The negative of the value stored in randNum. D A random number between 1 (inclusive) and 16 (exclusive). E Always 0.

C The negative of the value stored in randNum.

Consider the following method: public static ArrayList<Integer> setList(ArrayList<Integer> nums) { ArrayList<Integer> outputList = new ArrayList<Integer>(); for (int i=0; i<nums.size(); i++) { outputList.add(nums.get(i)); if (outputList.get(i)%10==0 && i>0) outputList.set(i-1, nums.get(i-1)*2); } return outputList; } --- What is the output after running the following section of code? ArrayList<Integer> numsList = new ArrayList<Integer>(); numsList.add(10); numsList.add(30); numsList.add(44); numsList.add(26); numsList.add(60); System.out.println(setList(numsList)); --- A [20, 60, 44, 26, 120] B [10, 30, 44, 26, 60] C [20, 30, 44, 52, 60] D [10, 60, 88, 52, 60] E No output is displayed. An error is thrown.

C [20, 30, 44, 52, 60]

Consider the following code segment. ArrayList<String> funList; funList = new ArrayList(); funList.add("cap"); funList.add("chair"); funList.add("jar"); funList.add("dog"); funList.add("mom"); for(int i=0; i< funList.size(); i++) { if(funList.get(i).length()==3) { funList.remove(i); i--; } } System.out.println(funList); --- What is printed as a result of executing the code segment? --- A [cap, chair, jar, dog, mom] B [chair, dog] C [chair] D [chair, jar, dog, mom] E [cap, chair, dog]

C [chair]

Consider the following code segment. String s ="goodbye"; System.out.println(s.substring(s.substring(s.length() - s.indexOf("d")).length())); --- What is printed as a result of executing the code segment? --- A bye B 3 C dbye D 2 E odbye

C dbye

The following method should return the average score for a single game bowled across all bowlers on your team who actually bowled that game. The 2-dimensional array named teamScores is organized as follows: 1) each row is a single bowler on your team, 2) each column is a single game bowled (game 1 is column 0, game 2 is column 1, etc.), and 3) an element of 0 means the bowler did not bowl that game. // Precondition: gameNum is the game to find the average score for // 1 <= gameNum <= number of columns in teamScores // (i.e. gameNum=1 is the first game, column 0) public static double findAvgGameScore (int[][] teamScores, int gameNum) { double sum =0, count=0; //insert your code if (count>0) return sum/count; return 0; } --- What code should be inserted at //insert your code so that this method returns the average score for a specific game? --- A for (int i=0; i<teamScores.length; i++) { if (teamScores[i][gameNum]>0) { sum += teamScores[i][gameNum]; count++; } } B for (int b=0; b<teamScores.length; b++) { sum += teamScores[b][gameNum-1]; count++; } C for (int b=0; b<teamScores.length; b++) { if (teamScores[b][gameNum-1]>0) { sum += teamScores[b][gameNum-1]; count++; } } D for (int r=0; r<teamScores.length; r++) { for (int c=0; c<teamScores[r].length; c++) { if (teamScores[r][c]>0) { sum += teamScores[r][c]; count++; } } } E for (int r=0; r<teamScores.length; r++) { for (int c=0; c<teamScores[r].length; c++) { if (teamScores[r][gameNum-1]>0) { sum += teamScores[r][ gameNum-1]; count++; } } }

C for (int b=0; b<teamScores.length; b++) { if (teamScores[b][gameNum-1]>0) { sum += teamScores[b][gameNum-1]; count++; } }

ArrayList a is an array list without a type parameter, so it can contain any type of Object. public static int countStrings(ArrayList a) { int count=0; for (int i=0; i<a.size(); i++) { /* insert if statement here */ } return count; } --- Which of the following if statements should replace /*insert if statement here */ such that the following method will correctly count the number of Strings that are greater than 3 characters long in ArrayList a? --- A if ( ((String) a.get(i)).length() > 3 && a.get(i) instanceof String) count++; B if ( ((String) a.get(i)).length() > 3) count++; C if (a.get(i) instanceof String && ((String) a.get(i)).length() > 3) count++; D if (a.get(i) instanceof String && a.get(i).length() > 3) count++; E if (!(a.get(i) instanceof String) || a.get(i).length() > 3) count++;

C if (a.get(i) instanceof String && ((String) a.get(i)).length() > 3) count++;

Assume that the mammals array has a list of mammal names, but we do not know how big the array is and whether it is full of mammal names. Each element is either populated with a valid mammal name or has not been assigned any value. --- Which of the following segments of code will always be able to print the count of the number of mammal names in the array? String[] mammals = // list of mammals --- A int count = 0; for (String m : mammals) if (m.length() > 0) count++; System.out.print(count); B int count = 0; for (String m : mammals) if (!m.equals("")) count++; System.out.print(count); C int count = 0; for (String m : mammals) if (m != null) count++; System.out.print(count); D int count = 0; for (String m : mammals) if (m.compareTo("")>0) count++; System.out.print(count); E System.out.println(mammals.length);

C int count = 0; for (String m : mammals) if (m != null) count++; System.out.print(count);

The Athlete class is used to keep track of an athlete's name, sport, ranking, and age. Assume that there are accessor methods named getName(), getSport(), getRank(), and getAge() in the Athlete class to retrieve this information from an Athlete object. --- Which segment of code will print the highest age of all golfers in the athletes array? Athlete[] athletes = // Assume that this array is full of Athlete objects --- A int max=0, maxI=-1; for (int i=0; i<athletes.length; i++) { if(athletes[i].getSport().equals("golf") && athletes[i].getAge() > max) maxI = i; } System.out.println(maxI); B int max=0, maxI=-1; for (int i=0; i<athletes.length; i++) { if(athletes.getSport().equals("golf") && athletes.getAge() > max) max = athletes.getAge(); } System.out.println(max); C int max=0, maxI=-1; for (int i=0; i<athletes.length; i++) { if(athletes[i].getSport().equals("golf") && athletes[i].getAge() > max) max = athletes[i].getAge(); } System.out.println(max); D int max=0, maxI=-1; for (int i=0; i<athletes.length; i++) { if(athletes.get(i).getSport().equals("golf") && athletes.get(i).getAge() > max) max = athletes.get(i).getAge(); } System.out.println(max); E int max=9999, maxI=9999; for (int i=0; i<athletes.length; i++) { if(athletes[i].getSport().equals("golf") && athletes[i].getAge() > max) max = athletes[i].getAge(); } System.out.println(max);

C int max=0, maxI=-1; for (int i=0; i<athletes.length; i++) { if(athletes[i].getSport().equals("golf") && athletes[i].getAge() > max) max = athletes[i].getAge(); } System.out.println(max);

Consider the following method. You are told that it is not optimized and can be improved to run faster. Pay close attention to the preconditions specified in the comments. Of the choices below, which is the BEST replacement for this method to better optimize it? // Preconditions: The scores array contains AP scores 1-5 or zero if not filled. // All valid AP scores are filled at the beginning of the scores array // starting at position 0. // The scores array may not be full of valid AP scores. public static double averageAPScore(int[] scores) { double sum = 0; for (int i=0; i < scores.length; i++) { sum += scores[i]; } int count=0; for (int j=0; j < scores.length; j++) { if (scores[j] > 0) count++; } return sum / count; } --- A public static double averageAPScore(int[] scores) { double sum = 0; int count = 0; for (int i=0; i < scores.length || scores[i] != 0; i++) { sum += scores[i]; count++; } return sum / count; } B public static double averageAPScore(int[] scores) { double sum = 0; for (int i=0; i < scores.length; i++) { if (scores[i] == 0) break; sum += scores[i]; } int count = 0; for (int j=0; j < scores.length; j++) { if (scores[j] == 0) break; count++; } return sum / count; } C public static double averageAPScore(int[] scores) { double sum = 0; int count = 0; for (int i=0; i < scores.length && scores[i] > 0; i++) { sum += scores[i]; count++; } return sum / count; } D public static double averageAPScore(int[] scores) { double sum = 0; int count = 0; for (int i=0; i < scores.length; i++) { if (scores[i] > 0) { sum += scores[i]; count++; } } return sum / count; } E public static double averageAPScore(int[] scores) { double sum = 0; for (int i=0; i < scores.length; i++) { sum += scores[i]; } int count = 0; for (int i=0; i < scores.length; i++) { if (scores[i] > 0) count++; } return sum / count; }

C public static double averageAPScore(int[] scores) { double sum = 0; int count = 0; for (int i=0; i < scores.length && scores[i] > 0; i++) { sum += scores[i]; count++; } return sum / count; }

Which of the following methods will correctly return the count of all of the different elements from array1 and array2, when comparing the elements at the same positions in each array? If one array ends before the other array, the remaining elements are considered to all be different. For example, if array1 = {5, 6, 7, 8, 9} and array2 = {5, 7, 7, 9, 9, 1, 2, 3}, then the method should return 5 since the elements at positions 1 and 3 from each array are not equal, and the last three elements from array2 do not have any matching elements from array1. --- A public static int countDiffElements(int[] array1, int[] array2) { int count=0; for (int i=0; i<array1.length; i++) { if (array1[i] != array2[i] && i<array2.length) count++; } if (array2.length>array1.length) count += array2.length - array1.length; return count; } B public static int countDiffElements(int[] array1, int[] array2) { int count=0; for (int i=0; i<array1.length && i<array2.length; i++) { if (array1[i] != array2[i]) count++; } return count; } C public static int countDiffElements(int[] array1, int[] array2) { int count=0; for (int i=0; i<array1.length; i++) { if (i>=array2.length || array1[i] != array2[i]) count++; } if (array2.length>array1.length) count += array2.length - array1.length; return count; } D public static int countDiffElements(int[] array1, int[] array2) { int count=0; for (int i=0; i<array1.length; i++) { if (i>=array2.length || array1[i] != array2[i]) count++; } if (array1.length>array2.length) count += array1.length - array2.length; return count; } E public static int countDiffElements(int[] array1, int[] array2) { int count=0; for (int i=0; i<array1.length; i++) { if (i>=array2.length) return count; else if (array1[i] != array2[i]) count++; } if (array1.length>array2.length) count ++; return count; }

C public static int countDiffElements(int[] array1, int[] array2) { int count=0; for (int i=0; i<array1.length; i++) { if (i>=array2.length || array1[i] != array2[i]) count++; } if (array2.length>array1.length) count += array2.length - array1.length; return count; }

Consider the following code segment. int[] a = {70, 50, 60, 90, 10}; int[] b = {3, 0, 4, 1, 2}; for (int i=0; i<a.length; i++) { if (a[i] > a[b[i]]) System.out.print(a[i]); else System.out.print(b[b[i]]); System.out.print(" "); } --- What is printed as a result of executing the code segment? --- A 3 0 60 90 2 B 1 3 2 0 4 C 0 1 60 90 4 D 1 3 60 90 4 E 70 50 60 90 10

D 1 3 60 90 4

Consider the following method. public static int mystery(int num) { int a, b; int c = 1, d = 0; while (num != 0) { a = num%2; b = a*c; d += b; num /= 2; c *= 10; } return d; } --- Which of the following is returned by the call mystery(51)? --- A 0 B 110000 C 111111 D 110011 E Nothing is returned because of an overflow error.

D 110011

Consider the following method. //Precondition: x >= 0 public static int mystery(int x) { int z = 1; for (int i = 1; i <=x; i++) z*=i; return z; } --- Which of the following is returned by the call mystery(5) ? --- A 1 B 24 C 60 D 120 E 720

D 120

Consider the following code segment. int[] q = {4, 2, 12, 13, -5}; System.out.println(q.length + q[3]); --- What is printed as a result of executing this code segment? --- A 15 B 16 C 17 D 18 E Nothing is printed because of an error.

D 18

Consider the following code segment. int[] myArray = {0,4,9,-16}; myArray[0]=myArray[3]; myArray[3]=myArray[0]; System.out.println(myArray[3]*-2); --- What is printed as a result of executing the following code segment? --- A 0 B 8 C 18 D 32 E ArrayIndexOutOfBoundsException

D 32

Consider the following code segment. // there are single spaces between each word in myPhrase String myPhrase = "Charlie does not tell lies"; int liePos = myPhrase.indexOf("lie" ) ; System.out.print( liePos ); --- What is printed as a result of executing the code segment? --- A 26 B 23 C 22 D 4 E 0

D 4

Which of the following methods will always return the index of the first element that equals the designated tree type or -1 if the tree type is not found? Note: The parameters for each method are the array of tree types and the tree type to search for. I. public static int findFirstTree(String[] trees, String treeType) { for (int i=0; i<trees.length-1; i++) if (trees[i].equals(treeType)) return -1; } II. public static int findFirstTree(String[] trees, String treeType) { int i=0; for (String tree : trees) { if (tree.equals(treeType)) return i; i+=1; } return -1; } III. public static int findFirstTree(String[] trees, String treeType) { int i=0; for (i<trees.length && !trees[i].equals(treeType); i++) ; if (i==trees.length) return -1; return i; } --- A I only. B I and II only. C I and III only. D II and III only. E I, II, and III.

D II and III only.

Which of the following statements will print out the last four characters of a string named phoneNum? Assume that phoneNum is at least four characters long. --- A System.out.println(phoneNum.substring(6)); B System.out.println(phoneNum.substring(phoneNum.length()-4, phoneNum.length()-1)); C System.out.println(phoneNum.substring(phoneNum.length()-3)); D System.out.println(phoneNum.substring(phoneNum.length()-4)); E System.out.println(phoneNum.substring(3));

D System.out.println(phoneNum.substring(phoneNum.length()-4));

Consider the following code segment. String animal = "coyote"; for (int i=0; i<animal.length(); i++) { if (animal.substring(i,i+1).equals("o")) System.out.println(animal.substring(i,i+1)); else System.out.print(animal.substring(i,i+1)); } --- What is printed as a result of executing the code segment? --- A coyote B c o y o t e C c oy ot e D co yo te E c o y o te

D co yo te

Consider the following methods. public static void method1() { method2(); System.out.println("cat"); } public static void method2() { method3(); System.out.println("dog"); } public static void method3() { method4(); System.out.println("bird"); } public static void method4() { System.out.println("fish"); } --- Which of the following is printed as a result of the call method2()? --- A cat dog bird fish B dog bird fish C fish dog bird D fish bird dog E nothing will print because of an overflow error

D fish bird dog

Consider the output of the following code segment. for (int i=1; i<=30; i++) if (0 == i%5) System.out.println(i); --- Which of the following code segments has the same output? --- A for (int i=0; i<=30; i++) if ((i+1)%5 == 0) System.out.println(i); B for (int i=30; i >0; i--) if (0 == i%5) System.out.println(i); C for (int i=1; i<=30; i+=5) System.out.println(i); D for (int i=5; i<=32; i+=5) System.out.println(i); E for (int i=1; i<=30; i++) System.out.println(i);

D for (int i=5; i<=32; i+=5) System.out.println(i);

The following method performs a sequential search on an ArrayList of Strings. If word is found the method returns the index and if word is not found the method returns -1. public static int seqSearch(ArrayList<String> list, String word) { /*missing code*/ } --- Which of the following should replace /*missing code*/? --- A for (int i = 0; i < list.size(); i++) { if (list[i].equals(word)) return i; } return -1; B for (int i = 0; i < list.size(); i++) { if (word == list.get(i)) return i; } return -1; C for (int i = 0; i < list.size(); i++) { if (word.equals(list.get(i))) return i; else return -1; } D int loc = -1; for (int i = list.size()-1; i > -1; i--) { if (list.get(i).equals(word)) { loc = i; return loc; } } return loc; E for (int i = list.size()-1; i > 0; i--) { if (word.equals(list.get(i))) return i; } return -1;

D int loc = -1; for (int i = list.size()-1; i > -1; i--) { if (list.get(i).equals(word)) { loc = i; return loc; } } return loc;

A rectangular puzzle exists with all pieces being identically-sized squares. The main part of the puzzle has a total of 600 pieces and each row of this main part of the puzzle is 30 pieces wide. Additionally, there is a border of artistic icons completely surrounding this main puzzle area, and this border is exactly one piece wide, with each piece of the border being the same size as a main puzzle piece. Finally, at the bottom of the puzzle is one additional row of square pieces that spells out the title of the puzzle. The puzzle is composed of all the before-mentioned pieces, each piece being square and the same size. The PuzzlePiece class is defined as follows: public class PuzzlePiece { private Image image; // Image represented by this piece private int row; // row where this piece goes private int col; // col where this piece goes public PuzzlePiece(Image i, int r, int c) { // Implementation not shown } // other instance variables and methods not shown } --- Of the following, which would be the BEST data representation for this puzzle in a Puzzle class? --- A public class Puzzle { private String[][] puzzle = new PuzzlePiece[20][30]; // other instance variables and methods not shown } B public class Puzzle { private PuzzlePiece[][] puzzle = new PuzzlePiece[21][31]; // other instance variables and methods not shown } C public class Puzzle { private PuzzlePiece[][] puzzle = new PuzzlePiece[22][32]; // other instance variables and methods not shown } D public class Puzzle { private PuzzlePiece[][] puzzle = new PuzzlePiece[23][32]; // other instance variables and methods not shown } E public class Puzzle { private String[][] puzzle = new String[22][31]; // other instance variables and methods not shown }

D public class Puzzle { private PuzzlePiece[][] puzzle = new PuzzlePiece[23][32]; // other instance variables and methods not shown }

Consider the following method. // only salaries that are more than $0 are considered valid salaries public static double methodB(int[] salaries) { int count = 0; double sum = 0; for (int salary : salaries) { if (salary>0) { sum+=salary; count++; } } if (count>0) return sum/count; return 0; } --- What does this method return? --- A the sum of all the salaries in the array B the average of all salaries in the array C the count of how many valid salaries are in the array D the average of all valid salaries in the array E the sum of all valid salaries in the array

D the average of all valid salaries in the array

Consider the following method. public static int mystery(int x) { if (x <= 0) return 0; if (x%2 == 0) return x + mystery(x-2); else return x + mystery(x-1); } --- Which of the following is returned when mystery(9) is called? --- A 25 B 26 C 27 D 28 E 29

E 29

Consider the following method. public static int nested(int x, int y) { int q = 0; if (x == 1) { q++; if (y == 2) q++; y++; } q++; if (y == 3) { q++; if (y == 2) { q++; q++; } } q++; return q; } --- Which of the following is returned as a result of the call nested(1, 2)? --- A 1 B 2 C 3 D 4 E 5

E 5

Consider the following code segment. int x = 2; int[] y = {5,7,5}; double[] z = {10,2,10}; System.out.println(y[x]/z[x] + y[x-1]/z[x-1] + x); --- What is printed as a result of executing the code segment? --- A 4.0 B 4.5 C 5.0 D 5.5 E 6.0

E 6.0

Consider the following code segment. int[] n = {8, 4, 5, 1, 7, 2, 3, 6}; System.out.println(n[n[n[n[6]]]]); --- What is printed as a result of executing the code segment? --- A 3 B 4 C 5 D 6 E 7

E 7

Consider the following code segment. int[] n = {8, 4, 5, 1, 7, 2, 3, 6}; System.out.println(n[n[n[n[6]]]]); --- What is printed as a result of executing the code segment? --- A 3 B 4 C 5 D 6 E 7

E 7

What is d initialized to? double a = 9/2.0; int b = 10; double c = a*b; int d = a+c*2; --- A 84.0 B 94.0 C 94.5 D 99.0 E A syntax error occurs.

E A syntax error occurs.

Consider the following code segment. ArrayList< String > sports; sports = new ArrayList< String >(); sports.add("football"); sports.add("soccer"); sports.add("baseball"); sports.add("basketball"); sports.add("golf"); int i = sports.size()-1; for(String sport : sports) { if(sport.indexOf("ball") >= 0) { sports.remove(i); i--; } i++; } System.out.println(sports); --- What is printed as a result of executing the code segment? --- A [football, baseball, basketball] B [soccer, basketball, golf] C [soccer, golf] D [football, soccer, baseball, basketball, golf] E An error occurs.

E An error occurs.

Consider the following classes. public class Animal { // instance variables, constructor, // and other methods not shown. public String getSpecies() { // code not shown } public void consumeFood(Food diet) { // code not shown } public int getAge() { // code not shown } } public class Food { // variables, constructors, and methods not shown } public class Pig extends Animal { // variables, constructors, and other methods not shown public int countPiglets(Pig[] piglets) { // code not shown } } --- A Pig consumes food in a different manner than other animals. What would be the BEST approach to take to define this difference? --- A Define the following method in the Animal class: public void consumePigFood(Food diet) { // code not shown } B Modify the following method in the Animal class: public void consumeFood(Food diet) { // code not shown } C Define the following method in the Pig class: public Food consumeFood(Food diet) { // code not shown } D Define the following method in the Pig class: public void consumePigFood(Food diet) { // code not shown } E Define the following method in the Pig class: public void consumeFood(Food diet) { // code not shown }

E Define the following method in the Pig class: public void consumeFood(Food diet) { // code not shown }

Which of the following methods would accept a list of pets and return a list that excludes birds? The order of the pets in the resulting array list is unimportant. Assume that the description of a bird in the array list has the word "bird" in it. I. public static ArrayList<String> noBirdsI(ArrayList<String> petList) { ArrayList<String> petsNoBirds = new ArrayList<String>(); for (String pet : petList) { if (pet.indexOf("bird") < 0) petsNoBirds.add(pet); } return petsNoBirds; } II. public static ArrayList<String> noBirdsII(ArrayList<String> petList) { ArrayList<String> petsNoBirds = new ArrayList<String>(); for (int i=0; i<petList.size(); i++) { if (petList.get(i).indexOf("bird") < 0) petsNoBirds.add(petList.get(i)); } return petsNoBirds; } III. public static ArrayList<String> noBirdsIII(ArrayList<String> petList) { ArrayList<String> petsNoBirds = new ArrayList<String>(); for (int i=petList.size()-1; i>=0; i--) { if (petList.get(i).indexOf("bird") < 0) petsNoBirds.add(petList.get(i)); } return petsNoBirds; } --- A I only. B II only. C III only. D I and II only. E I, II, and III.

E I, II, and III.

Which of the following segments of code will swap the values stored in x and y, assuming that they are both positive, non-zero integers? For example, if x = 6 and y = 11, then after the code executes, the result should be that x = 11 and y = 6. I. x = x * y; y = x / y; x = x / y; II. int t = x; x = y; y = t; III. x = x + y; y = x - y; x = x - y; --- A I only. B II only. C III only. D II and III only. E I, II, and III.

E I, II, and III.

Consider the following code segment. ArrayList<String> list = // assume a list of at least one String with no duplicate values int randIdx = (int) (Math.random()*list.size()); for (int i=0; i<list.size(); i++) { if (list.get(i).equals(list.get(randIdx))) { list.add(0, list.get(i)); } } --- For which of the following cases would executing this code segment result in an error or problem? --- A It always throws an index out of bounds error. B It throws an index out of bounds error when randIdx is greater than or equal to the number of elements in list. C It gets into an infinite loop once it reaches the randomly generated index. D It gets into an infinite loop when randIdx is the last position in list. E It gets into an infinite loop when randIdx is equal to 0.

E It gets into an infinite loop when randIdx is equal to 0.

Consider the following method. public static void animals(int x) { if (x < -3) System.out.println("cat"); else if (x < 1) System.out.print("dog"); System.out.println("snake"); else if (x != 0) { System.out.println("bird"); } else System.out.println("frog"); System.out.print("turtle"); if (x > 2) System.out.println("fish"); else System.out.println("mouse"); } --- What is printed when the following code segment is executed from some other method in the same class? for(int i = -1; i <2; i++) animals(i); --- A dogturtlemouse dogturtlemouse bird turtlemouse B dog-snake turtle-mouse dog-snake turtle-mouse bird turtle-mouse C dog-snake turtle-mouse dog-snake turtle-mouse snake bird turtle-mouse D dog-snake bird turtle-mouse dog-snake frog turtle-mouse snake bird turtle-mouse E Nothing is printed because of a syntax error.

E Nothing is printed because of a syntax error.

Consider the following method. public static void fruits(int q) { if (q < -2) System.out.println("Apple"); System.out.println("Pear"); if (q <=-2) System.out.println("Orange"); else System.out.println("Banana"); System.out.println("Grape"); if (q < 0) { System.out.println("Blueberry"); System.out.println("Kiwi"); } else if (q !=2) System.out.println("Cantaloupe"); else if (q<3) System.out.println("Watermelon"); else System.out.println("Mango"); if (q == 2 || q > -2) System.out.println("Strawberry"); System.out.println("Cherry"); } --- Which of the following is printed as a result of the call fruits(-2)? --- A Orange Blueberry Kiwi B Pear Orange Grape Blueberry Kiwi Cantaloupe Watermelon C Apple Orange Grape Blueberry Kiwi Cantaloupe Watermelon D Apple Orange Grape Blueberry Kiwi Watermelon Cherry E Pear Orange Grape Blueberry Kiwi Cherry

E Pear Orange Grape Blueberry Kiwi Cherry

Consider the following code segment. ArrayList<String> words; words = new ArrayList<String>(); words.add("alone"); words.add("note"); words.add("wagon"); words.add("none"); words.add("seen"); words.add("no"); for(int i=0; i<words.size(); i++) { if(words.get(i).substring(words.get(i).length()-2, words.get(i).length()-1).equals("n")) { System.out.print(words.get(i)); } } --- What is printed as a result of executing the code segment? --- A notenoneno B wagonseen C alonenotewagonnoneseenno D alonenone E alonenoneno

E alonenoneno

Your new class needs to define the following data: - The student's math SAT score (number with no decimals) - The full street address where the student lives - The percentage of the student's classes that are AP - Did the student pass high school? --- Of the choices below, which would be BEST to define this class data? --- A private double mathSATscore; private String street; private int percentAP; private int passHS; B private int mathSATscore; private boolean street; private double percentAP; private String passHS; C private double mathSATscore; private String street; private int percentAP; private String passHS; D private int mathSATscore; private String street; private String percentAP; private boolean passHS; E private int mathSATscore; private String street; private double percentAP; private boolean passHS;

E private int mathSATscore; private String street; private double percentAP; private boolean passHS;

Consider the following class. public class StringMania { private String str; public StringMania(String s) { str = s; } public int stringGymnastics(String s1, int a) { if (str.indexOf(s1)>=0) if (a>str.indexOf(s1)) return a; else return str.indexOf(s1); else return a*-1; } public String stringGymnastics(int a) { if (a<str.length()) return str.substring(a); else if (str.length()>0) return str.substring(str.length()-1); else return null; } public String stringGymnastics(int a, int b) { if (a>0 && a<b && b<=str.length()) return str.substring(a,b); else if (str.length()>0 && a>=0 && a<str.length()) return str.substring(a); else return "no"; } } --- Consider the following code segment. StringMania mania = new StringMania("kilometer"); System.out.print(mania.stringGymnastics(10) + " "); System.out.print(mania.stringGymnastics("me", 3) + " "); System.out.println(mania.stringGymnastics(5,8)); What is output to the console when this segment of code is executed? --- A r 3 eter B r -3 eter C null 4 eter D r 4 eter E r 4 ete

E r 4 ete


Kaugnay na mga set ng pag-aaral

Types of imagining, testing, procedures

View Set

Biology 201 Fall 2017 - Exam 2 - Uwe Pott - UWGB

View Set

Leadership and Performance Midterm

View Set