C++ Basics Flashcards

1
Q

What is the auto keyword? Provide an example.

A

Auto can be used during assignment to automatically set a variable to the type of it’s assigned variable.

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

What are the three ways to assign a value to a variable (e.g. int)?

A

a = 5;
b(3);
c{2};

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

Can you create a new type?

A

Yes, with the typedef keyword

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

What is the alternative way of writing false?

A

0

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

What is the alternative way of writing true?

A

1 (or ‘non-zero’)

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

What is the structure of a C++ array?

A

type name [size]

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

What is a null character in a string?

A

‘\0’

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

How do you get the memory address of that variable? E.g. int x = 7;

A

&x

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

Why do we need pointers?

A

-dynamically allocate memory

–you can write programs that can handle unlimited amounts of memory-allow a function to modify a variable passed to it

-easier to pass around the location of a huge amount of data than passing the data itself

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

How do you define a pointer?

A

*p;

int *y;

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

How can we get the content of the memory address pointed to by the pointer?

A

By using the dereference operator ‘*’

int x = 10;
//variable x = 10
int* y = &x; 
//pointer variable y = Address of x
int z = *y;
//z = “value pointed to by y” = x, i.e. z = x;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the l-value?

A

The expression that refer to a memory location e.g. a variable

Can be both side

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

What is the r-value?

A

The data stored in some memory address (can only be on the right side)

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

What are the compound data types in C++?

A

Any type that isn’t a primitive.

Arrays
Char sequences
Strings
Pointers
Dynamic memory
Data structures
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can pointers be declared as constants?

A

Yes but can’t change memory address

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

When casting to a lower size type - what happens to the value?

A

You chop the size of the lower type on the higher type from the LSB up to the size of the type

0000 0001 0100 0000 (short 2 bytes)
                    0100 0000 (char 1 byte)
17
Q

What is the difference between a class and structs?

A

Struct members are public by default

18
Q

Why use an enum?

A

Restricted set of variables

19
Q

How do access the attribute ‘name’ from a ‘Person’ struct Declared ‘Person person’?

A

person.name;

20
Q

How do access the attribute ‘name’ from a ‘Person’ struct Declared ‘Person * person = new Person’?

A

person->name;

21
Q

Is it possible to have arrays of structs?

22
Q

Where would you use an array of structs?

A

A database

23
Q

In OOP what are three things to consider with structs?

A

Member accessibly
Code visibility
Encapsulation

24
Q

What are the five program memory components?

A
Code segment
BSS Segment
Data Segment (DS)
Heap
Stack
25
What is contained in the BSS segment?
all uninitialized, global and static
26
What is contained in the DS segment?
Initialized variables
27
What are the three types of memory allocation in C++?
Static Automatic Dynamic
28
What is static memory used for?
For static and global variables
29
What is automatic memory used for?
For local variables and function params
30
What is static memory used for?
Heap memory
31
For function overloading, what part of the signature isn't considered for uniqueness?
the return type
32
What are the types available for integral promotion to int?
bool, char and short
33
What value is used for the return type in a template function?
The left most type
34
What issue may arise from template functions with different types?
Might end up with a type you weren't expecting
35
When do you use virtual?
To declare an overridable method in a parent class