AP Comp Sci Exam Review

¡Supera tus tareas y exámenes ahora con Quizwiz!

Consider the following code segment, which is intended to assign to num a random integer value between min and max, inclusive. Assume that min and max are integer variables and that the value of max is greater than the value of min. double rn = Math.random(); int num = /* missing code */; Which of the following could be used to replace /* missing code */ so that the code segment works as intended?

(int) (rn * (max - min + 1)) + min

Consider the following code segment. for(int r =3; r>0; r--) { int c: For (int c= 1; c < r; c++) { System.out print("-"); } For (c=r; c<=3; c++) { System.outoprint("*"); } System out println(); } What is printed as a result of executing the code segment? Responses

--* -** ***

The following method is intended to return a string containing the character at position n in the string str. For example, getChar("ABCDE", 2) should return "C". /* missing precondition */ public String getChar(String str, int n) { return str.substring(n, n + 1); } Which of the following is the most appropriate precondition for the method so that it does not throw an exception?

/* Precondition: 0 <= n <= str.length() - 1 */

Consider the following code segment. int k = 0; while (k < 10) { System.out.print((k % 3) + " "); if ((k % 3) == 0) k = k + 2; else k++; } What is printed as a result of executing the code segment? Responses

0 2 0 2 0 2 0

Consider the following code segment. String str = "0"; str += str + 0 + 8; System.out.println(str); What is printed as a result of executing the code segment?

0008

Consider the following code segment. int num = 1; int count = 0; while (num <= 10) { if (num % 2 == 0 && num % 3 == 0) { count++; } num++; } What value is stored in the variable count as a result of executing the code segment?

1

Consider the following method. public int mystery(int num) { int x = num; while (x > 0) { if (x / 10 % 2 == 0) return x; x = x / 10; } return x; } What value is returned as a result of the call mystery(1034) ?

103

Consider the following code segment. for (int num = 0; num < 10; num += 2) { for (int val = 0; val < 5; val++) { System.out.println("hop"); } } How many times will System.out.println("hop") be executed?

25

Consider the following code segment. int val = 48; int div = 6; while ((val % 2 == 0) && div > 0) { if (val % div == 0) { System.out.print(val + " "); } val /= 2; div--; } What is printed when the code segment is executed?

48 12 6

Consider the following method. public void numberCheck(int maxNum) { int typeA = 0; int typeB = 0; int typeC = 0; for (int k = 1; k <= maxNum; k++) { if (k % 2 == 0 && k % 5 == 0) typeA++; if (k % 2 == 0) typeB++; if (k % 5 == 0) typeC++; } System.out.println(typeA + " " + typeB + " " + typeC); } What is printed as a result of the call numberCheck(50) ?

5 25 10

Consider the following code segment. String str = "CompSci"; System.out.println(str.substring(0, 3)); int num = str.length(); What is the value of num when the code segment is executed?

7

Consider the following class definition. public class ExamScore { private String studentId; private double score; public ExamScore(String sid, double s) { studentId = sid; score = s; } public double getScore() { return score; } public void bonus(int b) { score += score * b/100.0; } } Assume that the following code segment appears in a class other than ExamScore. ExamScore es = new ExamScore("12345", 80.0); es.bonus(5); System.out.println(es.getScore()); What is printed as a result of executing the code segment? Responses

84.0

Consider the following code segment. String str1 = new String("Advanced Placement"); String str2 = new String("Advanced Placement"); if (str1.equals(str2) && str1 == str2) { System.out.println("A"); } else if (str1.equals(str2) && str1 != str2) { System.out.println("B"); } else if (!str1.equals(str2) && str1 == str2) { System.out.println("C"); } else if (!str1.equals(str2) && str1 != str2) { System.out.println("D"); } What, if anything, is printed when the code segment is executed?

B

Consider the following code segment. String oldStr = "ABCDEF"; String newStr = oldStr.substring(1, 3) + oldStr.substring(4); System.out.println(newStr); What is printed as a result of executing the code segment?

BCEF

Consider the following method. public void conditionalTest(int a, int b) { if ((a > 0) && (b > 0)) { if (a > b) System.out.println("A"); else System.out.println("B"); } else if ((b < 0) || (a < 0)) System.out.println("C"); else System.out.println("D"); } What is printed as a result of the call conditionalTest(3, -2)?

C

