P.S: This is just a study guide. The questions may not appear exactly like this.
1. The output of the following code will be 3.
int num1 = 1;
int* num2 = &num1; int& num3 = *num2; num1 = 3;
std::cout << num3 << std::endl;
• True
• False
2. If we have a program with a variable bob of type pointer-to-Employee, what is the following line equivalent to?
fn = bob->firstName;
• fn = *(bob).firstName;
• fn = *(bob.firstName);
• fn = (*bob).firstName;
• fn = (*bob.firstName);
3. The following code will print the number 7.
int num = 4; int* val = # num = 7;
std::cout << val << std::endl;
True
False
4. The name of an array acts as a pointer to the first element in the array.
• True
• False
5. A pointer is a variable that can store a memory address as its value.
• True
• False
6. What would the result of the following code be?
int nums[4] = {1,2,3,4}; nums[2] = 7;
std::cout<< *nums << std::endl;
• It will print 1.
• It will print a syntax error message.
• It will print garbage, because we are accessing memory we didn't allocate.
• It will print [1, 2, 7, 4].
Question 7
7. Pass by reference makes a copy of the value that is being passed and stores it in the corresponding parameter of the function.
• True
• False
8. You indicate that a function does not return a value by…
o putting a “&” in front of the function name.
o giving it a return type of “void”.
o leaving out the return type.
o giving a reference type as tthe return type.
9. Each class may have any number of constructors, but at most one destructor.
• True
• False
10. A class can contain variables of other class types.
True
False
11. In C++ a single array may store different data types.
o True
o False
12. What is the output of the following code?
int nums[] = {1,2,3,4};
nums[2] = 7;
for (int i=0; i<4; i++)
std::cout << nums[i] << " ";
std::cout << std::endl;
• 1 7 3 4
• 1 2 7 4
• 1 7 2 4
• 1 3 7 4
13. What string will the following code output?
string str = “infinite indigo origami”;
string output = “”;
int counter = 1;
while (str.at(counter) != ‘i’)
{
output += str.at(counter);
counter += 3;
}
std::cout << output << std::endl;
OUTPUT: nnengog
14. Assume there is a class called Cat, which has a string field called name, and a get method called getName. If you have an array of pointers to Cat objects, how would you get the name of the third Cat?
• catArray[2].getName()
• *catArray[2].getName()
• catArray[2]->getName()
• catArray[getName(2)]