COSC 1315

Programming Fundamentals

 Practice Text #108

Functions

Revised: February 3, 2007
By Richard G. Baldwin

File Pfsg00108.htm
Practice Text Index


Welcome

The practice tests in this series were written specifically for the benefit of my students in COSC 1315, Fundamentals of Programming.  They consists of questions, answers, and explanations.  The questions are based on the material covered in my series of online lecture notes for the course.  Each practice test is keyed to a specific lecture.  This practice test is keyed to lecture #108 titled Functions.

Questions



108-1.  Which of the following are common names for programming structures that resemble functions?

Answer and Explanation

108-2.  True or False.  The use of a function is often a better alternative than the use of in-line code.

Answer and Explanation

108-3.  True or False.  A function is a separate program module.

Answer and Explanation

108-4.  True or False.  C++ programming environments, such as Dev C++, usually provides a standard or prewritten function that can be used to calculate the square root of a number.

Answer and Explanation

108-5.  True or False.  If the programming environment that you are using doesn't provide a function to do what you need to do, you are simply out of luck.  In that case, what you need to do simply can't be done unless you change to another programming environment that does provide the function that you need.

Answer and Explanation

108-6.  True or False.  It is normally not desirable to write functions that take parameters.

Answer and Explanation

108-7.  True or False.  Parameters provide the mechanism by which one function can pass information to another function.

Answer and Explanation

108-8.  True or False.  Fact:  in some programming languages, parameters can be passed either by value or by reference.  However, in C++, parameters can only be passed by value.

Answer and Explanation

108-9.  True or False.  The process of causing a function to be executed is commonly referred to as calling or invoking the function.

Answer and Explanation

108-10.  True or False.  Fact:  when your program calls the square root function, it will need to tell the function the value of the number for which the square root is needed.

In general, however, most functions do not require that you provide information when you invoke them.  Computer technology has advanced to the point that most functions can figure out everything that they need to know, almost as if they have psychic powers.

Answer and Explanation

108-11.  True or False.  The process of  providing information to a function when you call the function is commonly referred to as passing parameters to the function.

Answer and Explanation

108-12.  Which is the correct answer?  A function will usually:

Answer and Explanation

108-13.  True or False.  The process of sending back an answer from a function is commonly referred to as returning a value.

Answer and Explanation

108-14.  True or False.  A function in C++ must return a value.

Answer and Explanation

108-15.  True or False.  When a C++ function returns a value, the program that called the function can either

Answer and Explanation

108-16.  True or False.  When the program in Listing 108-16 is compiled using Dev C++ and executed from the command prompt, the following screen output is produced by the program:

3.87298
4.47214
5.47723

Listing 108-16.
#include <iostream>
#include <math.h>
using namespace std;

int main(){//Global main function.
  cout << sqrt(15.0) << endl;
  cout << sqrt(20.0) << endl;
  cout << sqrt(25.0) << endl;
  cout << sqrt(30.0) << endl;
  return 0;
}//end main function

Answer and Explanation

108-17.  True or False.  The sqrt function that is called in Listing 108-17 requires two parameters.

Listing 108-17.
#include <iostream>
#include <math.h>
using namespace std;

int main(){//Global main function.
  cout << sqrt(15.0) << endl;
  cout << sqrt(20.0) << endl;
  cout << sqrt(25.0) << endl;
  cout << sqrt(30.0) << endl;
  return 0;
}//end main function

Answer and Explanation

108-18.  What output is produced by the program shown in Listing 108-18?

Listing 108-18.
#include <iostream>
using namespace std;

int main(){//Global main function.
  cout << sqrt(15.0) << endl;
  cout << sqrt(20.0) << endl;
  cout << sqrt(25.0) << endl;
  cout << sqrt(30.0) << endl;
  return 0;
}//end main function

Answer and Explanation

108-19.  True or False.  The function prototype for the function named goodbye is optional in the program shown in Listing 108-19.

Listing 108-19.
#include <iostream>
using namespace std;

void goodbye();//function prototype
void hello();//function prototype

void hello(){
  cout << "Hello World" << endl;
}//end hello
//---------------------------------------------//

int main(){//Global main function.
  hello();
  goodbye();
  return 0;
}//end main function
//---------------------------------------------//

