This quiz tests your knowledge of C++ Forward List container and its associated operations. It contains 10 MCQs.
Question 1
What is the missing line of code in this code to print maximum of two numbers?
#include <bits/stdc++.h>
using namespace std;
int main()
{
list<int> l = {1, 2, 3, 4};
auto it = l.front();
it++;
cout << *(it);
}
Compilation error
Undefined behaviour
2
1
Question 2
Predict the output of the following code:
#include <bits/stdc++.h>
using namespace std;
class A
{
public:
int x, y;
A(int a, int b) : x(a), y(b)
{
}
};
int main()
{
forward_list<A> fl = {{1, 2}};
fl.insert_after(fl.begin(), {3, 4});
auto it = ++fl.begin();
cout << it->x << " " << it->y;
return 0;
}
Compiler Error
3 4
1 2
0 0
Question 3
What is the result of l.unique() for list<int> l = {1, 1, 2, 3, 3};?
{1, 2, 3}
{1, 1, 2, 3, 3}
{1, 2, 3, 3}
Compilation error
Question 4
Which code reverses a forward_list?
reverse(fl.begin(), fl.end());
Neither
fl.reverse();
Both (a and c)
Question 5
Which code compiles without errors?
forward_list<int> fl;
fl.push_back(10);
forward_list<int> fl;
fl.erase(fl.begin());
list<int> l;
l.erase_after(l.begin());
list<int> l;
l.push_front(10);
Question 6
What is the output of this program?
#include <forward_list>
#include <iostream>
using namespace std;
int main()
{
forward_list<int> fl = {1, 2, 3};
fl.erase_after(fl.begin());
cout << *(++fl.begin());
return 0;
}
3
2
1
Undefined behavior
Question 7
What happens if you call pop_back() on a forward_list?
Removes the last element
Undefined behavior
Runtime error
Compilation error
Question 8
Which function merges two sorted lists into one sorted list?
merge()
join()
combine()
concat()
Question 9
What will the following code print?
#include <forward_list>
#include <iostream>
using namespace std;
int main()
{
forward_list<int> flist = {1, 2, 3};
flist.push_front(0);
std::cout << flist.front();
return 0;
}
0
1
2
3
Question 10
What is the underlying data structure of std::forward_list?
Doubly-linked list
Singly-linked list
Dynamic array
Balanced tree
There are 10 questions to complete.