AP CSA Semester Exam

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

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? A. 1 B. 3 C. 5 D. 7 E. 8

A. 1

Assume that x and y are boolean variables and have been properly initialized. The expression ! ( ! ( a != b ) && ( b > 7 ) ) is equivalent tot which of the following? A. ( a != b ) || ( b < 7 ) B. ( a != b ) || ( b <= 7 ) C. ( a == b ) || ( b <= 7 ) D. ( a != b ) && ( b <= 7 ) E. ( a == b ) && ( b <= 7 )

B. ( a != b ) || ( b <= 7 )

Consider the following code segments, which are each intended to convert grades from a 100-point scale to a 4.0-point scale and print the result. A grade of 90 or above should yield a 3.0, a grade of 80 to 89 should yield a 3.0, a grade of 70 to 79 should yield a 2.0, and any grade lower than 70 should yield a 0.0. Assume that grade is an int variable that has be properly declared and initialized. Code Segment I double point = 0.0; if (grade > 89) { points += 4.0; } if (grade > 79) { points += 3.0; } if (grade > 69) { points += 2.0; } else (grade > 70) { points += 0.0; } System.out.println(points); Code Segment II double point = 0.0; if (grade > 89) { points += 4.0; } if (grade > 79) { points += 3.0; } if (grade > 69) { points += 2.0; } else (grade < 70) { points += 0.0; } System.out.println(points); which of the following statements correctly compares the values printed by the two methods? A. The two code segments print the same value only when grade is below 80. B. The two code segments print the same value only when grade is 90 or above or grade is below 80. C. The two code segments print the same value only when grade is 90 or above. D. Both code segments print the same value for all possible values of grade. E. The two code segments print different values for all possible values of grade.

A. The two code segments print the same value only when grade is below 80.

Assume that x and y are boolean variables and have been properly initialized. ( x | | y ) && x Which of the following always evaluates to the same value as the expression above? A. x B. y C. x && y D. x || y E. x !=y

A. x

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? A. A B. B C. C D. D E. Nothing is Printed

B. B

Consider the following class definitions public class Person { private String name; public String getName( ) ( return name; ) } public class Book { private String author; private String title; private Person borrower; public Book(String a, String t) { author - a; title = t; borrower - null; } public void printDetails( ) { System.out.println("Author: " + author + " Title: " + title); if ( /* missing code */ ) { System.out.println(" Borrower: " + borrower.getName( ) ); } } public void setBorrower(Person b) { borrower = b; } } Which of the following can replace /* missing code */ so that the printDetails method CANNOT cause a run-time error? I. borrower.equals(null) II. borrower !=null III. borrower.getName() ! = null A. I only B. II only C. III only D. I and II E. II and III

B. II only

Assume that x and y are boolean variables and have been properly initialized. ( x && y ) && !( x || y ) Which of the following best describes the result of evaluating the expression above? A. true always B. false always C. true only when x is true and y is true D. true only when x and y have the same value E. true only when x and y have different values

B. false always

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 int j = 0; A. there will be one more value printed because the outer loop will iterate one additional time. B. there will be four more value printed because the outer loop will iterate one additional time. C. there will be one less value printed because the outer loop will iterate one fewer time. D. there will be four less value printed because the outer loop will iterate one fewer time.

B. there will be four more value printed because the outer loop will iterate one additional time.

Which of the following code segments produces the output "987654321" ? A. int num = 10; while (num > 0) { System.out.print(num); num --; } B. int num = 10; while (num >= 0) { System.out.print(num); num --; } C. int num = 10; while (num > 1) { System.out.print(num); num --; } D. int num = 10; while (num >= 1) { System.out.print(num); num --; } E. int num = 10; while (num <= 9) { System.out.print(num); num --; }

C. int num = 10; while (num > 1) { System.out.print(num); num --; }

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, +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? A. The method call will print fewer characters than it did before the change because the loop will iterate fewer times. B. The method call will print more characters than it did before the change because the loop will iterate more times. C. 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. D. 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 8 in a string whose last element is at index 7. E. The behavior of the code segment will remain unchanged.

