Part 2 - References, Pointers, Arrays and Strings Flashcards

1
Q

In C++ a variable of class type is associated with ________

A

a block of memory

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

If variables s1 and s2 are of type Student, the statement

s2 = s1;

will do what>

A

Make a copy of s1 and put it in the memory block associated with s2

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

Why would a loop like

for (i = 0; i<10; i++) {

Student s = a[i];

s. addMark(…);
s. updateAverage();

}

Not be suitable in C++?

What should be done instead?

A

s would make a copy of a[i];

Instead:

Student &s = a[i];

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

When declaring a reference variable what must be done?

i.e. what is not allowed?

A

Give it a value.

Student &s; would not be allowed

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

What is the default way of passing parameters in C++?

A

Call by value

Value is copied into local scope variable

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

What will

void swap(int &x, int &y) {

int temp = y;

y = x;

x = temp

}

do?

A

Swap the two integers passed in - references mean it is swapped outside the method also

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

When passing objects in C++ what type of calling is usually used? Why?

A

Call by reference

Because otherwise it would be pointlessly making a copy of the object

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

How should the function

void displayMarks(Student &s);

be defined if we want to guarantee s will not be modified?

A

void displayMarks (const Student &s);

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

When passing an array to a function what is actually passed?

A

The address of the start of the array

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

What will a return statement return if the return type is an object?

A

A copy of the object

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

Why should a reference to a local variable not be returned from a function?

What should be done instead?

A

Once control leaves the function the local variable will cease to exist

Instead return a copy

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

What do self referential classes need to use for the attributes of the same type?

A

Pointers

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

If a pointer p points to an object, how can the object it points to be accessed?

A

*p

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

Write a function to swap two integers using pointers

Show how it would be called

A

void swap(int* i1, int* i2) {

int t = *i1;

*i1 = *i2;

*i2 = t;

}

swap(&a, &b);

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

a->b is equal to what?

A

(*a).b

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

References are implicitly ___________ where as pointers must use the ________ operator

A

dereferenced

*

17
Q

Write a function that takes a function pointer (to a function that takes and returns an int), an array and a length

A

void applyAll(int (*func)(int), int arr[], int len) {

for (int i = 0; i < len; i++) {

(*func)(arr[i]);

}

}

18
Q

All data items on the stack must have a __________ size

A

fixed

19
Q

When creating data items whose sizes are not known until runtime the

a) stack
b) heap

must be used

A

b) heap

20
Q

If an array has been dynamically created like so:

int *anArray = new int[10];

How must it be deleted?

A

delete [] anArray;