cap 5-8 Flashcards
In the following function, what is passed to the first parameter?
void f1( int& value1, int value2);
int x,y;
f1(x,y);
a. The value of x
b. nothing, it is a void function
c. the value of y
d. the variable x (or its memory location)
the variable x (or its memory location)
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 «_space;x «” “ «_space;y «_space;endl;
a. 3 3
b. 4 3
c. 3 4
d. 4 4
3 3
A simplified version of a function which is used to test the main program is called
a. A stub
b. Abstraction
c. Polymorphism
d. A driver
A stub
A simplified main program used to test functions is called
a. A stub
b. Abstraction
c. Polymorphism
d. A driver
A driver
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
a. parameters
b. functional decomposition
c. boundary values
d. a black-box
boundary values
Call-by-reference should be used
a. For all variables
b. When the function needs to change the value of one or more arguments
c. Never
d. only in void functions
When the function needs to change the value of one or more arguments
Which of the following comments would be the best post-condition for this swap function void swap( int& left, int&right);
a. //Postcondition: None
b. //Postcondition: the values of left and right are exchanged.
c. //Postcondition: left has the value of right
d. //Postcondition: left and right are unchanged
//Postcondition: the values of left and right are exchanged.
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;
}
}
a. Neither function should return a value
b. Function definitions may not be nested
c. Both parameters to f2 should be pass-by reference
d. in f2, a can not be assigned b.
e. nothing is wrong
Function definitions may not be nested
If you write a function that should use call-by-reference, but forget to include the ampersand,
a. The program will not compile
b. The program will not link
c. The program will not run without a run-time error
d. The program will run with incorrect results
e. It doesn’t matter
The program will run with incorrect results
Testing your program should be done
a. As each function is developed
b. At the end of the coding
c. Only if there appear to be problems
d. Only if your instructor requires it.
As each function is developed
Which of the following is not used when using files for input and output
a. Opening the file stream
b. Ensuring that the stream opened
c. Closing the stream
d. Prompting for file data
Prompting for file data
Which include directive is necessary for file IO
a. #include <fstream>
b. #include <iomanip>
c. #include <cstdlib>
d. #include <fileIO></fileIO></cstdlib></iomanip></fstream>
include <fstream></fstream>
Which statement correctly opens an input stream named in_file and attaches it to a file name project.txt?
a. in_file=project.txt
b. in_file=”project.txt”
c. in_file.open(“project.txt”);
d. in_file.open(project.txt);
in_file.open(“project.txt”);
When is the external name of the file used in the program?
a. Any time you read or write to the file
b. Never
c. Only when reading from the file
d. When opening the file stream
When opening the file stream
A __________ is a variable that has functions as well as data associated with it.
a. member
b. int
c. object
d. float
object
A function that is associated with an object is called a _________ function.
a. input
b. output
c. member
d. instantiated
member
Which of the following is the correct way to close a file stream named outFile?
a. outFile.close();
b. outFile.close;
c. outFile.close(“project.txt”);
d. close(outFile);
outFile.close();
After a stream is opened, before it is used for input and output, it should be
a. declared
b. closed
c. checked to see if it opened correctly
d. none of the above
checked to see if it opened correctly
Which of the following is the correct way to determine if a file stream named inFile opened correctly?
a. if( inFile.open() )
b. if( inFile.fail() )
c. if( inFile.opened() )
d. if( inFile.failed() )
if( inFile.fail() )
If a file did not open correctly, you should
a. continue on anyway
b. display an error message and continue on
c. display an error message and take some suitable action such as exit
d. exit the program immediately
display an error message and take some suitable action such as exit
What are the valid indexes for the array shown below?
int myArray[25];
0-25
0-24
1-25
1-24
0-24
What is wrong with the following code?
float scores[10], total;
Cannot declare regular and array variables together.
Arrays must be integers
The 10 should be replaced with a variable name, whose value is input from the user
Nothing.
Nothing.
Given an array named scores with 25 elements, what is the correct way to access the 25th element?
scores+25
scores[24]
scores[25]
scores[last]
scores[24]
Why should you use a named constant for the size of an array?
Readability of code
Makes changes to the program easier
Helps reduce logic errors
All of the above
All of the above