1. Which of the following is NOT a correct way to indicate a newline at the end of "Hello World!"?
std::cout << "Hello World!"; std::cout << "\n";
std::cout << "Hello World!" << std::endl;
std::cout << "Hello World!\n";
std::cout << "Hello World!\endl";
2. Which of these numeric types is represented using the fewest bytes in C++?
long
char
float
double
long long
3. If I have an std::string called name, with the contents "John Wilkes Booth", and another called middle, which of the following techniques will set middle to be "Wilkes"?
name
middle
middle = name.substr(5,6);
middle = name[5]+name[6]+name[7]+name[8]+name[9]+name[10];
middle = (name + 5).resize(6);
middle = name[5:11];
middle = std::extract(name, 5, 11);
4. 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;
5. What error is in the following function? template <typename T> T & Multiply(const T in1, const T in2) { return in1 * in2; }
template <typename T>
T & Multiply(const T in1, const T in2) {
return in1 * in2;
}
const
6. You want to execute code if either var1 is true or var2 is true, but not if they are both true. Given that both variables are type bool, three of the following techniques work; which one will NOT work?
var1
var2
bool
if ( var1 ^^ var2) {
if ( (var1 || var2) && !(var1 && var2) ) {
if ( var1 != var2) {
if ( (var1 && !var2) || (var2 && !var1) ) {
7. What is the output of the following program: std::map<int, std::string> star_map; star_map[0] += "*"; star_map[2] += "*"; star_map[3] += "*"; star_map[0] += "*"; std::cout << star_map[0] << "-" << star_map[1] << "-" << star_map[2];
std::map<int, std::string> star_map;
star_map[0] += "*";
star_map[2] += "*";
star_map[3] += "*";
std::cout << star_map[0] << "-" << star_map[1] << "-" << star_map[2];
--***
**--*
*--**
8. What is the output from the following code fragment? char var1 = 'b'; if (var1 == 'b') { int var2 = 10; } std::cout << var1 << var2 << std::endl;
char var1 = 'b';
if (var1 == 'b') {
int var2 = 10;
std::cout << var1 << var2 << std::endl;
b10
10b
10
9. Given the following templated function: template <typename T> void PrintBoth(T in1, T in2) { std::cout << in1 << ", " << in2 << std::endl; } Which function call will cause a compilation error?
void PrintBoth(T in1, T in2) {
std::cout << in1 << ", " << in2 << std::endl;
PrintBoth(nullptr, nullptr);
PrintBoth("12", "34");
PrintBoth("12", 34);
PrintBoth(12, 34);
10. If you make a function call TestFunction(91), which version of the function will be called?
void TestFunction(double param);
void TestFunction(char param);
void TestFunction(int param1, int param2=0);
void TestFunction(std::string param);
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.