cap 5-8 Flashcards

1
Q

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)

A

the variable x (or its memory location)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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 &laquo_space;x «” “ &laquo_space;y &laquo_space;endl;

a. 3 3
b. 4 3
c. 3 4
d. 4 4

A

3 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A stub

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

A simplified main program used to test functions is called
a. A stub
b. Abstraction
c. Polymorphism
d. A driver

A

A driver

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

boundary values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A

When the function needs to change the value of one or more arguments

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

Function definitions may not be nested

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

The program will run with incorrect results

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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.

A

As each function is developed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

Prompting for file data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which include directive is necessary for file IO
a. #include <fstream>
b. #include <iomanip>
c. #include <cstdlib>
d. #include <fileIO></fileIO></cstdlib></iomanip></fstream>

A

include <fstream></fstream>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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

A

in_file.open(“project.txt”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

When opening the file stream

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

A __________ is a variable that has functions as well as data associated with it.
a. member
b. int
c. object
d. float

A

object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

A function that is associated with an object is called a _________ function.
a. input
b. output
c. member
d. instantiated

A

member

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

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

A

outFile.close();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

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

A

checked to see if it opened correctly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

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

A

if( inFile.fail() )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What are the valid indexes for the array shown below?
int myArray[25];

0-25
0-24
1-25
1-24

A

0-24

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

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.

A

Nothing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

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]

A

scores[24]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

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

A

All of the above

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Given an array of integers of size 5, how does the computer know where the 3rd indexed variable is located?
It adds 3 to the base address of the array
It adds space for 3 integers to the base address of the array
It remembers where all the indexed variables of the array are located.
None of the above

A

It adds space for 3 integers to the base address of the array

26
Q

What is wrong with the following code fragment?
const int SIZE =5;

float scores[SIZE];

for(int i=0; i<=SIZE;i++)

{

        cout << "Enter a score\n";

        cin >> scores[i];

}

Array indexes start at 1 not 0
Arrays must be integers
Array indexes must be less than the size of the array
Should be cin&raquo_space; scores[0];

A

Array indexes must be less than the size of the array

27
Q

Which of the following declare an array of 5 characters, and initializes them to some known values?
char array[5]={‘a’,’b’,’c’,’d’,’e’};
char array[4]={‘a’,’b’,’c’,’d’,’e’};
char array[5]={‘’};
char array[]={‘a’,’b’,’d’,’e’};
A and C
B and D
all of the above

A

char array[5]={‘a’,’b’,’c’,’d’,’e’}; and char array[5]={‘’};

28
Q

If you declare and initialize an integer array of size 10, but only list 5 values, what values are stored in the remaining 5 indexed variables?
0
garbage
0.0
‘0’

A

garbage

29
Q

Arrays are always passed to a function using
pass by value
pass by reference
pass by numbers
you cannot pass arrays to a function

A

pass by numbers

30
Q

Give the following declarations, which of the following is a legal call to this function?
int myFunction(int myValue);

int myArray[1000];

cout &laquo_space;myFunction(myArray);
cout &laquo_space;myFunction(myArray[0]);
myArray = myFunction(myArray);
myArray[1] = myFunction(myArray[0]);
A and B
A and C
B and D

A

cout &laquo_space;myFunction(myArray[0]); and myArray[1] = myFunction(myArray[0]);

31
Q

Which of the following function declarations correctly expect an array as the first argument?
void f1(int array, int size);
void f1(int& array, int size);
void f1(int array[100], int size);
void f1(float array[], int size);
All of the above
C and D
A and B

A

void f1(int array[100], int size); and void f1(float array[], int size);

32
Q

Which of the following function declarations correctly guarantee that the function will not change any values in the array argument?
void f1(int array[], int size) const;
void f1(int array[], int size);
void f1(int &array, int size);
void f1(const int array[], int size);
void f1(int array[], const int size);

A

void f1(const int array[], int size);

33
Q

The following function definition has an error in it. What line is this error on?
0. void f1(const double array[], int size)

  1. {
  2. int i=0;
  3. while(i< size)
  4. {
  5. array[i] += 2;
  6. cout «array[i];
  7. i++;
  8. }
  9. }

0
2
5
6
2

A

5

34
Q

Which of the following function declarations could be used to input data from the keyboard into the array?
void input(int array[], int &numElements, int MAX_SIZE);
void input(int array[], int numElements, int MAX_SIZE);
void input(int &array[], int numElements, int MAX_SIZE);
int array[] input(int array[], int &numElements, int MAX_SIZE);

A

void input(int array[], int &numElements, int MAX_SIZE);

35
Q

If we want a search function to search an array for some value and return either the index where the value was found, or -1 if not found, which of the following prototypes would be appropriate?
void search(const int array, int target, int numElements);
void search(const int array, int target);
int search(const int array[], int numElements);
int search(const int array[], int target, int numElements);

A

int search(const int array[], int target, int numElements);

36
Q

Given the following function definition for a search function, and the following variable declarations, which of the following are appropriate function invocations?

const int SIZE=1000;

int search(const int array[], int target, int numElements);

int array[SIZE], target, numberOfElements;

search(array[0], target, numberOfElements);
result=search(array[0], target, numberOfElements);
result=search(array, target, numberOfElements);
result=search(array, target, SIZE);

A

result=search(array, target, numberOfElements);

37
Q

Given the following function definition, will repeated calls to the search function for the same target find all occurrences of that target in the array?
int search(const int array[], int target, int numElements)

