C++ Level 4 Questions Organized by Knowledge Points
C++ Basic Applications of Two-Dimensional and Multidimensional Arrays
Question 1
-
Question: Given the array defined as arr, what is the value of *(*(arr + 1) + 2)?
cppRun
int arr[2][3]={{1,2,3},{4,5,6}};
-
Options:
A. 2
B. 5
C. 4
D. 6
-
Answer:D
-
Explanation: arr is a two-dimensional array, arr + 1 points to the address of the first element of the second row (index 1), * (arr + 1) gets the name of the second row, which is the address of the first element of the second row; (arr + 1) + 2 points to the element at index 2 of the second row, *(* (arr + 1) + 2) is the value of the element at index 2 of the second row, which is 6, so the answer is D.
Question 2
-
Question: Which of the following correctly defines a two-dimensional array?
-
Options:
A. int arr [3,4];
B. int arr [3][4];
C. int arr (3,4);
D. int a [3-4];
-
Answer:B
-
Explanation: The syntax for defining a two-dimensional array is “data type array name [size of first dimension][size of second dimension]”, option B is correct. Option A uses a comma to separate dimensions, which is a syntax error; option C uses parentheses, which is a syntax error; option D has a dimension of 3-4=-1, which cannot be negative, hence a syntax error.
Question 3
-
Question: Assuming int arr [2][3] = {{1,2,3},{4,5,6}};, what is the value of arr [1][2]?
-
Options:
A. 2
B. 3
C. 5
D. 6
-
Answer:D
-
Explanation: The index of a two-dimensional array starts from 0, arr [1][2] refers to the element in the second row and third column, which corresponds to the value 6, so the answer is D.
Question 4
-
Question: Which of the following correctly defines a two-dimensional array?
-
Options:
A. int a [3][];
B. int a [][];
C. int a [][4];
D. int a [][2] = {{1,2},{1,2},{3,4}};
-
Answer:D
-
Explanation: When defining a two-dimensional array, the size of the second dimension cannot be omitted, options A and B are incorrect; option C only declares without initialization and does not specify the size of the first dimension, making it impossible to determine the array size, which is incorrect; option D omits the size of the first dimension, which can be inferred from the initialization list to be 3, and the size of the second dimension is 2, which is syntactically correct, so the answer is D.
Question 5
-
Question: The following code passes the two-dimensional array arr to function f, where elements are accessed using arr [i][j], and the function parameter declaration int arr [][] 4 is incorrect. (True/False)
cppRun
void f(int arr[][4],int rows){
// Access arr[i][j]
}
int main(){
int arr[3][4]={/* Initialization */};
f(arr,3);
}
-
Answer:False
-
Explanation: When a two-dimensional array is passed as a function parameter, the size of the first dimension can be omitted, but the size of the second dimension must be specified, so the function parameter declaration int arr [][4] is correct, thus the statement in the question is false.
Concepts of Sorting and Stability in C++
Question 1
- Question: Which of the following statements about the stability of sorting algorithms is incorrect?
- Options:
- A. A stable sorting algorithm does not change the relative position of equal elements
- B. Bubble sort is a stable sorting algorithm
- C. Selection sort is a stable sorting algorithm
- D. Insertion sort is a stable sorting algorithm
- Answer:C
- Explanation: The definition of a stable sorting algorithm is that the relative position of equal elements remains unchanged after sorting, option A is correct. Bubble sort and insertion sort do not swap equal elements during sorting, making them stable sorts, options B and D are correct. Selection sort, however, may change the relative position of equal elements when it finds the minimum element and swaps it with the current position, for example, in the array [5, 3, 5, 2], the first selection of 2 swaps with the first 5, resulting in [2, 3, 5, 5], changing the relative position of 5. Therefore, selection sort is unstable, making option C incorrect.
Question 2
- Question: Which of the following statements about sorting stability is correct?
- Options:
- A. Stability refers to the constant time complexity of the algorithm
- B. Stable sorting guarantees that the relative order of equal elements remains unchanged
- C. Selection sort is a stable sort
- D. Insertion sort is not a stable sort
- Answer:B
- Explanation: Option A is incorrect as stability is unrelated to time complexity; it is another characteristic of sorting algorithms. Option B is correct as the definition of stable sorting is to guarantee that the relative order of equal elements remains unchanged after sorting. Option C is incorrect as selection sort is unstable. Option D is incorrect as insertion sort is stable.
Question 3
- Question: Both bubble sort and insertion sort are stable sorting algorithms. (True/False)
- Answer:True
- Explanation: In bubble sort, elements are only swapped when one is greater than the next, and equal elements are not swapped, maintaining their relative position. In insertion sort, when inserting an element into the correct position, equal elements are not moved forward, thus maintaining their relative position. Therefore, both are stable sorts, making the statement true.
Question 4
- Question: Selection sort is a stable sorting algorithm. (True/False)
- Answer:False
-
Explanation: During the sorting process, selection sort may swap later elements with earlier ones, changing the relative position of equal elements. For example, in the array [5, 4, 5, 3], the first selection of 3 swaps with 5, resulting in [3, 4, 5, 5], changing the relative position of the number 5. Therefore, selection sort is unstable, making the statement false.