A. Basic Knowledge

    客观题

Basic Knowledge

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

Problem 1. Basic knowledge

  1. Read the following code. Which of the following statements is/are true?
#ifndef NULL
#ifdef __cplusplus
#ifndef _WIN64
#define NULL 0
#else
#define NULL 0LL
#endif  /* W64 */
#else
#define NULL ((void *)0)
#endif
#endif

{{ multiselect(1) }}

  • The value of NULL is undetermined until running time.
  • If we defined NULL somewhere else, this code would report a Compile Error.
  • NULL is a pointer.
  • NULL will be defined multiple times in this code.
  • In C++, NULL is 0, which is not a pointer.
  1. Read the following header file. Suppose this file's name is cs100_hw3.h under current work directory.
#ifndef CS100_HW3_H
#define CS100_HW3_H

#include <stdio.h>

...

#endif //CS100_HW3_H

{{ multiselect(2) }}

  • If we include stdio.h twice, we would have Compile Error.
  • To include this header, write #include <cs100_hw3.h> in our main C file.
  • Before we include cs100_hw3.h, we could define CS100_HW3_H in our main C file (with no affects to including header file).
  • After we include cs100_hw3.h, we have defined CS100_HW3_H in our main C file.
  1. Read the following code. Fill the blank with output corresponding to printfs, omitting the newline \n character.

    #include <stdio.h>
    
    int main(void) {
        char str[] = "Hello_CS100";
        printf("%s\n", str);      // (1)
        printf("%s\n", str + 7);  // (2)
        str[5] = 0;          
        printf("%s\n", str);      // (3)
        const char *fstr = "%s/\\\n";
        printf(fstr, str+1);      // (4)
    }
    
Code (1) (2) (3) (4)
Output {{ input(301) }} {{ input(302) }} {{ input(303) }} {{ input(304) }}
  1. Read the following code. Assume memory address is 64-bit. Which of the following statements is/are true?
#include <stdlib.h>

char *str = "Hello_World";
char arr[] = "abcdef";
int main(void) {
    int *ptr = malloc(10*sizeof(int));
    static int b[] = {1, 2, 3};
    free(ptr);
}

{{ multiselect(4) }}

  • The value of sizeof(str) is 11.
  • The value of sizeof(arr) is 6.
  • Before free, the memory ptr points to is in heap memory.
  • &ptr is a memory address located in stack memory.
  • The element of static array b is located in stack memory.
  • The type of "abcdef" is char [7].
  • We can use code such as str[2]='b'; to change the content of str.
  1. Read the following code. Which of the following statements is/are true?
int main(void) {
    int x = 2, y = 2;
    const int N = 100;
    const int *p = &x;
    int* const q = &y;
}

{{ multiselect(5) }}

  • If we write *p = 10, this would lead to Compile-Error.
  • If we write p = &y, this would lead to Compile-Error.
  • If we write *q = 10, this would lead to Compile-Error.
  • If we write q = &x, this would lead to Compile-Error.
  • const int *p = &x; is equivalent to int const *p = &x;.
  • int *ptr = &N; would generate warning. Furthermore, *ptr = 100 would lead to undefined behavior.
  1. Which of the following statements is/are true?

{{ multiselect(6) }}

  • malloc() would return an address where value initialized to 0.
  • If we want to free half memory of int *ptr = malloc(n * sizeof(int));, we could write free(ptr + n/2);
  • If we forget to free memory of malloc, this memory would be freed by C Memory Manager after pointer lifetime ends.
  • The freed memory may be allocated by next malloc() call.
  1. Read the following code. Which of the following statements is/are true?
struct Account{
    char username[10];
    int ID;
};

{{ multiselect(7) }}

  • sizeof(struct Account) is equal to 10*sizeof(char) + sizeof(int).
  • We could use struct Account A = {.username = "Alice", .ID = 0xbadbeef}; to initialize a struct variable.
  • If A and B are both struct Account, we could write b=a; to copy a's value to b.
  • C's struct is equivalent to C++'s struct.
  1. Which of the following statements is/are true?

{{ multiselect(8) }}

  • #define TABLE(a, b, n) a*n + b, the value of TABLE(1, 2, 3) is 5.
  • #define TABLE(a, b, n) a*n + b, the value of TABLE(1, 2, 3) * 3 is 9.
  • #define TABLE(a, b, n) (a*n + b), the value of TABLE(1 + 1, 2, 3) is 8.
  • #define TABLE(a, b, n) ((a)*(n) + (b), the value of TABLE(1 + 1, 2, 3) is 8.
  1. Suppose we have allocated some memory as follows.
int *mem = malloc(sizeof(int) * 10);

Which of the following code snippets correctly deallocate(s) the memory?

{{ multiselect(9) }}

  • for (int i = 0; i < 10; ++i)
      free(mem[i]);
    
  • for (int i = 0; i < 10; ++i)
      free(mem + i);
    
  • free(mem);
    
  • free(mem + 5);
    free(mem);
    
  • free(mem, mem + 10);
    
  • free(mem, mem + 10 * sizeof(int));
    
  1. The standard library function malloc is declared as follows.
void *malloc(size_t size);

Which of the following statements is/are true?

{{ multiselect(10) }}

  • The asterisk (*) here is the dereference operator.
  • The return type of malloc is void *.
  • The return type of malloc is determined at runtime. For example, the return type of malloc(sizeof(int) * n) is int *.
  • If size > 0, it will try to allocate at least size bytes of memory.
  • If size is too large, malloc will fail and the program will crash.

CS100 Spring2025 Homework 3

未认领
状态
已结束
题目
4
开始时间
2025-3-20 14:00
截止时间
2025-3-28 23:59
可延期
0 小时