Hi c++ experts, please help with these mulitiple choice questions. Q1. Consider
ID: 661139 • Letter: H
Question
Hi c++ experts, please help with these mulitiple choice questions.
Q1. Consider the following code. Note: Line numbers are provided on the left side of the code, but are not part of the file.
1 #include <vector>
2
3 class scores
4 {
5 public:
6 void loadScores();
7 void displayScores();
8 private:
9 std:vector stats
10
11 };
Which line contains a syntax error?
Select one:
3
8
9
11
Q2. Consider the following function prototype:
void myclass::helloworld(std::vector& myvector)
Assuming the main function creates a vector named myvector and fills it with data, which statements would call the helloworld function properly?
Select one:
myclass test1;
myclass.helloworld(&myvector);
myclass test1;
test1.helloworld(myvector);
helloworld.helloworld(std::vector<std::string>&);
myvector(helloworld);
Q3Creating an object is similar to creating a variable, except that you must use the __________ as the data type.
Select one:
a function name
object name
any data type that is native to C++
a class name
Q4. Consider the following class definition:
class world
{
public:
void print_me();
};
Which statement(s) would you use to add a member variable named myinteger?
Select one:
private:
myinteger;
private:
int myinteger;
integer myinteger;
private:
integer myinteger();
Q5. Consider the following code from a client main function:
int main()
{
Time target;
target.setHour(5);
target.setMinute(20);
target.getTime();
target.countDown(10);
}
Which is an object?
Select one:
setHour
target
Time
setMinute
Q6. Which part(s) of a class is accessible to the main function and which is not?
Select one:
Public is accessible to the main function, private is not.
Only member functions have access to any part of a class.
Only nonmember functions have access to public and private.
Public and private are both accessible to the main function.
Q7. For a vector named v that has 12 elements, when would this for loop begin and end?
for (int i=0; i<v.size(); ++i)
{
sum += v[i];
}
Select one:
It would begin at index 0 of the vector and end at index 12 of the vector.
It would begin at index 1 of the vector and end at index 12 of the vector.
It would begin at index 11 of the vector and end at index 0 of the vector.
It would begin at index 0 of the vector and end at index 11 of the vector.
Q8. You are writing a nonmember function and have data that you need to store in the private variables of a class. Which of the following ways might be possible to accomplish that? (select all that apply)
Select one or more:
In the main function, use cin to get the data from the keyboard and store it using the equal operator.
Pass the data as an argument when you create an object of the class if the class has a constructor with a parameter.
Pass the data to a member function as an argument if the member function has a parameter.
Pass the data by reference using the return statement.
Q.9 Consider the following function implementation:
void check::check_input()
{
//initialize class variable
sum = 0;
int n = -1;
}
Which type of function is check_input?
Select one:
nonmember function
member function
local function
check_input is not a function; it is a variable
Q.10 With inheritance, any place in a program where a base class object is expected, you are only allowed to place an object that base class, not an object of the derived class.
Select one:
True
False
Q.11The salariedEmp class derives from the employee class. In the following function implementation, the numbers on the left represent line numbers and are not considered part of the code. What does line 4 do?
1 salariedEmp::salariedEmp(const string& newName,
2 const string& newSsn,
3 double newWeeklySalary)
4 : employee(newName, newSsn)
5 {
6 salary = newWeeklySalary;
7 netPay = salary;
8 }
Select one:
This produces an error because it is outside the curly braces of this function.
It creates a function called employee for the salariedEmp class.
This calls the base class constructor and sends the newName and newSsn as arguments.
It stores data in the employee, newName and newSSN private variables of the salariedEmp class.
Q.12 Consider the following function. The numbers on the left represent line numbers and are not part of the code.
1 float p( float x, int n )
2 {
3 if ( n == 0 )
4 return 1;
5 else
6 return x * p();
7 }
Which line contains an error?
Select one:
3
4
5
6
Q13. Consider the following function:
void super_write_vertical(int number)
// Postcondition: The digits of the number have been written,
// stacked vertically. If number is negative, then a negative
// sign appears on top.
// Library facilities used: iostream.h, math.h
{
if (number < 0)
{
cout << '-' << endl;
super_write_vertical(abs(number));
}
else if (number < 10)
cout << number << endl;
else
{
super_write_vertical(number/10);
cout << number % 10 << endl;
}
}
Which call will result in the most recursive calls?
Select one:
super_write_vertical(-1023)
super_write_vertical(0)
super_write_vertical(100)
super_write_vertical(1023)
14. If a class B is derived from A, then which of the following terms describes A?
Select one:
ancestor class.
base class.
parent class.
All of the above.
Why might you use a member initialization list for a derived class?
Select one:
To call a constructor for the base class
To initialize a dynamic array
To initialize the member variables of the derived class.
To declare protected variables
Q.15 Consider the following function implementation of a member function called triple_capacity that belongs to a dynamic container class called bag.
void bag::triple_capacity()
{
string *new_array = new string[capacity * 3];
int i;
for (i = 0; i < used; i++)
new_array[i] = data[i];
delete [ ] data;
data = new_array;
capacity = capacity * 3;
}
Which of the following would you expect in the private section of this class?
Select one:
int capacity;
int used;
string *data;
int capacity;
int i;
string *data[];
int capacity;
int used;
string data[];
int capacity;
int used;
string *data = new string[];
Q. 16 Consider the following code from a client main function:
sequence x;
sequence y;
x.insert(41);
x.insert(42);
y = x;
x.attach(43);
y.advance();
cout << "y size is " << y.size()
cout << " and y current item is " << y.current() << endl;
What data type is x?
Select one:
class
integer
string
double
Q.17 Assuming the vector of integers named v is not empty, which of the following recursive function has the same result as this for loop?
for(int i = 0; i<v.size(); i++)
cout << v[i] << endl;
Select one:
void recursVec(vector<int>& v, int index)
{
if(index >=0)
{
recursVec(v, index -1);
std::cout << v[index] << std::endl;
}
}
void recursVec(vector<int>& v)
{
if(index >=0)
{
recursVec(v);
std::cout << v[index] << std::endl;
}
}
void recursVec(vector<int>& v, int index)
{
if(index >=0)
{
std::cout << v[index] << std::endl;
recursVec(v, index -1);
}
}
You cannot replicate a for loop using recursion.
Q18. Consider the following code.
int Raise(int base, int exp);
int main()
{
cout << Raise(5,3) << endl;
return 0;
}
int Raise(int base, int exp)
{
return base * Raise(base, exp -1);
}
What is missing?
Select one:
The base case
The parameter list
The recursive call
Q19. Below you will find the steps necessary to make a dynamic array grow. Which of these lists the steps in the correct order?
Select one:
1. Create a new, larger array
2. Use a loop to copy each element from the original array to the new array
3. Deallocate the space in the heap for the original array
4. Make the pointer for the original array point to the new array
5. Reset the variable that tracks the size of the array to the larger size
1. Create a new, larger array
2. Reset the variable that tracks the size of the original array to the larger size
3. Use a loop to copy each element from the original array to the new array
4. Deallocate the space in the heap for the original array
5. Make the pointer for the original array point to the new array
1. Create a new, larger array
2. Make the pointer for the original array point to the new array
3. Reset the variable that tracks the size of the original array to the larger size
4. Use a loop to copy each element from the original array to the new array
5. Deallocate the space in the heap for the original array
None of these. You cannot make an array grow in size.
Explanation / Answer
1. 11. A method cannot end with a semicolon
2. myclass.helloworld(&myvector)
3. a class name
4. private: int myinteger;
5. target
6. public and private are both accessible to the main function
7. It would begin at index 0 of the vector and end at index 12 of the vector.
8. Pass the data to a member function as an argument if the member function has a parameter.
9. nonmember function
10. True
11. This calls the base class constructor and sends the newName and newSsn as arguments.
12. 6
13. super_write_vertical(1023)
14. All of the above.
15.int capacity;
int used;
string *data;
16. class
17.void recursVec(vector<int>& v, int index)
{
if(index >=0)
{
std::cout << v[index] << std::endl;
recursVec(v, index -1);
}
}
18.The parameter list
19. 1. Create a new, larger array
2. Make the pointer for the original array point to the new array
3. Reset the variable that tracks the size of the original array to the larger size
4. Use a loop to copy each element from the original array to the new array
5. Deallocate the space in the heap for the original array
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.