0:00

What is printed as a result of executing the following code segment?

List<Integer> list1 = new ArrayList<Integer>();
list1.add(new Integer(1));
list1.add(new Integer(2));
list1.add(new Integer(3));
list1.set(2, new Integer(4));
list1.add(2, new Integer(5));
list1.add(new Integer(6));
System.out.println(list1);
先替换第三位,再插入第三位。
3

Given the following code and assume that nums initially contains [0, 0, 4, 2, 5, 0, 3], what will nums contain as a result of executing numQuest?

private List<Integer> nums;

// precondition: nums.size() > 0;
// nums contains Integer objects
public void numQuest()
{
  int k = 0;
  Integer zero = new Integer(0);
  while (k < nums.size())
  {
   if (nums.get(k).equals(zero))
     nums.remove(k);
   else
      k++;
  }
}
删除所有的0
4

Which of the following best describes the behavior of process1 and process2 (shown below)?

public static List<Integer> process1(int n)
{
   List<Integer> someList = new ArrayList<Integer>();
   for (int k = 0; k < n; k++)
      someList.add(k);
   return someList;
}

public static List<Integer> process2(int n)
{
   List<Integer> someList = new ArrayList<Integer>();
   for (int k = 0; k < n; k++)
      someList.add(k, k);
   return someList;
}
注意process2实际上也一直是在列表尾部插入元素,和process1没有区别。
5

What is printed as a result of executing the following code segment?

List<Integer> aList = new ArrayList<Integer>();
aList.add(new Integer(1));
aList.add(new Integer(2));
aList.add(1, new Integer(5));
aList.set(1, new Integer(4));
aList.add(new Integer(6));
aList.add(new Integer(3));
System.out.println(aList);
先插入第二位,再替换第二位。
4

What is printed as a result of executing the following code segment?

List<Integer> aList = new ArrayList<Integer>();
aList.add(new Integer(1));
aList.add(new Integer(2));
aList.remove(1);
aList.add(1, new Integer(3));
aList.set(1, new Integer(4));
aList.add(new Integer(5));
System.out.println(list);
先删除第二位,再插入第二位并接着替换第二位。
2

What is printed as a result of executing the following code segment?

List<String> list1 = new ArrayList<String>();
list1.add("a");
list1.add("b");
list1.add(0, "c");
list1.add(1, "d");
list1.set(2, "e");
list1.add("f");
System.out.println(list1);
注意ea给替换了。
2

Given the list nums = [4, 2, 3, 4, 5] what is the result after executing nums.remove(4)?

索引4对应的是最后的元素5
4

What is printed as a result of executing the following code segment?

List<String> list1 = new ArrayList<String>();
list1.add("a");
list1.add("b");
list1.add(0,"c");
list1.set(1, "d");
list1.set(0, "e");
list1.add("b");
System.out.println(list1);
最后列表中的前两位元素都是中途被替换掉的。
2

Assume that numList has been initialized with the following Integer objects: [0, 1, 2, 3, 4]. What is the value of numList after mystery(5) executes?

private List<Integer> numList;
public void mystery(int n)
{
    for (int i = 0; i < n; i++)
    {
        Integer obj = numList.remove(0);
        numList.add(obj);
    }
}
每轮循环先删除第一位,再将其插入到最后一位。
3

Assume that numList has been initialized with the following Integer objects: [5, 7, 8, 12]. Which of the following shows the values in numList after a call to mystery(11)?

private List<Integer> numList;
public void mystery(int value)
{
    int i = 0;
    while (i < numList.size() && numList.get(i) < value)
    {
        i++;
    }
    numList.add(i, value);
}
i = 3时循环结束。
2


陈 欣

AADPS创始人

发表评论