A. Basic Knowledge

    客观题

Basic Knowledge

该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。

Problem 1. Basic knowledge

  1. Read the following code. Which of the following statements is/are true?
int a[] = {1, 2, 3};
int b[] = {10, 11, 12};
int c[100] = {1};
int main(void){
    ...
}

{{ multiselect(1) }}

  • The type of a is int [3].
  • The type of a is int *.
  • We can write b = a; in main function, this operation could copy array a to array b.
  • We can write b = a; in main function, this operation could let b be a pointing array which points to array a.
  • We can write b = {100, 101, 102}; in main function, this operation could reassign value in array b.
  • After initialization, array c has only one element c[0] is 1, all other elements are 0.
  • Array c has c[0]...c[100] all 101 elements.
  1. (1) Read the following code. Which of the following statements is/are true?
void swap(int *pa, int *pb) {
  int tmp = *pa;
  *pa = *pb;
  *pb = tmp;
}
int main(void) {
  int x = 10, y = 15;
  int *px = &x;
  int *py = &y;
  swap(px, py);
  printf("%d %d\n", x, y);
}

{{ multiselect(201) }}

  • The asterisk (*) in the function parameter declaration int *pa is a part of int *, meaning that pa is a pointer to int.
  • The asterisk (*) in the statement int tmp = *pa is the dereference operator, which is used to access the object that pa points to.
  • The asterisk (*) in the statement *pb = tmp is the pointer symbol, meaning that pb is a pointer.
  • The asterisk (*) in the statement *pb = tmp is the dereference operator, which is used to access the object that pb points to.
  • We can change the declaration and initialization of px and py to int* px = &x, py = &y, which is the same as we do in the code.

(2) Which of the following statements is/are true?

{{ multiselect(202) }}

  • The code cannot swap correctly, because we swapped the values that px and py point to, but not the values of x and y.

  • If we rewrite the function swap as follows

    void swap(int a, int b) {
      int tmp = a;
      a = b;
      b = tmp;
    }
    

    and replace swap(&x, &y) with swap(x, y) in the main function, the values of x and y can be exchanged successfully.

  • The parameter declarations of swap can be rewritten as int *pa, pb, because pa and pb are of the same type.

  • The parameter declarations of swap can be rewritten as int *pa, *pb, because pa and pb are of the same type.

  • The function swap will output the values of *pa and *pb.

  • None of the above.

  1. Read the following code. Which of the following statements is/are true?
void swap(int *pa, int *pb) {
  int tmp = *pa;
  *pa = *pb;
  *pb = tmp;
}
int a[] = {1, 2, 3};
int b[] = {9, 9, 8, 2, 4, 4, 3, 5, 3};
int main(void){
  int *pa = a, *pb = b;
  swap(pa, pb);
  for (int i = 0; i < 3; ++i)
    printf("%d ", pa[i]);
  for (int i = 0; i < 3; ++i)
    printf("%d ", pb[i]);
}

{{ multiselect(3) }}

  • The code cannot run, because we cannot assign value of an array to a pointer.

  • After swap, array a and array b are exchanged successfully, array a has content {9,9,8,2,4,4,3,5,3} and array b has content {1,2,3}.

  • The swap cannot be done successfully, because a and b are of different length, this would incur array subscript out of range.

  • The code would output 9 2 3 1 9 8 .

  • If we rewrite the function swap as follows

void swap(int **ppa, int **ppb){
  int *tmp = *ppa;
  *ppa = *ppb;
  *ppb = tmp;
}

and replace swap(pa, pb) with swap(&pa, &pb) in the main function, the code would output 9 9 8 1 2 3 .

  1. Write down the type of each expression. For pointer types, place exactly one space between the pointer type and the first asterisk, and no spaces between consecutive asterisks (e.g. int *, int **, or int ***, but not int* or int * *).
int *pa;
int x;
Expression pa *pa &pa *&pa x &x
Type {{ input(401) }} {{ input(402) }} {{ input(403) }} {{ input(404) }} {{ input(405) }} {{ input(406) }}
  1. Read the following code.
int min_element(int *array, int l, int r) {
  int pos = l;
  while (l < r) {
    if (array[l] < array[pos])
      pos = l;
    ++l;
  }
  return pos;
}

int a[] = {1, 4, 2, 8, 5, 7};

(1) Which of the following statements is/are true?

{{ multiselect(501) }}

  • min_element(array, l, r) returns min{\min\{ array[i] i[l,r)}\mid i\in[l,r)\}.
  • min_element(array, l, r) returns min{\min\{ array[i] i[l,r]}\mid i\in[l,r]\}.
  • min_element(array, l, r) returns the index of the first occurrence of min{\min\{ array[i] i[l,r)}\mid i\in[l,r)\}.
  • min_element(array, l, r) prints the index of the first occurrence of min{\min\{ array[i] i[l,r)}\mid i\in[l,r)\} to the standard output.
  • To make min_element(array, l, r) return the index of the last occurrence of min{\min\{ array[i] i[l,r)}\mid i\in[l,r)\}, replace array[l] < array[pos] (on line 4) with array[l] <= array[pos].

(2) Suppose we have the following main function.

int main(void) {
  printf("%d\n", min_element(a, 0, 6));
}

Which of the following statements is/are true?

{{ multiselect(502) }}

  • The function main contains undefined behavior.
  • Calling min_element(a+2, 0, 6) contains undefined behavior.
  • Calling min_element(NULL, 0, 0) contains undefined behavior.
  • The type of the parameter array in min_element we called is int [6].
  • The type of the parameter array is int [], where the length is determined at run-time and may change.
  • The type of the parameter array is int *.
  • Since we want to find the minimum element in the entire array a, we can simply write min_element(a), and the parameters l and r will be assigned with 0 and 6 automatically.
  1. Given the functions swap and min_element as defined in the previous two questions. Read the following code.
void sort(int *array, int n) {
  for (int i = 0; i < n - 1; ++i) {
    int min_pos = min_element(array, i, n);
    swap(&array[i], &array[min_pos]);
    // Note: If you have difficulty understanding this function, uncomment the following lines, run the code and look at the output.
    // for (int i = 0; i < n; ++i)
    //   printf("%d ", array[i]);
    // printf("\n");
  }
}
int main(void) {
  int a[] = {8, 5, 7, 1, 4, 2};
  sort(a, 6);
  for (int i = 0; i < 6; ++i)
    printf("%d ", a[i]);
  printf("\n");
}

(1) Regarding the sort function, which of the following statements is/are true?

{{ multiselect(601) }}

  • swap(&array[i], &array[min_pos]) is equivalent to swap(array + i, array + min_pos).
  • At each iteration, the smallest element in {array[i], ..., array[n - 1]} is found and then placed at index i by swapping with array[i].
  • The effect of sort on the array is not changed if we rewrite the loop condition as i < n.
  • sort(array, n) sorts the elements {array[0], ..., array[n - 1]} in non-decreasing order.

(2) Which of the following statements is/are true?

{{ multiselect(602) }}

  • The program prints 1 2 4 5 7 8 .
  • If we replace sort(a, 6) as sort(a, 4), the output becomes 1 2 4 5 8 7 .
  • If we replace sort(a, 6) as sort(a, 3), the output becomes 5 7 8 1 4 2 .
  • If we replace sort(a + 2, 4), the output becomes 8 5 1 2 4 7 , i.e. only the last four elements are sorted.

CS100 Spring2025 Homework 2

未认领
状态
已结束
题目
5
开始时间
2025-3-10 3:30
截止时间
2025-3-21 23:59
可延期
0 小时