Introduction to C++ Programming, Y. Daniel Liang

Chapter 5 Functions

Sections 5.2 Creating a Function
1  Suppose your function does not return any value, which of the following keywords can be used as a return type?

A. void
B. int
C. double
D. float
E. unsigned short

2  The signature of a function consists of ____________.

A. function name
B. function name and parameter list
C. return type, function name, and parameter list
D. parameter list

3  The signature of the main function is __________.

A. Main(String[] args)
B. Main(String args[])
C. void main(String[] args)
D. main(String[] args)
E. int main()

Sections 5.3 Calling a function
4  Arguments to functions always appear within __________.

A. brackets
B. parentheses
C. curly braces
D. quotation marks

5  Does the return statement in the following function cause syntax errors?

void f()
{
    int max = 0;
    if (max != 0)
      cout << max;
    else
      return;
}

A. Yes
B. No

6  Does the function call in the following function cause syntax errors?

  #include <iostream>
  #include <math>
  using namespace std;

  int main()
  {
    pow(2.0, 4);
  }

A. Yes
B. No

7  Each time a function is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion.

A. a heap
B. storage area
C. a stack
D. an array

Section 5.4 Passing Parameters by Values
8  When you invoke a function with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.

A. function invocation
B. call by value
C. call by reference
D. call by name

9  If a parameter is a reference variable, this parameter becomes an alias for the original variable. This is referred to as _________.

A. function invocation
B. pass by value
C. pass by reference
D. pass by name

Section 5.5 Passing Parameters by References
10  What is the printout of the following code?

  #include <iostream>
  using namespace std;

  void f(int &p1, int p2)
  {
    p1++;
    p2++;
  }

  int main()
  {
    int x1 = 1;
    int x2 = 1;
    f(x1, x2);
    cout << "x1 is " << x1 << " x2 is " << x2;
  }


A. x1 is 1 x2 is 1
B. x1 is 2 x2 is 2
C. x1 is 1 x2 is 2
D. x1 is 2 x2 is 1

11  Suppose

void nPrint(char ch, int n)
{
   while (n > 0)
   {
     cout << ch;
     n--;
   }
}

What is the printout of the call nPrint('a', 4)?

A. aaaaa
B. aaaa
C. aaa
D. invalid call

12  Suppose

void nPrint(char ch, int n)
{
   while (n > 0)
   {
     cout << ch;
     n--;
   }
}

What is k after invoking nPrint('a', k)?

int k = 2;
nPrint('a', k);

A. 0
B. 1
C. 2
D. 3

13  What is the output of the following code?

  #include <iostream>
  using namespace std;

  void f(double &p)
  {
    p += 2;
  }

  int main()
  {
    double x = 1;
    double y = 1;
 
    f(x);
    f(y);

    cout << "x is " << x;
    cout << " y is " << y << endl;

    return 0;
  }

A. x is 1 y is 1
B. x is 2 y is 1
C. x is 1 y is 2
D. x is 2 y is 2
E. x is 3 y is 3

Section 5.7 Overloading Functions
14  Analyze the following code:

   #include <iostream>
   using namespace std;
  
   int xfunction(int n, long t)
   {
     cout << "int";
     return n;
   }
  
   long xfunction(long n)
   {
     cout << "long";
     return n;
   }
  
   int main()
   {
     cout << xfunction(5);
   }


A. The program displays int followed by 5.
B. The program displays long followed by 5.
C. The program runs fine but displays nothing.
D. The program does not compile because the compiler cannot distinguish which xfunction to invoke.

15  Analyze the following code.

   #include <iostream>
   using namespace std;

   int m(int num)
   {
     return num;
   }

   void m(int num)
   {
     cout << num;
   }

   int main()
   {
     cout << m(2);
   }

A. The program has a syntax error because the two functions m have the same signature.
B. The program has a syntax error because the second m function is defined, but not invoked in the main function.
C. The program runs and prints 2 once.
D. The program runs and prints 2 twice.

Section 5.9 Default Arguments
16  Which of the following function declarations are illegal?

A. void t1(int x, int y = 0, int z);
B. void t2(int x = 0, int y = 0, int z);
C. void t3(int x, int y = 0, int z = 0);
D. void t4(int x = 0, int y = 0, int z = 0);

Section 5.12 The Scope of Variables
17  A variable defined inside a function is referred to as __________.

A. a global variable
B. a function variable
C. a block variable
D. a local variable

18  What is k after the following block executes?
{
   int k = 2;
}

A. 0
B. 1
C. 2
D. k is not defined outside the block.

19  What will be the output of the following code?
  #include <iostream>
  using namespace std;
 
  int j = 1;

  int main()
  {
    int i = 2;
    cout << "i is " << i << " j is " << j << endl;
  }

A. i is 2 j is 1
B. i is 1 j is 1
C. i is 2 j is 2
D. i is 1 j is 2

20  What will be the output of the following code?
  #include <iostream>
  using namespace std;
 
  int j = 1;

  int main()
  {
    int i = 2;
    int j = 2;
    cout << "i is " << i << " j is " << j << endl;
  }

A. i is 2 j is 1
B. i is 1 j is 1
C. i is 1 j is 2
D. i is 2 j is 2

21  The following program invokes p() three times. What is the printout from the last call of p()?
  #include <iostream>
  using namespace std;

  int j = 40;

  void p()
  {
    int i = 5;
    static int j = 5;
    i++;
    j++;

    cout << "i is " << i << " j is " << j << endl;
  }

  int main()
  {
    p();
    p();
    p();
  }

A. i is 6 j is 6 b. i is 6 j is 7
C. i is 6 j is 8
D. i is 6 j is 9

Section 5.14 The Math Functions
22  The client can use a function without knowing how it is implemented. The details of the implementation are encapsulated in the function and hidden from the client who invokes the function. This is known as __________.

18. Which of the following is a possible output from invoking rand()?

22. What is ceil(3.6)?

A. 2
B. 3
C. 4
D. 5

23  What is Math.floor(3.6)?

A. 2
B. 3
C. 4
D. 5.0

24  Which of the following are not the Math functions in C++?

A. random
B. round
C. rint
D. pow

Section 5.15 function Abstraction and Stepwise Refinement
25  __________ is to implement one function in the structure chart at a time from the top to the bottom.

A. Bottom-up approach
B. Top-down approach
C. Bottom-up and top-down approach
D. Stepwise refinement

26  __________ is a simple but incomplete version of a function.

A. A stub
B. A main function
C. A non-main function
D. A function developed using top-down approach