{

        int index=0;

        bool found=false;

       

        while((!found) && (index < numElements))

        {

                    if(array[index] == target)

                                found=true;

                    else

                                index++;

        }

        if(found==true)

                    return index;

        else

                    return -1;

}

Yes
No
Impossible to tell without looking at the values of the array
It depends on the value of target.

A

No

38
Q

Given the following function definition, what modifications need to be made to the search function so that it finds all occurrences of target in the array?
int search(const int array[], int target, int numElements)

{

        int index=0;

        bool found=false;

       

        while((!found) && (index < numElements))

        {

                    if(array[index] == target)

                                found=true;

                    else

                                index++;

        }

        if(found==true)

                    return index;

        else

                    return -1;

}

Add another parameter to indicate where to stop searching
Add another parameter to indicate where to start searching
This already can find all occurrences of a given target
Have the function return the whole array

A

Add another parameter to indicate where to start searching

39
Q

Which sort algorithm does the following outline define?
for i between 0 and number_used-1 inclusive

        put the ith smallest element at array[i]

sequential
selection
bubble
swap

A

selection

40
Q

Which of the following array declarations are legal?
int array[10];
int size;
cin&raquo_space; size;

int array[size];

int array[]={0,0,0};
const int size=9;
int array[size];

All of the above
All but C
All but B

A

int array[]={0,0,0};

41
Q

A character array terminated with the null character is most correctly called
a c-string
a character array
a string
none of the above

A

a c-string

42
Q

Which of the following declarations correctly creates a c-string that can hold the value “phonebook”
char s1;
char s1[9];
char s1=10;
char s1[10];

A

char s1[10];

43
Q

To declare a c-string and initialize it to the value of “phonebook”,
char s1=phonebook;
char s1[10]=”phonebook”;
c-string phonebook;
char s1[10]=phonebook;

A

char s1[10]=”phonebook”;

44
Q

Which of the following will print out the value in str?
char str[30];

cin&raquo_space; str;

cout &laquo_space;str;
for(int i=0;i<30;i++)
cout &laquo_space;str[i];

int i=0;
while(i<30 && str[i] != ‘\0’)

        cout << str[i++];

All of the above
A and B
A and C

A

cin&raquo_space; str; and, int i=0;
while(i<30 && str[i] != ‘\0’)

        cout << str[i++];
45
Q

When the extraction operator is used to read data into a string,
it skips all white spaces
it skips only new lines
it reads everything on the line
it reads as many characters that will fit into the c-string

A

it skips all white spaces

46
Q

If you want to read into a c-string, you must ensure that the user does not enter more characters than
The size of the c-string
The size of the c-string + 1
The size of the c-string -1
It doesn’t matter.

A

The size of the c-string -1

47
Q

What is wrong with the following attempted c-string declaration and initialization?
char str1[5]={‘a’, ‘b’, ‘c’};

There are only 3 values in the braces
The single quotes should be double quotes
The values do not constitute a c-string
nothing

A

The values do not constitute a c-string

48
Q

How can you assign the value “toaster” to a c-string name str of size 10?
str=”toaster;
str=toaster;
strcpy(str,”toaster”);
str.strcpy(“toaster”);

A

strcpy(str,”toaster”);

49
Q

What is the difference between strcmp and strncmp?
No difference
they both compare, one expects an integer for the number of characters to compare.
one copies, the other compares
They are in different libraries

A

they both compare, one expects an integer for the number of characters to compare.

50
Q

strcmp(first, second) returns
<0 if first < second, 0 if first == second, positive otherwise
true if first=second, false otherwise
nothing, it’s a void function
>0 if first < second, 0 if first > second, <0 otherwise

A

<0 if first < second, 0 if first == second, positive otherwise

51
Q

What is wrong with the following code fragment?
char str1[10]=”Mark”, str2[15]=”What’s my name”;

strcpy(str1,str2);

Nothing
str2 has white space in it
str1 does not have enough room
str2 does not have enough room

A

str1 does not have enough room

52
Q

Which assignment statements will copy the value “ toaster” into a string variable (str1)?
strcpy(str1,”toaster”);
str1 = “toaster”;
str1 = toaster;
str1 += toaster;

A

str1 = “toaster”;

53
Q

What is the value of str after the following code?
string str;

a garbage string
the empty string
the null character
unknown

A

the empty string

54
Q

Which is the proper way to determine how many characters are in the string variable named str?
str.getLength()
str.length()
length(str)
getLength(str)

A

str.length()

55
Q

If the name of a file to open is in the string variable name fileName, which of the following will correctly open the file for output?
out_file.open(fileName);
out_file.open(“fileName”);
fileName.open(outfile);
out_file.open(fileName.c_str());

A

out_file.open(“fileName”);

56
Q

Which of the following would correctly read an entire line from an input file stream named fin into a string variable named line.
getline(fin, line);
fin.getline(line);
fin.getline(line,’\n');
fin.getline(line,80);

A

getline(fin, line);

57
Q

Which of the following returns the fourth character in the string variable named str and checks if there is a fourth character in the string?
str(3);
str.at(3);
str[3];
All of the above

A

str.at(3);

58
Q

Given the following declarations, which of the following is legal syntax?
string str=”Your name”;

  char c_string[20]="My name";

str = c_string;
c_string = str;
strcpy(c_string, str.c_str());
strcpy(c_string, str);
A and C

A

str = c_string; and strcpy(c_string, str.c_str());

59
Q

The notation vector<Base_Type> means that the vector is
an array
a template class
primitive data type
all of the above</Base_Type>

A

a template class

60
Q

The base type for a vector can be
int
float or double
char
any data type

A