C. 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 String str = "a black cat sat on a table"; nt counter = 0; for (int i = 0; i < str.length( ) - i; i++) { If (str.substring(i, i + 1). equals("a") && !str,substring(i + 1, i + 2). equals("b") ) { counter++; } } Sysmte.out.println(counter); What is printed as a result of executing this code? A. 1. B. 2 C. 3 D. 5 E. 6

D. 5

Assume that a, b, and c are boolean variables that have been properly declared and initialized. Which of the following boolean expressions is equivalent to !(a && b) || c ? A. a && b && c B. a || b || c C. !a && b || c D. !a && !b && c E. !a || !b || c

E. !a || !b || c

Consider the following statement. Assume that a and b are properly declared the initialized boolean variables. booleans c = ( a && b ) | | ( ! a && b ) ; Under which of the following conditions will c be assigned the value false? A. Always B. Never C. When a and b have the same value D. When a has the value false E. When b has the value false

E. When b has the value false

Consider the following method that is intended to determine fi the double values d1 and d2 are close enough to be considered equal. For example, given a tolerance of 0.001, the values 54. 32271 and 54.32294 would be considered equal. /** @return true if d1 and d2 are within the specified tolerance * false otherwise */ public boolean almostEqual (double d1, double d2, double tolerance) { /* missing code */ } Which of the following should replace /* missing code */ so that almost equal will work as intended? A. return ( d1 - d2 ) <= tolerance B. return (( d1 + d2 ) / 2 ) <= tolerance C. return ( d1 - d2 ) >= tolerance D. return (( d1 + d2 ) / 2 ) >= tolerance E. return Math.abs( d1 - d2 ) <= tolerance

E. return Math.abs( d1 - d2 ) <= tolerance

Assume that the boolean variables a and b have been declared and initialized. Consider the following expression. (a && (b || !a)) == a && b Which of the following best describes the conditions under which the expression will evaluate to true? A. only when a is true B. only when b is true C. only when both a and b are true D. the expression will never evaluate to true E. the expression will always evaluate to true

E. the expression will always evaluate to true

Consider the following method, biggest, which is intended to return the greatest of three integer. It does not always work as inteded. public static int biggest(int a. int b, int c) { if ( ( a > b ) && ( a > c ) ) { return a; } else if ( ( b > a ) && ( b > c ) ) { return b; } else { return c; } } Which of the following best describes the error in the method? A. biggest always returns the value of A B. biggest may not work correctly when c has the greatest value C. biggest may not work correctly when a and b has the greatest value D. biggest may not work correctly when a and c has the greatest value E. biggest may not work correctly when b and c has the greatest value

C. biggest may not work correctly when a and b has the greatest value

Consider the following code segment int count = 5; while (count < 100) { count = count * 2; } count = count +1; What will be the value of count as a result of executing the code segment? A. 100 B. 101 C. 160 D. 161 E. 321

D. 161

Assume that the boolean variables a and b have been declared and initialized. Consider the following expression. !( !(a && ) || ( c || !d )) Which of the following best describes the conditions under which the expression will evaluate to true? A. ( a && b) && ( !c && d ) B. ( a || b) && ( !c && d ) C. ( a && b) || ( c && !d ) D. ( !a || !b ) && ( !c && d ) E. !( a && b) || ( c && !d )

A. ( a && b) && ( !c && d )

Consider the following Boolean expressions. I. A && B II. !A && !B Which of the following best describes the relationship between values produces by expression I and expression II? A. Expression I and expression II evaluate to different values for all values of A and B. B. Expression I and expression II evaluate to same values for all values of A and B. C. Expression I and expression II evaluate to same values only when A and B are the same. D. Expression I and expression II evaluate to same values only when A and B are the different. E. Expression I and expression II evaluate to same values whenever A is true.

D. Expression I and expression II evaluate to same values only when A and B are the different.

