Question 51
MediumWhat is the difference between v.size() and v.capacity() for a std::vector?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Programming Languages
What is the difference between v.size() and v.capacity() for a std::vector?
They are always equal
capacity is the number of bytes
size is the number of elements; capacity is how many fit before reallocation
size includes deleted elements
Sign up free to see the answer and a step-by-step explanation.
What does 'const int *p' mean in C++?
p is an int
Both p and *p are constant
p is a constant pointer
p points to a const int: *p can't be changed through p, but p itself can be repointed
Sign up free to see the answer and a step-by-step explanation.
Which C++ feature lets one function or class work with many types, checked at compile time?
Virtual functions
Friend functions
Templates
Macros
Sign up free to see the answer and a step-by-step explanation.
What does std::move actually do?
Moves the object to the heap
Casts its argument to an rvalue reference so a move constructor or move assignment can be chosen
Copies the object
Deletes the object
Sign up free to see the answer and a step-by-step explanation.
What is the output of this program?
#include <iostream>
#include <memory>
using namespace std;
int main() {
auto p = make_shared<int>(5);
auto q = p;
cout << p.use_count() << " ";
q.reset();
cout << p.use_count();
return 0;
}
1 1
1 2
Sign up free to see the answer and a step-by-step explanation.
What is the output of this program?
#include <iostream>
#include <queue>
using namespace std;
int main() {
priority_queue<int> pq;
for (int x : {1, 3, 2}) pq.push(x);
while (!pq.empty()) { cout << pq.top() << " "; pq.pop(); }
return 0;
}
1 3 2
1 2 3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this program?
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
vector<int> w = move(v);
cout << !w.empty() << " " << !v.empty();
return 0;
}
1 1
0 0
Sign up free to see the answer and a step-by-step explanation.
What is the output of this program?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
transform(v.begin(), v.end(), v.begin(), [](int x) { return x * 2; });
for (int x : v) cout << x << " ";
return 0;
}
2 4 6
1 2 3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this program?
#include <iostream>
using namespace std;
int main() {
int x = 10;
auto byVal = [=]() { return x; };
auto byRef = [&]() { return x; };
x = 11;
cout << byVal() << " " << byRef();
return 0;
}
10 11
11 10
Sign up free to see the answer and a step-by-step explanation.
Why is iterating a std::vector and erasing elements inside a range-for loop dangerous?
vectors can't be erased
range-for loops can't modify elements
erase invalidates iterators at and after the erased position, so the loop's iterator becomes invalid
It is always safe
Sign up free to see the answer and a step-by-step explanation.
2 2
2 1
3 1 2
3 2 1
0 1
1 0
Error
2 3 4
10 10
11 11