c++ for java programmers tutorial series quiz Flashcards

(25 cards)

1
Q

dynamic vs static memory allocation

A

static memory allocation is done at compile time, before program is run..
dynamic memory allocation is done DURING EXECUTION.

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

get the address of variable “item”

A

&item

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

int *myVar;

A

declares a pointer called “myVar”;

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

pointer

A
  • stores an address in memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

int *p = &x;

A

sets pointer p to the address of x

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

arrays in c++ differences from java

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

pass by value

A

pass in a copy of the object (this is the default)

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

pass by reference

A

pass in a reference–the address the object is at

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

What does this do?
int p* = &cat;
p* = 7;

A
  1. sets pointer p to be the address of cat
  2. deferences p, setting the thing at p to 7.
  3. now, cat is 7
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Function prototype

A

the “signature” of a function placed at the top of a file (to get around the fact that a function has to have “seen” the other function above it in order to reference it

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

If you do NOT initialize a variable, what could happen?
example
int cat;

A
  1. it will assign a pointer for this at some memory location
  2. accessing cat will now give you whatever was last written to that location!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

true/false: c++ will throw an error if you index out of bounds in an array

A

false! it will allow you to access that

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

an array in java is really just ______

A

a pointer to the first element in the array.
the ith element of the array is just pointer + size_of_int * i

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

the value of a boolean expression is ___ if true and ___ if false

A

literally anything other than 0 if true, 0 if false

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

advantages of declaring a number as unsigned

A

get a larger range of positive values out of it

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

evaluate this.
if (c = 0)

A

assigns 0 to c, returning 0 -> false always

17
Q

what happens if you do this?
int arr[10] = {1,2};

A

arr will contain 1, 2,, then 0’s for all the other elements

18
Q

what does this do?
void myMeth(const int myInt&)

A

method with pass-by ref arg, (you pass in the value, but it takes the ref of it) that won’t allow you to modify it in the function
- that way, you don’t have the expense of copying it, but you don’t have to worry about overwriting it

19
Q

multidim array

A

int arr[3][2]; // 3x2 array

20
Q

how do c strings work?

A

read along string, char by char, until null is reached.
if null not reached, it would go on forever?
like other arrays, they are unbounded

21
Q

What does this do?
*(arr + 3) = 7;

A

look at pos of first element in arr. then look at element 3*int_size) away element address. deref that to get what it is, set the thing at that memory address to 7.

i.e. arr[3] = 7;

22
Q

What does this do?
delete myVar;

A

mark the position in memory occupied as ok to be allocated again for dynamic allocation with new.

it does NOT set the value there to null, it does not modify the pointer.

if we try to access the pointer and look there in memory, it might give something else that’s weird now.

23
Q

memory leak

A

when you forget to delete something when you’re done (that memory can’t be used now)

24
Q

dangling pointer

A

a pointer that points at something that has been freed/deleted

25
If a method can be overridden, it must be marked as ___.
Virtual