1、After executing the followingC++ code, the value of c is ( ).
int a = 10, b = 3;double c = a / b;
A. 3.33333
B. 3.333
C. 3.0
D. 3.3
【Analysis】
Answer:C
Key Point:Integer Division and Type Conversion
Analysis:
a / b is integer division, resulting in3 (the decimal part is truncated)
Integer3 is then assigned to the double variable c, converting it to 3.0
Trap:It is easy to mistakenly believe that floating-point division will occur automatically
Correct way:double c = (double)a / b; or double c = a * 1.0 / b;
2、Among the followingC++ expressions, the one that evaluates to true is ( ).
A. (5 <= 5) && (7 < 5)
B. !(10 > 5)
C. (10 != 10) || (5 >= 3)
D. (5 == 3) && (4 > 2)
【Analysis】
Answer:C
Key Point:Logical Operators and Boolean Operations
Analysis:
A: (5<=5)→true, (7<5)→false, true&&false→false
B: !(10>5)→!true→false
C: (10!=10)→false, (5>=3)→true, false||true→true
D: (5==3)→false, (4>2)→true, false&&true→false
Preparation Suggestion:Master the short-circuit characteristics of logical operators.
3、The following statements aboutC++ arrays are incorrect ( ).
A. Array indices typically start from 0 .
B. int arr[5]; declares an array containing5 integers.
C. The size of an array must be determined at compile time and cannot use variables to define size.
D. It is possible to access the last element of the int arr[5]; array using arr[5] .
【Analysis】
Answer:D
Key Point:Array Indexing and Boundaries
Analysis:
An array int arr[5] has valid indices from0 to 4
arr[5] is an out-of-bounds access, which is undefined behavior
C option is not entirely correct in C++ (C++ supports dynamic arrays), but compared to D option, D is clearly incorrect
Note:Out-of-bounds access is a common programming error.
4、After executing the followingC++ code, the value of the variable sum is ( ).
int sum = 0;for (int i = 1; i <= 5; i += 2){ sum += i; int sum=0;}
A. 6
B. 9
C. 15
D. Infinite Loop
【Analysis】
Answer:B
Key Point:Loops and Variable Scope
Analysis:
The loop variable i takes values:1, 3, 5
sum accumulates:0+1=1 → 1+3=4 → 4+5=9
The inner int sum=0 is a new local variable, which does not affect the outer sum
Trap:Be aware of nested variable scopes.
5、To correctly define a function that returns the larger of two integers max , it should use ( ).
A. void max(int a, int b) { return a > b ? a : b; }
B. int max(int a, int b) { if (a > b) return a; else return b; }
C. int max(a, b) { if (a > b) return a; else return b; }
D. void max(a, b) { cout << (a > b ? a : b); }
【Analysis】
Answer:B
Key Point:Function Definition and Return Values
Analysis:
A:Return type isvoid but uses return to return a value, incorrect
B:Correct, returnsint type, with a clear return value
C:Parameter type declaration is missing, incorrect
D:Return type isvoid and parameters have no type, incorrect
Preparation Suggestion:Function definitions should be complete and standardized.
6、After executing the followingC++ code, the content of the array arr is ( ).
int arr[4] = {1, 2, 3};arr[3] = arr[0] + arr[2];
A. {1, 2, 3, 3}
B. {1, 2, 3, 4}
C. {1, 2, 3, 5}
D. {1, 2, 3, 6}
【Analysis】
Answer:B
Key Point:Array Initialization and Assignment
Analysis:
When partially initialized, remaining elements are automatically initialized to0
arr[0]=1, arr[2]=3, 1+3=4
The final array:{1,2,3,4}
Note:Rules for partial initialization of arrays.
7、The following statements aboutC++ functions are correct ( ).
A. Functions must have parameters.
B. Functions can only return one value through return statement. However, multiple values can be returned through many indirect ways.
C. The main function can be called by other functions.
D. Function definitions can be directly nested, meaning one function can truly define another function inside it.
【Analysis】
Answer:B
Key Point:Function Characteristics
Analysis:
A:Functions can have no parameters, incorrect
B:Correct, multiple values can be returned through reference parameters, pointers, structures, etc.
C:main function is the program entry point and should not be called by other functions, incorrect
D:C++ does not support nested function definitions, incorrect
Knowledge Expansion:Common methods for returning multiple values.
8、The followingC++ code count++ executes ( ).
int i = 10;int count=0;while (i > 0){ i -= 3; continue; count++;}
A. 2
B. 3
C. 4
D. 0
【Analysis】
Answer:D
Key Point:Effect of the continue Statement
Analysis:
continue statement skips the remaining code of the current loop
count++ is after continue, so it will never execute
The loop executes normally, but count remains 0
Trap:Code after continue will not execute.
9、The output of the followingC++ code segment is ( ).
for (int i = 0; i < 4; i++){ for (int j = 0; j <= i; j++) { cout << j; } cout << "#";}
A. 0#01#012#0123#
B. 1#12#123#1234#
C. 0#1#2#3#
D. 0#01#012#01243#
【Analysis】
Answer:A
Key Point:Nested Loops and Output Format
Analysis:
i=0: j=0 → Output“0#”
i=1: j=0,1 → Output“01#”
i=2: j=0,1,2 → Output“012#”
i=3: j=0,1,2,3 → Output“0123#”
Note:The inner loop outputs the value of j.
10、The following statements aboutC++ variable scope are incorrect ( ).
A. Variables declared in for loop statements have a scope limited to that loop body.
B. Variables declared inside a function (local variables) are only valid within that function.
C. Variables declared outside all functions are valid throughout the entire program.
D. Local variables in different functions can have the same name, representing different memory units.
【Analysis】
Answer:C
Key Point:Variable Scope
Analysis:
C:Incorrect,Global variable scope starts from the declaration to the end of the file, not the entire program
Other options correctly describe their respective scope rules
Knowledge Expansion:The extern keyword is used to extend the scope of global variables.
11、The following code’s statement is correct ( ).
int reversed = 0;while (x != 0){ int digit = x % 10; x /= 10; reversed = reversed * 10 + digit;}
A. Can reverse any integer of any digit
B. The maximum positive integer that can be reversed is2147483647
C. The maximum positive integer that can be reversed is2147483648
D. The maximum positive integer that can be reversed is1463847412
【Analysis】
Answer:D
Key Point:Integer Reversal and Overflow Issues
Analysis:
The code can reverse integers, but there is a risk of overflow
2147483647 when reversed becomes7463847412, exceedingint range(2147483647)
1463847412 when reversed becomes2147483641, withinint range
The maximum safely reversible number is1463847412
Trap:Integer overflow is a common boundary condition issue.
12、The following C++ code attempts to find the maximum value in an array, the underlined part should be filled in ( ).
int findMax(int arr[], int size){ int maxVal = ________; //underlined part for (int i = 1; i < size; i++) { if (arr[i] > maxVal) { maxVal = arr[i]; } } return maxVal;}
A. 0
B. arr[-1]
C. arr[0]
D. size
【Analysis】
Answer:C
Key Point:Algorithm Initialization
Analysis:
When finding the maximum value, the initial value should be set to an element in the array
arr[0] is the most suitable initial value
Setting it to0 may be incorrect (if the array is all negative)
arr[-1] is illegal access
Best Practice:Initialize with the first element of the array.
13、The following statements aboutC++ functions are correct ( ).
A. Function parameter passing has only one way: value passing.
B. Function parameters still occupy memory space after the function call ends
C. A function without a return value must be declared asvoid type and cannot contain return statement
D. C++11 and later standards require functions to explicitly declare return types, not allowing default return int
【Analysis】
Answer:D
Key Point:Function Parameters and Memory Management
Analysis:
A:Incorrect,C++ supports three parameter passing methods: ①Value Passing: passing a copy of the parameter; ②Reference Passing: passing a reference to the parameter; ③Pointer Passing: passing the address of the parameter.
B:Incorrect, the lifetime of parameters is limited to the function execution period, and the memory for parameters is released after the function call ends
C:Incorrect,void functions can contain return; (no return value)
D:Correct,C++11 and later standards require all functions to explicitly declare return types; omitting the return type will lead to a compilation error; this is to improve code clarity and type safety
Note:Understand historical compatibility.
14、The following C++ code contains several errors ( ).
int main(){ const int SIZE = 5; int arr[SIZE]; for (int i = 0; i <= SIZE; i++) { arr[i] = i * 2; } cout << arr[SIZE] << endl; return 0;}
A. 0 at
B. 1 at
C. 2 at
D. 3 at
【Analysis】
Answer:C
Key Point:Array Boundary Checking
Analysis:
Two out-of-bounds accesses: loop conditioni<=SIZE and arr[SIZE]
Valid indices are0 to 4, but index 5 is accessed
Common error:Confusing array size with maximum index.
15、The following statements aboutC++ string class and character arrays( char[] ) are incorrect ( ).
A. string objects can be assigned using = , while character arrays need to use strcpy .
B. string object’s length can be obtained usinglength() member function, while character arrays need to use strlen() function.
C. string objects are dynamically allocated in memory, thus can automatically handle changes in string length.
D. string objects and character arrays can both use == operator to directly compare whether two strings are equal.
【Analysis】
Answer:D
Key Point:Comparison of string class and character arrays
Analysis:
D:Incorrect,string class overloads the == operator, but character arrays use == to compare addresses
Character array comparison should usestrcmp()
Other options correctly describe the differences between the two
Preparation Suggestion:Master the convenience of the string class
Overall Preparation Suggestions
1. Key Content to Master
1、Type Conversion:Integer Division, Implicit Type Conversion
2、Array Operations:Index Boundaries, Initialization Rules
3、Function Design:Parameter Passing, Return Values, Scope
4、Control Flow:Loops, Conditions, Jump Statements
5、String Handling:string class and character array differences
2. Summary of Common Mistakes
1、Truncation characteristics of integer division
2、Array indices start from0, maximum index issize-1
3、continue skips the remaining code of the current loop
4、Character arrays cannot be directly compared using== to compare contents
3. Learning Methods
1、Write more code to verify understanding
2、Pay attention to boundary conditions and special cases
3、Understand the essence of concepts rather than rote memorization
4、Master debugging skills to locate issues