Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1 Which statement is incorrect? A) Linear search data in the set can be

ID: 3857934 • Letter: Q

Question

Question 1

Which statement is incorrect?

A) Linear search data in the set can be unsorted, Binary search data in the set must be sorted

B)Linear search uses less comparisons than Binary search if the target is at the beginning of the set

C) Binary search uses less comparisons than Linear search if the target is exactly in the middle of the set

D)Linear search uses more comparisons than Binary search if the target is at the end of the set

QUESTION 2

Which statement is incorrect?

A)The statement

vector<int>  partnums[ 4];

creates a vector of initial size 4;

B)The statement

partnums[3] = 144;

works regardless of whether partnums is an array or a vector

C)The statement

int partnums[]={136, 122, 109, 146};

creates an array of size 4

D)The statement

cout << partnums.size() << endl;

works only if partnums is a vector

QUESTION 3

Which statement is incorrect?

A)An array cannot be resized

B) An array can be used to create a vector

C)An array can be resized to hold more elements but not less

D)A vector resizes automatically

QUESTION 4

To open a file for read-write and random access does NOT require

A)The usual definition of an ofstream or ifstream object

B)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

C)#include<fstream>, using std::fstream; and using std::ios;

D)The stream to be defined using the class fstream. defined in the <fstream> header file

QUESTION 5

Which of the following positions the file pointer for a file that has been opened for reading and writing?

A)Use the size() member function on the file stream to position the file pointer

B)Use the seekp(arg) fstream member function with the number of bytes to the record in question (counting the first byte as 0) as argument to position the file pointer

C)Use the seekp(arg) fstream member function with the number of records (counting the first record as 0) as argument to position the file pointer

D)Use the sizeof operator to determine the number of bytes in the file stream.

QUESTION 6

Which function takes a single char value from the input file, without regard to whether it is a whitespace?

A)putline()

B)put()

C)get()

D)getline()

QUESTION 7

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;

A)*123 *

B)*123**

C)123*123*

D)+123*+123*

QUESTION 8

Which statement is incorrect?

A)An output stream flows from your program to somewhere outside the program, either to a file or to some device such as the screen

B)A stream is a flow of data into or out of your program

C)An input stream is a stream of data flowing from your program, either to a file, or to the keyboard

D)cin is an input stream

QUESTION 9

Which of the following sets of statements will set floating point output to the stream outStream to fixed point with set 3 places of decimals?

(Two correct answers)

A)outStream.setf(ios::fixed | ios::showpoint);

outStream << setprecision(2);

B)outStream << setflag(ios::fixed);

outStream << setflag(ios::showpoint);

outStream << setprecision(2);

C)outStream.flags(ios::fixed);

outStream.flags(ios::showpoint);

outStream.precision(2);

D)outStream.setf(ios::fixed);

outStream.setf(ios::showpoint);

outStream.precision(2);

QUESTION 10

Which of the following are correct ways to end a loop using a test for end-of-file?

(Two correct answers)

A)inStream.get(next)

while(!eof(inStream))

{

  cout << next;

  inStream.get(next);

}

B)while(inStream->next)

  {

  cout << next;

}

C)inStream.get(next);

while(!inStream.eof( ))

{

  cout << next;

  inStream.get(next);

}

D)while(inStream >> next)

    cout << next;

QUESTION 11

Which statement is incorrect?

A)When you write

ifstream inStream;

inStream.open("infile.dat");

the file infile.dat must be located in the directory where the program is being run

B)When you use the open member function to tie a file name to a file stream, the file name is called the external file name, the program refers to the file by the stream variable used to open the file

C)The flush function copies the file buffer to the file on the file medium (disk, etc)

D)A file is automatically closed when a program terminates, so there is never a need to close a file

QUESTION 12

To determine whether a file was opened successfully, the program can use the fstream function:

Hint: google

A)fail()

B)open()

C)eof()

D)close()

QUESTION 13

Which statement is incorrect?

A)When program calls the setf function on an output stream, the effect continues until the program calls unsetf with the same argument to turn it off

B)The manipulators endl and function setf() and precision are provided by the iostream header. The manipulators setw and setprecision are provided by the iomanip header.

C)Setting the width of output with call do the width() function, affects only the next output

C)Setting the width of the output with the manipulator setw has a different result from setting the width with the width function

QUESTION 14

Which of the following is a restriction on use of a stream variable (either ifstream or ofstream)?

A)To use a stream variable as a function parameter you can only use call-by-reference

B)A file variable can be used in any way any other variable can be use

C)To use a stream variable as a function parameter you can only use call-by-value

D)To use a stream variable as a function parameter you can use call-by-value or call-by-value

QUESTION 15

A file stream, fStr, is open and attached to physical file.txt. To reset this file stream so that the file can be read again starting at the first line requires

A)Only calling the member function open() using fStr as the calling object with the "file.txt" as argument.

B)"File stream fStr, reset yourself to the start of the file."

C)Calling the reset() member function using fStr as the calling object but with no argument

D)With calling object fStr call close()then call open( ) with argument "fStr".

QUESTION 16

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?

A)j == 4, k == 3;

B)j == 17, k == 3;

C)j == 4, k == 18;

D)j == 17, k == 18;

QUESTION 17

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;

A) 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

