C++ & Classes
Pointers & References
Memory
Operators & Big Three
Templates & Data Structures
100

Why would we write a class? (three reasons)

  1. Avoid repeating code

  2. Control access of certain variables and functions

  3. Encapsulation

100

Why would we pass by reference instead of by value for most complex objects?

Avoid making a copy, which takes up a lot of memory

100

The "new" keyword does this.

What is allocating memory on the heap?

100

These are the Big Three.

What are Copy Constructor, Assignment Operator, and Destructor?

100

What is the access order of a queue?

FIFO
(First in, first out)

200

The last step in the C++ program compilation process.

What is linking?

200

What is printed?

int b = 5;
int* a = &b;

cout << a << endl;

Memory address of b

200

What is the main difference between the stack and the heap?

The stack is memory that is managed automatically based on scope, while heap memory is allocated and deallocated manually

200

Given this code snippet, what does "b.print()" output?

blue balloon holding 4.0

200

This feature is the reason why we don’t split template classes into .h and .cpp files.

What is the compiler?

300

Given the following class, what is printed?

CAR 0100560

MALA 321040

300

With respect to pointers, what do the operators -> and * do?

-> allows you to access an object variable or function through a pointer to that object. 

* dereferences a pointer, giving you access to the object

300

In this class, what are the sizes (in bytes) of a char, a short, an int, and a pointer?

char: 1

short: 2

int: 4

pointer: 4

300

What is the expected return type of an overloaded /=, where T is the type of an object?

T&

300

What are the three differences between linked lists and arrays?

  1. Linked Lists are not random access, but arrays are

  2. Linked lists are non-contiguous, but arrays are contiguous

  3. Linked lists require more memory space than linked lists.

400

What does the static keyword do when applied to a) a function, and b) a class?

a) It allows a variable to live longer than the function

b) It creates a variable shared by all instances of a class

400

Given the following code snippet:

What is printed?

8

400

On what line does an error first appear? What is the error? Why?

A segmentation fault occurs on "arr[1] = 16".

This is because we try to access memory that we are no longer using.

400

What member of the big 3 is called on line 5?

Copy constructor

400

Why could there be an error in this function?

Some classes don't have a getBool() function.

500

What is held constant in this function? What is returned?

int* const MyClass::func(const int* p) const;

The function has a const parameter and the object calling the function is held constant; the function returns a const pointer to an int.

500

What is printed?

7 6 5

500

How many bytes of memory are leaked?

16 bytes

500

What members of the big 3 are called on which lines?

Copy constructor - lines 2, 5, 7

Destructor - lines 9, 11, 12

500

Briefly explain how to insert a node into the middle of a singly Linked List.

First, iterate to one before where you want to insert the new node. 

Then, point the new node’s next pointer to the current next node, and point the current node’s next pointer to the new node

M
e
n
u