C++ Special Exercises: Formal Parameters, Actual Parameters, and Scope

C++ Level 4 Questions Organized by Knowledge Points

CCF-GESP C++ Assessment Standards

Hong Yang, WeChat Official Account: Hong Yang’s Programming ClassCCF-GESP C++ Assessment StandardsFormal Parameters, Actual Parameters, and Scope

Question 1

  • Question: After running the following program, the value of variable a is ( ).

cppRun

<span><span><span>int</span></span><span> a </span><span><span>=</span></span><span><span>42</span></span><span><span>;</span></span></span>

<span><span><span>int</span></span><span><span>*</span></span><span> p </span><span><span>=</span></span><span><span>&</span></span><span>a</span><span><span>;</span></span></span>

<span><span><span>*</span></span><span>p </span><span><span>=</span></span><span><span>*</span></span><span>p </span><span><span>+</span></span><span><span>1</span></span><span><span>;</span></span></span>

  • Options:

    A. 42

    B. 43

    C. Compilation Error

    D. Uncertain

  • Answer:B

  • Explanation: Pointer p points to variable a, p accesses the value of variable a. Executing *p = *p + 1 means a = a + 1, 42 + 1 = 43, thus the value of a is 43, and the answer is B.

Question 2

  • Question: The following code will output ( ).

cppRun

int x =5;
void foo(){
    int x =10;
    cout &lt;&lt; x &lt;&lt;" ";
}
void bar(){
    cout &lt;&lt; x &lt;&lt;" ";
}
int main(){
    foo();
    bar();
}
  • Options:

    A. 5 5

    B. 10 10

    C. 5 10

    D. 10 5

  • Answer:D

  • Explanation: In the foo function, a local variable x is defined with a value of 10, and the function outputs the local variable x, which is 10; in the bar function, no local variable x is defined, so it accesses the global variable x, which is 5. Therefore, the output is 10 5, and the answer is D.

Question 3

  • Question: The result of running the following program is ( ).

cppRun

void increaseA(int x){
    x++;
}
void increaseB(int* p){
    (*p)++;
}
int main(){
    int a =5;
    increaseA(a);
    cout &lt;&lt; a &lt;&lt;" ";
    increaseB(&amp;a);
    cout &lt;&lt; a;
}
  • Options:

    A. 6 7

    B. 6 6

    C. 5 6

    D. 5 5

  • Answer:C

  • Explanation: The increaseA function uses value passing, where the parameter x is a copy of the argument a, and x++ only modifies the copy, not affecting the original variable a, so after the call, a remains 5; the increaseB function uses pointer passing, where *p accesses the value of a, and (*p)++ increments the value of a, making a equal to 6. The output is 5 6, and the answer is C.

Question 4

  • Question: Running the following code will output ( ).

cppRun

int value =100;

void print1(){
    int value =50;
    cout &lt;&lt; value &lt;&lt;" ";
    cout &lt;&lt;::value &lt;&lt;" ";
}
void print2(){
    cout &lt;&lt; value &lt;&lt;" ";
}
int main(){
    print1();
    print2();
}
  • Options:

    A. 100 100 100

    B. 50 50 50

    C. 50 100 100

    D. 50 50 100

  • Answer:C

  • Explanation: In the print1 function, the local variable value is 50, outputting the local variable value 50; ::value accesses the global variable value, which is 100, outputting 100; in the print2 function, no local variable is defined, accessing the global variable value, outputting 100. Therefore, the output is 50 100 100, and the answer is C.

Question 5

  • Question: Executing the following code will output ( ).

cppRun

int x =10;

void func(){
    int x =20;
    std::cout &lt;&lt; x;
}
int main(){
    func();
    std::cout &lt;&lt; x;
    return0;
}
  • Options:

    A. 2020

    B. 2010

    C. 1020

    D. Compilation Error

  • Answer:B

  • Explanation: In the func function, x is a local variable with a value of 20, outputting 20; in the main function, x is a global variable with a value of 10, outputting 10. Therefore, the final output is 2010, and the answer is B.

Question 6

  • Question: Executing the following code will output ( ).

cppRun

void swap(int a,int &amp;b){
    int temp = a;
    a = b;
    b = temp;
}
int main(){
    int x =1, y =2;
    swap(x, y);
    std::cout &lt;&lt; x &lt;&lt; y;
    return0;
}
  • Options:

    A. 12

    B. 21

    C. 22

    D. 11

  • Answer:D

  • Explanation: In the swap function, a is passed by value, receiving a copy of x, and modifying a does not affect x; b is passed by reference, pointing to y, modifying b also modifies y. After the swap function executes, a becomes 2, b becomes 1, but x remains 1, and y becomes 1. The output of x and y is 11, and the answer is D.

Question 7

  • Question: After running the following code snippet, the results of x and *p are ( ).

cppRun

<span><span><span>int</span></span><span> x </span><span><span>=</span></span><span><span>20</span></span><span><span>;</span></span></span>

<span><span><span>int</span></span><span><span>*</span></span><span> p </span><span><span>=</span></span><span><span>&</span></span><span>x</span><span><span>;</span></span></span>

<span><span><span>*</span></span><span>p </span><span><span>=</span></span><span><span>*</span></span><span>p </span><span><span>+</span></span><span><span>2</span></span><span><span>;</span></span></span>

  • Options:

    A. 20 20

    B. 20 22

    C. 22 20

    D. 22 22

  • Answer:D

  • Explanation: Pointer p points to x, and *p = *p + 2 means x = 20 + 2 = 22. The value of x is 22, and *p accesses the value of x, which is also 22, so the answer is D.

Leave a Comment