Font size
WorksheetsFinal Exam in Data Structures and Algorithm
Total questions: 65
Worksheet time: 46mins
What will be the output of this code?
int marks[5] = {70, 85, 90, 60, 75};
marks[2] = 95;
for(int i=0;i<5;i++)
cout << marks[i] << " ";
70 85 95 60 75
70 85 90 60 75
95 85 90 60 75
Compilation error
Why does the variable pos store this value after execution?
int arr[5] = {1, 2, 3, 4, 5};
int key = 3, pos = -1;
for(int i=0;i<5;i++){
if(arr[i]==key){ pos=i; break;}
}
cout << pos;
2 – Index of first occurrence of 3
-1 – Element not found
0 – Always returns first element
Compilation error
How does this code sort the array, and which algorithm is it using?
int arr[5] = {5, 2, 4, 1, 3};
for(int i=0;i<5-1;i++){
for(int j=0;j<5-i-1;j++){
if(arr[j]>arr[j+1])
swap(arr[j], arr[j+1]);
}
}
Bubble sort – swaps adjacent elements repeatedly
Selection sort – selects minimum at each iteration
Insertion sort – inserts elements in order
Quick sort – divides array recursively
What index will be printed, and why is binary search appropriate here?
int arr[6] = {1, 3, 5, 7, 9, 11};
int key = 7, low=0, high=5, mid;
while(low<=high){
mid=(low+high)/2;
if(arr[mid]==key){ cout << mid; break;}
else if(arr[mid]<key) low=mid+1;
else high=mid-1;
}
3 – Array is sorted, key at index 3
2 – Middle index returned
1 – Incorrect search
0 – Indexing error
How does this loop compute the sum of the array elements?
int arr[5] = {10, 20, 30, 40, 50};
int sum=0;
for(int i=0;i<5;i++) sum+=arr[i];
cout << sum;
150 – All elements included
100 – Partial sum
120 – Some elements skipped
200 – Incorrect sum
How does this code rotate the array?
int arr[4] = {1, 2, 3, 4};
int temp = arr[0];
for(int i=0;i<3;i++) arr[i]=arr[i+1];
arr[3]=temp;
for(int i=0;i<4;i++) cout << arr[i] << " ";
Rotates array left by 1
Rotates array right by 1
Reverses array
Shifts elements incorrectly
What will be the output of finding the maximum element?
int arr[5]={1,3,5,7,9};
int max=arr[0];
for(int i=1;i<5;i++){
if(arr[i]>max) max=arr[i];
}
cout << max;
9 – Maximum element
7
5
1
How does this insertion sort work on the given array?
int arr[5]={5,4,3,2,1};
for(int i=1;i<5;i++){
int key=arr[i], j=i-1;
while(j>=0 && arr[j]>key){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
}
for(int i=0;i<5;i++) cout << arr[i] << " ";
Insertion sort – inserts elements into sorted subarray
Bubble sort
Selection sort
Merge sort
What is the value of i if key is not found?
int arr[5]={2,4,6,8,10};
int key=5, i=0;
while(i<5 && arr[i]!=key) i++;
cout << i;
5 key not found
0
4
3
How does this code modify the array?
int arr[6]={1,2,3,4,5,6};
for(int i=0;i<6;i++){
if(arr[i]%2==0) arr[i]+=1;
cout << arr[i] << " ";
}
1 3 3 5 5 7 – Even numbers incremented
2 3 4 5 6 7
1 2 3 4 5 6
1 3 3 5 5 6
What will be printed when squaring each element?
int arr[4]={1,2,3,4};
for(int i=0;i<4;i++){
arr[i]*=arr[i];
cout << arr[i] << " ";
}
1 4 9 16
1 2 3 4
1 8 27 64
1 2 6 12
How many odd numbers are in the array?
int arr[5]={1,2,3,4,5};
int count=0;
for(int i=0;i<5;i++){
if(arr[i]%2!=0) count++;
}
cout<<count;
3
2
5
0
How does this selection sort code organize elements?
int arr[3]={5,1,3};
for(int i=0;i<3;i++)
for(int j=i+1;j<3;j++)
if(arr[i]>arr[j]) swap(arr[i], arr[j]);
for(int i=0;i<3;i++) cout<<arr[i]<<" ";
Selection sort – selects minimum in each iteration
Bubble sort
Insertion sort
Quick sort
What will be the output if all elements are doubled?
int arr[5]={1,2,3,4,5};
for(int i=0;i<5;i++){
arr[i]=arr[i]*2;
}
for(int i=0;i<5;i++) cout<<arr[i]<<" ";
2 4 6 8 10
1 2 3 4 5
1 4 9 16 25
5 10 15 20 25
How many odd numbers exist in the array?
int arr[5]={1,2,3,4,5};
int count=0;
for(int i=0;i<5;i++){
if(arr[i]%2!=0) count++;
}
cout<<count;
3
2
5
0
What will be the output of this code after two push operations?
#include<iostream>
using namespace std;
int main(){
int stack[5], top=-1;
stack[++top]=10;
stack[++top]=20;
cout<<stack[top]<<" "<<stack[top-1];
return 0;
}
20 10
10 20
0 10
Compilation error
How does this code perform a pop operation, and what will it print?
#include<iostream>
using namespace std;
int main(){
int stack[5]={5,10,15}, top=2;
cout<<stack[top--];
return 0;
}
15 – Removes top element
5 – Removes bottom element
10 – Middle element
Compilation error
Why does this code check for overflow?
#include<iostream>
using namespace std;
int main(){
int stack[3], top=-1;
for(int i=0;i<4;i++){
if(top==2) cout<<"Overflow\n";
else stack[++top]=i*5;
}
return 0;
}
Prevents writing beyond stack size
Ensures correct output
Optimizes memory
D. Compilation
How will this code display the current stack elements?
#include<iostream>
using namespace std;
int main(){
int stack[4]={1,2,3}, top=2;
for(int i=top;i>=0;i--) cout<<stack[i]<<" ";
return 0;
}
3 2 1 – Top to bottom
1 2 3 – Bottom to top
3 1 2 – Random
Compilation error
What is the output after pushing and popping?
#include<iostream>
using namespace std;
int main(){
int stack[3], top=-1;
stack[++top]=100;
stack[++top]=200;
top--;
cout<<stack[top];
return 0;
}
100 – Top element after pop
200 – Popped element
0 – Empty stack
Compilation error
Why is the LIFO property important in this stack implementation?
(a)
How does this code check for underflow?
#include<iostream>
using namespace std;
int main(){
int stack[3], top=-1;
if(top==-1) cout<<"Underflow";
return 0;
}
Prevents popping from an empty stack
Prevents pushing beyond capacity
Ensures correct output
Compilation error
What will be printed by this stack traversal code?
#include<iostream>
using namespace std;
int main(){
int stack[5]={2,4,6,8}, top=3;
for(int i=0;i<=top;i++) cout<<stack[i]<<" ";
return 0;
}
2 4 6 8 – Bottom to top
8 6 4 2 – Top to bottom
2 4 6 – Only first three
Compilation error
How does this code implement a push operation safely?
#include<iostream>
using namespace std;
int main(){
int stack[2], top=-1;
int n=2;
for(int i=0;i<n;i++){
if(top<1) stack[++top]=i*10;
else cout<<"Overflow\n";
}
return 0;
}
Prevents adding beyond stack size
Prevents popping empty stack
Traverses stack
. Compilation error
Why would you use a stack in expression evaluation?
To reverse order of operations (LIFO)
To sort numbers
To traverse arrays
To store files
What will be the output after enqueueing two elements?
#include<iostream>
using namespace std;
int main(){
int queue[5], front=0, rear=-1;
queue[++rear]=10;
queue[++rear]=20;
cout<<queue[front]<<" "<<queue[rear];
return 0;
}
10 20
. 20 10
0 10
Compilation error
How does this code perform a dequeue operation, and what will it print?
#include<iostream>
using namespace std;
int main(){
int queue[5]={5,10,15}, front=0, rear=2;
cout<<queue[front++];
return 0;
}
(a)
Why is it important to check for queue overflow?
#include<iostream>
using namespace std;
int main(){
int queue[3], front=0, rear=-1;
for(int i=0;i<4;i++){
if(rear==2) cout<<"Overflow\n";
else queue[++rear]=i*5;
}
return 0;
}
(a)
How will this code display the queue elements?
#include<iostream>
using namespace std;
int main(){
int queue[4]={1,2,3}, front=0, rear=2;
for(int i=front;i<=rear;i++) cout<<queue[i]<<" ";
return 0;
}
(a)
What will be printed after enqueue and dequeue operations?
#include<iostream>
using namespace std;
int main(){
int queue[3], front=0, rear=-1;
queue[++rear]=100;
queue[++rear]=200;
front++;
cout<<queue[front];
return 0;
}
(a)
How does the circular queue prevent overflow in this code?
#include<iostream>
using namespace std;
int main(){
int queue[3], front=0, rear=0, n=3;
for(int i=0;i<5;i++){
if((rear+1)%n==front) cout<<"Overflow\n";
else queue[rear]=(i+1)*10, rear=(rear+1)%n;
}
return 0;
}
Wraps around to reuse empty spaces
Stops enqueueing after one element
Deletes elements automatically
Compilation error
Why does this code check for underflow?
#include<iostream>
using namespace std;
int main(){
int queue[3], front=0, rear=-1;
if(front>rear) cout<<"Underflow";
return 0;
}
Prevents dequeue from an empty queue
Prevents enqueue beyond capacity
Traverses queue
Compilation error
How does this code implement a double-ended queue (deque)?
#include<iostream>
using namespace std;
int main(){
int deque[5], front=0, rear=-1;
deque[++rear]=10; // enqueue rear
deque[++rear]=20;
front++; // dequeue front
cout<<deque[front];
return 0;
}
Supports enqueue at rear and dequeue at front
Only supports front enqueue
Only supports rear dequeue
Compilation error
What will this code print after multiple enqueue and dequeue operations?
#include<iostream>
using namespace std;
int main(){
int queue[5], front=0, rear=-1;
queue[++rear]=1;
queue[++rear]=2;
queue[++rear]=3;
front++;
cout<<queue[front]<<" "<<queue[rear];
return 0;
}
(a)
Why would you use a queue for process scheduling?
(a)
What will be the output of this preorder traversal?
#include<iostream>
using namespace std;
struct Node{
int data;
Node left,right;
};
void preorder(Node* root){
if(root){
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}
int main(){
Node n1={1,NULL,NULL}, n2={2,NULL,NULL}, n3={3,&n1,&n2};
preorder(&n3);
return 0;
}
(a)
How does inorder traversal visit nodes?
#include<iostream>
using namespace std;
struct Node{
int data;
Node left,right;
};
void inorder(Node* root){
if(root){
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
}
int main(){
Node n1={1,NULL,NULL}, n2={2,NULL,NULL}, n3={3,&n1,&n2};
inorder(&n3);
return 0;
}
(a)
Why is a binary tree used instead of a linear structure here?
(a)
How many nodes will this level-order traversal print?
#include<iostream>
using namespace std;
struct Node{ int data; Node left,right;};
int main(){
Node n1={1,NULL,NULL}, n2={2,NULL,NULL}, n3={3,&n1,&n2};
cout<<n3.data<<" "<<n3.left->data<<" "<<n3.right->data;
return 0;
}
(a)
What is the output after inserting a node in a binary search tree?
#include<iostream>
using namespace std;
struct Node{ int data; Node left,right;};
int main(){
Node n1={10,NULL,NULL}, n2={5,NULL,NULL}, n3={15,NULL,NULL};
Node* root=&n1;
root->left=&n2;
root->right=&n3;
cout<<root->right->data;
return 0;
}
15 – Right child of root
10 – Root
. 5 – Left child
Compilation error
How does this code compute the height of the binary tree?
#include<iostream>
using namespace std;
struct Node{ int data; Node left,right;};
int height(Node* root){
if(!root) return 0;
return 1 + max(height(root->left), height(root->right));
}
int main(){
Node n1={1,NULL,NULL}, n2={2,NULL,NULL}, n3={3,&n1,&n2};
cout<<height(&n3);
return 0;
}
2 – Maximum depth from root to leaf
3 – Incorrect calculation
1 – Only root counted
0 – Empty tree
Why is recursion used in tree traversals?
(a)
What will be the output of this postorder traversal?
#include<iostream>
using namespace std;
struct Node{ int data; Node left,right;};
void postorder(Node* root){
if(root){
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}
}
int main(){
Node n1={1,NULL,NULL}, n2={2,NULL,NULL}, n3={3,&n1,&n2};
postorder(&n3);
return 0;
}
1 2 3
3 1 2
2 1 3
1 3 2
How can a binary search tree be used for searching?
Compare target with root, recursively traverse left/right
Traverse all nodes randomly
Only check root node
. Traverse left only
What will be printed if we traverse the tree in level-order manually?
#include<iostream>
using namespace std;
struct Node{ int data; Node left,right;};
int main(){
Node n1={1,NULL,NULL}, n2={2,NULL,NULL}, n3={3,&n1,&n2};
cout<<n3.data<<" "<<n3.left->data<<" "<<n3.right->data;
return 0;
}
3 1 2
1 2 3
3 2 1
Compilation error
What will be the output of this linear search code?
#include<iostream>
using namespace std;
int main(){
int arr[5]={2,4,6,8,10};
int key=6, pos=-1;
for(int i=0;i<5;i++){
if(arr[i]==key){ pos=i; break; }
}
cout<<pos;
}
(a)
How does binary search improve search efficiency?
(a)
What is the output of this binary search code?
#include<iostream>
using namespace std;
int main(){
int arr[6]={1,3,5,7,9,11};
int key=7, low=0, high=5, mid;
while(low<=high){
mid=(low+high)/2;
if(arr[mid]==key){ cout<<mid; break; }
else if(arr[mid]<key) low=mid+1;
else high=mid-1;
}
}
(a)
How many comparisons will bubble sort make on this array?
int arr[4]={4,3,2,1};
(a)
Why is insertion sort efficient for nearly sorted arrays?
(a)
What will be the output of selection sort on this array?
int arr[4]={64,25,12,22};
for(int i=0;i<3;i++)
for(int j=i+1;j<4;j++)
if(arr[i]>arr[j]) swap(arr[i],arr[j]);
for(int i=0;i<4;i++) cout<<arr[i]<<" ";
12 22 25 64
64 25 12 22
22 12 25 64
Compilation error
How many swaps will selection sort make in the best case?
n-1 – Minimum swaps for n elements
0
n/2
n
What is the output after inserting 50 at the correct position using insertion sort?
int arr[5]={10,20,30,40,60};
int key=50,i=4;
while(i>=0 && arr[i]>key){
arr[i+1]=arr[i]; i--;
}
arr[i+1]=key;
for(int i=0;i<6;i++) cout<<arr[i]<<" ";
10 20 30 40 50 60
50 10 20 30 40 60
10 20 30 50 40 60
Compilation error
How is quicksort different from bubble sort?
(a)
What will be printed by merge sort on this array?
int arr[5]={5,2,4,1,3};
// Assume mergeSort is implemented correctly
(a)
Why is hashing efficient for search operations?
(a)
What is the output of this code using linear search?
int arr[4]={10,20,30,40};
int key=25, pos=-1;
for(int i=0;i<4;i++){
if(arr[i]==key){ pos=i; break; }
}
cout<<pos;
(a)
How does the complexity of insertion, selection, and bubble sort compare in worst-case scenario?
All O(n²) – Quadratic time
Insertion O(n), others O(n²)
Bubble O(n), others O(log n)
All O(n log n)
What will be the output of this code deleting an element from array?
int arr[5]={1,2,3,4,5};
int pos=2;
for(int i=pos;i<4;i++) arr[i]=arr[i+1];
for(int i=0;i<4;i++) cout<<arr[i]<<" ";
(a)
What will be the output of this code after inserting in a sorted array?
int arr[5]={1,3,5,7};
int key=4, i=3;
while(i>=0 && arr[i]>key){
arr[i+1]=arr[i]; i--;
}
arr[i+1]=key;
for(int i=0;i<5;i++) cout<<arr[i]<<" ";
(a)
What is the best-case complexity of bubble sort?
O(n) – Already sorted
O(n²)
O(log n)
O(1)
What is the worst-case complexity of quicksort?
(a)
How many comparisons are needed for linear search in worst-case?
(a)
How many comparisons are needed for binary search in worst-case?
(a)
What will this code print after reversing an array?
int arr[4]={1,2,3,4};
for(int i=0;i<2;i++) swap(arr[i],arr[3-i]);
for(int i=0;i<4;i++) cout<<arr[i]<<" ";
(a)
