1. Majority of template classes and functions contained in STL were originally developed by
Explanation:
Correct answer: d) Alexander Stepanov and Meng Lee
The majority of template classes and functions contained in the C++ Standard Template Library (STL) were originally developed by Alexander Stepanov and Meng Lee. They designed the STL while working at HP Labs in the early 1990s.
2. To implement STL components in a program, we must include
Explanation:
Correct answer: d) <functional> header file
To implement STL components in a C++ program, you need to include the appropriate header files that provide the STL functionality. The <functional> header file is one of the essential headers in STL, as it contains various function objects (functors) and predefined function object templates, such as std::less, std::greater, std::plus, etc.
3. An STL container can be used to
Explanation:
Correct answer: d) None of the above
An STL container is a data structure that stores and organizes data in a specific way. While STL containers are used in C++ programs that use template classes, they are not used specifically for compiling such programs. Similarly, they can store objects of any data type, but this is not their primary purpose. The primary purpose of an STL container is to organize and manage data efficiently for specific use cases, and it is not meant for general-purpose data processing in memory.
4. Which one of the following is FALSE?
Explanation:
Correct answer: c) STL algorithms are member functions of containers
The statement in option c is false. STL algorithms are not member functions of containers. Instead, they are standalone functions designed to operate on containers. STL algorithms are used to perform various operations on container elements, such as searching, sorting, modifying, or analyzing the data stored in the containers. To use an STL algorithm, you pass the range of elements you want to process along with the algorithm function as arguments. In this way, algorithms can be used with different containers and are not tied to any specific container as member functions.
5. An STL algorithm is
Explanation:
Correct answer: c) a standalone function designed to operate on containers
The statement in option c is correct. An STL algorithm is a standalone function designed to operate on containers. It is not a friend function or a member function of a container class. STL algorithms are generic functions that can work with various container types as long as the containers provide the required interface (e.g., begin(), end() functions) to access their elements. Algorithms in STL follow the "Separation of Concerns" principle, where the algorithms are separate from the containers, making them more versatile and reusable across different containers.
6. Which one of the following is TRUE?
Explanation:
Correct answer: b) An iterator can always move forward or backward through a container
The statement in option b is true. An iterator is an object that allows you to traverse the elements of a container, and it provides operations to move forward or backward through the container. In most cases, an iterator can move both forward and backward, depending on the type of container it is associated with. For example, a bidirectional iterator can move both forward and backward through a container like a linked list or a deque. However, there are also more restricted types of iterators, such as forward iterators (e.g., for singly-linked lists), which can only move forward and do not support backward movement.
7. Which one of the following is a sequence container?
Explanation:
Correct answer: a) deque
The statement in option a is true. A deque (double-ended queue) is one of the sequence containers provided by the C++ Standard Template Library (STL). It is a dynamic array-like container that allows efficient insertions and deletions at both the front and back ends of the container. Deques are useful when you need to add or remove elements from the front and back frequently. Other examples of sequence containers include vector and list.
8. A vector container
Explanation:
Correct answer: b) allows insertions and deletions fast at the back
The statement in option b is true. A vector is one of the sequence containers provided by the C++ Standard Template Library (STL). It is implemented as a dynamic array, allowing efficient insertions and deletions at the back end of the container. Adding or removing elements at the back of a vector has constant time complexity (O(1)), making it suitable for scenarios where elements need to be added or removed from the end frequently. Additionally, vectors also permit direct access to elements using index-based access.
9. Which one of the following is a non-mutating algorithm?
Explanation:
Correct answer: c) for_each()
The statement in option c is true. The for_each() algorithm is a non-mutating algorithm provided by the C++ Standard Template Library (STL). Non-mutating algorithms do not modify the elements of the container they operate on; instead, they perform some operation on the elements without changing them. The for_each() algorithm applies a specified function to each element of the container but does not modify the elements. In contrast, mutating algorithms, such as unique() and partition(), modify the order or values of elements in the container during their operation.
10. Which of the following algorithms is used to replace an element with a specified value?
Explanation:
Correct answer: d) replace()
The statement in option d is true. The replace() algorithm is used to replace all occurrences of a specified value with a new value in the given container. When you call the replace() function, it scans the container and replaces all elements that match the old value with the new value. After the operation, the container will have the updated elements. For example, you can use the replace() algorithm to replace all occurrences of a certain number in a vector with another number.
11. In associative containers,
Explanation:
Correct answer: b) keys are used to access elements
The statement in option b is true. In associative containers, elements are stored in a way that allows efficient access using keys. Instead of using integer-based indices like in sequence containers (e.g., vector, deque), associative containers use keys to access elements. Each element in an associative container is associated with a unique key, and you can use the key to quickly locate and retrieve the element. Examples of associative containers include std::map, std::multimap, std::set, and std::multiset. These containers use different data structures (e.g., red-black trees, hash tables) to organize elements in a way that allows fast key-based access.
12. A list container
Explanation:
Correct answer: a) permits the use of bidirectional iterators
The statement in option a is true. A list is one of the container classes provided by the C++ Standard Template Library (STL), and it supports bidirectional iterators. A bidirectional iterator is an iterator that can move forward and backward through the container's elements. Lists are implemented as doubly-linked lists, where each element (node) points to both the next and previous elements. This linked structure allows efficient insertions and deletions at any position in the list, as well as backward traversal using bidirectional iterators. However, random access is not supported by lists, meaning that you cannot use index-based access (e.g., list[3]) to directly access elements in the list.
13. A map container
Explanation:
Correct answer: c) stores pairs of objects (or values)
The statement in option c is true. A map is one of the associative container classes provided by the C++ Standard Template Library (STL), and it stores pairs of objects (key-value pairs). Each element in the map is a pair that associates a unique key with a corresponding value. The keys are used to access and identify the values in the map. When you insert elements into a map, you provide both the key and the associated value, and the map organizes these pairs internally for efficient key-based access. Maps are useful when you need to maintain an ordered set of key-value pairs with fast access and efficient updates based on keys.
14. To use the function object modulus < int > ( ) in a program, we must include the header file
Explanation:
Correct answer: b) <functional>
The statement in option b is true. To use the function object modulus <int>()
in a C++ program, you need to include the <functional> header file. The <functional> header file provides various function objects (functors) and function templates that allow you to perform various operations, such as comparisons, arithmetic operations, logical operations, etc., using objects as if they were functions. The modulus <int> function object is used to perform the modulo operation between two integers, and it is part of the <functional> header.
15. Consider the following code:
int main()
{
vector<int> x;
for (int i = 1; i < 4; i++)
x.push_back(i);
x.push_back(7.6);
vector<int>::iterator p = x.begin();
p = p + 1;
x.insert(p, 5);
x.erase(x.begin() + 3);
return 0;
}
When executed, the vector will contain the elements
Explanation:
Correct answer: c) 1, 2, 5, 7
Let's analyze the code step by step:
- The vector
x
is initially empty. - A for loop is used to push integers 1, 2, and 3 into the vector using
push_back()
. The vector will contain [1, 2, 3]. x.push_back(7.6)
attempts to add the floating-point number 7.6 to the vector. However, the vector is of typevector<int>
, and it cannot store floating-point numbers directly. The number 7 will be added to the vector, and it will now contain [1, 2, 3, 7].vector<int>::iterator p = x.begin();
creates an iteratorp
that points to the first element of the vector (1).p = p + 1;
increments the iteratorp
, so it now points to the second element (2) of the vector.x.insert(p, 5);
inserts the value 5 at the position pointed by the iteratorp
, which is the second position in the vector. The vector will now contain [1, 5, 2, 3, 7].x.erase(x.begin() + 3);
erases the element at the fourth position (index 3) of the vector. After this operation, the vector will contain [1, 5, 2, 7].
THE STUDENT FRIENDLY BOOK TO LEARN C++. Let us C++ by Famous author Yashavant Kanetkar Indian writer |
Topic wise C++ MCQs Index ≡
- Principles of Object-Oriented Programming
- Beginning with C++
- Tokens, Expressions, and Control Structures
- Functions in C++
- Classes and Objects
- Constructors and Destructors
- Operator Overloading
- Inheritance
- Pointers, Virtual Functions, and Polymorphism
- Managing Console I/O Operations
- Working with Files
- Templates
- Exception Handling
- Standard Template Library
- Manipulating Strings
- New Features in ANSI C++ Standards
- Object-Oriented System Development
1 Comments
dsvd
ReplyDelete