Exam 1 Flashcards

1
Q

Who created c++ and when?

A

1979: Bjarne Stroustrup began work on “c with classes.” Renamed to c++ in 1983. standardized by ISO in 1998.

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

What is the difference between g++ and c++?

A

g++ is the compiler, c++ is the language

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

true or false: all c programs are valid c++ programs, but not the other way around.

A

true

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

true or false: main is a method

A

false. main is a function

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

what is a method?

A

a function inside of a class

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

what do you need in order to access IO facilities?

A

include <iostream></iostream>

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

what do a main function’s return values mean?

A

0 means success
>0 means failure
<0 means something weird happened

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

true or false: variable size is standard across implementations

A

false. size is determined by implementation

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

what does the const quantifier do?

A

it makes it so you cannot change a variable

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

what does the constexpr quantifier do?

A

it is a compile-time const

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

what does the static quantifier do?

A

it means the variable has a longer than function lifetime, but makes it private if it’s a global variable.

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

true or false: you must initialize variables declared as auto

A

true

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

what is cin?

A

standard input

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

what is cout?

A

standard output

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

what is cerr?

A

standard error

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

what operator is used for output with cout?

A

the insertion operator: «

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

what does cout &laquo_space;endl do?

A

it flushes terminal output

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

what operator is used for input with cin?

A

the extraction operator:&raquo_space;

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

how do you read an entire line from a file?

A

use getline. params are file name & what you will call the line

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

true or false: you can read a string with&raquo_space;

A

True. this will read a whitespace delimited string

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

how do you read a raw char?

A

use get(). you can also use&raquo_space; to read a whitespace delimited char

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

what does unget() do?

A

it will back up by one char

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

what does peek() do?

A

it will get and unget a char to peek at it

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

how do you get the stream state?

A

test the state by evaluating the string as a bool, since cin&raquo_space; n returns a reference to n

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

what does eof do?

A

it tells us if the previous read hit the end of the file

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

what do you need to do to use vectors?

A

include <vector></vector>

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

what is a vector?

A

a stretchy array. no fixed size

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

how do you declare a vector?

A

vector<int> v; or
vector<int> v(10);</int></int>

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

what does v.size() do?
v is a vector

A

returns the current length as a size_t

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

what does v.push_back() do?
v is a vector.

A

adds an element to the end of a vector

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

what does v.pop_back() do?
v is a vector

A

removes an element from the end

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

what does v[i] or v.at(i) do?
v is a vector

A

indexing

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

when does a vector grow in size?

A

when given a size_t ctor arg, when using push_back(), or when using resize()

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

what do you need to do to use c++ strings?

A

include <string></string>

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

what are literals?

A

constants.

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

what does a c++ string literal look like?

A

“foobar”s

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

what is string pasting?

A

string pasting is done by the compiler. two adjacent string literals are merged into one at compile time.

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

What does a raw string look like?

A

a raw string starts with R”( and ends with )”

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

how do you compare c++ strings?

A

convert them to c strings and use strcmp()

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

true or false: if a variable is const, it is read-only

A

False. it doesn’t mean that it’s read-only, it just means that YOU can’t change it.

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

what is a method?

A

a method is a function inside of a class

42
Q

true or false: in c++ functions, trailing arguments can have default values

A

true

43
Q

true or false: in c++ function overloading, types & return value across multiple functions must be the same.

A

false

44
Q

is c++ a pass by value or pass by reference language?

A

pass by value.

45
Q

what does pass by value mean?

A

copies are made of the arguments, and the copies are passed to a function or method. if we want to change the argument, we must instead pass by reference.

46
Q

how can you pass a read-only reference?

A

use const

47
Q

true or false: arrays are always passed by reference

A

true

48
Q

how do you dynamically allocate memory in c++?

A

new and delete

49
Q

what happens if you call new and delete incorrectly, or you call the wrong one?

A

undefined behavior

50
Q

what happens if you dont call delete?

A

memory leak

51
Q

what happens if you call delete more than once?

A

undefined behavior

52
Q

what is a dangling pointer?

A

a pointer that has been deleted. the data still exists, but it is not yours.

53
Q

true or false: accessing the value of a deleted pointer is undefined behavior

A

true

54
Q

what does the ampersand mean when used in an expression in c++?

A

it means the “address of”

55
Q

what does an ampersand mean when used in a definition in c++?

A

it means “i am a reference”

56
Q

what is a null pointer?

A

a pointer that is not intended to point to anything useful