void goodbye(){
  cout << "Goodbye cruel world" << endl;
}//end goodbye

Answer and Explanation

108-20.  True or False.  The function prototype for the function named hello is optional in the program shown in Listing 108-20.

Listing 108-20.
#include <iostream>
using namespace std;

void goodbye();//function prototype
void hello();//function prototype

void hello(){
  cout << "Hello World" << endl;
}//end hello
//---------------------------------------------//

int main(){//Global main function.
  hello();
  goodbye();
  return 0;
}//end main function
//---------------------------------------------//

void goodbye(){
  cout << "Goodbye cruel world" << endl;
}//end goodbye

Answer and Explanation

108-21.  True or False.  The general syntax for a function consists of two main parts:

Answer and Explanation

108-22.  True or False.  As a minimum, the function signature consists of:

Answer and Explanation

108-23.  True or False.  The function signature specifies the type of return type as void for the case where the function doesn't return a value.

Answer and Explanation

108-24.  True or False.  The formal parameter list is empty if the function requires no parameters.

Answer and Explanation

108-25.  True or False.  If the function does require parameters, the formal parameter list contains one or more pairs of words separated by semicolons.

Answer and Explanation

108-26.  True or False.  Each pair of words in the formal parameter list (a parameter declaration) consists of the following:

Answer and Explanation

108-27.  True or False.  Just like Java, C++ code is insensitive to the relative locations of function definitions and the code that calls the functions.

Answer and Explanation

108-28.  True or False.  In order for C++ code to successfully call a function, the function must have either been:

Answer and Explanation

108-29.  True or False.  A function prototype is used to declare a function to make it possible for code to call the function before it is actually defined.

Answer and Explanation

108-30.  True or False.  Just like in the signature, parameter names must always be included in the function prototype.

Answer and Explanation

108-31.  True or False.  When a called function terminates, the flow of control is transferred from the calling function to the called function.

Answer and Explanation

108-32.  True or False.  When a function is called, the flow of control is transferred from the calling function to the called function.

Answer and Explanation

108-33.  True or False.  C++ allows a parameter to be passed to a function either by value or by reference.

Answer and Explanation

108-34.  True or False.  When a program passes a variable as a parameter to a function by value

The function can do just about anything that it wants to do with the incoming parameter.  This includes modifying the contents of the original variable.

Answer and Explanation

108-35.  True or False.  When a program passes a variable as a parameter to a function by reference, the variable's address in memory is passed to the function.  The function can do just about anything that it wants to do with the incoming parameter.  This includes modifying the contents of the original variable.

Answer and Explanation

108-36.  True or False.  Listing 108-36 passes two parameters of different types to a function by reference.

Listing 108-36.

#include <iostream>
using namespace std;

void aFunction(int x,double y){
  cout << "In aFunction" << endl;
  cout << "x = " << x << " y = " << y << endl;
  cout << "Change x and y" << endl;
  x = 100;
  y = 200.1;
  cout << "Still in aFunction" << endl;
  cout << "x = " << x << " y = " << y << endl;
}//end aFunction
//---------------------------------------------//

int main(){//Global main function.
  cout << "In main" << endl;
  int a = 10;
  double b = 20.1;
  cout << "a = " << a << " b = " << b << endl;
  aFunction(a,b);
  cout << "Back in main" << endl;
  cout << "a = " << a << " b = " << b << endl;
  return 0;
}//end main function

Answer and Explanation

108-37.  What output is produced by the program shown in Listing 108-37.

Listing 108-37.

#include <iostream>
using namespace std;

void aFunction(int x,double y){
  cout << "In aFunction" << endl;
  cout << "x = " << x << " y = " << y << endl;
  cout << "Change x and y" << endl;
  x = 100;
  y = 200.1;
  cout << "Still in aFunction" << endl;
  cout << "x = " << x << " y = " << y << endl;
}//end aFunction
//---------------------------------------------//

int main(){//Global main function.
  cout << "In main" << endl;
  int a = 10;
  double b = 20.1;
  cout << "a = " << a << " b = " << b << endl;
  aFunction(a,b);
  cout << "Back in main" << endl;
  cout << "a = " << a << " b = " << b << endl;
  return 0;
}//end main function

Answer and Explanation

108-38.  True or False.

