Home »
Aptitude Questions and Answers »
C++ Aptitude Questions and Answers
C++ List Container Aptitude Questions and Answers
C++ List Container Aptitude: This section contains C++ List Container Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on March 07, 2021
1) There are the following statements that are given below, which of them are correct about list container in C++?
- A list container class is a derived container.
- The list supports bi-directional iteration.
- The list stores elements in the contiguous memory location.
- The traversing of the list element is slow compared to the vector.
Options:
- A and B
- A, B, and C
- B, C, and D
- A, B, C, and D
Correct Answer - 3
B, C, and D
Explanation:
Statements B, C, and D are correct about list containers in C++.
2) Which of the following header file is required to use list container class in C++?
- <list>
- <List>
- <Ilist>
- <Linkedlist>
Correct Answer - 1
<list>
Explanation:
The <list> header file is required to use list container class in C++.
3) Can we access elements from the list randomly?
- Yes
- No
Correct Answer - 2
No
Explanation:
No, we cannot access elements from the list randomly.
4) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
L.push_back(10);
L.push_back(20);
for (int i = 0; i < L.size(); i++) {
cout << L[i] << " ";
}
return 0;
}
Options:
- 10 20
- Garbage value
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error.
5) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
L.push_back(10);
L.push_back(20);
for (int i = 0; i < L.size(); i++) {
cout << *L++ << " ";
}
return 0;
}
Options:
- 10 20
- 20 10
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error.
6) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(20);
for (I = L.begin(); I != L.end(); ++I)
cout << *I << " ";
return 0;
}
Options:
- 10 20
- 20 10
- Syntax error
- Runtime error
Correct Answer - 1
10 20
Explanation:
The above code will print "10 20" on the console screen.
7) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(20);
for (I = L.begin(); I != L.end();)
cout << *++I << " ";
return 0;
}
Options:
- 10 20
- 20 10
- Syntax error
- Runtime error
Correct Answer - 2
20 10
Explanation:
The above code will print "20 10" on the console screen.
8) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(20);
for (I = L.begin(); I != L.end();)
cout << *++I++ << " ";
return 0;
}
Options:
- 10 20
- 20 10
- Syntax error
- Runtime error
Correct Answer - 2
20 10
Explanation:
The above code will print "20 10" on the console screen.
9) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(20);
for (I = L.begin(); I != L.end();)
cout << *I++ << " ";
return 0;
}
Options:
- 10 20
- 20 10
- Syntax error
- Runtime error
Correct Answer - 1
10 20
Explanation:
The above code will print "10 20" on the console screen.
10) What is the correct output of the given code snippet?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(20);
L.push_front(30);
L.push_back(40);
L.pop_back();
L.pop_back();
cout << L.front();
return 0;
}
Options:
- 10
- 20
- 30
- 40
Correct Answer - 3
30
Explanation:
The above code will print "30" on the console screen.
11) What is the correct output of the given code snippets?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
const list<int>::iterator I;
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_back(40);
for (I = L.begin(); I != L.end();)
cout << *I++ << " ";
return 0;
}
Options:
- 10 20 30 40
- 40 30 20 10
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error because we cannot modify iterator.
12) What is the correct output of given code snippets?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_back(40);
L.reverse();
for (I = L.begin(); I != L.end();)
cout << *I++ << " ";
return 0;
}
Options:
- 10 20 30 40
- 40 30 20 10
- Syntax error
- Runtime error
Correct Answer - 2
40 30 20 10
Explanation:
The above code will print "40 30 20 10" on the console screen.
13) What is the correct output of given code snippets?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<string> L;
list<string>::iterator I;
L.push_back("India ");
L.push_back("is ");
L.push_back("great ");
L.push_back("country ");
I.reverse();
for (I = L.begin(); I != L.end();)
cout << *I++ << " ";
return 0;
}
Options:
- India is great country
- country great is India
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error because the reverse() function cannot be called using the iterator object.
14) What is the correct output of the given code snippets?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L1;
list<int> L2;
list<int>::iterator I;
L1.push_back(10);
L1.push_back(20);
L2.push_back(30);
L2.push_back(40);
L1.merge(L2);
for (I = L2.begin(); I != L2.end(); ++I)
cout << *I << " ";
return 0;
}
Options:
- 10 20 30 40
- 40 30 20 10
- No output
- Syntax error
Correct Answer - 3
No output
Explanation:
The above code will not print anything on the console screen.
15) What is the correct output of the given code snippets?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L1;
list<int> L2;
list<int>::iterator I;
L1.push_back(10);
L1.push_back(20);
L2.push_back(30);
L2.push_back(40);
L1.merge(L2);
for (I = L1.begin(); I != L1.end(); ++I)
cout << *I << " ";
return 0;
}
Options:
- 10 20 30 40
- 40 30 20 10
- Syntax error
- No output
Correct Answer - 1
10 20 30 40
Explanation:
The above code will print "10 20 30 40" on the console screen.
16) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(40);
L.push_back(30);
L.push_back(20);
L.sort(1);
for (I = L.begin(); I != L.end();)
cout << *I++ << " ";
return 0;
}
Options:
- 10 20 30 40
- 40 30 20 10
- No output
- Syntax error
Correct Answer - 4
Syntax error
Explanation:
The above code will generate a syntax error.
17) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(40);
L.push_back(30);
L.push_back(20);
L.sort();
L.reverse();
for (I = L.begin(); I != L.end();)
cout << *I++ << " ";
return 0;
}
Options:
- 10 20 30 40
- 40 30 20 10
- No output
- Syntax error
Correct Answer - 2
40 30 20 10
Explanation:
The above code will print "40 30 20 10" on the console screen.
18) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(40);
L.push_back(10);
L.push_back(20);
L.push_back(20);
cout << L.unique();
return 0;
}
Options:
- 0
- 40
- Syntax error
- Runtime error
Correct Answer - 3
Syntax error
Explanation:
The above code will generate a syntax error.
19) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(40);
L.push_back(10);
L.push_back(20);
L.push_back(20);
L.push_back(40);
L.unique();
for (I = L.begin(); I != L.end();)
cout << *I++ << " ";
return 0;
}
Options:
- 10 40 20
- 10 40 10 20 40
- Syntax error
- Runtime error
Correct Answer - 2
10 40 10 20 40
Explanation:
The above code will print "10 40 10 20 40" on the console screen.
20) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(40);
L.push_back(10);
L.push_back(20);
L.push_back(20);
L.push_back(40);
L.unique();
L.sort();
for (I = L.begin(); I != L.end();)
cout << *I++ << " ";
return 0;
}
Options:
- 10 20 40
- 10 10 20 40 40
- Syntax error
- Runtime error
Correct Answer - 2
10 10 20 40 40
Explanation:
The above code will print "10 10 20 40 40" on the console screen.
21) What is the correct output of the given code snippets?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(40);
L.push_back(10);
L.push_back(20);
L.push_back(20);
L.push_back(40);
L.sort();
L.unique();
for (I = L.begin(); I != L.end();)
cout << *I++ << " ";
return 0;
}
Options:
- 10 20 40
- 10 10 20 40 40
- Syntax error
- Runtime error
Correct Answer - 1
10 20 40
Explanation:
The above code will print "10 20 40" on the console screen.
22) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_back(40);
L.assign(2, 12);
for (I = L.begin(); I != L.end();)
cout << *I++ << " ";
return 0;
}
Options:
- 10 20 12 12 30 40
- 12 12
- Syntax error
- Runtime error
Correct Answer - 2
12 12
Explanation:
The above code will print "12 12" on the console screen.
23) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_back(40);
L.resize(3);
for (I = L.begin(); I != L.end();)
cout << *I++ << " ";
return 0;
}
Options:
- 10 20 30 40
- 10 20 30
- Syntax error
- Runtime error
Correct Answer - 2
10 20 30
Explanation:
The above code will print "10 20 30" on the console screen.
24) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L;
list<int>::iterator I;
L.push_back(10);
L.push_back(20);
L.push_back(30);
L.push_back(40);
L.resize(3);
L.resize(4);
for (I = L.begin(); I != L.end();)
cout << *I++ << " ";
return 0;
}
Options:
- 10 20 30 40
- 10 20 30 30
- 10 20 30 0
- Syntax error
Correct Answer - 3
10 20 30 0
Explanation:
The above code will print "10 20 30 0" on the console screen.
25) What is the correct output of given code snippets in C++?
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> L1;
list<int> L2;
list<int>::iterator I;
L1.push_back(10);
L1.push_back(20);
L2.push_back(30);
L2.push_back(40);
I = L1.begin();
L1.splice(I, L2);
for (I = L1.begin(); I != L1.end();)
cout << *I++ << " ";
return 0;
}
Options:
- 10 20 30 40
- 30 40 10 20
- 40 30 20 10
- Syntax error
Correct Answer - 2
30 40 10 20
Explanation:
The above code will print "30 40 10 20" on the console screen.