57
Q

true or false: a reference is impermanent and can be changed.

A

false. a reference is permanent

58
Q

which is faster, call by reference or call by value?

A

call by reference

59
Q

what is .size()

A

how many elements the container currently contains

60
Q

what is .capacity()

A

how many elements are allocated to the container

61
Q

what is .max_size()

A

biggest possible .size() on this computer

62
Q

what does .reserve() do?

A

changes the allocated size

63
Q

what does .resize() do?

A

changes the size. if it had more than n elements, we lose some. if it had less, we gain some.

64
Q

true or false: sizeof(), size(), and .size() are the same

A

false.
sizeof() is a compile-time function that returns the size of an object in bytes
.size() is a container method that returns the number of elements
size() is a free function that is identical to .size() but also works on C arrays

65
Q

what is heckendorn’s rule?

A

never emit a constant error message.

66
Q

What information should be included in an error message?

A

program name
file name
operation attempted
error code via errno
text error description strerror()

67
Q

what is variable scope?

A

visibility; which code can access the variable?

68
Q

what is variable lifetime?

A

duration; when the variable is created and destroyed

69
Q

what does it mean to make a global variable static?

A

it changes the scope of the global variable to be only the current file

70
Q

what does it mean to make a local variable static?

A

it increases the lifetime of a local variable

71
Q

what do you need to do in order to use numeric_limits?

A

include <limits></limits>

72
Q

how do classes and structs differ?

A

methods, data, and inheritance are by default private for a class and public for a struct

73
Q

what do attributes do?

A

they add extra information to a variable, function, type, or statement

74
Q

what does the [[deprecated]] attribute mean?

A

it means support will be withdrawn soon- stop using it

75
Q

what does the [[fallthrough]] attribute mean?

A

indicates that a switch statement “falling through” to later cases is ok/intentional

76
Q

what does the [[maybe_unused]] attribute mean?

A

it tells the compiler not to complain that a type, variable, argument, or function isn’t being used

77
Q

what are some alternatives to using the [[maybe_unused]] attribute?

A

omit the name of the argument/variable
if the name has mnemonic value, place it in a /* comment */

78
Q

what does the [[nodiscard]] attribute mean?

A

the return value must be used

79
Q

what does the [[noreturn]] attribute mean?

A

it indicates that the function does not return.

80
Q

what do the -O flag and -O1 flag mean in g++

A

general optimization

81
Q

what does the -O2 flag mean in g++?

A

more optimization than O and O1

82
Q

what does the -O3 flag mean in g++?

A

the most optimization possible

83
Q

what does the -flto flag mean in g++?

A

perform link-time optimization; tells the compiler to perform optimization between the individual .o object files

84
Q

what does the special class variable “this” do?

A

it is a pointer to the current object

85
Q

how do you make a class work for output?

A

define the &laquo_space;operator properly

86
Q

true or false: methods must be declared and defined inside of the class

A

false. methods are declared in the class, but must be defined outside of the class

87
Q

what three types of protection can class members have?

A

public, private, and protected

88
Q

what does it mean for a class member to be public?

A

anyone can access it

89
Q

what does it mean for a class member to be private?

A

nobody except other methods of this class may access them

90
Q

what does it mean for a class member to be protected?

A

they can be accessed by this class and by immediately derived classes.

91
Q

what does it mean for a class to declare something as its frend?

A

it allows the friend class, function, or method to access private data

92
Q

how would you declare a class named Bar as a friend?

A

friend class Bar;

93
Q

how would you declare a method named double zulu(int) const from the class Zip as a friend?

A

friend double Zip::zulu(int) const;

94
Q

how would you declare a function named int alpha() as a friend?

A

friend int alpha();

95
Q

true or false: class A can request friendship from class B

A

false. friendship is offered, not demanded

96
Q

true or false: if class A is class B’s friend, then class B is class A’s friend.

A

false. friendship is not always symmetric

97
Q

true or false: if class A and B are friends, and B and C are friends, then A and C are friends.

A

false. friendship is not transitive

98
Q

true or false: if class A and B are friends, and class C is A’s child, then B and C are friends.

A

false. friendship is not inherited

99
Q

true or false: if class A is class B’s child, then class A and class B are friends.

A

false. inheritance does not imply friendship.

100
Q

true or false: a static class member variable has class scope

A

true

101
Q

what level of protection can a class static variable have?

A

public, protected, or private

102
Q

true or false: a static class variable can be called without an instance of the class.

A

true