C++ Flashcards

1
Q

What is the keyword to add to prevent reassignment?

A

const

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

How to instantiate an array? How to access elements in the array?

A

int array[ARRAY_CAPACITY];
array[INDEX];

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

What are statements that start with #?

A

Preprocessor directives

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

What happens to the preprocessor directives when we compile a C++ program?

A

When we compile, the first step is preprocessing. The preprocessor scans the cpp file for any lines starting with #. It replaces these lines with the actual content of the included file. The output of the preprocessor is a modified version of the source code.

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

Does the preprocessor need to understand C++ semantics?

A

No, all it does is text-based manipulations. It just modifies the code before the compiler sees it.

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

Why is it beneficial to have a header file separate from the source code?

A

The source code will be compiled into a library .o. It allows other programs to use the functions by including the header file only. Since the library implementation is already compiled into an object file, it saves time during compilation.

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

What is a library?

A

A collection of precompiled object files packaged together.

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

What does it mean to compile into a library?

A
  1. Compile a .cpp file into an object file
  2. Package those object files into a library
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why is having a library useful?

A

The library can be reused across programs. The developer does not have to distribute the source code and recompile every time, only the library file.

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

What is the difference between a static library vs a dynamic/shared library?

A

When you link a program with a static library, all the required code is copied into the final executable. When you use a dynamic library, the library code is not copied. The executable refers to the library, which is loaded into memory at runtime.

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

Would there be errors if you include the same header file multiple times into your program (either directly or indirectly)?

A

Yes

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

What is the directive to prevent a header file from being included multiple times in one cpp file?

A

pragma once

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

What are include guards? How to write them?

A

ifndef MYHEADER_H

They are a traditional way to prevent multiple inclusions.

#define MYHEADER_H


#endif

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

What does a cpp file need to have to be compiled into an executable?

A

It needs a int main(), if not, it can only be compiled into an object file.

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

Does a main() always return an int?

A

Yes, it is always defined as int main() and returns an int. The return value serves as an exit status, so it always expects 0, indicating a successful execution of the program.

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

Give a version of int main() that takes arguments.

A

int main(int argc, char* argv[])

argc is the argument count (includes program name)
argv is argument vector

17
Q

What are variables defined in functions, classes, elsewhere called respectively?

A

Functions: local
Classes: member
Elsewhere: global

18
Q

What are functions defined in classes, elsewhere called?

A

Classes: member
Elsewhere: global

19
Q

What is static used for local variables, global variables, functions, classes?

A

Local variables: makes the variable only accessible within the function and the value persists between function calls
Global variables: makes the variable local to the file
Functions: restrict the function to file scope (only visible to other functions within the same file)
Classes: class scope (shared across all objects of the class)

20
Q

How to define a namespace?

A

namespace i_am_lia {

}

21
Q

How to access variables outside of the namespace?

A

i_am_lia::sleeping();
using namespace i_am_lia;

22
Q

Is using namespace used in production code?

A

No

23
Q
A