MidTermCOP
Which of the following are NOT members of the Big Three?
Default constructor constructor
In C++, objects must be constructed using the new keyword before they can be used
False
14, 15 ,16
Go back
What is the return type for constructors?
No return type
Q1 The first stage of C++ build process is the ---?
Preprocessor
(video) What are the 3 main stages of building a C++ program
Preprocessor Compiler Linker
References, once initialized (or "bound") to something, for all intents and purposes, ARE that thing.
TRUE
Bonus question C++ technically doesn't call it the heap... what is another name for the heap?
The free store
Which of the following are true about pointers AND references?
They allow for data to be shared between functions without creating copies of the original
Why do we write the Big Three?
They are needed if your class uses dynamically allocated memory that it needs to track/clean up. (side note: You CAN write a class that allocates memory and returns pointers, leaving the clean-up duty to some other class or process)
(video) Why do we pass objects by reference (or by pointer)?
To avoid copying the object Pointers and references let us access something that "lives" somewhere else in memory
Why do we pass or return variables by const* or const&
To pass or return quickly AND prevent changes to them
There are no limits to the number of operators you can implement in any given class.
True
What is the default value of p? int* p;
Undefined. It MIGHT be nullptr, MAYBE it points to something valid... or maybe it's set to point to a random memory address
Q3 What is the default value of a pointer?
Unknown, it is uninitialized
What is this code called (collectively, all of it together)?
class definition
Which of the following lines would create a constant variable?
const float PI = 3.14159;
Q1 Which of the following is the appropriate way to print "Hello, world!" to the screen?
cout << "Hello, world!";
Template classes and functions allow a programmer to reuse code that operates on different
data types
Which of the following declarations is an accessor?
int Student::GetGrade() {return grade}
Q1 This is the final stage of the building process creates the executable
linker
What do pointers point to?
memory addresses
Which of the following is a destructor for a class named Menu?
~Menu();
While a class can only have one default constructor (more would be a compiler error), which of the following COULD be a default constructor for a class called Spaceship?
***Spaceship(string name = "Spaceship", float weight = 0.0f); Spaceship(); Spaceship(int serialNumber, bool movingSpaceship = true);
When writing classes, a header file is where you should write the:
**Prototype definitions
When writing classes, a source file is where you should write this:
**function definition
Q1 What is the minimum value of any unsigned data type
0
Which of the following statements is true about public member functions of a class?
A class user can call the function to operate on the object
What is a default constructor?
A constructor which either: Has no parameters Has parameters, all of which have a default value
Memory that is allocated with new, but never deleted with delete (or delete[]), will result in what?
A memory leak
What is the difference between "shallow copy" and "deep copy"
A shallow copy is one in which you copy dynamically allocated memory by ONLY copying the pointer A deep creates a NEW ARRAY, and copies each ELEMENT, one at a time
Given the following code: // If the boss is at half health... if (bossMonster.GetHitPoints() <= bossMonster.GetMaxHitPoint() *0.5f) { // Look out player, you won't like him when he's angry // TODO: write really powerful boss behavior } What are the functions being called in the if statement considered?
Accessors
which variables in this function are created on the stack?
All of them. Both size and mem created on the stack. The variable mem is pointing to an array of integers created on the heap. All variables created in a function are on the stack.
What are the "Big Three"?
Copy assignment operator Copy constructor Destructor
In order to access a pointer's pointee (i.e. the thing it's pointing to), you must do what?
Dereference the pointer
What class member function is called when an object falls out of scope?
Destructor
Is the memory allocated for the values variable deallocated correctly?
False
Q1 Warnings will stop your code from compiling and prevent you from building your program
False
Template classes should be split into header files and source files, like any other class
False
The Queue data structure stores information in a specific order. What order is that?
First in, First out
Which of the following is a copy assignment operator for a class Hero?
Hero& operator=(const Hero& h);
What is the Rule of Three?
If you define one member of the Big Three, you should define the other two
Q1 What value does the following variable have?
Impossible to know
(video) Why should we avoid copying objects?
It can impact the speed of a program--copying an object can take a long time (depending on the size of the object) Pointers/references are very small by comparison--typically around 4-8 bytes in size
The Stack data structure stores information in a specific order. What order is that?
Last in, First out
For the following function, which line of code could complete the desired functionality. Assume the classes are properly defined and any variables written do what their names suggest
Look it up
What is a memory leak?
Memory which is allocated, but never deallocated (or freed) by the program.
What does new and delete do?
New allocates space for some data on the heap Delete frees up space previously allocated by new
What is the preferred way of indicating that a pointer does not point to anything?
Setting it to nullptr
What type of class/function can be used with any type of data?
Template classes and functions can be used with any type of data:
Q1 Sometimes, when building a program, we get a lot of compiler errors. Depending on what you're working on, it could be literally hundreds of them. Which of these errors should you fix first?
The first one.
Where is dynamically allocated memory stored?
The heap
(video) What is the difference between the stack and the heap?
The stack is for short term storage--functions and the variables they create The heap is for: Long term storage--something needs to exist for longer than one function Storage of large objects--objects too big for the stack Objects which needs to be dynamically allocated (such as an array, whose size is not known at compile-time)
Q1 What happens if you try to set an unsigned variable to a value less than the minimum possible value?
The value will underflow
These are examples of accessing the data a pointer is pointing to. What is this process called?
To access the data a pointer points to, you must dereference that pointer
Which of the following causes a stack overflow error?
When your program tries to use more stack memory than is avaiable
For a class Widget, identify the correct way(s) to call a copy constructor to copy an existing Widget named object1 to object2
Widget object2 = object1; Widget object2(object1);
For a class Widget, identify the incorrect way(s) to call a copy constructor to copy an existing Widget named object 1 to object2
Widget object2(&object1); Widget object2.copy(object1);
Given a class called Widget, which of the following are valid constructors?
Widget(int x); Widget(float x, double y, unsigned int num = 0); Widget();
What is the purpose of const in this case?
const ensures whatever "catches" the return can't change the data being pointed to: const int* numbers= Randomization(32); numbers[10] = 27; // error // Error, numbers2 must be const int* int* numbers 2 = randomization(26);
Grouping data and functionality together, otherwise known as --, is the core of why we use classes
encapsulation
Q1 Warning can always be safely ignored
false
Class variables are -- by default
private
Q1 A --- has everything a function has, except the implementation
prototype
A copy constructor that does a member-to-member copy of all class variables, including dynamically allocated pointers, is performing what type of copy?
shallow copy
Which of the following are valid template declarations?
template <typename T> template <typename CustomType>
If you don't define a copy assignment operator for your class...
the compiler implicitly defines one that performs a member-to-member copy
A class function could be used a number of times, by a number of different instances of that class. In a class member function, how would you access the object that has called the function?
this->