Midterm Pratice Flashcards
Which of the following statements allows us to simply type “cout” instead of “std::cout” everytime we wish to use the cout variable?
using namespace std;
Using the same string variable called ‘name’ from the previous question, display the name to the console in a single line of code followed by an end of line.
Answers marked incorrect by the auto-grader will be manually reviewed for correctness and partial credit by your instructor.
cout «_space;name «_space;endl;
Assume there is a string called ‘name’ that has already been defined. In a single line of code, read the user’s ENTIRE name into the string from the console.
Answers marked incorrect by the auto-grader will be manually reviewed for correctness and partial credit by your instructor.
getline(cin,name);
Assume that there is a string variable named “title” with some phrase of length 10 or more. Display the 7th character to the console and nothing else.
cout «_space;title[7];
Which of the following also known as an instance of a class?
Object
A struct is declared as follows:
struct NameType { char first[10]; char last[20]; int age; } The first letter in the first name is stored in location 1000. Each letter takes one location and an integer takes 2 locations. What is the address of last[2]?
1,012 (with margin: 0)
What happens when a class with parameterized constructors and having no default constructor is used in a program and we create an object that needs a zero-argument constructor?
Compile-time error.
#include using namespace std;
class Test { public: Test(); };
Test::Test() {
cout «_space;“Constructor Called” «_space;endl;
}
int main() { cout << "Start" << endl; Test t1(); cout << "End" << endl; return 0; }
Start
End
#include using namespace std;
class Test { public: Test(); };
Test::Test() {
cout «_space;” Constructor Called. “;
}
void fun() { static Test t1; }
int main() { cout << " Before fun() called. "; fun(); fun(); cout << " After fun() called. "; return 0; }
Before fun() called. Constructor Called. After fun() called.
#include using namespace std; class Point { Point() { cout << "Constructor called"; } };
int main() { Point t1; return 0; }
Compiler Error
Using the same double pointer, gpaPtr, from the previous question, delete its memory.
delete gpaPtr;
int *p; int i, k; i = 142; k = i; p = &i; Which of the following statements changes the value of i to 143 ?
*p = 143;
In a function, passing a parameter by value does not necessarily protect the contents of the caller’s data structures from being affected by the function under which of the following conditions?
The value parameters are pointer variables
Which of the following also known as an instance of a class? Member Variables Friend Functions Object Member Functions
Object
Assume that a class called Weapon exists that has a default constructor. Create a pointer to a Weapon (named myWeapon) and the Weapon associated with it.
Weapon* myWeapon = new Weapon();
Assume that a pointer to a Weapon object exists named “myWeapon”. Call the Fire method on myWeapon. Assume the Fire method does not have any parameters. Ignore any return values from the Fire method.
myWeapon->Fire();
A deep copy copies one class object to another without copying any pointed-to data.
false
A shallow copy copies one class object to another without copying any pointed-to data.
true
A class should provide a copy constructor in which of the following cases?
At least one data member is a pointer.
What is the right way to declare a copy constructor of a class if the name of the class is MyClass?
MyClass (constant MyClass &arg)
A copy constructor is automatically built into any class that needs it.
false
The capacity method of the vector class represents the number of items stored in the vector.
false
Using the same dynamic array called ‘names’ from the previous question, assume that we have another pointer called ‘item’ that points to the some item in the array. Move ‘item’ to point to the NEXT item in the array.
Answers marked incorrect by the auto-grader will be manually reviewed for correctness and partial credit by your instructor.
item++;
Assume that there is an integer variable called ‘size’ and that we have already asked the user to provide a value for the size of a dynamic array. Assume that we have created a dynamic array of strings called ‘names’ that has space for ‘size’ number of strings.
Delete the ‘names’ array.
Answers marked incorrect by the auto-grader will be manually reviewed for correctness and partial credit by your instructor.
delete[] names;