C++ Test 2 Chapters 4 5 6

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is the output of the following function call? //function body int factorial(int n) { int product = 0; while(n > 0) { product = product * n; n--; } return product; } //function call cout << factorial(4);

0

What is the value of i after the following function call? //function definition int doSomething(int value) { value = 35; return value; value = 13; } //fragment of main program int i = 0; cout << doSomething(i);

0

What is the output of the following code fragments? int trial( int& a, int b) { if(b > a) { a = b; return -a; } else { return 0; } } float x = 0, y= 10,z; z = trial(y,x); cout << z << " " << x <<" " << y << endl;

0 0 10

What is the output of the following program fragment? cout << static_cast < float > (3/4) << endl;

0.0

What is the output of the following code fragment? double size, volume = 16.0; size = sqrt(sqrt(volume)) / 3; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << size;

0.67

What is the output of the following program fragment? cout << static_cast < double > (3)/4 << endl;

0.75

Given the function definition void something ( int a, int& b ) { int c; c = a + 2; a = a * 3; b = c + a; } what is the output of the following code fragment that invokes something? (All variables are of type int.) r = 1; s = 2; t = 3; something(t, s); cout << r << ' ' << s << ' ' << t << endl;

1 14 3

If the user types in the characters 10, and your program reads them into an integer variable, what is the value stored into that integer?

10

Given the following function definition, void shift(int& a, int&b) { a = b; b = a; } what is the output after the following function call? int first = 0, second = 10; shift(first, second); cout << first <<" "<< second << endl;

10 10

What is the output of the following program fragment? cout << pow(4,2) << endl;

16

What is wrong with the following code? int f1( int x, int y) { x = y * y; return x; int f2( float a, float& b) { if(a < b) b = a; else a = b; return 0.0; } }

Function definitions may not be nested.

When overloading a function, what must be true?

The names should be the same with different number and/or types of parameters

In the function declaration shown, the mechanism used to call this function is known as: double pow(double base, double exp);

call by value

To open a file with a user supplied name, you would need to store the name in a variable. If the file name was to have no more than 20 characters in it, which would be an appropriate declaration of the file name variable?

char filename[21];

After a stream is opened, before it is used for input and output, it should be

checked to see if it opened correctly.

The functions pow( ), sqrt( ), and fabs( ) are found in which include file?

cmath

Multiple arguments to a function are separated by

commas

If you have the following variable declaration in your program, const int SIZE = 34; then which of the following statements are legal?

cout << SIZE;

If a file did not open correctly, you should

display an error message and take some suitable action such as exit.

The command outFile.width(10) is used to

display the next value in at least 10 characters.

Call-by-reference should be used

when the function needs to change the value of one or more arguments.

Which of the following loop condition statements will read all the data in the file assuming that each record in the file is two integer values, and you will read the first one into a variable named intOne, and the other into intTwo?

while(inFile >> intOne >> intTwo)

You should make a parameter a reference parameter if

you need the function to change the value of the argument passed to the function.

Which include directive is necessary for file IO?

#include < fstream >

Which of the following comments would be the best post-condition for this swap function? void swap( int& left, int&right);

//Postcondition: the values of left and right are exchanged.

What is the value of the following? sqrt(sqrt(pow(2,4)));

2

If the variable x has the original value of 3.4, what is the value in x after the following? cout << static_cast < int > (x);

3

Given the following function definitions and program fragments, what is the output? void f1(int& z, int &q) { int temp; temp = q; q = z; z = temp; } void f2( int& a, int& b) { if( a < b) f1(a,b); else a = b; } int x = 3, y = 4; f2(y,x); cout << x <<" " << y << endl;

3 3

What is the value returned by the following function? int function( ) { int value = 35; return value + 5; value += 10; }

40

What is the value of the following? floor(4.999) + ceil(2.0)

6.0

What is the value of x after the following code fragment executes? float x = 36.0; x = sqrt(x);

6.0

What is the value of choice after the following statements? void getChoice(int& par_choice, in par_count); int choice, count = 3; getChoice(choice, count); void getChoice(int& par_choice, in par_count) { if(par_count < 0) par_choice = 0; if(par_count = 0) par_choice = -1; else par_choice = 99; return; }

99

Testing a function or program using test values that are at or near the values that change the outcome of the program is known as using

boundary values.

Which of the following are valid function calls to the fabs function?

A and B

Which of the following are true?

A function can call another function.

Which of the following function prototypes are not valid?

All are valid

When is the external name of the file used in the program?

when opening the file stream

Which of the following is true for a void function?

Nothing is returned

If you have the two functions as shown, int someFunction(int value); float someFunction(float value); and a variable x, which is a double, which function is called by the following statement? cout << someFunction(x);

Nothing, it is a syntax error.

We have a file that has a name in it, but the name is written one character per line. We need to write this name to the screen. What is wrong with the following code? ifstream fileIn; fileIn.open("file.txt"); char ch; fileIn.get(ch) while(!fileIn.eof( )) { cout.put(ch); fileIn.get(ch); }