Which of the following expressions evaluates to 3.5? I. (double) 2 / 4 + 3 II. (double) ( 2 / 4 ) + 3 III. (double) ( 2 / 4 + 3 ) A. I only B. II only C. I and II only D. II and III only E. I, II, and III

A. I only

Consider the following code segment, which is intended to simulate a random process. The code is intended to set the value of the variable even to exactly one of the values 1, 2, or 3, depending on the probability of an event occurring. The value of event, should be set to 1 if the probability is 70 percent or less. The value of event should be set to 1, if the probability is greater than 70 precent but no more than 80 precent. The value of event should be set to 3 if the probability is greater than 8- percent. The variable randomNumber is used to simulated the probability of the event occurring. int event = 0; if (randomNumber <= 0.70) { event = 1; } if (randomNumber <= 0.80) { event = 2; } else { event = 3; } The code does not work as intended. Assume that the variable randomNumber has been properly declared and initialized. Which of the following initializations for randomNumber will demonstrate that the code segment will not work as intended? A. randomNumber = 0.70; B. randomNumber = 0.80; C. randomNumber = 0.85; D. randomNumber = 0.90; E. randomNumber = 1.00;

A. randomNumber = 0.70;

Consider the following two code segments. Assume that the int variables m and n have been properly declared and initialized and are both greater than 0. I. for (int 1 = 0; i < m * n; i++) { System.out.print("A"); } II. for (int j - 1; j <= m; j++) { for (int k = 1; k < n; k++ { System.out.print("B"); } } Assume that the initial values of m and n are the same in code segment I as they are in code II. Which of the following correctly compares the number of times that "A" and "B" are printed when each code segment is executed? A. "A" is printed m fewer times than "B" B. "A" is printed n fewer times than "B" C. "A" is printed m more times than "B" D. "A" is printed n more times than "B"

C. "A" is printed m more times than "B"

Consider the following two code segments where the int variable choice has been properly declared and initialized. Code Segment A if (choice > 10) { System.out.println("blue"); } else if (choice < 5) { System.out.println("red"); } else { System.out.println("yellow"); } Code Segment B if (choice > 10) { System.out.println("blue"); } if (choice < 5) { System.out.println("red"); } else { System.out.println("yellow"); } Assume that both code segments initialized choice to the same integer value. Which of the following best describes the conditions on the initial value of the variable choice that will cause the two code segments to produce different output? A. choice < 5 B. choice >= 5 and choice <= 10 C. choice > 10 D. choice == 5 or choice == 10 3. There is no value for choice that will cause the two code segments to produce different output.

C. choice > 10

Consider the following code segment in which int variable x has been properly declared and initialized. if ( x % 2 == 1) { System.out.println("YES"); } else { System.out.println("NO"); } Assuming that x is initialized to the same positive integer value as the original, which of the following code segments will produce the same output as the original code segment. I. if ( x % 2 == 1) { System.out.println("YES"); } if ( x % 2 == 0) { System.out.println("NO"); } II. if ( x % 2 == 1) { System.out.println("YES"); } if ( x % 2 == 0) { System.out.println("NO"); } else { System.out.println("NONE"); } III. boolean test = x % 2 == 0; if (test) { System.out.println("YES"); } else { System.out.println("NO"); } A. I only B. II only C. III only D. I and II only E. I, II, and III

D. I and II only

Assume that x and y have been declared and initialized with int values. Consider the following Java expression ( y > 10000 ) | | ( x > 1000 && x < 1500 ) Which of the following is equivalent to the expression given above? A. ( y > 10000 || x > 1000 ) && ( y > 10000 || x < 1500 ) B. ( y > 10000 || x > 1000 ) || ( y > 10000 || x < 1500 ) C. ( y > 10000 ) && ( x > 1000 || x < 1500 ) D. ( y > 10000 && x > 1000 ) || ( y > 10000 && x < 1500 ) E. ( y > 10000 && x > 1000 ) && ( y > 10000 && x < 1500 )

A. ( y > 10000 || x > 1000 ) && ( y > 10000 || x < 1500 )

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? A. containsArt("rattrap","similar","today") B. containsArt("start","article","Bart") C. containsArt("harm","chortle","crowbar") D. containsArt("matriculate","caral","arbitrary") E. containsArt("darkroom","cartoon","articulate")

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

Consider the following code segments. Code segment 2 is a revision of code segment 1 in which the loop increment has been changed. Code Segment 1 int sum = 0; for (int k - i; k <= 30; k++) { sum += k; } System.out.println("The sum is: " + sum); Code Segment 2 int sum = 0; for (int k - i; k <= 30; k = k +2) { sum += k; } System.out.println("The sum is: " + sum); Code segment 1 prints the sum of the integers from 1 through 30, inclusive. Which of the following best explains how the output changes from code segment 1 to code segment 2? A. Code segment 1 and code segment 2 will produce the same output. B. Code segment 2 will print the sum of only the even integers from 1 through 30, inclusive because it starts sum at zero, increments k by twos, and terminated when k exceeds 30. C. Code segment 2 will print the sum of only the odd integers from 1 through 30, inclusive because it starts k at one, increments k by twos, and terminated when k exceeds 30. D. Code segment 2 will print the sum of only the even integers from 1 through 60, inclusive because it starts sum at zero, increments k by twos, and iterates 30 times. E. Code segment 2 will print the sum of only the odd integers from 1 through 60, inclusive because it starts k at one, increments k by twos, and iterates 30 times.

C. Code segment 2 will print the sum of only the odd integers from 1 through 30, inclusive because it starts k at one, increments k by twos, and terminated when k exceeds 30.

Consider the following class definition. Each object of the class Item will store item's name as itemName, the item's regular print, in dollars, as regPrice, and the discount that is applied to the regular print when the item is on sale is discountPercent. For example, a discount of 15% is stored in discountPercent as 0.15 public class Item { private String itemName; private double regPrice: private double discountPercent; public Item (String name, double price, double discount) { itemName = name; regPrice = price; discountPercent = discount; } public Item (String name, double price_ { itemName = name; regPrice = price; discountPercent = 0.35; } /* Other methods not shown */ } Which of the following code segments, found in a class other than Item, can be used to create an item with a regular prince of $10 and a discount of 25%? I. Item b = new Item("blanket", 10.0, 0.25); II. I. Item b = new Item("blanket", 10.0); III. Item b = new Item("blanket", 0.25, 10.0); A. I only B. II only C. III only D. I and II only E. I, II and III

D. I and II only

Consider the following class public class SomeMethods { public void one(int first) { / * implementation not shown * / } publics 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 * / } A. I only B. I and II only C. I and III only D. II and III only

D. II and III only

Consider the following methods. /** Precondition: a > 0 and b > 0 */ public static int methodOne(int a, int b) { int loopCount = 0; for (int 1 = 0; 1 < a / b; i++) { loopCount++; } return loopCount: } /** Precondition: a > 0 and b > 0 */ public static int methodTwo(int a, int b) { int loopCount = 0; int i = 0; while (i < a) { loopCount++; i += b; } return loopCount; } Which of the following best describes the condition sunder which methodOne and methodTwo return the same value?? A. when a and b are both even B. when a and b are both odd C. when a is even and b is odd D. when a % b is equal to zero E. when a % b is equal to one

D. when a % b is equal to zero


Conjuntos de estudio relacionados

AP art history snapshots 125-152

View Set

Lesson 28: Florence: The Cradle of the Renaissance

View Set

Final Threats and Attacks Group Chapter 13-17, Network Defense Exam Chapter 18-20, Cryptography and Endpoint Protection Exam Chapter 21-23, Protocols and Log Files Group Chapter 24-25, Analyzing Security Data Chapter 26-28

View Set

Medical Terminology, Chapter 3, Directional Terms, Anatomic Planes, Regions, and Quadrants (STUDY GUIDE)

View Set

Местоимения: Упражнение (для ЕГЭ) на все типы местоимений

View Set