Question 31
MediumWhat is the output of this program?
#include <iostream>
std;
{ t = a; a = b; b = t; }
{
x = , y = ;
(x, y);
cout << x << << y;
;
}
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 output of this program?
#include <iostream>
std;
{ t = a; a = b; b = t; }
{
x = , y = ;
(x, y);
cout << x << << y;
;
}
1 2
2 1
2 2
1 1
Sign up free to see the answer and a step-by-step explanation.
What does std::unique_ptr do?
Owns a heap object exclusively and deletes it automatically when it goes out of scope
Copies the object on every assignment
Points only to stack variables
Allows many owners of one object
Sign up free to see the answer and a step-by-step explanation.
What is the output of this program?
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
int main() {
vector<int> v = {1, 2, 3};
cout << accumulate(v.begin(), v.end(), 0);
return 0;
}
6
9
Sign up free to see the answer and a step-by-step explanation.
What is the difference between a pointer and a reference in C++?
A reference must be initialised and can't be reseated or null; a pointer can be changed and can be null
A pointer can't be null
References use more memory
They are identical
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 a = 5;
int &r = a;
r += 3;
int b = r;
b++;
cout << a << " " << b;
return 0;
}
8 9
9 9
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 = {5, 1, 4, 2};
sort(v.begin(), v.end(), greater<int>());
for (int x : v) cout << x;
return 0;
}
1245
5421
Sign up free to see the answer and a step-by-step explanation.
What is the output of this program?
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
map<string, int> m;
m["banana"] = 2;
m["apple"] = 5;
m["cherry"] = 1;
for (auto &p : m) cout << p.first[0];
cout << m.size();
return 0;
}
abc3
bac3
Sign up free to see the answer and a step-by-step explanation.
What is the output of this program?
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
map<string, int> m;
cout << m["x"] << " " << m.size();
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 <set>
using namespace std;
int main() {
set<int> s = {4, 1, 4, 2, 1};
cout << s.size() << " " << *s.begin() << " " << *s.rbegin();
return 0;
}
3 1 4
5 4 1
Sign up free to see the answer and a step-by-step explanation.
What is the output of this program?
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v;
for (int i = 1; i <= 5; i++) v.push_back(i * i);
v.erase(v.begin() + 1);
v.insert(v.begin(), 0);
cout << v.size() << " " << v[2];
return 0;
}
6 9
5 9
Sign up free to see the answer and a step-by-step explanation.
3
0
5 6
8 8
2415
5142
abc2
cba3
Error
0 1
5 1 4
3 4 1
4 16
5 4