1. Which of the following is an illegal use of the keyword "auto"?
auto my_var = std::string("abcd");
auto & my_stream = std::cout;
auto i;
auto my_str = std::string("Test.");
2. What is the output from the following code fragment? char var = 'a'; { int var = 10; std::cout << var; } std::cout << var << std::endl;
char var = 'a';
{
int var = 10;
std::cout << var;
}
std::cout << var << std::endl;
10a
a
a10
10
3. Which of the following is NOT a good reason a to define a member variable in the private section of a class definition?
4. You have two variables 'day' and 'temperature'. You want to stop a loop if either day becomes greater than 365 or if temperature goes below zero. You want to keep looping as long as neither of those conditions triggers. What should your loop look like?
while (day <= 365 || temperature >= 0) {
while (day <= 365 && temperature >= 0) {
while (day > 365 && temperature < 0) {
while (day > 365 || temperature < 0) {
5. How is std::npos used in the standard library?
std::npos
std::ostream
6. 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;
7. Which of these numeric types is represented using the fewest bytes in C++?
float
uint64_t
char
double
long
8. Assuming that the variable cost is set to 5.1, how can you print it as "$5.10"?
cost
std::cout << std::setprecision(2) << std::fixed << '$' << cost;
std::cout << std::as_dollars << cost;
std::cout << '$' << std::insert_dot(cost*100, 2);
std::cout << $cost;
9. What does the following line of code do? assert(x == 10);
assert(x == 10);
x
10. If we have an array defined as "int my_array[3117];", one way to get a pointer to index 42 is "&(my_array[42])". What is another way?
int my_array[3117];
&(my_array[42])
my_array{42}
my_array & 42
my_array + 42
*(my_array[42])
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.