C++ Fundamentals Including C++ 17 Flashcards

1
Q

What does “shortcutting” refer to for the && and || operators?

A

If the result can be determined after evaluating only the first argument, the second will not be evaluated. (Related Clip: Comparisons)

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

A member function should be marked as const…

A

… when you first write it, unless it changes a member variable. (Related Clip: Member Functions)

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

Who makes the decision about adding new features to the C++ language?

A

An International Standards Organization committee. (Related Clip: Standardization)

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

Which of these statements about references and polymorphism is correct?

  • Only const references are polymorphic.
  • References support polymorphism more strongly than pointers do.
  • References do not support polymorphism.
  • References support polymorphism in the same way as pointers.
A

-References support polymorphism in the same way as pointers. (Related Clip: References and Inheritance)

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

What does the break statement mean in a while loop?

A

Control should exit the loop and continue after it. (Related Clip: While)

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

What does this line of code do?

std::cout &laquo_space;“Type your name” &laquo_space;std::endl;

A

Prints “Type your name” (without the quotes) on the screen and moves the cursor down one line. (Related Clip: Demo: Visual Studio Hello World)

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

Why would any programmer create an object on the free store, also called the heap?

A

So that the memory can stay in scope after the creating function has ended. (Related Clip: The Free Store)

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

When moving data of one type into a variable of another type, when do checks on the coversion validity occur?

A

When your code is compiled. (Related Clip: Demo: Fundamental Types)

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

If you want to be sure that a developer doesn’t forget to do something, such as closing a file or connection, how can you make sure it gets done?

A

Put the closing call into the destructor for a class representing the file or connection. (Related Clip: Scope)

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

A template may count on the types you us it with to implement a particular member function or operator overload. Which technique will not help you to discover the constraints on your type?

  • Reading documentation for the template
  • Understanding the template error messages when you try to use it
  • Reading the source code of the template
  • Looking at the constraints in the declaration of the template.
A

-Looking at the constraints in the declaration of the template. (Related Clip: Template Specialization)

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

If you declare a function but do not implement it, and code tries to call it, what kind of build error will occur?

A

Linker error. (Related Clip: Demo: Understanding Error Messages)

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

After this loop, what is the value of y?

int y = 0;
for (int i = 0; i<5; i = i + 1)
{
   y = y + i;
}
A

10 (Related Clip: For)

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

When your code relies on classes and functions in a namespace, the best way to minimize your typing while preserving your program’s intent is to:

  • add using statements referring to the entire namespace, e.g. using namespace std;
  • always type the full name including the namespace, e.g. std::cout , whenever referring to the name
  • You should not use code form a different namespace
  • add using statements referring to the specific name within the namespace, e.g. using std::cout;
A

-add using statements referring to the specific name within the namespace, e.g. using std::cout; (Related Clip: Demo: Namespaces)

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

If you write a class with a virtual function in it, what other function should be marked as virtual?

A

The destructor (Related Clip: Demo: Indirection and Inheritance)

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

If you delcare an integer pointer like this:

int const * p;

Then attempt to compile these two lines:

p = &something;
*p = 2;

Will they compile?

A

The first, which changes p, will. The second, which dereferences p, will not. (Related Clip: Const with Indirection)

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

When you use a template function in your code, the work of stamping out the version of the function you need is done when?

A

at compile-and-link time. (Related Clip: Templates)

17
Q

Which of these is the correct sequence when constructing an object that is an instance of a derived class?

  • The derived constructor runs and then the base constructor
  • The base constructor runs and then the derived constructor.
  • Only the derived constructor runs.
  • All the initializers run and then the derived constructor.
  • The derived constructor must call the base constructor to make it run.
A

-The base constructor runs and then the derived constructor. (Related Clip: Demo: Inheritance)

18
Q

Building in C++ involves compiling and linking. What is the difference between them?

A

Each source file is individually compiled and the results are linked together into a single output file. (Related Clip: Building)

19
Q

Templates allow you to write code once, and use it for any type, such as int, Employee, or Person. What important C++ feature do they retain while working across type?

A

Type safety (Related Clip: Template Classes)

20
Q

include

What does this line of code do?

A

Makes the string class in the Standard Library available to your code. (Related Clip: Introduction)

21
Q

How do you indicate that a C++ function, other than a constructor or destructor, is like a subroutine, not returning a value.

A

Use the return type of void. (Related Clip: Demo: Member Functions)

22
Q

If I delcare two integers like this:

int const a = 10;
const int b = 10;

In what way are a and b different in ther const-ness?

A

There is no difference other than style. (Related Clip: Const After or Before?)

23
Q

Two fo the three special functions referred to in the Rule of Three are the copy constructor and the copy assignment operator. For a class called Person these would be

Person::Person(Person const& p)

and

Person::operator=(Person const& p)

What is the third?

A

The destructor.

Person::~Person()

(Related Clip: Manual Memory Management)

24
Q

Which of these is a fundamental type in C++?

  • date
  • object
  • string
  • boolean
A

-boolean (Related Clip: Fundamental Types)

25
Q

How do your write a conditional expression that means “a and b are not equal”?

A

a != b

Related Clip: Comparisons

26
Q

Why would an operator overload for a class classed Date be a free function instead of a member function of the Date class?

A

It handles the case where something that is not a Date is on the left of the operator; e.g. 3 + tomorrow

(Related Clip: Writing an Overload)

27
Q

Which of these is not a difference between older enumerations (enum) and the new scoped enumerations (enum class) ?

  • An enum member can be referred to by its name alone, but for an enum class you must use a full name including the name of the enum class.
  • Two different scoped enumerations can have elements with the same name; names in an enum must be unique across all enums.
  • The compiler can assign values to the members of an enum class but they must be provided for enums.
  • The underlying data type of enum is always int but can be other types for an enum class
A
-The compiler can assign values to the members of an enum class but they must be provided for enums.
(Related Clip: Enumerations)
28
Q

When you use dynamic_cast<> to cast something to an incompatible type, what happens?

A

it returns nullptr (Related Clip: Casting and Indirection)

29
Q

When a function takes a parameter by reference…

  • The application will run more slowly
  • The function is called a reference function and should also return a reference
  • changes to that parameter in the function also change the value that was passed to it.
  • it must always be a const reference to prevent compiler errors.
A

-changes to that parameter in the function also change the value that was passed to it. (Related Clip: Free Functions)

30
Q

Which of these is not a keyword that is part of a switch construct?

  • default
  • case
  • break
  • select
A

-select (Related Clip: Switch)

31
Q

A memory leak occurs when…

A

-memory has been allocated with new, but no pointer to that memory is in scope to use with delete. (Related Clip: Manual Memory Management)

32
Q

Describe what is Liskov substitutibility? Give an example.

A

The ability for functions to work with objects passed by reference that are not instances of the required type, but instead are instances that are derived from a base class of that required type. The “is a” relationship is respected.

Suppose tweeter.h is derived from Person.h, a function that accepts a Person instance passed by reference can still accept a tweeter instance and access the Person methods in the base class. (Related Clip: References and Inheritance)