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

I am working on a study guide for a final exam for my C++ class and would like a

ID: 3581425 • Letter: I

Question

I am working on a study guide for a final exam for my C++ class and would like a second opinion on the answers to the study guide questions since I do not have an answer key.

Multiple Choice:

1) Given the function definition,
void something ( int a, int& b )
{
int c;
c = a + 2;
a = a * 3;
b = c + a;
}
What is the output of the following code fragment that invokes something? (All variables are of type int.)
r = 1;
s = 2;
t = 3;
something(t, s);
cout << r << ' ' << s << ' ' << t << endl;
a. 1 14 3
b. 1 10 3
c. 5 14 3
d. 1 14 9

2) What are the valid indexes for the array shown below?
int myArray[25];
a. 0-25
b. 0-24
c. 1-25
d. 1-24

3) What is wrong with the following code?
float scores[10], total;
a. Cannot declare regular and array variables together.
b. Arrays must be integers
c. The 10 should be replaced with a variable name, whose value is input from the user
d. Nothing.

4)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 ";
cin >> scores[i];
}
a. Array indexes start at 1 not 0
b. Arrays must be integers
c. Array indexes must be less than the size of the array
d. Should be cin >> scores[0];

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

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

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

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

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

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) What is wrong with the following structure definition?
struct MyStruct
{
int size;
float weight;
}
a. Nothing
b. Can not have mixed data types in a structure
c. missing semicolon
d. Braces are not needed.

13) Given the following strucure definitions, what is the correct way to print the person's birth year?
struct DateType
{
int day;
int month;
int year;
};
struct PersonType
{
int age;
float weight;
DateType birthday;
};
PersonType person;
a. cout << person.birthday.year;
b. cout << year;
c. cout << birthday.year;
d. cout << peson.year;

14) Given the following class definition and the following member function header, which is the correct way to output the private data?
class Person
{
public:
void outputPerson(ostream& out);
private:
int age;
float weight;
int id;
};
void Person::outputPerson(ostream& out)
{
//what goes here?
}
a. out << person.age << person.weight << person.id;
b. out << person;
c. out << age << weight << id;
d. outputPerson(person);

15) 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
c. nothing
d. cannot mix pass-by-reference and pass-by-value parameters

Explanation / Answer

1) Answer a. 1 14 3

t is taken by value, change in t will be restricted to something function block only
s is taken by reference, so it will be modified
c = a + 2 = 3 + 2 = 5
a = a*3 = 9
b = c+a = 5+9 = 14

2) Answer b. 0-24
myArray[25] means it has 25 elements
They are indexed from 0 to 24

3) Answer d. Nothing
It works, no syntactic error.

4) Answer c. Array indexes must be less than the size of the array
scores[SIZE]
will have valid indices from 0 till SIZE-1

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