Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Engineering Problem Solving with C++, 3e Chapter 6 Test Bank

1. What is the output from the following program


#include <iostream >
using namespace std;
int X{6}, C{3};
int TestMe(int &Y, int Z);
int main ()
{
int A, B, W;
A = 5;
B = 2;
X = 1;
W = TestMe(A, B);
// Last two output lines
cout << "A = " << A << " B = " << B << endl;
cout << "X= " << X<< " C = " << C << endl;
return 0;
}
int TestMe (int &Y, int Z)
{
int C;
C = 4;
cout << "Y = " << Y <<" Z = " << Z << endl;
cout << "C = " << C << endl;
cout << "X = " << X << endl;
Z = 7;
X = C + Z;
Y = X + Z;
cout << "X = " << X <<" Z = " << Z << endl;
cout << "Y = " << Y << endl;
return Z;
}
Y=5Z=2
C=4
X=1
X = 11 Z = 7
Y = 18
A = 18 B = 2
X = 11 C = 3

©2017 Pearson Education, Inc. Hoboken, NJ. All Rights Reserved.


Engineering Problem Solving with C++, 3e Chapter 6 Test Bank

2. Write a function that will calculate the length of the hypotenuse of a right triangle. The
function will take two real number parameters for the other two sides of the right triangle.
The function will return the result it computes.

double hypotenuse(double side1, double side2)


{
return sqrt(side1*side1 + side2*side2);
}

3. What is the output of the following program?


#include <iostream>
using namespace std;
void TestIt(int Y);
int main ()
{
int A, B;
A = 5;
TestIt(A);
B = 3;
TestIt(B);
return 0;
}
void TestIt (int Y)
{
int Z(7);
static int X(2);
Z= Y*3 - Z;
Y = Y * 2;
X ++;
cout << "X = " << X << " Y = " << Y << " Z = " << Z << "\n\n" ;
return;
}
X = 3 Y = 10 Z = 8
X=4Y=6 Z=2
4. In C++, pass by reference for an integer parameter can be achieved …
A. by placing an & in front of the value when invoking the function.
B. by placing an & following the data type of a parameter in the function prototype and the
function header.
C. by placing an & before the data type of a parameter in the function prototype and the
function header.
D. by doing nothing special, since pass by reference is the default way to pass integers.
5. The argument for a parameter which is pass by value …
A. may be a declared constant of the same data type as the parameter.
B. must be a variable.
C. may be a call to a function that returns a value of the same data type as the parameter.
D. All of the above.
©2017 Pearson Education, Inc. Hoboken, NJ. All Rights Reserved.
Engineering Problem Solving with C++, 3e Chapter 6 Test Bank

6. The argument and the parameters of a function


A. must agree in number, and identifier name.
B. must agree in data type and identifier name.
C. must agree in number and order.
D. must agree in order and identifier name.

7. Which of the following is not true when using functions to create a modular solution.
A. solution is easier to debug and maintain
B. solution allows for reuse of code
C. only one person can work on a modular solution
D. solution allows for encapsulation

8. The key word "static" is used to declare a variable when you want the variable …
A. to be available in more than one function.
B. to be created new each time the function is entered, and destroyed when the function
finishes execution.
C. to maintain its value from one execution, of a function to the next execution of that
function.
D. to be placed in a high speed register.
9. Which of the following is a storage class?
A. global
B. local
C. automatic
D. Both A and B are storage classes.
10. The scope of a variable refers to …
A. when an identifier exists, i.e. its lifespan.
B. the range of values a variable can take on.
C. where an identifier can be used or referenced.
D. the rules for the construction of the identifier for a variable.
11. Which of the following is the scope of a variable which is declared outside of the main
program?
A. Automatic
B. External
C. Global
D. Local
12. In the following function prototype:
int fun1(double& Num1, double& Num 2);
A. Function fun1 can modify the arguments passed to both Num1 and Num2
B. Function fun1 can not modify the arguments passed to Num1 and Num2
C. Function fun1 can only modify the argument passed to Num1
D. Function fun1 can only modify the argument passed to Num2
13. Which library contains the function used to seed the random number generator?
A. cstdlib

©2017 Pearson Education, Inc. Hoboken, NJ. All Rights Reserved.


Engineering Problem Solving with C++, 3e Chapter 6 Test Bank

B. ctime
C. crandom
D. cmath
14. Which of the following statements properly invokes the random number generator storing the
resulting random number into myNumber.
A. myNumber = srand();
B. myNumber = srand(time(0));
C. myNumber = rand();
D. None of the above.
15. A good way to assure that the random number generator will not give the same sequence of
random numbers each time your program is run is to
A. only run your program once in your lifetime.
B. use the random number generator in a for loop.
C. seed it using 32767 as the seed.
D. seed it using time() as the seed.

16. The incremental search method for finding the root of an equation requires that the two
guesses , a and b, at the root, satisfy the following criteria
A. F(a) * F(b) is less than 0
B. F(a) * F(b) is greater than or equal to 0
C. a * b is less than 0
D. a * b is greater than or equal to 0
17. If a variable which is declared to be an integer is passed by value to a parameter which is
declared to be a double, an error message will occur at compile time.
A. True
B. False
18. A function prototype must include the names of the parameters.
A. True
B. False

19. Constants that are declared to be global are available throughout the program in the main
function and any function that does not redeclare that identifier.
A. True
B. False
20. The Newton–Raphson method algorithm for locating the root of a function is guaranteed to
locate a root in a known number of iterations.
A. True
B. False
21. A better approximation of the area under a function is obtained by decreasing the number of
trapezoids used.
A. True
B. False

©2017 Pearson Education, Inc. Hoboken, NJ. All Rights Reserved.


Engineering Problem Solving with C++, 3e Chapter 6 Test Bank

22. When a local variable in a function is declared using the automatic storage class, the variable
will retain its value from one invocation of the function to the next.
A. True
B. False

23. The public interface of a well-defined data type provides a complete set of public methods
for interacting with the object while hiding the implementation details of the data type.
A. True
B. False

24. The objective of encapsulation is to increase the transparency of how objects are
implemented.
A. True
B. False

25. Which methods are typically value-returning methods with no parameters, whose purpose is
to return the value of a class attribute.
A. Accessor
B. Mutator
C. Constructor
D. None of these.

26. Which methods are typically void methods with one or more input parameters, whose
purpose is to change the value of a class attribute.
A. Accessor
B. Mutator
C. Constructor
D. None of these.
27. It is impossible to predict the sequence of random numbers produced by a random number
generators.
A. True
B. False

©2017 Pearson Education, Inc. Hoboken, NJ. All Rights Reserved.

You might also like