QUESTION 12 Which of these lines is NOT produced by the following code, assuming
ID: 3858455 • Letter: Q
Question
QUESTION 12
Which of these lines is NOT produced by the following code, assuming they are correctly implemented in a program?
(You may need to fix them, but without altering the output, i.e. fix the double quotes)
cout << "*" << setw(5) << 123 << "*"
<< 123 << "*" << endl;
cout.setf(ios::showpos);
cout << "*" << setw(5) << 123 << "*"
<< 123 << "*" << endl;
cout.unsetf(ios::showpos);
cout.setf(ios::left);
cout << "*" << setw(5) << 123 << "*"
<< setw(5) << 123 << "*" << endl;
*123**
+123*+123*
123*123*
*123 *
10 points
QUESTION 13
Which of the following are correct ways to end a loop using a test for end-of-file?
(Two correct answers)
inStream.get(next);
while(!inStream.eof( ))
{
cout << next;
inStream.get(next);
}
inStream.get(next)
while(!eof(inStream))
{
cout << next;
inStream.get(next);
}
while(inStream >> next)
cout << next;
while(inStream->next)
{
cout << next;
}
10 points
QUESTION 14
Which function takes a single char value from the input file, without regard to whether it is a whitespace?
putline()
getline()
get()
put()
10 points
QUESTION 15
To open a file for read-write and random access does NOT require
#include, using std::fstream; and using std::ios;
The stream to be defined using the class fstream. defined in the header file
The usual definition of an ofstream or ifstream object
The stream to be connected to the physical file object with first argument a C-string containing the physical file name and a second argument, ios::in | ios::out that specifies that the i/o with the file should be for either reading or writing
10 points
QUESTION 16
Why does this version of the swap function fail to work? Is there a fix?
void swap(int & lhs, int& rhs)
{
lhs = rhs;
rhs = lhs;
}
We can fix if we just reverse the order of the lines.
It works OK.
It fails because the programmer forgot to make the parameters call-by-reference
It fails because the first line destroys the old value of lhs without saving it. Then both variables have the old value of rhs in them.
10 points
QUESTION 17
Consider the following function and code segment.
void One( int first, int & second )
{
first = 17;
second = first + 1;
}
int main()
{
// other code ...
int j = 4;
int k = 3;
One(j, k);
// other code ..
}
After the call to One(j, k); what are the values of j and k?
j == 17, k == 18;
j == 4, k == 3;
j == 4, k == 18;
j == 17, k == 3;
10 points
QUESTION 18
Which is correct?
There is only one kind of parameter passing in C++, namely call-by-value
The call-by-reference mechanism is specified in the function declaration and definition, using a $ between the type and the parameter
The position of the ampersand in the function header is of no importance to the proper compiling and execution of code that uses call-by-reference parameters
A call-by-reference parameter may pass data only out of a function
10 points
QUESTION 19
Which is correct?
Names of parameters in functions, especially function prototypes, have no meaning, and may be selected quite arbitrarily.
The compiler ha no problem distinguishing these two function definitions:
void func(double &x){/*…*/}
void func(double x){/*…*/}
Call-by-reference is restricted to void functions
There is no problem with the compiler distinguishing these two function definitions:
void func(double x){/*…*/}
int func(double x){/*…*/ return something_double;}
10 points
QUESTION 20
Consider the function, where the programmer inadvertently left out the ampersand in the definition of parRef. What is the output of the code?
void doStuff(int parValue, int parRef)
{
parValue = 100;
cout << “parValue in call to doStuff = “
<< parValue << endl;
parRef =222;
cout << “parRef in call to doStuff = “
<< parRef << endl;
}
and consider the call, which we assume is in a complete and correct program
int n1 = 1, n2 =2;
doStuff(n1, n2);
cout << “n1 after call to doStuff = “ << n1 << endl;
cout << “n2 after call to doStuff = “ << n2 << endl;
parValue in the call to doStuff = 100;
parValue in the call to doStuff = 222;
n1 after function call = 1;
n2 after function call = 222
parValue in the call to doStuff = 100;
parValue in the call to doStuff = 222;
n1 after function call = 100
n2 after function call = 222
parValue in the call to doStuff = 100;
parValue in the call to doStuff = 222;
n1 after function call = 1;
n2 after function call = 2
x parValue in the call to doStuff = 100;
parValue in the call to doStuff = 222;
n1 after function call = 100
n2 after function call = 2
QUESTION 9
Which of the following is a restriction on use of a stream variable (either ifstream or ofstream)?
To use a stream variable as a function parameter you can only use call-by-reference
To use a stream variable as a function parameter you can only use call-by-value
To use a stream variable as a function parameter you can use call-by-value or call-by-value
A file variable can be used in any way any other variable can be use
QUESTION 12
Which of these lines is NOT produced by the following code, assuming they are correctly implemented in a program?
(You may need to fix them, but without altering the output, i.e. fix the double quotes)
cout << "*" << setw(5) << 123 << "*"
<< 123 << "*" << endl;
cout.setf(ios::showpos);
cout << "*" << setw(5) << 123 << "*"
<< 123 << "*" << endl;
cout.unsetf(ios::showpos);
cout.setf(ios::left);
cout << "*" << setw(5) << 123 << "*"
<< setw(5) << 123 << "*" << endl;
*123**
+123*+123*
123*123*
*123 *
10 points
QUESTION 13
Which of the following are correct ways to end a loop using a test for end-of-file?
(Two correct answers)
inStream.get(next);
while(!inStream.eof( ))
{
cout << next;
inStream.get(next);
}
inStream.get(next)
while(!eof(inStream))
{
cout << next;
inStream.get(next);
}
while(inStream >> next)
cout << next;
while(inStream->next)
{
cout << next;
}
10 points
QUESTION 14
Which function takes a single char value from the input file, without regard to whether it is a whitespace?
putline()
getline()
get()
put()
10 points
QUESTION 15
To open a file for read-write and random access does NOT require
#include, using std::fstream; and using std::ios;
The stream to be defined using the class fstream. defined in the header file
The usual definition of an ofstream or ifstream object
The stream to be connected to the physical file object with first argument a C-string containing the physical file name and a second argument, ios::in | ios::out that specifies that the i/o with the file should be for either reading or writing
10 points
QUESTION 16
Why does this version of the swap function fail to work? Is there a fix?
void swap(int & lhs, int& rhs)
{
lhs = rhs;
rhs = lhs;
}
We can fix if we just reverse the order of the lines.
It works OK.
It fails because the programmer forgot to make the parameters call-by-reference
It fails because the first line destroys the old value of lhs without saving it. Then both variables have the old value of rhs in them.
10 points
QUESTION 17
Consider the following function and code segment.
void One( int first, int & second )
{
first = 17;
second = first + 1;
}
int main()
{
// other code ...
int j = 4;
int k = 3;
One(j, k);
// other code ..
}
After the call to One(j, k); what are the values of j and k?
j == 17, k == 18;
j == 4, k == 3;
j == 4, k == 18;
j == 17, k == 3;
10 points
QUESTION 18
Which is correct?
There is only one kind of parameter passing in C++, namely call-by-value
The call-by-reference mechanism is specified in the function declaration and definition, using a $ between the type and the parameter
The position of the ampersand in the function header is of no importance to the proper compiling and execution of code that uses call-by-reference parameters
A call-by-reference parameter may pass data only out of a function
10 points
QUESTION 19
Which is correct?
Names of parameters in functions, especially function prototypes, have no meaning, and may be selected quite arbitrarily.
The compiler ha no problem distinguishing these two function definitions:
void func(double &x){/*…*/}
void func(double x){/*…*/}
Call-by-reference is restricted to void functions
There is no problem with the compiler distinguishing these two function definitions:
void func(double x){/*…*/}
int func(double x){/*…*/ return something_double;}
10 points
QUESTION 20
Consider the function, where the programmer inadvertently left out the ampersand in the definition of parRef. What is the output of the code?
void doStuff(int parValue, int parRef)
{
parValue = 100;
cout << “parValue in call to doStuff = “
<< parValue << endl;
parRef =222;
cout << “parRef in call to doStuff = “
<< parRef << endl;
}
and consider the call, which we assume is in a complete and correct program
int n1 = 1, n2 =2;
doStuff(n1, n2);
cout << “n1 after call to doStuff = “ << n1 << endl;
cout << “n2 after call to doStuff = “ << n2 << endl;
parValue in the call to doStuff = 100;
parValue in the call to doStuff = 222;
n1 after function call = 1;
n2 after function call = 222
parValue in the call to doStuff = 100;
parValue in the call to doStuff = 222;
n1 after function call = 100
n2 after function call = 222
parValue in the call to doStuff = 100;
parValue in the call to doStuff = 222;
n1 after function call = 1;
n2 after function call = 2
x parValue in the call to doStuff = 100;
parValue in the call to doStuff = 222;
n1 after function call = 100
n2 after function call = 2
QUESTION 9
Which of the following is a restriction on use of a stream variable (either ifstream or ofstream)?
To use a stream variable as a function parameter you can only use call-by-reference
To use a stream variable as a function parameter you can only use call-by-value
To use a stream variable as a function parameter you can use call-by-value or call-by-value
A file variable can be used in any way any other variable can be use
Explanation / Answer
Question 12:
output would be :
* 123*123*
* +123*+123*
*123 *123 *
Question 14:
get()
Question 16:
It fails because the first line destroys the old value of lhs without saving it. Then both variables have the old value of rhs in them.
temp = lhs;
lhs = rhs;
rhs = temp;
Question 17:
j == 4, k == 18;
This is because the function One(), creates a reference parameter for k. So, whatever is done with the reference parameter, affects the original value of k. j is a normal call by value, so the function change does not affect original value of j
Question 18:
The call-by-reference mechanism is specified in the function declaration and definition, using a $ between the type and the parameter
Question 19:
The compiler ha no problem distinguishing these two function definitions:
void func(double &x){/*…*/}
void func(double x){/*…*/}
Question 20:
parValue in the call to doStuff = 100;
parValue in the call to doStuff = 222;
n1 after function call = 1;
n2 after function call = 2
This is because since the & is missing in the function, the operations on parRef inside the function have no effect on the actual n2 value. Now, n1 and n2 have essentially been passed using call by value.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.