Consider the following code segment. /* missing loop header */ { for (int k = 0; k < 4; k++) { System.out.print(k); } System.out.println(); } The code segment is intended to produce the following output. 0123 0123 0123 Which of the following can be used to replace /* missing loop header */ so that the code segment works as intended? for (int j = 0; j < 3; j++) for (int j = 1; j < 3; j++) for (int j = 1; j <= 3; j++)

I and III

At a certain high school students receive letter grades based on the following scale. Integer Score Letter Grade 93 or above A From 84 to 92 inclusive B From 75 to 83 inclusive C Below 75 F Which of the following code segments will assign the correct string to gradefor 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"; ІI. If (score >= 93) grade = "A"; If(84<= score <=92) grade "B"; If(75 <= score <=83) 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";

I and III only

Consider the following code segment. int x = 1; while ( /* condition */ ) { if(x % 2 ==0) { System.out.print (× + " "); } x = x + 2; } The following conditions have been proposed to replace /* condition */ in the code segment. x < 0 x <= 1 x < 10 For which of the conditions will nothing be printed?

I, II, and III

Consider the following instance variables and method that appear in a class representing student information. private int assignmentsCompleted; private double testAverage; public boolean isPassing() { /* implementation not shown */ } A student can pass a programming course if at least one of the following conditions is met. The student has a test average that is greater than or equal to 90. The student has a test average that is greater than or equal to 75 and has at least 4 completed assignments. Consider the following proposed implementations of the isPassing method. I. if (testAverage >= 90) return true; if (testAverage >= 75 && assignmentsCompleted >= 4) return true; return false; II. boolean pass = false; if (testAverage >= 90) pass = true; if (testAverage >= 75 && assignmentsCompleted >= 4) pass = true; return pass; III. return (testAverage >= 90) || (testAverage >= 75 && assignmentsCompleted >= 4); Which o

I, II, and III

Consider the following class. public class SomeMethods {public void one(int first) { / * implementation not shown * / } public void one(int first, int second) { / * implementation not shown * / } public void one(int first, String second) { / * implementation not shown * / } } Which of the following methods can be added to the SomeMethods class without causing a compile-time error? I.) public void one(int value) { / * implementation not shown * / } II.) public void one (String first, int second) { / * implementation not shown * / } III.) public void one (int first, int second, int third) { / * implementation not shown * / }

II and III only

Consider the following class definition. public class Something { private static int count = 0; public Something() { count += 5; } public static void increment() { count++; } } The following code segment appears in a method in a class other than Something. Something s = new Something(); Something.increment(); Which of the following best describes the behavior of the code segment?

The code segment creates a Something object s. The class Something's static variable count is initially 0, then increased by 5, then increased by 1.

Consider the following Bugs class, which is intended to simulate variations in a population of bugs. The population is stored in the method's int attribute. The getPopulation method is intended to allow methods in other classes to access a Bugs object's population value; however, it does not work as intended. public class Bugs { private int population; public Bugs(int p) { population = p; } public int getPopulation() { return p; } } Which of the following best explains why the getPopulation method does NOT work as intended?

The instance variable population should be returned instead of p, which is local to the constructor.

