Questions seen Flashcards

1
Q

When applied to a built-in array or a std::array. What does the non-member function end() return?

A. A pointer to the element before the last element.
B. A pointer to the last element.
C. A pointer to the element just beyond the last element.
D. nullptr

A

C. A pointer to the element just beyond the last element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In a container such as an std::vector. What does the iterator returned by a call to end() point to?

A. The last element in the container.
B. The element before the last elemenet in the container
C. The end() function cannot be applied to a std::vector.
D. The element after the last element in the container.

A

D. The element after the last element in the container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

p is delcared as a const pointer. What will happen when you try to modify the content of p?

int main(){
int some_num = 12;
int *const p = &some_num;
(*p)++;
std::cout << *p << std::endl;
}

A. The code will compile without any errors, but it will crash when accessing the pointer.
B. The code will fail to compile because const variable can not be assigned a value.
C. The code will compile without any errors and display 13 on the console.
D. The code will compule and execute without errors and display the address of the pointer on the console.

A

C. The code will compile without any errors and display 13 on the console.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an abstract class?

A. A class that has one or more pure virtual functions.
B. A derived class that doesn't implement the virtual functions of the base class.
C. A class that is defined with abstract specifier.
D. A class that has one or more virtual functions.
A

A. A class that has one or more pure virtual functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How could you replace this const float with a #define, maintaining the same value and type?

A. #define PI 3.14159
B. #define PI = 3.14159
C. #define PI 3.14159f
D. #define float PI = 3.14159

A

C. #define PI 3.14159f

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In the following code, Car derives from Vehicle. When the code is executed the output is “In Vehicle” on the console. What must be changed in order to print “In Car” when show_vehicle(c) is called?

class Vehicle {
     public: void show() const {
     std::cout << "In Vehicle"  << std::endl; }
};
class Car: public Vehicle{
     public:  virtual void show() const {
     std::cout << "In Car"  << std::endl; }
};
void show_vehicle(const Vehicle& v) {
   v.show();  }
int main(){
 Car c;
 show_vehicle(c);  }

a. Vehicle::show() must include override specifier.
b. Car::show() must not be a virtual function.
c. Car::show() must include override specifier.
d. Vehicle::show() must be a virtual function.

A

d. Vehicle::show() must be a virtual function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What standard library function can be used to obtain an rvalue reference bound to an lvalue?

a. std::move()
b. std::bind()
c. std::lvalue()
d. std::rvalue()

A

a. std::move()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When overloading a binary operator as a non-member function, how many explicit operands does the function take?

a. 3
b. 2
c. 1
d. 0

A

b. 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A programmer defined this base and derived class:

class Base { virtual int method() { return 0; } };
class Derived : Base { int method() { return 1; } };

However, the following test fails to compile:

int main() {
    std::unique_ptr ptr = std::make_unique();
}
The compiler prints the following message:
error: conversion from 'unique_ptr' to non-scalar type 'unique_ptr' requested
Why is this error occurring?

a. Since inheritance defaults to private, the compiler can’t tell that Derived inherits from Base at this scope.
b. The compiler can’t tell that Derived inherits from Base at this scope, because Derived’s method isn’t marked with override.
c. Derived is not actually inheriting from Base, because inheritance has not been specified using the virtual keyword.
d. Derived is inheriting from Base correctly, but the assignment is not allowed because each type has a different size.

A

a. Since inheritance defaults to private, the compiler can’t tell that Derived inherits from Base at this scope.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How can you create a move-only type?

a. Use the [[move_only]] attribute.
b. Implement the move operations that will implicitly delete the copy operations.
c. Make the class inherit from the standard move_only template.
d. Mark the class with the move_only qualifier.

A

b. Implement the move operations that will implicitly delete the copy operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

You have a vector of integers with duplicates:
vector nums {4, 1, 3, 2, 3, 4, 6, 5};
How would you remove the duplicate integers?

a. std::sort(nums);
std: unique(nums);

b. std::sort(nums.begin(), nums.end());
auto e = std:unique(nums.begin(), nums.end());
nums.erase(e, nums.end());

c. std::sort(nums.begin(), nums.end());
std: remove_dup(nums.begin(), nums.end());

d. std::sort(nums.begin(), nums.end());
auto e = std:find_dup(nums.begin(), nums.end());
nums.erase(e, nums.end());

A

b. std::sort(nums.begin(), nums.end());
auto e = std:unique(nums.begin(), nums.end());
nums.erase(e, nums.end());

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What serves as a default constructor?

a. Only a constructor whose arguments are all defaults
b. Only a constructor with an empty (void) argument list
c. Any constructor that can be called with no arguments (there can be default arguments)
d. Only the one supplied by the compiler

A

c. Any constructor that can be called with no arguments (there can be default arguments)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

For this code the user enters an input of string “abc”. Unfortunately this causes the std::cin object to enter error state. What do you need to do in order to use std::cin object again to read val2?

int main() {
 int val1, val2;
 std::cout << "Enter an integer: ";
 std::cin >> val1;
 std::cout << "Enter another integer: ";
 std::cin >> val2;
 std::cout <<  val1  << val2 << std::endl; }

a. The std::cin object has to be cleared using std::cin.clear() before it can be used again.
b. The std::cin object cannot be reliably used to input integer values. Only string values should be inputted using std::cin. The entered string must be converted to an integer if required.
c. The std::cin object can be brought back to the normal state by calling std::cin.fail() function.
d. The std::cin object can be brought back to the normal state by calling std::cin.clear(std::cin.rdstate()) function.

A

a. The std::cin object has to be cleared using std::cin.clear() before it can be used again.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly