Midterm Exam AP (2) PDF

Summary

This is an AP computer science midterm exam paper. The paper covers various concepts regarding programming and has questions about Java, String, math, calculations, etc.

Full Transcript

# Copy of Midterm 2 ## AP Computer Science A Test Booklet **1.** In the code segment below, assume that the *int* variable *n* has been properly declared and initialized. The code segment is intended to print a value that is 1 more than twice the value of *n*. ```java /* missing code */ System.ou...

# Copy of Midterm 2 ## AP Computer Science A Test Booklet **1.** In the code segment below, assume that the *int* variable *n* has been properly declared and initialized. The code segment is intended to print a value that is 1 more than twice the value of *n*. ```java /* missing code */ System.out.print(result); ``` Which of the following can be used to replace /* missing code */ so that the code segment works as intended? * I. *int result = n * 2;* *result = result + 1;* * II. *int result = n + 1;* *result = result * 2;* * III. *int result = (n + 1) * 2;* **(A)** I only **(B)** II only **(C)** III only **(D)** I and III **(E)** II and III **2.** Consider the following code segment. ```java int a = 5; int b = 8; int c = 3; System.out.println(a + b / c * 2); ``` What is printed as a result of executing this code? **(A)** 2 **(B)** 6 **(C)** 8 **(D)** 9 **(E)** 14 **3.** Consider the following code segment. ```java 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? **(A)** ABCD **(B)** BCDE **(C)** BCEF **(D)** BCDEF **(E)** ABCDEF **4.** In the code segment below, assume that the *int* variables *a* and *b* have been properly declared and initialized. ```java int c = a; int d = b; c += 3; d--; double num = c; num /= d; ``` Which of the following best describes the behavior of the code segment? **(A)** The code segment stores the value of *(a + 3) / b* in the variable *num*. **(B)** The code segment stores the value of *(a + 3) / (b - 1)* in the variable *num*. **(C)** The code segment stores the value of *(a + 3) / (b - 2)* in the variable *num*. **(D)** The code segment stores the value of *(a + 3) / (1 - b)* in the variable *num*. **(E)** The code segment causes a runtime error in the last line of code because *num* is type *double* and *d* is type *int*. **5.** Consider the following code segment, which is intended to find the average of two positive integers, *x* and *y*. ```java int x; int y; int sum = x + y; double average = (double) ( sum / 2); ``` Which of the following best describes the error, if any, in the code segment? **(A)** There is no error, and the code works as intended. **(B)** In the expression *(double) (sum / 2)*, the cast to *double* is applied too late, so the average will be less than the expected result for even values of *sum*. **(C)** In the expression *(double) (sum / 2)*, the cast to *double* is applied too late, so the average will be greater than the expected result for even values of *sum*. **(D)** In the expression *(double) (sum / 2)*, the cast to *double* is applied too late, so the average will be less than the expected result for odd values of *sum*. **(E)** In the expression *(double) (sum / 2)*, the cast to *double* is applied too late, so the average will be greater than the expected result for odd values of *sum*. **6.** Consider the following methods, which appear in the same class. ```java public void slope(int x1, int y1, int x2, int y2) { int xChange = x2 - x1; int yChange = y2 - y1; printFraction(yChange, xChange); } public void printFraction(int numerator, int denominator) { System.out.print(numerator + "/" + denominator); } ``` Assume that the method call *slope(1, 2, 5, 10)* appears in a method in the same class. What is printed as a result of the method call? **(A)** 8/4 **(B)** 5/1 **(C)** 4/8 **(D)** 2/1 **(E)** 1/5 **7.** Consider the following method, which is intended to calculate and return the expression $(x+y)^2 \over a-b$ ```java public double calculate(double x, double y, double a, double b) { return /* missing code */; } ``` Which of the following can replace /* missing code */ so that the method works as intended? **(A)** Math.sqrt(x ^ 2, y ^ 2, a - b) **(B)** Math.sqrt((x + y) ^ 2) / Math.abs(a, b) **(C)** Math.sqrt((x + y) ^ 2 / Math.abs(a - b)) **(D)** Math.sqrt(Math.pow(x + y, 2) / Math.abs(a, b)) **(E)** Math.sqrt(Math.pow(x + y, 2) / Math.abs(a - b)) **8.** Consider the following method: ```java public double myMethod(int a, boolean b) { /* implementation not shown */ } ``` Which of the following lines of code, if located in a method in the same class as myMethod, will compile without error? **(A)** int result = myMethod(2, false); **(B)** int result = myMethod(2.5, true); **(C)** double result = myMethod(0, false); **(D)** double result = myMethod(true, 10); **(E)** double result = myMethod(2.5, true); **9.** Consider the following code segment. ```java double num = 9 / 4; System.out.print(num); System.out.print(" "); System.out.print((int) num); ``` What is printed as a result of executing the code segment? **(A)** 2 2 **(B)** 2.0 2 **(C)** 2.0 2.0 **(D)** 2.25 2 **(E)** 2.25 2.0 **10.** Consider the following code segment. ```java double x = (int) (5.5 - 2.5); double y = (int) 5.5 - 2.5; System.out.println(x - y); ``` What is printed as a result of executing the code segment? **(A)** -1.0 **(B)** -0.5 **(C)** 0.0 **(D)** 0.5 **(E)** 1.0 **11.** Consider the following code segment. ```java int w = 1; int x = w / 2; double y = 3; int z = (int) (x + y); ``` Which of the following best describes the results of compiling the code segment? **(A)** The code segment compiles without error. **(B)** The code segment does not compile, because the *int* variable *x* cannot be assigned the result of the operation *w / 2*. **(C)** The code segment does not compile, because the integer value 3 cannot be assigned to the *double* variable *y*. **(D)** The code segment does not compile, because the operands of the addition operator cannot be of different types *int* and *double*. **(E)** The code segment does not compile because the result of the addition operation is of type *double* and cannot be cast to an *int*. **12.** Consider the following code segment. ```java double x = 4.5; int y = (int) x * 2; System.out.print(y); ``` What is printed as a result of executing the code segment? **(A)** 8 **(B)** 8.0 **(C)** 9 **(D)** 9.0 **(E)** 10 **13.** Consider the following code segment, which is intended to simulate a random process. The code is intended to set the value of the variable *event* 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 2 if the probability is greater than 70 percent but no more than 80 percent. The value of *event* should be set to 3 if the probability is greater than 80 percent. The variable *randomNumber* is used to simulate the probability of the event occurring. ```java 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;* **14.** The code segment below is intended to calculate the circumference *c* of a circle with the diameter *d* of 1.5. The circumference of a circle is equal to its diameter times pi. ```java /* missing declarations */ c = pi * d; ``` Which of the following variable declarations are most appropriate to replace /* missing declarations */ in this code segment? **(A)** *int pi = 3.14159;* *int d = 1.5;* *final int c;* **(B)** *final int pi = 3.14159;* *int d = 1.5;* *int c;* **(C)** *final double pi = 3.14159;* *double d = 1.5;* *double c;* **(D)** *double d = 1.5;* *final double c = 0.0;* *final double pi = 3.14159;* **(E)** *final double d = 1.5;* *final double c = 0.0;* **15.** Consider the following code segment. ```java String temp = "comp"; System.out.print(temp.substring(0) + " " + temp.substring(1) + " " + temp.substring(2) + " " + temp.substring(3)); ``` What is printed when the code segment is executed? **(A)** comp **(B)** comp **(C)** comp com co c **(D)** comp omp mp p **(E)** comp comp comp comp **16.** Consider the following code segment. ```java int a = 5; int b = 4; int c = 2; a *= 3; b += a; b /= c; System.out.print(b); ``` What is printed when the code segment is executed? **(A)** 2 **(B)** 4 **(C)** 9 **(D)** 9.5 **(E)** 19 **17.** Consider the following code segment. ```java String str = "CompSci"; int num = str.length(); System.out.println(str.substring(0, 3)); ``` What is the value of *num* when the code segment is executed? **(A)** 3 **(B)** 4 **(C)** 5 **(D)** 6 **(E)** 7 **18.** Consider the following code segment. ```java String str = "0"; str += str + 0 + 8; System.out.println(str); ``` What is printed as a result of executing the code segment? **(A)** 8 **(B)** 08 **(C)** 008 **(D)** 0008 **(E)** Nothing is printed, because numerical values cannot be added to a *String* object. **19.** The following method is intended to return *true* if and only if the parameter *val* is a multiple of 4 but is not a multiple of 100 unless it is also a multiple of 400. The method does not always work correctly. ```java public boolean isLeapYear(int val) { if ((val % 4) == 0) { return true; } else{ return (val % 400) == 0; } } ``` Which of the following method calls will return an incorrect response? **(A)** isLeapYear(1900) **(B)** isLeapYear(1984) **(C)** isLeapYear(2000) **(D)** isLeapYear(2001) **(E)** isLeapYear(2010) **20.** A student has created a *Car* class. The class contains variables to represent the following. * A *String* variable called *color* to represent the color of the car. * An *int* variable called *year* to represent the year the car was made. * A *String* variable called *make* to represent the manufacturer of the car. * A *String* variable called *model* to represent the model of the car. The object *vehicle* will be declared as type *Car*. Which of the following descriptions is accurate? **(A)** An instance of the *vehicle* class is *Car*. **(B)** An instance of the *Car* object is *vehicle*. **(C)** An attribute of the *year* object is *int*. **(D)** An attribute of the *vehicle* object is *color*. **(E)** An attribute of the *Car* instance is *vehicle*. **21.** Consider the code segment below. ```java int a = 1988; int b = 1990; String claim = "that the world's athletes " + "competed in Olympic Games in "; String s = "It is " + true + claim + a + " but " + false + claim + b + "."; System.out.println(s); ``` What, if anything, is printed when the code segment is executed? **(A)** It is trueclaima but falseclaimb. **(B)** It is trueclaim1998 but falseclaim1990. **(C)** It is true that the world's athletes competed in Olympic Games in a but false that the world's athletes competed in Olympic Games in b. **(D)** It is true that the world's athletes competed in Olympic Games in 1988 but false that the world's athletes competed in Olympic Games in 1990. **(E)** Nothing is printed because the code segment does not compile. **22.** Consider the following code segment, which is intended to print the digits of the two-digit *int* number *num* in reverse order. For example, if *num* has the value 75, the code segment should print 57. Assume that *num* has been properly declared and initialized. ```java /* missing code */ System.out.print(onesDigit); System.out.print(tensDigit); ``` Which of the following can be used to replace /* missing code */ so that the code segment works as intended? **(A)** *int onesDigit = num % 10;* *int tensDigit = num / 10;* **(B)** *int onesDigit = num / 10;* *int tensDigit = num % 10;* **(C)** *int onesDigit = 10 / num;* *int tensDigit = 10 % num;* **(D)** *int onesDigit = num % 100;* *int tensDigit = num / 100;* **(E)** *int onesDigit = num / 100;* * int tensDigit = num % 100;* **23.** Which of the following expressions evaluate to 7 ? * I. 9 + 10 % 12 * II. (9 + 10) % 12 * III. 9 - 2 % 12 **(A)** I only **(B)** II only **(C)** I and III **(D)** II and III **(E)** I, II, and III **24.** Consider the following statement. ```java boolean x = (5 < 8) == (5 == 8); ``` What is the value of *x* after the statement has been executed? **(A)** 3 **(B)** 5 **(C)** 8 **(D)** true **(E)** false **25.** Consider the following Boolean expression in which the *int* variables *x* and *y* have been properly declared and initialized. ```java ( x <= 10 ) == ( y > 25) ``` Which of the following values for *x* and *y* will result in the expression evaluating to *true* ? **(A)** *x = 8 and y = 25* **(B)** *x = 10 and y = 10* **(C)** *x = 10 and y = 30* **(D)** *x = 15 and y = 30* **(E)** *x = 25 and y = 30* **26.** Consider the following class. ```java public class WindTurbine { private double efficiencyRating; public WindTurbine() { efficiencyRating = 0.0; } public WindTurbine(double e) { efficiencyRating = e; } } ``` Which of the following code segments, when placed in a method in a class other than *WindTurbine*, will construct a *WindTurbine* object *wt* with an *efficiencyRating* of 0.25? **(A)** *WindTurbine wt = new WindTurbine(0.25);* **(B)** *WindTurbine wt = 0.25;* **(C)** *WindTurbine wt = new WindTurbine();* *wt = 0.25;* **(D)** *WindTurbine wt = new WindTurbine();* *wt.efficiencyRating = 0.25;* **(E)** *new WindTurbine wt = 0.25;* **27.** Consider the following static method. ```java public static int calculate(int x) { x = x + x; x = x + x; x = x + x; return x; } ``` Which of the following can be usedto replace the body of *calculate* so that the modified version of *calculate* will return the same result as the original version for all *x*? **(A)** *return 3 + x;* **(B)** *return 3 * x;* **(C)** *return 4 * x;* **(D)** *return 6 * x;* **(E)** *return 8 * x;* **28.** Consider the following code segment. ```java System.out.print("Hello System.out.println"); System.out.print("!!!"); ``` What is printed as a result of executing the code segment? **(A)** Hello!!! **(B)** Hello System.out.println!!! **(C)** Hello !!! **(D)** !!! Hello System.out.println **(E)** Nothing is printed because the text "System.out.println" cannot appear inside a print statement. **29.** Consider the following code segment. ```java double firstDouble = 2.5; int firstInt = 30; int secondInt = 5; double secondDouble = firstInt - secondInt / firstDouble + 2.5; ``` What value will be assigned to *secondDouble* when the code segment is executed? **(A)** 5.0 **(B)** 12.5 **(C)** 25.5 **(D)** 29.0 **(E)** 30.5 **30.** Consider the following code segment. ```java num += num; num *= num; ``` Assume that *num* has been previously declared and initialized to contain an integer value. Which of the following best describes the behavior of the code segment? **(A)** The value of *num* is two times its original value. **(B)** The value of *num* is the square its original value. **(C)** The value of *num* is two times the square of its original value. **(D)** The value of *num* is the square of twice its original value. **(E)** It cannot be determined without knowing the initial value of *num*. **31.** Consider the following code segment. ```java System.out.print("One"); // Line 1 System.out.print("Two"); // Line 2 System.out.print("Three"); // Line 3 System.out.print("Four"); // Line 4 ``` The code segment is intended to produce the following output, but does not work as intended. ``` OneTwo ThreeFour ``` Which of the following changes can be made so that the code segment produces the intended output? **(A)** Changing *print* to *println* in line 1 only **(B)** Changing *print* to *println* in line 2 only **(C)** Changing *print* to *println* in line 3 only **(D)** Changing *print* to *println* in lines 2 and 3 only **(E)** Changing *print* to *println* in lines 1, 2, 3, and 4 **32.** Consider the following code segment. ```java int a = 3 + 2 * 3; int b = 4 + 3 / 2; int c = 7 % 4 + 3; double d = a + b + c; ``` What is the value of *d* after the code segment is executed? **(A)** 14.0 **(B)** 18.0 **(C)** 20.0 **(D)** 20.5 **(E)** 26.0 **33.** Consider the following code segment, where *k* and *count* are properly declared and initialized *int* variables. ```java k++; k++; count++; k--; count++; k--; ``` Which of the following best describes the behavior of the code segment? **(A)** The code segment leaves both *k* and *count* unchanged. **(B)** The code segment increases both *k* and *count* by 2. **(C)** The code segment increases *k* by 4 and *count* by 2. **(D)** The code segment leaves *k* unchanged and increases *count* by 2. **(E)** The code segment increases *k* by 2 and leaves *count* unchanged. **34.** Which of the following expressions evaluate to 3.5? * I. (double) 2 / 4 + 3 * II. (double) (2 / 4) + 3 * III. (double) (2 / 4 + 3) **(A)** I only **(B)** III only **(C)** I and II only **(D)** II and III only **(E)** I, II, and III **35.** Consider the following code segment. ```java int x = 5; int y = 6; /* missing code */ z = (x + y) / 2; ``` Which of the following can be used to replace /* missing code */ so that the code segment will compile? * I. *int z = 0;* * II. *int z;* * III. *boolean z = false;* **(A)** I only **(B)** II only **(C)** I and II only **(D)** II and III only **(E)** I, II, and III

Use Quizgecko on...
Browser
Browser