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

1) Which of the following statements is NOT true about floating-point C++ data t

ID: 3581556 • Letter: 1

Question

1) Which of the following statements is NOT true about floating-point C++ data types?
a. An exponential (base-2) representation is used.
b. The exponent part is a power of 2.
c. The precision (number of decimal places) works as in a pocket scientific calculator.
d. The length of the fields can go from 4 up to 16 bytes.

2) What is wrong with the following function body?
void calculate(int count, float price, float& cost)
{
if (count < 0)
cost=0.0;
else
cost=count*price;
return;
}
a. void functions may not have a return statement.
b. void functions must return a value
8
c. nothing
d. cannot mix pass-by-reference and pass-by-value parameters

3) When is the external name of the file used in the program?
m. Any time you read or write to the file
n. Never
o. Only when reading from the file
p. When opening the file stream

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

5) Which of the following function declarations correctly expect an array as the first argument?
a. void f1(int array, int size);
b. void f1(int& array, int size);
c. void f1(int array[100], int size);
d. void f1(float array[], int size);
e. All of the above
f. C and D
g. A and B

6) The following function definition has an error in it. On what line is this error?
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. }

a. 0
b. 2
c. 5
d. 6
e. 2

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

8) What is wrong with the following code fragment?
char str1[10]="Annie", str2[15]="What's my name";
strcpy(str1,str2);
a. Nothing
b. str2 has white space in it
c. str1 does not have enough room
d. str2 does not have enough room

9) What is the value of str after the following code?
string str;
a. a garbage string
b. the empty string
c. the null character
d. unknown

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

11) Given the following code, what is the correct statement to insert the string str2 into str1, directly after the 'd'?
string str1="abcdefg";
string str2="ABCDE";
a. str1.insert(4,str2);
b. str2.insert(4,str1);
c. insert(str1,4)=str2;
d. insert(str2,4)=str1

12) If you have a class with a member function called display(ostream& out), that will send the values in the class to the parameter stream, and you need to call that function from within another member function, how would you call it to print the data to the screen?

Explanation / Answer

2) What is wrong with the following function body?
void calculate(int count, float price, float& cost)
{
if (count < 0)
cost=0.0;
else
cost=count*price;
return;
}
Answwer: Option: c. nothing

3) When is the external name of the file used in the program?
Answer: Option :o. Only when reading from the file

4) Which of the following declare an array of 5 characters, and initializes them to some known values?
a. char array[5]={'a','b','c','d','e'};
b. char array[4]={'a','b','c','d','e'};
c. char array[5]={''};
d. char array[]={'a','b','d','e'};
Answer: f. B and D

5) Which of the following function declarations correctly expect an array as the first argument?
Answer: c. void f1(int array[100], int size);

6) The following function definition has an error in it. On what line is this error?
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. }

Answer: Option c. 5

7) How can you assign the value "toaster" to a c-string name str of size 10?
Answer:c. strcpy(str,"toaster");

8) What is wrong with the following code fragment?
char str1[10]="Annie", str2[15]="What's my name";
strcpy(str1,str2);
Answer: b. str2 has white space in it

9) What is the value of str after the following code?
string str;
Answer:b. the empty string

10) Which of the following would correctly read an entire line from an input file stream named fin into a string variable named line.
Answer: a. getline(fin, line);

11) Given the following code, what is the correct statement to insert the string str2 into str1, directly after the 'd'?
string str1="abcdefg";
string str2="ABCDE";
Answer:a) str1.insert(4,str2);

12) If you have a class with a member function called display(ostream& out), that will send the values in the class to the parameter stream, and you need to call that function from within another member function, how would you call it to print the data to the screen?

Answer: display(cout);