C++ Programming Questions and Answers – seq_con List

«
»

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

Answer: b
Explanation: There are two list sequence containers are provided by STL namely forward_list and list.
advertisement

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

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
View Answer

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>
View Answer

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)
View Answer

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.
advertisement

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

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;
View Answer

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<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 2 3 10 10 10 10
advertisement

b)

10 2 3 10 10 10 10
10 10 10 10 10

c) Error
d) Segmentation fault
View Answer

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<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
b) 1 3 4 5
c) 2 3 4 5
d) 1
View Answer

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.
advertisement

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 5
View Answer
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.
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

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.
Output:

$ ./a.out 
1 2 3 4 5 6 7 8 9 10

Sanfoundry Global Education & Learning Series – C++ Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn