AP计算机2008年练习卷:多项选择
0:00
Consider the following method.
public static int mystery(int[] arr) { int x = 0; for (int k = 0; k < arr.length; k = k + 2) x = x + arr[k]; return x; }
Assume that the array nums has been declared and initialized as follows.
int[] nums = {3, 6, 1, 0, 1, 4, 2};
What value will be returned as a result of the call mystery(nums)
?
3
、1
、1
、2
相加。Consider the following partial Class declaration.
public class SomeClass { private int myA; private int myB; private int myC; // Constructor(s) not shown public int getA() { return myA; } public void setB(int value) { myB = value; } }
The following declaration appears in another Class.
SomeClass obj = new SomeClass();
Which of the following code segments will compile without error?
getA()
没有参数且非static
,private myA
不能在class
外直接访问。Which of the following Changes to SomeClass
will allow other Classes to access but not modify the value of myC
?
getA()
写就可以,选项考察了对method返回值的理解。Consider the following code segment.
int x = 7; int y = 3; if ((x < 10) && (y < 0)) System.out.println("Value is: " + x * y); else System.out.println("Value is: " + x / y);
What is printed as a result of executing the code segment?
false
)和整数除法。Consider the following method.
public ArrayList<Integer> mystery(int n) { ArrayList<Integer> seq = new ArrayList<Integer>(); for (int k = l; k <= n; k++) seq.add(new Integer(k * k + 3)); return seq; }
Which of the following is printed as a result of executing the following statement?
System.out.println(mystery(6));
k
从1
取到6
,依次计算k * k + 3
并插入seq
。Consider the following method that is intended to determine if 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 almostEqual
will work as intended?
tolerance
。Consider the following class declaration.
public class Person { private String myName; private int myYearOfBirth; public Person(String name, int yearOfBirth) { myName = name; myYearOfBirth = yearOfBirth; } public String getName() { return myName; } public void setName(String name) { myName = name; } // There may be instance variables, constructors, and methods that are not shown. }
Assume that the following declaration has been made.
Person student = new Person("Thomas", 1995);
Which of the following statements is the most appropriate for changing the name of student
from "Thomas"
to "Tom"
?
setName()
来更新field。事实上新建object的办法也可行,不过题目问的是最合适的途径,鉴于这是一道送分题,就不用想太多了。Consider the following class declaration.
public class Student { private String myName; private int myAge; public Student() { /* implementation not shown */ } public Student(String name, int age) { /* implementation not shown */ } // No other constructors }
Which of the following declarations will compile without error?
I. Student a = new Student();
II. Student b = new Student("Juan", 15);
III. Student c = new Student("Juan", "15");
Consider the following method that is intended to return the sum of the elements in the array key
.
public static int sumArray(int[] key) { int sum = O; for (int i = 1; i <= key.length; i++) { /* missing code */ } return sum; }
Which of the following statements should be used to replace /* missing code */
so that sumArray
will work as intended?
0
循环到length - 1
(即i < length
),如果i
在循环首尾都比正常大1
的话,索引需要减去1
。Consider the following instance variable and methods. You may assume that data
has been initialized with length > 0. The methods are intended to return the index of an array element equal to target
, or -1
if no such element exists.
private int[] data; public int seqSearchRec(int target) { return seqSearchRecHelper(target, data.length - 1); } private int seqSearchRecHelper(int target, int last) { // Line 1 if (data[last] == target) return last; else return seqSearchRecHelper(target, last - 1); }
For which of the following test cases will the call seqSearchRec(5)
always result in an error?
I. data
contains only one element.
II. data
does not contain the value 5.
III. data
contains the value 5 multiple times.
last
为-1
时的情况,这在data
不包含target
时会发生。Which of the following should be used to replace // Line 1
in seqSearchRecHelper
so that seqSearchRec
will work as intended?
last
为负数会导致索引越界,故直接return -1
表示没有找到。因为采用的是递归求解,不出现while
。Consider the following method.
public String mystery(String input) { String output = ""; for (int k = 1; k < input.length(); k = k + 2) { output += input.substring(k, k + 1); } return output; }
What is returned as a result of the call mystery("computer")
?
Consider the following code segment.
int[] arr = {7, 2, 5, 3, 0, 10}; for (int k = 0, k < arr.length - 1; k++) { if (arr[k] > arr[k + 1]) System.out.print(k + " " + arr[k] + " "); }
What will be printed as a result of executing the code segment?
Consider the following interface and class declarations.
public interface Vehicle { /** @return the mileage traveled by this Vehicle */ double getMileage(); } public class Fleet { private ArrayList<Vehicle> myVehiCles; /** @return the mileage traveled by all vehicles in this Fleet */ public double getTotalMileage() { double sum = 0.0; for (Vehicle v : myVehicles) { sum += /* expression */ ; } return sum; } // There may be instance variables, constructors, and methods that are not shown. }
Which of the following can be used to replace /* expression */
so that getTotalMileage
returns the total of the miles traveled for all vehicles in the fleet?
Consider the following method, isSorted
, which is intended to return true
if an array of integers is sorted in nondecreasing order and to return false
otherwise.
/** @param data an array of integers * @return true if the values in the array appear in sorted (nondecreasing) order */ public static boolean isSorted(int[] data) { /* missing code */ }
Which of the following can be used to replace /* missing code */
so that isSorted will work as intended?
I.
for (int k = 1; k < data.length; k++) { if (data[k - 1] > data[k]) return false; } return true;
II.
for (int k = 0; k < data.length; k++) { if (data[k] > data[k + 1]) return false; } return true;
III.
for (int k = 0; k < data.length - 1; k++) { if (data[k] > data[k + 1]) return false; else return true; } return true;
else
会导致在完成检查前就结束并返回。Consider the following incomplete method that is intended to return an array that contains the contents of its first array parameter followed by the contents of its second array parameter.
public static int[] append(int[] a1, int[] a2) { int[] result = new int[a1.length + a2.length]; for (int j = 0; j < a1.length; j++) result[j] = a1[j]; for (int k = 0; k < a2.length; k++) result[ /* index */ ] = a2[k]; return result; }
Which of the following expressions can be used to replace /* index */
so that append
will work as intended?
k = 0
时,索引要从a1.length
开始。Consider the following code segment.
int[] arr = {1, 2, 3, 4, 5, 6, 7}; for (int k = 3; k < arr.length - 1; k++) arr[k] = arr[k + 1];
Which of the following represents the contents of arr
as a result of executing the code segment?
Assume that myList
is an ArrayList
that has been correctly constructed and populated with objects. Which of the following expressions produces a valid random index for myList?
Assume that a
and b
have been defined and initialized as int
values. The expression
!(!(a != b) && (b > 7))
is equivalent to which of the following?
Consider the following method.
public static void arrayMethod(int nums[]) { int j = 0; int k = nums.length — 1; while (j < k) { int x = nums[j]; nums[j] = nums[k]; nums[k] = x; j++; k--; } }
Which of the following describes what the method arrayMethod()
does to the array nums
?
21-25题涉及当前已被移出考试范围的GridWorld case study。
Assume that the array arr
has been defined and initialized as follows.
int[] arr = /* initial values for the array */ ;
Which of the following will correctly print all of the odd integers contained in arr
but none of the even integers contained in arr
?
%
的用法。public static int mystery(int n) { int x = 1; int y = 1; // Point A while (n > 2) { x = x + y; // Point B y = x - y; n--; } // Point C return x; }
What value is returned as a result of the call mystery(6)
?
x
和y
的值分别是什么。Which of the following is true of method mystery
?
y
一直为正,故x
在// Point B
一定是大于初始值1
的,但考虑到循环可能因为初始n
过小而不执行,// Point C
处x
仍有可能为1
。如果循环能执行到
// Point B
的话,n
必然是满足判断条件,因而总是大于2
的。Consider the following code segment.
for (int k = 1; k <= 100; k++) if ((k % 4) == 0) System.out.println(k);
Which of the following code segments will produce the same output as the code segment above?
1
至100
间4
的倍数。Consider the following method.
public static String scramble(String word, int howFar) { return word.substring(howFar + 1, word.length()) + word.substring(0, howFar); }
What value is returned as a result of the call scramble("compiler", 3)
?
substring
的定义,注意method事实上把索引为3
的字符p
给吃掉了。Consider the following method.
public void mystery(int[] data) { for (int k = 0; k < data.length - 1; k++) data[k + 1] = data[k] + data[k + 1]; }
The following code segment appears in another method in the same class.
int[] values = {5, 2, 1, 3, 8}; mystery(values); for (int v : values) System.out.print(v + " "); System.out.println();
What is printed as a result of executing the code segment?
Consider the following method.
public int compute(int n, int k) { int answer = 1; for (int i = 1; i <= k; i++) answer *= n; return answer; }
Which of the following represents the value returned as a result of the call compute(n, k)
?
n
和自己相乘,因而实现了求n
的k
次方。Consider the following code segment.
int sum = 0; int k = 1; while (sum < 12 || k < 4) sum += k; System.out.println(sum);
What is printed as a result of executing the code segment?
k
不会变化,因而循环的判据会一直成立。Consider the following class declarations.
public class Point { private double x; // x-coordinate private double y; // y-coordinate public Point() { x = 0; y = 0; } public Point(double a, double b) { x = a; y = b; } // There may be instance variables, constructors, and methods that are not shown. } public class Circle { private Point center; private double radius; /** Constructs a circle where (a, b) is the center and r is the radius. */ public Circle(double a, double b, double r) { /* missing code */ } }
Which of the following replacements for /* missing code */
will correctly implement the Circle
constructor?
I.
center = new Point();
radius = r;
II.
center = new Point(a, b);
radius = r;
III.
center = new Point();
center.x = a;
center.y = b;
radius = r;
class
外给private
赋值。Consider the following code segment.
int num = 2574; int result = 0; while (num > 0) { result = result * 10 + num % 10; num /= 10; } System.out.println(result);
What is printed as a result of executing the code segment?
num
按位倒序组合起来。Consider the following method.
public void test(int x) { int y; if (x % 2 == 0) y = 3; else if (x > 9) y = 5; else y = 1; System.out.println("y = " + y); }
Which of the following test data sets would test each possible output for the method?
9
的奇数,和小于等于9
的奇数各一。Consider the following code segment.
int x = 1; while ( /* missing code */ ) { System.out.print(x + " "); x = x + 2; }
Consider the following possible replacements for /* missing code */
.
I. x < 6
II. x != 6
III. x < 7
Which of the proposed replacements for /* missing code */
will cause the code segment to print only the values 1 3 5
?
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?
Consider the following recursive method.
public int recur(int n) { if (n <= 10) return n * 2; else return recur(recur(n / 3)); }
What value is returned as a result of the call recur(27)
?
recur(27)
等于recur(18)
等于recur(12)
。Consider the following recursive method.
public static void whatsItDo(String str) { int len = str.length(); if (len > 1) { String temp = str.substring(0, len - 1); whatsItDo(temp); System.out.println(temp); } }
What is printed as a result of the call whatsItDo("WATCH")
?
substring()
的用法,注意输出在调用递归之后。
0 条评论