Our output has new lines in it.

What is the output of the following function and function call? void calculateCost(int count, float& subTotal, float taxCost); float tax = 0.0, subtotal = 0.0; calculateCost(15, subtotal,tax); cout << "The cost for 15 items is " << subtotal << ", and the tax for " << subtotal << " is " << tax << endl; //end of fragment void calculateCost(int count, float& subTotal, float taxCost) { if ( count < 10) { subTotal = count * 0.50; } else { subTotal = count * 0.20; } taxCost = 0.1 * subTotal; }

The cost for 15 items is 3.00, and the tax for 3.00 is 0.00;

What is the output of the following function and function call? void calculateCost(int count, float& subTotal, float& taxCost); float tax = 0.0, subTotal = 0.0; calculateCost(15, subTotal,tax); cout << "The cost for 15 items is " << subTotal << ", and the tax for " << subTotal << " is " << tax << endl; //end of fragment void calculateCost(int count, float& subTotal, float& taxCost) { if ( count < 10) { subTotal = count * 0.50; } else { subTotal = count * 0.20; } taxCost = 0.1 * subTotal; }

The cost for 15 items is 3.00, and the tax for 3.00 is 0.30;

Information Hiding is analogous to using

a black-box methodology.

A simplified main program used to test functions is called

a driver

A simplified version of a function which is used to test the main program is called

a stub

ios::showpos is a flag that

always displays a + in front of positive integers

When a void function is called, it is known as

an executable statement.

Testing your program should be done

as each function is developed.

If a function needs to modify more than one variable, it must

be a call by reference function.

Which of the following is a legal call to the displayOutput function? void displayOutput(int total);

displayOutput(myTotal);

The command outFile.precision(2);

displays all floating point values sent to outFile with 2 decimal places.

Which of the following is the correct way to determine if a file stream named inFile opened correctly?

if( inFile.fail( ) )

If the name of the input file was in a variable named filename, which of the following is the correct way to open the input stream named inFile and associate it with this file?

inFile.open(filename);

Which statement correctly opens an input stream named in_file and attaches it to a file name project.txt?

in_file.open("project.txt");

Which of the following are not legal function declarations?

int 3ave(int a, int b, intc);

If you need to write a function that will compute the cost of some candy, where each piece costs 25 cents, which would be an appropriate function declaration?

int calculateCost(int count);

Which of the following functions is a properly overloaded function of the following? int doSomething(int first, float second);

int doSomething(int first, int second, float third);

In order to use the stream manipulators, you must include the ________ library

iomanip

Which function returns true if the character argument is a letter?

isalpha

A function that is associated with an object is called a ________ function.

member

In order to read data from a file you

must know the kind of data in the file.

What is wrong with the following function body? void calculate(int count, double price, double& cost) { if (count < 0) cost = 0.0; else cost = count*price; return; }

nothing

A ________ is a variable that has functions as well as data associated with it.

object

The get function reads

one character value.

The put function outputs

one character value.

Which of the following is the correct way to close a file stream named outFile?

outFile.close( );

To open an output file and add to the end of the data already in the file you would write ________.

outFile.open("project.txt", ios::app);

Which of the following are valid function calls to the pow function?

pow(1.1,3.0);

Using functions in a program is known as

procedural abstraction

Which of the following is not used when using files for input and output?

prompting for file data

The fabs(double num) function

returns the absolute value of num.

When a variable is local to a function, we say that it has ________ of the function.

scope

The member function setf stands for

set the flags.

Given the following function declaration and local variable declarations, which of the following is not a correct function call? int myInt; double myDouble; char ch; void someFunction(int& first, double second, char third);

someFunction(1, 2.0, ch);

The postcondition of a function

tells what will be true after the function executes.

Call-by-reference parameters are passed

the actual argument

What is the output of the following code? char ch = 'G'; cout << tolower(ch) << endl;

the integer value of 'g'

If you write a function that should use call-by-reference, but forget to include the ampersand,

the program will run with incorrect results.

In the following function, what is passed to the first parameter? void f1( int& value1, int value2); int x,y; f1(x,y);

the variable x (or its memory location)

When parameters are passed between the calling code and the called function, parameters and arguments are matched by

their relative positions in the parameter and argument lists

The expression static_cast < double > (3) is called a

type cast

When the function below is called, the ________ of the actual parameters is passed to the function definition. double sqrt(double value);

value

If you were to write a function for displaying the cost of an item to the screen, which function prototype would be most appropriate?

void display(double myCost);

If you need a function to get both the number of items and the cost per item from a user, which would be a good function declaration to use?

void getData(int& count, double& cost);

The precondition(s) for a function describe

what must be true before the function executes.


Kaugnay na mga set ng pag-aaral

Part-66 Questions; Gas Turbine Engines

View Set

Arithmetic and Number Property Rules (GRE)

View Set

Week 3 Chapters 4 & 5 Psychology 456

View Set

AP Euro Multiple Choice Study Guide

View Set