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)?

将数组中索引为偶数、从零开始的3112相加。
3

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()没有参数且非staticprivate myA不能在class外直接访问。
1

Which of the following Changes to SomeClass will allow other Classes to access but not modify the value of myC?

仿照getA()写就可以,选项考察了对method返回值的理解。
2

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)和整数除法。
3

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));
k1取到6,依次计算k * k + 3并插入seq
3

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
5

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"?

应该用object的setName()来更新field。事实上新建object的办法也可行,不过题目问的是最合适的途径,鉴于这是一道送分题,就不用想太多了。
4

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");

I和II对应题中给出的constructor,三则不然。
3

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
2

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时会发生。
2

Which of the following should be used to replace // Line 1 in seqSearchRecHelper so that seqSearchRec will work as intended?

第一行时,last为负数会导致索引越界,故直接return -1表示没有找到。因为采用的是递归求解,不出现while
2

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")?

将数组中索引为奇数、从一开始的字符合并到一起。
3

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?

找到前一位比后一位大的数。
2

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?

需要掌握for-each的用法。
5

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;
II会导致数组越界,III画蛇添足的else会导致在完成检查前就结束并返回。
1

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?

可以实际弄两个长度分别为3和4的数组在草稿纸上模拟一下,显然当k = 0时,索引要从a1.length开始。
4

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?

从第四个数开始进行替换,最后一个数保持不变。
3

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?

这个就背住吧。
2

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?

德摩根定律需要背住,或者用真值表迅速排除干扰项。
2

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?

将数组元素逆序的标准算法。
5

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?

需要掌握for-each和整数取模运算%的用法。
1

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)?

循环需要一共执行4次,可以在草稿纸上算一下每轮结束后xy的值分别是什么。
4

Which of the following is true of method mystery?

y一直为正,故x// Point B一定是大于初始值1的,但考虑到循环可能因为初始n过小而不执行,// Point Cx仍有可能为1
如果循环能执行到// Point B的话,n必然是满足判断条件,因而总是大于2的。
5

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?

输出的是11004的倍数。
5

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给吃掉了。
3

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?

后一个元素和之前的相加,注意存在累加,因此最后数组是单调递增的。
3

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和自己相乘,因而实现了求nk次方。
3

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不会变化,因而循环的判据会一直成立。
5

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;
I没有正确初始化圆心,III则试图在class外给private赋值。
2

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?

method会把num按位倒序组合起来。
5

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的奇数各一。
3

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?

II始终成立,会导致死循环。
4

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?

布尔代数的分配律,同样可以利用真值表试算。
1

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)
4

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()的用法,注意输出在调用递归之后。
3


陈 欣

AADPS创始人

发表评论