Answer and Explanation

108-39.  What output is produced by the function named aFunction shown in Listing 108-39?

Listing 108-39.
#include <iostream>
using namespace std;

void aFunction(int x,double y){
  cout << "In aFunction" << endl;
  cout << "x = " << x << " y = " << y << endl;
  cout << "Change x and y" << endl;
  x = 100;
  y = 200.1;
  cout << "Still in aFunction" << endl;
  cout << "x = " << x << " y = " << y << endl;
}//end aFunction
//---------------------------------------------//

int main(){//Global main function.
  cout << "In main" << endl;
  int a = 10;
  double b = 20.1;
  cout << "a = " << a << " b = " << b << endl;
  aFunction(a,b);
  cout << "Back in main" << endl;
  cout << "a = " << a << " b = " << b << endl;
  return 0;
}//end main function

Answer and Explanation

108-40.  What output is produced by the program shown in Listing 108-40?

Listing 108-40.
#include <iostream>
using namespace std;

void aFunction(int x,double y){
  cout << "In aFunction" << endl;
  cout << "x = " << x << " y = " << y << endl;
  cout << "Change x and y" << endl;
  x = 100;
  y = 200.1;
  cout << "Still in aFunction" << endl;
  cout << "x = " << x << " y = " << y << endl;
}//end aFunction
//---------------------------------------------//

int main(){//Global main function.
  cout << "In main" << endl;
  int a = 10;
  double b = 20.1;
  cout << "a = " << a << " b = " << b << endl;
  aFunction(a,b);
  cout << "Back in main" << endl;
  cout << "a = " << a << " b = " << b << endl;
  return 0;
}//end main function

Answer and Explanation

108-41.  What output is produced by the program shown in Listing 108-41?

Listing 108-41.
#include <iostream>
using namespace std;

int main(){//Global main function.
  cout << "In main" << endl;
  int a = 10;
  double b = 20.1;
  cout << "a = " << a << " b = " << b << endl;
  aFunction(a,b);
  cout << "Back in main" << endl;
  cout << "a = " << a << " b = " << b << endl;
  return 0;
}//end main function
//---------------------------------------------//
void aFunction(int &x,double y){
  cout << "In aFunction" << endl;
  cout << "x = " << x << " y = " << y << endl;
  cout << "Change x and y" << endl;
  x = 100;
  y = 200.1;
  cout << "Still in aFunction" << endl;
  cout << "x = " << x << " y = " << y << endl;
}//end aFunction

Answer and Explanation

108-42.  What output is produced by the program shown in Listing 108-42?

Listing 108-42.
#include <iostream>
using namespace std;

void aFunction(int &x,double y){
  cout << "In aFunction" << endl;
  cout << "x = " << x << " y = " << y << endl;
  cout << "Change x and y" << endl;
  x = 100;
  y = 200.1;
  cout << "Still in aFunction" << endl;
  cout << "x = " << x << " y = " << y << endl;
}//end aFunction
//---------------------------------------------//

int main(){//Global main function.
  cout << "In main" << endl;
  int a = 10;
  double b = 20.1;
  cout << "a = " << a << " b = " << b << endl;
  aFunction(a,b);
  cout << "Back in main" << endl;
  cout << "a = " << a << " b = " << b << endl;
  return 0;
}//end main function

Answer and Explanation

108-43.  What output is produced by the program shown in Listing 108-43?

Listing 108-43.
#include <iostream>
using namespace std;

int doubleIt(int x){
  return x + x;
}//end doubleIt
//---------------------------------------------//

int main(){//Global main function.
  int a = 2;
  cout << "a = " << a << endl;
  cout << "a doubled = " << doubleIt(a) << endl;
  return 0;
}//end main function

Answer and Explanation

108-44.  What output is produced by the program shown in Listing 108-44?

Listing 108-44.
#include <iostream>
using namespace std;

void doubleIt(int x){
  return x + x;
}//end doubleIt
//---------------------------------------------//

int main(){//Global main function.
  int a = 2;
  cout << "a = " << a << endl;
  cout << "a doubled = " << doubleIt(a) << endl;
  return 0;
}//end main function

Answer and Explanation

108-45.  What output is produced by the program shown in Listing 108-45 when it is compiled using Dev C++ and executed from the command line?  (Note the missing return statement.)

