Code HS quizes
Which of the following is the correct syntax for using inclusion guards in your header file? #define _program_h #ifndef _program_h ... #endif #ifndef _program_h #define _program_h ... #endif #ifndef _program_h #define _program_h ... #enddefine #endif #define _program_h #ifndef _program_h ... #endif #enddefine
#ifndef _program_h #define _program_h ... #endif
Given a library with a header file called lib.h and an implementation file called lib.cpp, what is the correct way to import the library into a program? #include <lib.h> #include "lib.h" #include "lib.cpp" #inlcude "lib.h" #include "lib.cpp"
#include "lib.h"
Which of the following expressions is equivalent to itr->first? *itr.*first *itr.first (*itr).first *(itr.first)
(*itr).first
Given the followinfgcode snippet: vector<string> soup; soup.push_back("tomato"); soup.push_back("mushroom"); soup.push_back("chicken"); soup.push_back("chowder"); for (string s : soup) { int sum = 0; if (s.length() < 7) { sum += s.length(); } cout << sum << endl; } What would the last line of output be? 0 6 7 20
0
Given the following function bool mystery(string soFar, string goal, string letters){ if (soFar == goal){ return true; } else if (soFar.length() == goal.length()) { return false; } else { for (int i = 0; i <= letters.length(); i++){ soFar += letters[i]; if (mystery(soFar, goal, letters)) { return true; } soFar = soFar.substr(0,soFar.length() - 1); } } return false; } What would be printed if the following statements were executed? cout << mystery("", "CAB", "ABCDEF") << endl; cout << mystery("", "ABC", "ABCDEF") << endl; 0 0 0 1 1 0 1 1
1 1
Given the following recursive function void printList(node *item){ if (item == NULL){ return; } else { printList(item->next); cout << item->data << endl; } } If this function is used to print a doubly linked list where the tail element is initially passed to the function what would be printed out if the nodes are in the following order? 1 (head) 2 3 4 5 (tail) 5 1 1 2 3 4 5 5 4 3 2 1
1 2 3 4 5
Question: 3 Which of the following statements are true about C++ Programs: The order of the code matters. A function must be defined before it is called in the code. C++ programs always start execution statements in the main function The main function typically returns an integer, but it is optional 2 Only 1 and 2 Only 2 and 3 Only 1, 2, and 3
1 and 2 Only
Which of the following are examples of valid throw and catch statements? 1. try { ... throw logic_error("Not Valid"); } catch (exception const& e) { ... } 2. try { ... throw logic_error("Not Valid"); } catch (logic_error const& e) { ... } 3. try { ... throw exception("Not Valid"); } catch (exception const& e) { ... } 2 Only 1 and 2 Only 2 and 3 Only 1, 2, and 3
1 and 2 only
Which of the following are valid function prototypes? 1. void addNums(int a, int b, int c = 0); 2. void addNums(int a, int b = 0, int c = 0); 3. void addNums(int a = 0, int b = 0, int c); 1 Only 1 and 2 Only 2 and 3 Only 1, 2, and 3 Answered
1 and 2 onlyl
The following recursive function is designed to return the sum of all odd numbers from 1 to the number passed to the function. Assuming anya positive integer is passed to the function, which of the following will return the correct result? 1. int sumOdd(int num) { if (num <= 1) { return 1; } else { if (num%2 == 1) { return num + sumOdd(num - 1); } else { return sumOdd(num - 1); } } } 2. int sumOdd(int num) { if (num <= 1) { return 1; } else { if (num%2 == 1) { return num + sumOdd(num - 2); } else { return sumOdd(num - 1); } } } 3. int sumOdd(int num) { if (num < 1) { return 0; } else { if (num%2 == 1) { return num + sumOdd(num - 2); } else { return sumOdd(num - 1); } } } 1 Only 1 and 2 Only 1 and 2 Only 1, 2, and 3
1, 2, and 3
Which of the following will create a 2D vector with the following values? 1 0 0 0 0 0 0 0 0 1. vector<vector<int> > v2D {{1,0,0},{0,0,0},{0,0,0}}; 2. vector<int> v {0, 0, 0}; vector<vector<int> > v2D; for (int i = 0; i < 3; i++) { v2D.push_back(v); } v2D[0][0] = 1; 3. vector<vector<int> > v2D; for (int i = 0; i < 3; i++) { vector<int> v; for (int j = 0; j < 3; j++){ v.push_back(0); } v2D.push_back(v); } v2D[0][0] = 1; 1 Only 2 and 3 Only 1 and 3 Only 1, 2, and 3
1, 2, and 3
Given the follow code, what will be the output? stack<int> s; s.push(3); s.push(10); s.top(); s.push(7); s.pop(); s.push(8); s.pop(); cout << s.top() << endl; 3 7 8 10
10
Given the following code snippet, what will the output be? vector<int> nums {2, 4, 6, 8}; nums.push_back(101); nums.push_back(102); vector<int>::iterator it = nums.begin(); nums.insert(it + 1, 103); it = nums.begin(); nums.insert(it, 104); cout << nums[nums[4]] + nums[1] << endl; 8 103 104 204
103
Given the following function void mystery(int num) { if (num == 0) { return; } else { mystery(num / 10); cout << num % 10; } } What would be printed if mystery(12345) is called? 012345 12345 543210 54321
12345
Given the following graph: If a breadth-first search was performed starting at node S, which connection would not get discovered? 1 2 3 4
2
Given the following struct: struct classroom { vector<string> students; int grade; }; Which of the following would print the grade and name of each student in a classroom with the name jones? 1. for (int i = 0; i < jones.size(); i++) { cout << jones[i].students << " - Grade " << jones[i].grade << endl; } 2. for (int i = 0; i < jones.students.size(); i++) { cout << jones.students[i] << " - Grade " << jones.grade << endl; } 3. for (string student : jones) { cout << student << " - Grade " << jones.grade << endl; } 1 Only 2 Only 3 Only 2 and 3 Only
2 Only
If a file contains a list of words on separate lines, which of the following loops will correctly count how many words are in the files? Assume the input file has a variable name of infile and has been successfully opened. 1. int count = 0; string line; while (!infile.fail()){ getline(infile, line); count ++; } 2. int count = 0; string line; while (true) { getline(infile, line); if (infile.fail()) { break; } count ++; } 1 only 2 Only Both of these Neither of these Answered
2 Only
Which of the following correctly display the contents of vector, v? 1. vector<int>::iterator itr; for (itr = v.begin(); itr <= v.end(); itr ++){ cout << *itr << " "; } 2. vector<int>::iterator itr; for (itr = v.begin(); itr != v.end(); itr ++){ cout << *itr << " "; } 3. vector<int>::iterator itr = nums.begin(); while (itr != nums.end()) { itr ++; cout << *itr << " "; } 1 Only 2 Only 2 and 3 Only 1, 2, and 3
2 Only
A user wants to loop through all the string values in a queue str, and remove any value with a length less than 3, but leave the queue in the same order. Which of the following loops would accomplish this? 1. for (int i = 0; i < str.size(); i++){ string next = str.front(); str.pop(); if (next.size() >= 3){ str.push(next); } } 2. int size = str.size(); for (int i = 0; i < size; i++){ string next = str.front(); str.pop(); if (next.size() >= 3){ str.push(next); } } 1 Only 2 Only Both of these Neither of these
2 only
Given the following 2D Vector: vector<vector<double> > v = {{2.5, 6.8, 8.3, 2.3, 0.0}, {6.1, 10.2, 1.3, -2.5, -9.9}, {1.1, 2.3, 5.8, 13.21, 34.55}}; What will be returned by v.size()? 2 3 4 5
3
How many lines will this code snippet print on? cout << "Hello\nWorld!" << "/n/n"; cout << "It is a good day! "<< endl; cout << "Good luck on the test!"; 1 2 3 4
3
Given the following code snippet int count = 0; bool mystery(string soFar, string goal, string letters){ count ++; cout << soFar << endl; if (soFar == goal){ return true; } else if (soFar.length() == goal.length()) { return false; } else { for (int i = 0; i <= letters.length(); i++){ soFar += letters[i]; if (mystery(soFar, goal, letters)) { return true; } soFar = soFar.substr(0,soFar.length() - 1); } } return false; } What would the value of count be if mystery("", "AB", "ABCD") is called? 2 3 4 8
4
Given a standard linked list where the following data was entered in this order: 1 2 3 4 If this function was called on the head of the list and then the results were printed out starting with the head, what would be the output? void mystery(item *it){ item *nextItem = it->next; it->next = nextItem->next; } 1 3 4 4 3 1 1 2 4 4 2 1
4 2 1
Given the following code, what would the output be? stack<int> stackOne; queue<int> queueTwo; for (int i = 0; i < 5; i++) { stackOne.push(i); } while (!stackOne.empty()){ queueTwo.push(stackOne.top()); stackOne.pop(); } for (int i = 5; i < 10; i++) { queueTwo.push(i); } while (!queueTwo.empty()) { cout << queueTwo.front() << " "; queueTwo.pop(); } 0 1 2 3 4 5 6 7 8 9 4 3 2 1 0 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0 5 6 7 8 9 0 1 2 3 4
4 3 2 1 0 5 6 7 8 9
Given the following code, what will the output be? queue<int> q; stack<int> s; for (int i = 0; i < 5; i++){ q.push(i); s.push(i); } while(!s.empty()){ int sNum = s.top(); int qNum = q.front(); if (sNum > qNum) { cout << sNum << " "; } else { cout << qNum << " "; } q.pop(); s.pop(); } 0 1 1 0 0 1 2 1 0 4 3 3 4 4 3 2 3 4
4 3 2 3 4
Given the following code snippet int count = 0; void mystery(int num) { count ++; if (num == 0) { return; } else { mystery(num / 10); cout << num % 10; } } What would the value of count be if mystery(39847) is called? 4 5 6 7
6
Given the following function int mystery(int num) { if (num <= 0) { return 0; } if (num % 2 == 0) { return 1 + mystery(num / 2); } return mystery(num - 1); } What would be the final returned value if mystery(100) is called? 6 7 8 9
6
Given the following code snippet, what will the output be? set<int> numbers; for (int i = 0; i < 5; i++){ for (int j = 0; j < 5; j++) { numbers.insert(i + j); } } cout << numbers.size() << endl; 5 9 11 25
9
Given the following code, what will the output be? stack<int> stackOne; stack<int> stackTwo; for (int i = 0; i < 5; i++) { stackOne.push(i); } while (!stackOne.empty()){ stackTwo.push(stackOne.top()); stackOne.pop(); } for (int i = 5; i < 10; i++) { stackTwo.push(i); } while (!stackTwo.empty()) { cout << stackTwo.top() << " "; stackTwo.pop(); } 9 8 7 6 5 0 1 2 3 4 0 1 2 3 4 5 6 7 8 9 5 6 7 8 9 0 1 2 3 4 0 1 2 3 4 9 8 7 6 5
9 8 7 6 5 0 1 2 3 4
Which of the following statements about a depth-first search is most accurate? A depth-first search explores all the nodes at one level before moving deeper to the next level until it visits all nodes. A depth-first search searches the tree to find the largest number, then finds the next largest, etc. until it visits all nodes. A depth-first search keeps drilling down until it can't go any further, then it backtracks and drills down again until it visits all nodes. A depth-first search chooses a random path to drill down on, it then backtracks to the top and repeats until it visits all nodes.
A depth-first search keeps drilling down until it can't go any further, then it backtracks and drills down again until it visits all nodes.
Which of the following applications would a linked list NOT be an efficient data structure to use? A restaurant that wants to track a waitlist for customers A word processor that needs to track an undo history A doctor's office that wants to track all of its patients' records alphabetically A playlist on your phone
A doctor's office that wants to track all of its patients' records alphabetically
Which of the following would be best to implement using a graph in C++? Position of a moving ball over time A social network showing the relationships between friends The motor vehicle department tracking customer to service A grocery list containing items and quantities
A social network showing the relationships between friends
Which statement about sets and maps is NOT true? Both sets and maps are efficient at finding values A traditional for loop from 0 to size - 1 will work for a set, but not a map Sets have unique values and maps have uniquie keys Both sets and maps can be traversed with an iterator.
A traditional for loop from 0 to size - 1 will work for a set, but not a map
Given a graph with a large number of nodes with many connections, which implementation would be best? Set of Arcs Adjacency Matrix Adjacency List: List of Nodes
Adjacency Matrix
What is the advantage of using a library? It allows for the easy reuse of the same code in multiple programs Libraries can provide simplified processes for tasks that are done often Libraries provide a level of abstraction for some complex functions All of these options
All of these options
What will be the result of executing this code? vector<int> count {1, 2, 3, 4}; cout << count[4] << endl; 0 4 An undefined behavior such as printing an invalid value or throwing an error Index Out of Bounds error will be thrown
An undefined behavior such as printing an invalid value or throwing an error
Given the following graph: Assuming the graph starts a breadth first searching from node S, and generally moves from left to right as it search, which node would be the 4th node to get discovered (not including S)? A B C D
B
Given the following function header void mystery(int *num) Which of the following will correctly call the function? 1. int numb = 10; mystery(&numb); 2. int *numb = new int(10); mystery(numb); 1 Only 2 Only Both 1 and 2 Neither 1 nor 2
Both 1 and 2
Which of the following is an advantage of a breadth-first search? Breadth-first searches will generally find a path between two nodes quicker than a depth-first search Breadth-first searches take a clearer path through a graph Breadth-first searches use fewer system resources compared to depth-first searches Breadth-first searches always find the shortest path between two nodes Answered
Breadth-first searches always find the shortest path between two nodes Answered
Which of the following best describes the access level of a struct? By default, structs are private. They can be used only within the current program file they are created with. By default, structs are private. They can be used only within the current class that they are created with. By default, structs are public. They can be used by any program that references them. By default, structs are protected. They can be used with the current class and any that extend that class.
By default, structs are public. They can be used by any program that references them.
Which of the following statements about errors in C++ is NOT true according to the lesson? C++ tends to throw more errors than other languages, so it is important to use catch statements C++ allows you to throw and catch variables C++ allows you to throw a subclass error and catch using the parent class When using a try block, you also need to use a catch block
C++ tends to throw more errors than other languages, so it is important to use catch statements
A user created a map and added the following key/value pairs in this order: KeyValueChicago89Washington82New York79Tampa93 When iterating through the map, what is the likely order that the cities will print in? Chicago, Washington, New York, Tampa New York, Washington, Chicago, Tampa Chicago, New York, Tampa, Washington Washington, Tampa, New York, Chicago
Chicago, New York, Tampa, Washington
Given the following graph: If a depth-first search were performed, which node would be the 3rd node searched AFTER node C? D H I J
D
Given the following graph: If a breadth-first search were performed, which node would be the next node searched AFTER node D? A E I J
E
Which of the following best describes the difference between functional recursion and procedural recursion? Functional recursion is a recursive function that accomplishes a task without returning a value, while procedural recursion is a recursive function that returns a value. Functional recursion is a recursive function that returns a value, while procedural recursion is a recursive function that accomplishes a task without returning a value. Functional recursion is a loop that performs like a recursive function, while procedural recursion is an actual recursive call. Functional recursion is an actual recursive call, while procedural recursion is a loop that performs like a recursive function.
Functional recursion is a recursive function that returns a value, while procedural recursion is a recursive function that accomplishes a task without returning a value.
Given the following code snipet, what will be printed as a result if the user enters Karel The Dog at the prompt? cout << "Please enter your full name" << endl; string fullName; cin >> fullName; cout << "Hello " << fullName << endl; Hello Karel The Dog Hello Karel Hello Karel The Dog The code will error because of too many inputs
Hello Karel
Which statement about a header file is NOT correct? If you import a C++ library (such as iostream), you need to be careful not to import it into your program since importing the library twice may cause an error. Header files are included with the file name inside quotes instead of angled brackets. When calling a function from the library, you do not need to reference the library name before the function. A file include statement needs to occur higher up in the program than the statement calling functions from the library.
If you import a C++ library (such as iostream), you need to be careful not to import it into your program since importing the library twice may cause an error.
Which of the following statements about a stack is NOT true? The pop item does not change the stack. If you push 8 items into the stack, then top 8 times, the stack will be empty. The item that has been in the stack the longest is at the bottom of the stack. The stack is a first-in, last-out data structure.
If you push 8 items into the stack, then top 8 times, the stack will be empty.
Which statement best describes implementation files? Implementation files are required when creating a library. Implementation files provide an alternative way for users to include a library in their program. Implementation files provide a level of abstraction for a library as the user only needs to see the header file. Implementation files typically contain functional prototypes, while the header file contains the function definitions.
Implementation files provide a level of abstraction for a library as the user only needs to see the header file. Implementation files typically contain
Which of the following best describes the basic algorithm of a recursive backtracking process? Is the problem solved? If so, return true Try the next choice Does this solve the problem? If so, return true Backout that choice Return false. Is the problem solved? If so, return true For all remaining choice: Try the next choice Does this solve the problem? Backout that choice After all choices have been tried without success, return false. Is the problem solved? If so, return true For all remaining choice: Try the next choice Does this solve the problem? If so, return true After all choices have been tried without success, return false. Is the problem solved? If so, return true For all remaining choice: Try the next choice Does this solve the problem? If so, return true Backout that choice After all choices have been tried without success, return false.
Is the problem solved? If so, return true For all remaining choice: Try the next choice Does this solve the problem? If so, return true Backout that choice After all choices have been tried without success, return false.
Which statement about recursion and loops is MOST accurate? Loops and recursion solve very different problems and are seldom interchangeable Loops and recursion solve similar problems and are just as easy to code in all cases Loops and recursion solve similar problems, but there are times when recursion can be much more efficient from a coding perspective Recursion should always be used instead of loops when possible since it offers much more efficient use of memory
Loops and recursion solve similar problems and are just as easy to code in all cases
Given the following set and insert statements, what will the order be when traversing the set? set<string> states {"New York", "Pennsylvania", "Virginia"}; states.insert("Maryland"); states.insert("Virginia"); New York, Pennsylvania, Virginia, Maryland New York, Pennsylvania, Maryland, Virginia Maryland, New York, Pennsylvania, Virginia Virginia, Maryland, New York, Pennsylvania
Maryland, New York, Pennsylvania, Virginia
Jennifer is trying to save enough to buy some new headphones and needs to save at least 100. She writes a code to help solve her problem but finds that it runs as an infinite loop. Which of the following fixes can she apply to fix the infinite loop and get her code to run correctly?Original Code: 1. int savings = 26; 2. int weeks = 0; 3. while (saving <= 100) { 4. weeks ++; 5. savings += 10; 6. if (savings > 100) { 7. cout << "You can buy the headphones after " << weeks << " weeks." << endl; 8. savings -= 100; 9. } 10. } Change line 3 to read: while (weeks <= 100) { Move lines 6 through 9 to after the loop Change line 6 to read greater than or equals: if (savings >= 100) { Remove line 4
Move lines 6 through 9 to after the loop
What would the output of the following function for the following call:names("Zach", "Jeremy");? void names(string one, string two = "Karel", string three = "Tracy"){ cout << "Name one: " << one << endl; cout << "Name two: " << two << endl; cout << "Name three: " << three << endl; } Name one: Zach Name two: Jeremy Name three: Karel Name one: Zach Name two: Jeremy Name three: Tracy Name one: Name two: Karel Name three: Tracy Name one: Zach Name two: Jeremy Name three:
Name one: Zach Name two: Jeremy Name three: Tracy
Which of the following uses correct syntax when applying pointers to a vector?1. vector<int*> nums; int *num1 = new int(50); nums->push_back(num1); 2. vector<int> *nums; int *num1 = new int(50); nums->push_back(num1); 1 Only 2 only Both 1 and 2 Neither 1 nor 2
Neither 1 nor 2
Which of the following is NOT typically included in a node for a doubly linked list? Node id Link to the next node Link to the previous node Data fields for the node
Node id
Which of the following cannot easily be done using a queue? Access items in the front of the queue Remove items from the front of the queue Access items in the back of the queue Remove items from the back of the queue
Remove items from the back of the queue
hich of the following statements about the stack is NOT true? The stack allows data to be more flexible and store larger volumes of data. Data in the stack is typically stored in the CPU Variables such as integers are stored in the stack The stack provides quick access to variables
The stack allows data to be more flexible and store larger volumes of data.
Which statement about the util library is NOT true? When using the util library, you do not need to import iostream or declare the std namespace The util library makes it easier to read information from the console with built-in functions The util library automatically handles errors The util library allows you to set a seed for the random number generator so that the random numbers can be reproduced.
The util library automatically handles errors
Which of the following would be the best for a stack implementation? A grocery store inventory management system Undo system in a word processing application A customer service chat window A cell phone pre-order tracking system
Undo system in a word processing application
A Web browser must keep track of the sites that you have visited so that when you click the "back" button it will return you to the most recent site. Which of the following data structures has a functionality that best supports the described display of previously visited sites? Stack Vector Queue Struct
Stack
Given the following header/implementation file, what will the result be if a file includes mystery.h as an include on line 1?mystery.h: using namespace std; int add(int num1, int num2); int subtract(int num1, int num2); mystery.cpp: #include <iostream> #include "mystery.h" int add(int num1, int num2) { cout << (num1 + num2) << endl; } int subtract(int num1, int num2) { cout << (num1 - num2) << endl; } The program contains an error since the iostream library was not included in the mystery.h file. The program will crash since a safe import was not used in the header file. The program contains an error since the namespace std is not included in the mystery.cpp file. The functions will work as expected.
The functions will work as expected.
Given the following header file: #include <iostream> int readInt(string prompt){ cout << prompt; int newInput; cin >> newInput; return newInput; } Assuming this file is imported into a program on line 1, what will be the result? The readInt function will be available to be used in the program. The import will fail because the header file should be split into a header and implementation file. The import will fail because the include statement should not be included in the header file. The import will fail because the namespace is not set and the cin and cout commands need to specify the std namespace.
The import will fail because the namespace is not set and the cin and cout commands need to specify the std namespace.
What is the return value for the set insert command? The insert command returns a boolean. True if the value was added, false otherwise. The insert command returns an iterator that points to the location of the value just inserted or the location of the value if it already exists in the set. The insert command returns a pair containing both an iterator and boolean. The insert command doesn't return anything.
The insert command returns a pair containing both an iterator and boolean.
iven a map of int values m, the following is intended to create a set of the map values. Which of the following best describes the error? 1: set<int> values; 2: 3: for (map<string,int>::iterator it = m.begin(); 4: it != m.end(); it++) { 5: values.insert(it->first); 6: } The values set was not properly initialized The iterator is accessing the key on line 5, not the value. On line 5, the iterator should use a dot, . instead of an arrow, ->. The code should execute as expected Answered Continue
The iterator is accessing the key on line 5, not the value.
Given the following code, how many elements will be in the map after the code executes? map<string, int> pets; pets.insert(pair<string, int>("Karel", 9)); pets.insert(pair<string, int>("Tracy", 5)); pets["Monty"] = 1; pets["Tracy"] = 3; pets.at("Karel") = 7; pets.at("Pluto") = 6; 3 4 6 The program will throw an error.
The program will throw an error.
Given the following function void mystery(string str) { if (str.length() <= 1){ cout << str << endl; } else { cout << str[str.length() / 2]; mystery(str.substr(1)); } } What would the result be if mystery("HelloWorld") is called? WWoorrlld World WWoorrlldd dllrrooWW
WWoorrlldd
n the following recursive function, what is the base case? int mystery(int num) { if (num <= 1) { return 1; } else if (num%2 == 0) { return num * mystery(num - 2); } else { return num * mystery(num - 1); } } When num is less than or equal to 1 When num is greater than 1 When num is less than or equal to 1 or num%2 is equal to 0 When num is greater than 1 and num%2 does not equal 0
When num is less than or equal to 1
When reading in a file, how do you know when you reached the end of the file? You use a for loop to loop to the length of the file You use a while loop and loop until getline returns NULL When you try to do a getline statement, but it returns an empty value, you know you are at the end of the file When you try to do a getline statement, but it returns a fail state, you know you are at the end of the file.
When you try to do a getline statement, but it returns a fail state, you know you are at the end of the file.
Given the following code snippet, what will be printed out? string *mystery1, *mystery2; mystery1 = new string("Hello"); mystery2 = mystery1; *mystery2 = "World"; cout << *mystery1 << endl; Hello World A memory location such as 0x55a2e38212a0 The program will produce an error
World
Can you create a set of a struct? Yes, you can create them just like any other variable. Nothing extra needs to be done. No, set values need to be unique and sorted and C++ does not know how to sort a struct. Yes, but the set will not be sorted as it gets stored, making it much less efficient. Yes, as long as you define how to sort the struct using an operator overload. Answered
Yes, as long as you define how to sort the struct using an operator overload. Answered
Given the following code, what will the output be? queue<string> q; q.push("apple"); q.push("pear"); q.push("banana"); q.push(q.front()); cout << q.front() << endl; q.push("orange"); q.pop(); q.push(q.front()); q.pop(); cout << q.front() << endl; apple banana apple apple pear banana banana apple
apple banana
Which of the following is NOT a valid catch? catch (invalid_argument) catch (invalid_argument e) catch (exception const& e) catch (age) Answered
catch(age)
The following recursive function is designed to count down to zero and then display Blastoff!. What is the correct code to go into the /* Missing Code */ block? void countDown(int num) { if (num == 0) { cout << "Blastoff!" << endl; } else { /* Missing Code */ } } return countDown(num - 1); cout << num << endl; cout << num << endl; return countDown(num - 1); countDown(num - 1); cout << num << endl; cout << num << endl; countDown(num - 1);
cout << num << endl; countDown(num - 1);
A user wants to use the util.h library to generate a random double number between 10 and 20, (including 10, but not 20). Which of the following will accomplish that? double num = (randInt() % 100) / 10 + 10; double num = (randInt() % 10) + 10; double num = randDouble(10, 20); double num = (randInt() % 100) / 100 + 10;
double num = (randInt() % 100) / 10 + 10;
Which of the following will print out all for the following set: set<int> nums {1, 2, 3, 4}; for (set<int>::iterator it = nums.begin(); it != nums.end(); it ++){ cout << it < endl; } for (iterator it = nums.begin(); it != nums.end(); it ++){ cout << *it < endl; } for (set<int>::iterator it = nums.end(); it != nums.begin(); it --){ cout << *it < endl; } for (set<int>::iterator it = nums.begin(); it != nums.end(); it ++){ cout << *it < endl; }
for (set<int>::iterator it = nums.begin(); it != nums.end(); it ++){ cout << *it < endl; }
Given the following vector: vector<int> v {1, 2, 3, 4, 5, 6}; Which of the following loops will create an infinite loop? for (vector<int>::iterator itr = v.begin(); itr != v.end(); itr += 1) { cout << *itr << endl; } for (vector<int>::iterator itr = v.begin(); itr != v.end(); itr += 2) { cout << *itr << endl; } for (vector<int>::iterator itr = v.begin(); itr != v.end(); itr += 3) { cout << *itr << endl; } for (vector<int>::iterator itr = v.begin(); itr != v.end(); itr += 4) { cout << *itr << endl; }
for (vector<int>::iterator itr = v.begin(); itr != v.end(); itr += 4) { cout << *itr << endl; }
Given the following struct: struct team { string name; int wins, losses; }; If a vector of teams called league was created, which of the following would print the team name with the most wins? int maxWins = 0; for (team t : league) { if (t.wins > maxWins) { maxWins = t.wins; } } cout << maxWins << endl; int maxWins = 0; int maxWinIndex = -1; for (int i = 0; i < league.size(); i++) { if (league.wins[i] > maxWins) { maxWins = league.wins[i] ; maxWinIndex = i; } } cout << league.name[maxWinIndex] << endl; int maxWins = 0; int maxWinIndex = -1; for (int i = 0; i < league.size(); i++) { if (league[i].wins > maxWins) { maxWins = league[i].wins; maxWinIndex = i; } } cout << league[maxWinIndex].name << endl; int maxWins = 0; int maxWinIndex = -1; for (int i = 0; i < league.size(); i++) { if (league[i].wins > maxWins) { maxWins = league[i].wins; cout << league[i].name << endl; } }
int maxWins = 0; int maxWinIndex = -1; for (int i = 0; i < league.size(); i++) { if (league[i].wins > maxWins) { maxWins = league[i].wins; maxWinIndex = i; } } cout << league[maxWinIndex].name << endl;
Which of the following functions does not overload this function? void mystery(int a, int b) { ... } void mystery(double a, double b) { ... } void mystery(int a, int b, int c) { ... } double mystery(double a, double b) { ... } int mystery(int a, int b) { ... }
int mystery(int a, int b) { ... }
Which of the following will create a map with default values? map<int, double> prices {{1, 5.99}, {2, 9.99}, {3, 6.99}}; map<int, double> prices [{1, 5.99}, {2, 9.99}, {3, 6.99}]; map<int, double> prices {(1, 5.99), (2, 9.99), (3, 6.99)}; map<int, double> prices {[1, 5.99], [2, 9.99], [3, 6.99]};
map<int, double> prices {{1, 5.99}, {2, 9.99}, {3, 6.99}};
Which of the following is NOT a valid way to add to a map? map<string, string> m {{"A", "Apple"}}; map<string, string> m; m.insert("A", "Apple"); map<string, string> m; m.insert(pair<string,string> ("A", "Apple")); map<string, string> m; m["A"] = "Apple";
map<string, string> m; m.insert("A", "Apple");
Given the following code snippet int num1 = 10; int *num2; Which of the following assignment statements will NOT produce an error? num2 = num1; num2 = *num1; &num2 = num1; num2 = &num1;
num2 = &num1;
Which of the following will create an output file called output.txt and write C++ Master to it? ofstream out; out.open("output.txt"); out << "C++ Master"; out.close(); ofstream out; out = "output.txt"; out << "C++ Master"; out.close(); ofstream out; out = "output.txt"; out.open(); out << "C++ Master"; out.close(); ofstream out; out.open("output.txt"); out >> "C++ Master"; out.close();
ofstream out; out.open("output.txt"); out << "C++ Master"; out.close();
Which of the following will correctly swap the two values of a pair? pair<int, int> p {0, 1}; int t = p.one; p.one = p.two; p.two = t; pair<int, int> p {0, 1}; int t = p->one; p->one = p->two; p->two = t; pair<int, int> p {0, 1}; int t = p[0]; p[0] = p[1]; p[1] = t; pair<int, int> p {0, 1}; int t = p.first; p.first = p.second; p.second = t;
pair<int, int> p {0, 1}; int t = p.first; p.first = p.second; p.second = t;
Which of the following is NOT a valid call for the readInt function? readInt("Please enter a number between 1 and 5: ", 1, 5); readInt(1, 5); readInt("Please enter a number between 1 and 5: ", "Try again: "); readInt();
readInt("Please enter a number between 1 and 5: ", 1, 5);
Which of these would be a proper functional prototype for this function: string sayHello(string name) { return hello + name; } string sayHello(string); sayHello(string); string sayHello(string name); sayHello();
string sayHello(string name);
Given the following while loop that traverses a linked list while(nextItem != NULL){ cout << nextItem->data << endl; nextItem = nextItem->next; } Which of the following functions would print the same code? void mystery(item *head){ if (head != NULL){ mystery(head->next); cout << head->data<<endl; } } void mystery(item *tail){ if (tail != NULL){ mystery(tail->prev); cout << tail->data<<endl; } } void mystery(item *head){ if (head != NULL){ cout << head->data<<endl; mystery(head->next); } } void mystery(item *tail){ if (tail != NULL){ cout << tail->data<<endl; mystery(tail->prev); } }
void mystery(item *head){ if (head != NULL){ cout << head->data<<endl; mystery(head->next); } }
Consider the following function, sumTo10, that traverses a 2D vector checking to see if the sum of all integers in each vector are equal to 10. The function will return true if all vectors within the 2D vector sum to 10, and false otherwise. bool sumTo10(vector<vector<int> > nums) { for(vector<int> row : nums) { int sum = 0; int counter = 0; while (counter < nums.size()) { sum += row[counter]; counter++; } if(sum != 10) { return false; } } return true; } For example, the 2D vector {{2,8}, {4, 6}} would return true because the content of each vector sums to 10. The function, however, doesn't always work as intended. Which of the following 2D vectors input values does sumTo10 not work for? {{5, 3}, {3, 7}} {{6, 1, 3}, {2, 1, 7}} {{5, 4, 1}, {2, 2, 6}, {3, 4, 3}} {{5, 5}, {2, 8}}
{{6, 1, 3}, {2, 1, 7}}