B) parValue in the call to doStuff = 100;

parValue in the call to doStuff = 222;

n1 after function call = 1;

n2 after function call = 2

C) parValue in the call to doStuff = 100;

parValue in the call to doStuff = 222;

n1 after function call = 1;

n2 after function call = 222

D) parValue in the call to doStuff = 100;

parValue in the call to doStuff = 222;

n1 after function call = 100

n2 after function call = 222

QUESTION 18

Which is correct?

A)There is only one kind of parameter passing in C++, namely call-by-value

B) The call-by-reference mechanism is specified in the function declaration and definition, using a $ between the type and the parameter

C) 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

D) A call-by-reference parameter may pass data only out of a function

QUESTION 19

Consider the following function definition:

void tripler(int& n)

{

n = 3*n;

}

Given this definition, which of the following is NOT an acceptable function call?

int a[3] = {3,4,5}, number = 2;

A)tripler(number);

B)tripler(a[number]);

C)tripler(a[2]);

D)tripler(a[3]);

QUESTION 20

Which is correct?

A)The compiler ha no problem distinguishing these two function definitions:

void func(double &x){/*...*/}

void func(double x){/*...*/}

B)There is no problem with the compiler distinguishing these two function definitions:

void func(double x){/*...*/}

int func(double x){/*...*/ return something_double;}

C)Names of parameters in functions, especially function prototypes, have no meaning, and may be selected quite arbitrarily.

D)Call-by-reference is restricted to void functions

Explanation / Answer

1.

Solution:

The binary search requires the input data to be sorted but in the linear search, the input data is not required to be sorted. So, the statement A) is correct. The statement C) is also correct that the binary search uses less comparisons if the target is exactly in the middle of the set, that is O(1) The statement B) that Linear search uses less comparisons than binary search is target is at beginning of the set is also true, because linear search will take O(1) time.

The statement D) that linear search uses more comparisons than binary search if target is at the end of the set is also true, it will take O(N) time while binary search will take O(logn) time even in worst case.

Hence, all the options are correct, none is incorrect.

2.

Solution:

The statement D) is incorrect because the partnums can be array or vector. So the statement:

cout<<partnums.size()<<endl holds true for vector or array. It does not work only for vectors.

Hence the statement D) is incorrect.

3.

Solution:

It is true that array cannot be resized in C++ or java. So, option A) is correct. It is also true that an array can be used to create a vector. So, the statement B) is correct. The statement C) is incorrect that an array can be resized to hold more elements but not less.

Hence, the statement C) is incorrect.

4.

Solution:

To open a file for read-write and random access, the usual definition of an ostream or ifstream object is not required.

Hence, option A) is correct.

5.

Solution:

The option A) is incorrect because the size() member is not present in the fstream class. The option B) is incorrect because it uses record count rather than byte count.

In the option, D) the size of the stream object is provided which is incorrect.

The option C) is correct because the member function seekp(arg) fstream member function sets the position where the next character is required to be inserted in the output/ input stream.

Hence, the option C) is correct.

6.

Solution:

The get () function takes a single shar value from the input file ignoring the whitespace.

Hence, option C) is correct

7.

Solution:

The correct result will be as follows:

Hence, the incorrect options will be A) and B)

8.

Solution:

The option C) is incorrect because, the output stream flows from the program to out of the program, which can be a file or a device such as screen.

Hence, the option C) is incorrect

9.

Solution:

The statements A) and D) are correct. In option B), manipulators and non-manipulators are not used correctly. In option C) flags member function is not used correctly

Hence, the correct options are A) and D)

10.

Solution:

The correct options are A) and D)

11

Solution:

The option A) is incorrect because if infile.dat is present in the directory where the program is running, then a path can be supplied in addition to the file name

The option A) is correct.

12.

Solution:

The fstream function that can be used to determine whether a file was opened successfully is open().

The correct option is B).

13.

Solution:

The incorrect option is that setting the width of the output with call to the width() function, affects only the next output. The effect of setting a flag with setf continues until you call unsetf with the same argument to turn this flag off

The correct option is C)

14.

Solution:

The restriction on use of a stream variable is that to use a stream variable as a function parameter, you can only use call by reference because a stream parameter cannot be a call by value parameter.

The option A) is correct.

15.

Solution:

To reset this file stream so that the file can be read again starting at the first line requires that with calling object fStr the function close() is called and then open() with argument “fStr” is called.

The option D) is correct

16.

Solution:

The value of j and k after executing the function are 4 and 18 respectively.

The option C) is correct

17.

Solution:

The correct options are as follows:

parValue in the call to doStuff = 100;

parValue in the call to doStuff = 222;

n1 after function call = 1;

n2 after function call = 2

The correct option is B)

18.

Solution:

The correct option is that the position of the ampersand in the function header is of no importance to the proper compiling and execution of the code that uses call by reference parameters.

The correct option is C)

19.

Solution:

The non-acceptable function call is tripler(a[3]) because a[3] is outside valid array bounds.

The correct option is C)

20.

Solution:

The correct option is that the compiler has no problem distinguishing these two function definitions:

void func(double &x){/*…*/}

void func(double x){/*…*/}

Both these functions are same and the compiler will not be able to distinguish because the prototypes are same, only the parameters are different.

The correct option is A)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote