Refresher on Functions and Classes 1 Flashcards

1
Q

What is an Iterator in C++?

A

An object that can traverse a container class without the user knowing how the container is implemented.

Examples include std::map and std::vector.

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

What types of iterators do containers provide?

A
  • iterator: read/write iterator
  • const_iterator: read-only iterator
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the begin() function return?

A

An iterator representing the beginning of the elements in the container.

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

What does the end() function return?

A

An iterator representing the element just past the end of the elements.

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

What is the purpose of the dereference operator (*) for iterators?

A

To return the element that the iterator is pointing at.

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

What does the operator++ do for iterators?

A

Moves the iterator to the next element in the container.

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

What is a set in C++?

A

A collection of unique values containing no duplicates, stored in sorted order.

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

What happens when you try to insert a duplicate value into a set?

A

The duplicate value does not get added.

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

What does the find() method do in a set?

A

Returns an iterator pointing to the element if it exists or end() if it does not.

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

What is a map in C++?

A

An associated container that has a key/value mapping with unique keys.

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

What is the difference between a set and a map?

A

A map has a value associated with each unique key, while a set only contains unique values.

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

What does the insert() method expect when adding to a map?

A

An std::pair representing the key and value.

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

What is a function template in C++?

A

A template that allows the creation of functions that can operate on different data types.

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

What is the syntax to declare a template for a single type?

A

template <typename></typename>

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

What is the purpose of the Rule of 3 in C++?

A

It states that if a class manages resources, it should define a destructor, copy constructor, and copy assignment operator.

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

What does the term ‘deep copy’ mean?

A

Creating a new copy of an object along with all objects it references.

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

What is meant by ‘shallow copy’?

A

Copying an object while sharing references to the same resources it points to.

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

What is the purpose of a constructor in a C++ class?

A

To initialize an object when it is created.

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

What does the term ‘accessor’ refer to in C++ classes?

A

A member function that retrieves the value of a private member variable.

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

Fill in the blank: A _______ is an object that can traverse a container class.

A

Iterator

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

True or False: A set can contain duplicate values.

A

False

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

What is the output of the following piece of code? std::vector<int> nums = {10, 20, 30, 40, 50}; std::vector<int>::iterator it = nums.begin(); while (it != nums.end()) { if (*it % 20 == 0) { it = nums.erase(it); } else { ++it; } } for (it = nums.begin(); it != nums.end(); ++it) { std::cout << *it << ' '; }</int></int>

A

10 30 50

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

What does the erase() function do in a vector?

A

Removes an element from the vector and returns an iterator to the next element.

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

What data structure is assumed to be used in the set class?

A

Binary Search Tree

This could have been any data structure.

25
What is the purpose of a constructor in a class?
Helps with initialization ## Footnote Same name as the class, no return type.
26
What is a default constructor?
No arguments passed
27
What is a parameterized constructor?
Constructor with arguments
28
What is a copy constructor?
Takes an existing object and sets all of its fields to the fields of that object
29
What does the destructor do in a class?
Destroys objects created by a constructor
30
When is a destructor called?
When a reference to an object goes out of scope or when a pointer to the object is called with delete
31
What is the copy assignment operator used for?
Overloads the assignment operator =
32
What is the difference between a copy constructor and an assignment operator?
Copy constructor creates a new object, assignment operator copies data into an existing object
33
What is data hiding/abstraction?
Use private/public access to members
34
What are accessor functions?
Functions that return private members’ values
35
What are mutator functions?
Functions that set private members’ values
36
What is the difference between deep copy and shallow copy?
Deep copy copies contents to a different memory location, shallow copy copies the reference of memory
37
What is the Rule of Three in C++?
If you implement copy constructor, destructor, or assignment operator, you should implement all three
38
What does the Rule of Five add to the Rule of Three?
It adds rules for move semantics
39
What is the purpose of a class constructor?
To initialize an object of the class
40
What does the statement 'set s4 = s2;' invoke?
The copy constructor of the s4 object
41
What is a template in C++?
A feature that allows functions and classes to operate with generic types
42
Fill in the blank: A function that simply ______ private members’ values is called an accessor function.
returns
43
True or False: A destructor has parameters.
False
44
What does the copy assignment operator require before copying new data?
Deleting any existing data
45
What type of access do private members have?
Cannot be directly accessed using an object
46
What is the primary reason to implement a deep copy in a copy constructor?
To avoid shared references to the same memory
47
What is the purpose of the 'this' pointer in a member function?
To refer to the current object
48
Fill in the blank: The ______ constructor is called when creating a new object from an existing one.
copy
49
What is the main objective of using function templates?
To enable functions to operate with generic types
50
What is the purpose of the getter function getVector() in the MyVec class?
To return the vector v ## Footnote The getter function allows access to the private member v of the class.
51
What is the first step in the copy constructor MyVec::MyVec(MyVec &myvec) for deep copying?
Create a new vector() ## Footnote This initializes a new vector to store copied elements.
52
In the deep copy constructor, what does the loop do?
Copies elements from myvec's vector to the new vector ## Footnote The loop iterates through each element of myvec's vector and pushes it into the new vector.
53
Which of the following options indicates a deep copy in the MyVec class?
Code A ## Footnote Code A creates a new vector and copies each element, ensuring that the original vector and the copied vector do not reference the same memory.
54
What is the issue with the second version of the copy constructor MyVec::MyVec(MyVec &myvec)?
It does not perform a deep copy ## Footnote This version assigns the entire myvec to v, which can lead to shared references.
55
What mistake is present in the third version of the copy constructor MyVec::MyVec(MyVec &myvec)?
It assigns myvec's vector directly to v ## Footnote This creates a shallow copy, where v and myvec's vector point to the same memory location.
56
What does the term 'deep copy' mean in the context of the MyVec class?
Creating a new instance of the vector with its own memory ## Footnote A deep copy ensures that changes to one vector do not affect the other.
57
Fill in the blank: The variable v in the MyVec class is of type _______.
vector ## Footnote This defines v as a vector that holds integers.
58
True or False: A shallow copy in MyVec would mean both instances share the same vector memory.
True ## Footnote If a shallow copy is made, modifications to one instance would affect the other.