This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “seq_con List”.
1. How many list sequence containers are provided by STL?
a) 1
b) 2
c) 3
d) 4
View Answer
Explanation: There are two list sequence containers are provided by STL namely forward_list and list.
2. Which type of list a Forward_list sequence container implements?
a) Singly Linked List
b) Doubly Linked List
c) Both type of list
d) A simple sequence of array
View Answer
Explanation: Forward_list sequence container implements a Singly Linked List.
3. Which type of list a List sequence container implements?
a) Singly Linked List
b) Doubly Linked List
c) Both type of list
d) A simple sequence of array
View Answer
Explanation: List sequence container implements Doubly Linked List.
4. Which of the following header file is required for forwawrd_list?
a) <forward_list>
b) <list>
c) <f_list>
d) <Forward_List>
View Answer
Explanation: One needs to implement <forward_list> header file to use forward_list in a program.
5. Which of the following(s) is/are the correct way of assigning values to a forward_list f?
a) f.assign({1,2,3,4,5})
b) f.assign(10,5)
c) both f.assign({1,2,3,4,5}) and f.assign(10,5)
d) f.assign(1,1,1,1)
View Answer
Explanation: Both f.assign({1,2,3,4,5}) and f.assign(10,5) are correct way of assigning values to a forward_list. The first assignment initializes the list with the elements 1,2,3,4 and 5 whereas the second assignment initializes the list 10 elements with value 5 i.e. 5 10 times.
6. How the list differs from vectors?
a) Vector is contiguous whereas List is non-contiguous
b) Insertion in the list takes constant time whereas it is not constant in vectors
c) There is no capacity defined for list
d) All of the mentioned
View Answer
Explanation: List is non-contiguous that means elements of a list are not the contiguous manner in memory. Insertion in a list is constant for because we are not increasing the size of the list anywhere which was the case of a vector. Vectors have a capacity defined whereas there is no such capacity defined for Lists.
7. What is the syntax of declaraing a forward_list?
a) forward_list f;
b) forward_list<type> f;
c) forward_list f<type>;
d) forward_list<type,size> f;
View Answer
Explanation: forward_list<type> f; is the correct syntax of declaring a forward-list.
8. What will be the output of the following C++ code?
#include <iostream> #include <vector> #include <forward_list> using namespace std; int main() { forward_list<int> fl1; fl1.assign(5,10); for (int&c : fl1) cout << c << " "; cout<<endl; fl1.insert_after(fl1.begin(), {1,2,3}); for (int&c : fl1) cout << c << " "; cout<<endl; return 0; }
a)
10 10 10 10 10 10 1 2 3 10 10 10 10
b)
10 2 3 10 10 10 10 10 10 10 10 10
c) Error
d) Segmentation fault
View Answer
Explanation: The program is syntactically correct therefore no error and also memory are handled carefully therefore no segmentaion fault. Hence the program runs perfectly. The insert_after() function inserts the elements provided at the position mention in the first argument.
9. What will be the output of the following C++ code?
#include <iostream> #include <vector> #include <forward_list> using namespace std; int main() { forward_list<int> fl1 = {1,2,3,4,5}; for (int&c : fl1) cout << c << " "; cout<<endl; forward_list<int>::iterator ptr = fl1.begin(); fl1.erase_after(ptr); for (int&c : fl1) cout << c << " "; cout<<endl; return 0; }
a)
1 2 3 4 5 1 2 3 4 5
b)
1 2 3 4 5 1 3 4 5
c)
1 2 3 4 5 2 3 4 5
d)
1 2 3 4 5 1View Answer
Explanation: erase_after() function is used to erase/delete the element present next to the provided element. So in the given program we provided fl1.begin() i.e. 1 as the element to erase_after() function hence the element after 1 i.e. 2 is deleted.
10. What will be the output of the following C++ code?
#include <iostream> #include <vector> #include <forward_list> using namespace std; int main() { forward_list<int> fl1 = {1,2,3,4,5}; for (int&c : fl1) cout << c << " "; cout<<endl; fl1.remove_if([](int x){ return x > 3;}); for (int&c : fl1) cout << c << " "; cout<<endl; return 0; }
a)
1 2 3 4 5 1 2 3 4 5
b)
1 2 3 4 5 1 2
c)
1 2 3 4 5 1 2 3
d)
4 5 1 2 3 4 5View Answer
Explanation: remove_if() function is provided in list to remove element based on the conditions provided in the function. So in the program we asked to delete all the element which are greater then 3, hence 4 and 5 are deleted and we are remained with 1,2 and 3.
output:
$ ./a.out 1 2 3 4 5 1 2 3
11. What will be the output of the following C++ code?
#include <iostream> #include <vector> #include <forward_list> using namespace std; int main() { forward_list<int> fl1 = {1,7,8,9,10}; forward_list<int> fl2 = {2,3,4,5,6}; fl1.splice_after(fl1.begin(), fl2); for (int&c : fl1) cout << c << " "; cout<<endl; return 0; }
a) 1 2 3 4 5
b) 1 2 3 4 5 6 7 8 9 10
c) 1 7 8 9 10
d) 2 3 4 5 6
View Answer
Explanation: splice_after() function is used to insert a forward-list into another list after a given position. So in this program we are trying to insert list2 into list1 after fl1.bein() i.e. 1. Hence the list1 becomes 1 2 3 4 5 6 7 8 9 10.
Output:
$ ./a.out 1 2 3 4 5 6 7 8 9 10
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check C++ Books
- Practice Computer Science MCQs
- Apply for C++ Internship
- Practice Programming MCQs
- Check Programming Books