Answers To Sample Test
Answers To Sample Test
25/25p
1.
Which of the following is not a feature of Object-Oriented Programming?
Polymorphism
Abstraction
Sequential
Encapsulation
2.
What distinguishes object-oriented programming from procedural programming?
Dependency on compilers
3.
What does the cin object in C++ represent?
Input stream
Output Stream
Namespace
Class
5.
Which of the following is true about constructors in C++?
6.
Which of the following is a non-linear data structure?
Stack
Array
Queue
Tree
7.
The worst-case time complexity of linear search is:
O(log n)
O(n)
O(n/2)
O(1)
9.
What is the correct way to declare a 2D array of integers in C++?
arr[3]
arr[][3]
arr[2][3]
int arr[2][3]
10.
What is the role of a pointer in C++?
Stores a class object
Refers to a function
Stores the address of another variable
Refers to a reference variable
Passage:
A disaster relief camp uses the following C++ code to manage the food distribution line. Elderly citizens are
given priority and temporarily pushed into a stack before the regular queue is served.
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
struct Person {
string name;
bool isElderly;
};
queue<Person> q;
stack<Person> s;
void distribute() {
while (!q.empty()) {
Person p = q.front(); q.pop();
if (p.isElderly)
s.push(p);
else {
while (!s.empty()) {
cout << s.top().name << " (priority)\n";
s.pop();
}
cout << p.name << " (normal)\n";
}
}
while (!s.empty()) {
cout << s.top().name << " (priority-end)\n";
s.pop();
}
}
int main() {
q.push({"Ali", false});
q.push({"Dadi", true});
q.push({"Riya", false});
q.push({"Nana", true});
q.push({"Kabir", false});
distribute();
}
11.
Which data structures are used for elderly and non-elderly people respectively?
Stack for non-elderly, queue for elderly
Queue for both
Stack for elderly, queue for non-elderly
Only queue is used
12.
What will be the output order printed by the distribute() function?
Ali (normal), Dadi (priority), Riya (normal), Nana (priority), Kabir (normal)
Ali (normal), Dadi (priority), Riya (normal), Nana (priority), Kabir (normal), Dadi (priority-
end), Nana (priority-end)
Ali (normal), Dadi (priority), Nana (priority), Riya (normal), Kabir (normal)
Dadi (normal), Adi (priority), Riya (normal), Nana (priority), Kabir (normal)
13.
Suppose one more elderly person {"Dadaji", true} is added after Kabir . Where in the output will
his name appear?
Before Riya
After Kabir
After Nana but before Kabir
At the very beginning
15.
If the stack used for elderly in the code is replaced with a linked list that inserts elderly members at the
beginning, what key functional change will occur in the program’s output order?
Elderly members will now be printed in the same order they arrived.
Elderly members will not be printed at all.
Elderly members will be printed in reverse order compared to stack.
There will be no change in the order of output.