Revised: February 3, 2007
By Richard G. Baldwin
File Pfsg00108.htm
Practice Text Index
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.
108-2. True or False. The use of a function is often a better alternative than the use of in-line code.
108-3. True or False. A function is a separate program module.
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.
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.
108-6. True or False. It is normally not desirable to write functions that take parameters.
108-7. True or False. Parameters provide the mechanism by which one function can pass information to another function.
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.
108-9. True or False. The process of causing a function to be executed is commonly referred to as calling or invoking the function.
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.
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.
108-12. Which is the correct answer? A function will usually:
108-13. True or False. The process of sending back an answer from a function is commonly referred to as returning a value.
108-14. True or False. A function in C++ must return a value.
108-15. True or False. When a C++ function returns a value, the program that called the function can either
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 |
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 |
108-18. What output is produced by the program shown in Listing 108-18?
3.87298 4.47214 5 5.47723
#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 |
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 |
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 |
108-21. True or False. The general syntax for a function consists of two main parts:
108-22. True or False. As a minimum, the function signature consists of:
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.
108-24. True or False. The formal parameter list is empty if the function requires no parameters.
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.
108-26. True or False. Each pair of words in the formal parameter list (a parameter declaration) consists of the following:
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.
108-28. True or False. In order for C++ code to successfully call a function, the function must have either been:
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.
108-30. True or False. Just like in the signature, parameter names must always be included in the function prototype.
108-31. True or False. When a called function terminates, the flow of control is transferred from the calling function to the called function.
108-32. True or False. When a function is called, the flow of control is transferred from the calling function to the called function.
108-33. True or False. C++ allows a parameter to be passed to a function either by value or by reference.
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.
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.
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 |
108-37. What output is produced by the program shown in Listing 108-37.
In main a = 10 b = 20.1 In aFunction x = 10 y = 20.1 Change x and y Still in aFunction x = 100 y = 200.1 Back in main a = 10 b = 20.1
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 |
108-38. True or False.
108-39. What output is produced by the function named aFunction shown in Listing 108-39?
In main a = 10 b = 20.1 In aFunction x = 10 y = 20.1 Change x and y Still in aFunction x = 100 y = 200.1 Back in main a = 10 b = 20.1
#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 |
108-40. What output is produced by the program shown in Listing 108-40?
In main a = 10 b = 20.1 In aFunction x = 10 y = 20.1 Change x and y Still in aFunction x = 100 y = 200.1 Back in main a = 100 b = 20.1
#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 |
108-41. What output is produced by the program shown in Listing 108-41?
In main a = 10 b = 20.1 In aFunction x = 10 y = 20.1 Change x and y Still in aFunction x = 100 y = 200.1 Back in main a = 100 b = 20.1
#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 |
108-42. What output is produced by the program shown in Listing 108-42?
In main a = 10 b = 20.1 In aFunction x = 10 y = 20.1 Change x and y Still in aFunction x = 100 y = 200.1 Back in main a = 100 b = 20.1
#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 |
108-43. What output is produced by the program shown in Listing 108-43?
a = 2 a doubled = 4
#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 |
108-44. What output is produced by the program shown in Listing 108-44?
a = 2 a doubled = 4
#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 |
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.)
a = 2 a doubled = 4
#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 |
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.
a = 2 a doubled = 4
a = 2 a doubled = 4
In main a = 10 b = 20.1 In aFunction x = 10 y = 20.1 Change x and y Still in aFunction x = 100 y = 200.1 Back in main a = 100 b = 20.1
In main a = 10 b = 20.1 In aFunction x = 10 y = 20.1 Change x and y Still in aFunction x = 100 y = 200.1 Back in main a = 10 b = 20.1
The statement is the reverse of what it should be.
Should say sensitive rather than insensitive.
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.
Explanation 16 The program output is:
3.87298 4.47214 5 5.47723
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.
-end-