wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Week 8: Vectors

Total questions: 14

Worksheet time: 4hrs 30mins

Name
Class
Date
1.
Can you change the size of an array?
a)
Always
b)
Not after it's initialized
c)
Only when it's full
d)
Only when it's empty
2.
Can you change the size of a vector?
a)
Yes
b)
Only if it's empty
c)
Only if it's full
d)
Never
3.
What type can a vector hold?
a)
Only ints
b)
All types
c)
Only floats
d)
All types but booleans
4.
How do you get the size of a vector? Assume a vector v exists.
a)
v.length()
b)
len(v)
c)
size(v)
d)
v.size()
5.
How do you get the size of an array? Assume an array a exists.
a)
a.size()
b)
size(a)
c)
len(a)
d)
None of the above
6.
What is the result of this code?
vector<int> v = {1, 2, 3, 4, 5}
a)
Creates 5 vectors
b)
Creates 1 vector with no elements
c)
Creates 1 vector with 5 elements
d)
Checks the weather
7.
Given this vector
v = {1, 5, 3, 2, 6}
How do you get the last value?
a)
v.push_back(6)
b)
v.back()
c)
v.at(5)
d)
v.pop_back()
8.
Which is different?
a)
vector<int> v = {-1, -1, -1};
b)
vector<int> v(3, -1);
c)
vector<int> v(3);
d)
vector<int> v;
v.push_back(-1);
v.push_back(-1);
v.push_back(-1);
9.
What are the contents of the vector v after the following statement?
vector<int> v(8);
a)
8
b)
8,8,8,8,8,8,8,8
c)
0,0,0,0,0,0,0,0
d)
8 garbage values
10.
Why do we use v.at(i) instead of v[i] for a vector v and integer i?
a)
.at() checks for out of bounds errors
b)
Because miller says so
c)
v[i] is incorrect syntax, it wont compile
d)
[] checks for out of bounds errors
11.
How do we print the contents of a vector v?
a)
cout << v;
b)
print(v);
c)
for(int i = 0; i < v.size(); i++){
cout << v.at(i);
}
d)
while(!v.isdone()){
cout << v.at(i);
}
12.
Which of the following is invalid?
a)
vector<vector<int> >v;
b)
vector<int>v;
c)
vector<bool>v;
d)
vector<vector>v;
13.
What is the size of the following vector v:
vector<vector<int> >v = { {1,2,3}, {4,5,6}, {7,8,9} };
a)
9
b)
3
c)
1
d)
0
14.
Which of the following functions changes the following vectors size?:
vector<int> v (5);
a)
v.at(3);
b)
v.push_back(0);
c)
v.resize(5);
d)
v.back();