1. !If you are creating a C++ class, you can explicitly set certain functions to be default. Which of the following is allowed in class called "DefaultTest"?
DefaultTest
DefaultTest(const std::string &) = default;
DefaultTest() = default;
DefaultTest(DefaultTest, DefaultTest) = default;
DefaultTest & operator=(const std::string &) = default;
2. Given that a Node is defined as: struct Node{ int value = 0; Node * next = nullptr; }; Consider the following code: Node * node = new Node; node->value = 100; node->next = new Node; std::cout << node->next->value; delete node; delete node->next; Which of the following is a problem with this code:
struct Node{
int value = 0;
Node * next = nullptr;
};
Node * node = new Node;
node->value = 100;
node->next = new Node;
std::cout << node->next->value;
delete node;
delete node->next;
value
unsigned
node
node->next->value
3. You have a "Book" class and want to add a member function called "CountWord" that takes a word as its parameter and returns the number of times that word was used. Which of the following is the best way to declare this function so that it will function correctly and be callable in a variety of contexts?
Book
CountWord
int CountWord(const std::string & word) const;
int CountWord(const std::string & word);
int CountWord(std::string * word);
int CountWord(std::string & word);
int CountWord(std::string & word) const;
4. What is a difference between pointers and references in C++? +
5. What is the value of y after these lines are run in C++? int x = 6; int y = 4 + x * 3 - 1;
int x = 6;
int y = 4 + x * 3 - 1;
6. What is a difference between a native array (such as "int a[100]") and std::vector (such as "std::vector<int> v(100)")?
int a[100]
std::vector<int> v(100)
7. If I created an array of strings with "std::string word_list[5000]", what should I do when I'm done using word_list?
std::string word_list[5000]
delete [] word_list
delete word_list[]
delete word_list
8. Which of the following is NOT a legal function declaration
std::string MyFun(std::string name=default);
const std::string & MyFun(std::string);
void MyFun(int x, int y);
std::string MyFun(std::string name="default");
9. What is the value of var2 after the following code executes? int var1 = 5; var1 += 5; int var2 = 2 * ++var1;
int var1 = 5;
var1 += 5;
int var2 = 2 * ++var1;
10. Which of these numeric types is represented using the fewest bytes in C++?
int
float
long long
double
char
Click Check Answers to identify any errors and try again. Click Show Answers if you also want to know which answer is the correct one.