Consider the following method definition. The method printAllCharacters is intended to print out every character in str, starting with the character at index 0. public static void printAllCharacters(String str) { for (int x = 0; x < str.length(); x++) // Line 3 { System.out.print(str.substring(x, x + 1)); } } The following statement is found in the same class as the printAllCharacters method. printAllCharacters("ABCDEFG"); Which choice best describes the difference, if any, in the behavior of this statement that will result from changing x < str.length() to x <= str.length() in line 3 of the method?

The method call, which worked correctly before the change, will now cause a run-time error because it attempts to access a character at index 7 in a string whose last element is at index 6.

Consider the following code segment. The code is intended to read nonnegative numbers and compute their product until a negative number is read; however, it does not work as intended. (Assume that the readInt method correctly reads the next number from the input stream.) int k = 0; int prod = 1; while (k >= 0) { System.out.print("enter a number: "); k = readInt(); // readInt reads the next number from input prod = prod * k; } System.out.println("product: " + prod); Which of the following best describes the error in the program?

The negative number entered to signal no more input is included in the product.

Consider the following class definition. public class Password { private String password; public Password (String pwd) { password = pwd; } public void reset(String new_pwd) { password = new_pwd; } } Consider the following code segment, which appears in a method in a class other than Password. The code segment does not compile. Password p = new Password("password"); System.out.println("The new password is " + p.reset("password")); Which of the following best identifies the reason the code segment does not compile? Responses

The reset method does not return a value that can be printed.

Consider the following class definition. public class Info { private String name; private int number; public Info(String n, int num) { name = n; number = num; } public void changeName(String newName) { name = newName; } public int addNum(int n) { num += n; return num; } } Which of the following best explains why the class will not compile?

The variable num is not defined in the addNum method.

Consider the following code segment. int j = 1; while (j < 5) { int k = 1; while (k < 5) { System.out.println(k); k++; } j++; } Which of the following best explains the effect, if any, of changing the first line of code to int j = 0; ?

There will be four more values printed because the outer loop will iterate one additional time.

Consider the following code segment. int x = 7; int y = 4; boolean a = false; boolean b = false; if (x > y) { if (x % y >= 3) { a = true; x -= y; } else { x += y; } } if (x < y) { if (y % x >= 3) { b = true; x -= y; } else { x += y; } } What are the values of a, b, and x after the code segment has been executed? Responses

a = true, b = false, x = 7

Consider the following code segment. String str = "abcdef"; for (int rep = 0; rep < str.length() - 1; rep++) { System.out.print(str.substring(rep, rep + 2)); } What is printed as a result of executing this code segment?

abbccddeef

Consider the following method, which is intended to return true if at least one of the three strings s1, s2, or s3 contains the substring "art". Otherwise, the method should return false. public static boolean containsArt(String s1, String s2, String s3) { String all = s1 + s2 + s3; return (all.indexOf("art") != -1); } Which of the following method calls demonstrates that the method does not work as intended? Responses

containsArt ("rattrap", "similar", "today")

Consider the method getHours, which is intended to calculate the number of hours that a vehicle takes to travel between two mile markers on a highway if the vehicle travels at a constant speed of 60 miles per hour. A mile marker is a sign showing the number of miles along a road between some fixed location (for example, the beginning of a highway) and the current location. The following table shows two examples of the intended behavior of getHours, based on the int parameters marker1 and marker2. marker1marker2Return Value1002202.0100700.5 Consider the following implementation of getHours. public static double getHours(int marker1, int marker2) { /* missing statement */ return hours; } Which of the following statements can replace /* missing statement */ so getHours works as intended?

double hours = Math.abs(marker1 - marker2) / 60.0;

Consider the following method. public String wordPlay(String word) { String str = ""; for (int k = 0; k < word.length(); k++) { if (k % 3 == 0) { str = word.substring(k, k + 1) + str; } } return str; } The following code segment appears in another method in the same class as wordPlay. System.out.println(wordPlay("Computer Science")); What is printed as a result of executing the code segment?

eeSepC

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?

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

Consider the following method, which is intended to return the product of 3 and the nonnegative difference between its two int parameters. public int threeTimesDiff (int num1, int num2) { return 3 * (num1 - num2); } Which, if any, precondition is required so that the method works as intended for all values of the parameters that satisfy the precondition?

num1 >= num2

The Car class will contain two string attributes for a car's make and model. The class will also contain a constructor. public class Car { /* missing code */ } Which of the following replacements for /* missing code */ is the most appropriate implementation of the class?

private String make; private String model; public Car(String myMake, String myModel) { /* implementation not shown */ }

Consider the following class definition. public class Bird { private String species; private String color; private boolean canFly; public Bird(String str, String col, boolean cf) { species = str; color = col; canFly = cf; } } Which of the following constructors, if added to the Bird class, will cause a compilation error?

public Bird(String col, String str, boolean cf) { species = str; color = col; canFly = cf; }

Consider the following class definition. public class Example { private int x; // Constructor not shown. } Which of the following is a correct header for a method of the Example class that would return the value of the private instance variable x so that it can be used in a class other than Example ?

public int getX()


Conjuntos de estudio relacionados

Accounting Chapter 4 Concept Review

View Set

Entrepreneurship and new venture management

View Set

Part 8: Opioid-Associated Life-Threatening Emergencies

View Set

Computer Networks-Chapter 7 Review Questions

View Set

Chapter 13: ICMP Chapter questions and end chapter QUIZ

View Set

Precalculus Sine, Cosine, Tangent Wheel

View Set