Introduction to Programming with C++, Third Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

Chapter 6 Functions


Sections 6.2 Defining a Function
6.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

6.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

6.3  The header of the main function in C++ is __________.
A. Main(String[] args)
B. Main(String args[])
C. void main(String[] args)
D. main(String[] args)
E. int main()

Sections 6.3-6.4
6.4  Arguments to functions always appear within __________.
A. brackets
B. parentheses
C. curly braces
D. quotation marks

6.5  Does the return statement in the following function cause compile errors?

void f()
{
   int max = 0;
   if (max != 0)
     cout << max;
   else
     return;
}
A. Yes
B. No

6.6  What is the output of the following code?

int f()
{
  return 1;
}

int main()
{
  cout << f() << endl;
  return 0;
}
A. 0
B. 1
C. nothing
D. 1 0
E. 0 1

6.7  What is the output of the following code?

void f()
{
  cout << 1 << endl;
}

int main()
{
  f();
  return 0;
}
A. 0
B. 1
C. nothing
D. 1 0
E. 0 1

6.8  Does the function call in the following function cause compile errors?

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

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

   return 0;
 }
A. Yes
B. No

6.9  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

6.10  Which of the following functions should be defined as void?
A. Return a sales commission, given the sales amount and the commission rate.
B. Print the calendar for a month, given the month and year.
C. Return a square root for a number.
D. Return a bool value indicating whether a number is even.
E. Print a character a specified number of times.

6.11  Which of the following should be declared as a void function?
A. Write a function that prints integers from 1 to 100.
B. Write a function that returns a random integer from 1 to 100.
C. Write a function that checks whether an integer is from 1 to 100.
D. Write a function that converts an uppercase letter to lowercase.

6.12  You should fill in the blank in the following code with ______________.

 #include <iostream>
 using namespace std;

 // Print grade for the score
 _______ printGrade(double score)
 {
   if (score >= 90.0)
     cout << 'A';
   else if (score >= 80.0)
     cout << 'B';
   else if (score >= 70.0)
     cout << 'C';
   else if (score >= 60.0)
     cout << 'D';
   else
     cout << 'F';
 }

 int main()
 {
   cout << "Enter a score: ";
   double score;
   cin >> score;

   cout << "The grade is ";
   printGrade(score);

   return 0;
 }
A. int
B. double
C. bool
D. char
E. void

6.13  You should fill in the blank in the following code with ______________.

 #include <iostream>
 using namespace std;

 ________ getGrade(double score)
 {
   if (score >= 90.0)
     return 'A';
   else if (score >= 80.0)
     return 'B';
   else if (score >= 70.0)
     return 'C';
   else if (score >= 60.0)
     return 'D';
   else
     return 'F';
 }

 int main()
 {
   cout << "Enter a score: ";
   double score;
   cin >> score;

   cout << "The grade is ";
   cout << getGrade(score) << endl;

   return 0;
 }
A. int
B. double
C. bool
D. char
E. void

6.14  Consider the following incomplete code:

 #include <iostream>
 using namespace std;

 int f(int number)
 {
   return number;
 }

 int main()
 {
   cout << f(5) << endl;
   return 0;
 }

The missing function body should be ________.
A. return "number";
B. cout << number << endl;
C. cout << "number" << endl;
D. return number;

Section 6.5 Passing Arguments by Values
6.15  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

6.16  The following code displays ______________.

 #include <iostream>
 using namespace std;

 void maxValue(int value1, int value2, int max)
 {
   if (value1 > value2)
     max = value1;
   else
     max = value2;
 }

 int main()
 {
   int max = 0;
   maxValue(1, 2, max);
   cout << "max is " << max << endl;

   return 0;
 }
A. max is 0
B. max is 1
C. max is 2
D. max is undefined

Section 6.7 Overloading Functions
6.17  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);
    return 0;
  }
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.

6.18  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);
    return 0;
  }
A. The program has a compile error because the two functions m have the same signature.
B. The program has a compile 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 6.8 Function Prototypes
6.19  Which of the following are correct funciton prototypes for a function that returns the max value between two int values?
A. int max(int num1, int num2);
B. int max(int num1, int num2)
C. int max(int, int);
D. int max(num1, num2);
E. max(int num1, int num2);

Section 6.9 Reuse of Functions by Different Programs
6.20  Suppose you define a function in a header file named f.h. To use it in your program, use _________.
A. #include f.h;
B. #include "f.h";
C. #include "f.h"
D. #include "f.h"
E. include "f.h"

Section 6.10 Separating Function Headers from Implementation
6.21  Suppose you define a function in a header file named f.h. To use it in your program, use _________.
A. #include f.h;
B. #include "f.h";
C. #include "f.h"
D. #include "f.h"
E. include "f.h"

Section 6.11 Default Arguments
6.22  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 6.12 Inline Functions
6.23  Analyze the following statements.
A. Inline functions and regular functions can perform the same function. The difference is in performance and memory use.
B. Inline functions use more memory than regular functions.
C. Inline functions execute faster than regular functions.
D. Inline functions are appropriate for very short functions.

6.24  What is the output of the following code?

inline void print(int i)
{
  cout << i << endl;
}

int main() 
{
  print(1);
  return 0;
}
A. 0
B. 1
C. 2
D. nothing

Section 6.13 Local, Global, and Static Local Variables
6.25  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

6.26  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.

6.27  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;

   return 0;
 }
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

6.28  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;

   return 0;
 }
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

6.29  The following program invokes p() three times. What is the output 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();

   return 0;
 }
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 6.14 Passing Arguments by References
6.30  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

6.31  What will be displayed by 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;

   return 0;
 }
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

6.32  Suppose

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

What will be displayed by the call nPrint('a'4)?

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

6.33  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

6.34  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 6.15 Constant Reference Parameters
6.35  Analyze the following code.

// Return the max between two numbers
int max(const int &num1, int num2) 
{
  if (num1 > num2)
    return num1;
  else
    return num2;
}
A. numb1 is a constant parameter and cannot be changed
B. numb2 is a pass-by-value parameter and cannot be changed
C. numb1 is a constant parameter and can be changed in the function
D. numb2 is a pass-by-value parameter and can be changed in the function

Section 6.17 Function Abstraction and Stepwise Refinement
6.36  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 __________.
A. information hiding
B. encapsulation
C. function hiding
D. simplifying function

6.37  __________ 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