Listing 108-45.
#include <iostream>
using namespace std;

int doubleIt(int x){
  x + x;
}//end doubleIt
//---------------------------------------------//

int main(){//Global main function.
  int a = 2;
  cout << "a = " << a << endl;
  cout << "a doubled = " << doubleIt(a) << endl;
  return 0;
}//end main function

Answer and Explanation



Copyright 2007, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com


Answers and Explanations

Answer 45

Back to Question 45

Explanation 45

Answer 44

A.  Compiler Error

Back to Question 44

Explanation 44

Answer 43

Back to Question 43

Explanation 43

Answer 42

Back to Question 42

Explanation 42

Answer 41

A.  Compiler Error

Back to Question 41

Explanation 41

Answer 40

D.  None of the above.

Back to Question 40

Explanation 40

Answer 39

D.  None of the above.

Back to Question 39

Explanation 39

Answer 38

True

Back to Question 38

Explanation 38

Answer 37

Back to Question 37

Explanation 37

Answer 36

False

Back to Question 36

Explanation 36

Answer 35

True

Back to Question 35

Explanation 35

Answer 34

False

Back to Question 34

Explanation 34

Answer 33

True

Back to Question 33

Explanation 33

Answer 32

True

Back to Question 32

Explanation 32

Answer 31

False

Back to Question 31

Explanation 31

The statement is the reverse of what it should be.

Answer 30

False

Back to Question 30

Explanation 30

Answer 29

True

Back to Question 29

Explanation 29

Answer 28

True

Back to Question 28

Explanation 28

Answer 27

False

Back to Question 27

Explanation 27

Should say sensitive rather than insensitive.

Answer 26

True

Back to Question 26

Explanation 26

Answer 25

False

Back to Question 25

Explanation 25

Answer 24

True

Back to Question 24

Explanation 24

Answer 23

True

Back to Question 23

Explanation 23

Answer 22

False

Back to Question 22

Explanation 22

Answer 21

True

Back to Question 21

Explanation 21

Answer 20

True

Back to Question 20

Explanation 20

Answer 19

False

Back to Question 19

Explanation 19

Answer 18

A.  Compiler Error

Back to Question 18

Explanation 18

A program must include the header file named math.h in order to be able to access the sqrt function from the library of functions.

Answer 17

False

Back to Question 17

Explanation 17

Answer 16

False

Back to Question 16

Explanation 16  The program output is:

3.87298
4.47214
5
5.47723

Answer 15

True

Back to Question 15

Explanation 15

Answer 14

False

Back to Question 14

Explanation 14

Answer 13

True

Back to Question 13

Explanation 13

Answer 12

C.  Some combination of the two.

Back to Question 12

Explanation 12

Answer 11

True

Back to Question 11

Explanation 11

Answer 10

False

Back to Question 10

Explanation 10

Answer 9

True

Back to Question 9

Explanation 9

Answer 8

False

Back to Question 8

Explanation 8

Answer 7

True

Back to Question 7

Explanation 7

Answer 6

False

Back to Question 6

Explanation 6

Answer 5

False

Back to Question 5

Explanation 5

Answer 4

True

Back to Question 4

Explanation 4

Answer 3

True

Back to Question 3

Explanation 3

Answer 2

True

Back to Question 2

Explanation 2

Answer 1

E.  All of the above.

Back to Question 1

Explanation 1



Copyright 2007, Richard G. Baldwin.  Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited.

About the author

Richard Baldwin is a college professor (at Austin Community College in Austin, TX) and private consultant whose primary focus is a combination of Java and XML. In addition to the many platform-independent benefits of Java applications, he believes that a combination of Java and XML will become the primary driving force in the delivery of structured information on the Web.

Richard has participated in numerous consulting projects involving Java, XML, or a combination of the two.  He frequently provides onsite Java and/or XML training at the high-tech companies located in and around Austin, Texas.  He is the author of Baldwin's Java Programming Tutorials, which has gained a worldwide following among experienced and aspiring Java programmers. He has also published articles on Java Programming in Java Pro magazine.

Richard holds an MSEE degree from Southern Methodist University and has many years of experience in the application of computer technology to real-world problems.

Baldwin@DickBaldwin.com

-end-