1. How many list sequence containers are provided by STL?
A. 1
B. 2
C. 3
D. 4
Answer: B
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
Answer: A
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
Answer: B
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>
Answer: A
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)
Answer: C
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
Answer: D
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;
Answer: B
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 fl1;
fl1.assign(5,10);
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
Answer: A
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 fl1 = {1,2,3,4,5};
for (int&c : fl1)
cout << c << " ";
cout<::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
1
Answer: B
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 fl1 = {1,2,3,4,5};
for (int&c : fl1)
cout << c << " ";
cout< 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 5
Answer: C
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.
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 fl1 = {1,7,8,9,10};
forward_list 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
Answer: B
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.