C++ Level 4 Questions Organized by Knowledge Points
C++ Pointer Types: Concepts and Basic Applications
Question 1
-
Question: Which of the following descriptions about arrays is ( ) incorrect.
-
Options:
A. The array name is a pointer constant
B. Random access to array elements is convenient and fast
C. Arrays can be incremented like pointers
D. sizeof(arr) returns the total number of bytes occupied by the entire array arr
-
Answer:C
-
Explanation:The array name is a pointer constant that points to the address of the first element of the array and cannot be modified, so option A is correct. Arrays can be accessed directly by index, making random access efficient, so option B is correct. The array name is a constant and cannot be incremented or decremented, while pointer variables can, so option C is incorrect. sizeof(arr) calculates the total number of bytes occupied by the array, which equals the number of elements multiplied by the size of a single element, so option D is correct.
Question 2
-
Question: The following code can correctly initialize a pointer. (True/False)
cppRun
int a = 5;
int *p = a;
-
Answer:False
-
Explanation:The pointer variable should be initialized to point to the address of the variable, the correct syntax is int *p = &a;. Directly assigning the value of variable a to pointer p will treat 5 as an address value, leading to the pointer pointing to invalid memory, resulting in an initialization error.
Question 3
-
Question: After executing the following code, the value of variable a is ( ).
cppRun
int a = 10;
int *p = &a;
*p = 20;
-
Options:
A. 10
B. 20
C. Random value
D. Compilation error
-
Answer:B
-
Explanation:The pointer p points to the address of a, and executing p = 20 modifies the value of a to 20, so the value of a is 20, and the answer is B.
Question 4
-
Question: Running the following code, the output on the screen is ( ).
cppRun
double *p_arr = new double[3];
p_arr[0] = 0.2;
p_arr[1] = 0.5;
p_arr[2] = 0.8;
p_arr += 1;
cout << p_arr[0] << endl;
p_arr -= 1;
delete p_arr;
-
Options:
A. 0.2
B. 0.5
C. 1.2
D. 1.5
-
Answer:B
-
Explanation:p_arr initially points to the first element of the array (index 0), after p_arr += 1, the pointer points to the element at index 1 of the array, so p_arr[0] refers to the original p_arr[1], which has a value of 0.5, thus the output is 0.5, and the answer is B.
Question 5
-
Question: Assuming an integer is 32 bits, for a 2-row 3-column two-dimensional integer array array, if the address of the first element in memory is 0x7ffee4065820, then the address of &array[1][1] is ( ).
cppRun
int array[2][3] = {{0, 1, 2}, {3, 4, 5}};
-
Options:
A. 0x7ffee4065824
B. 0x7ffee4065828
C. 0x7ffee406582c
D. 0x7ffee4065830
-
Answer:C
-
Explanation:Each int is 4 bytes, array[1][1] is in the second row (index 1) and second column (index 1), with 3 elements from the previous row and 1 element from the current row, totaling 4 elements, 4*4=16 bytes, so 0x7ffee4065820 + 16 = 0x7ffee4065830, thus the answer is D.