wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Final Exam in Data Structures and Algorithm

Total questions: 65

Worksheet time: 46mins

Name
Class
Date
1.

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] << " ";

a)

70 85 95 60 75

b)

70 85 90 60 75

c)

95 85 90 60 75

d)

Compilation error

2.

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;

a)

2 – Index of first occurrence of 3

b)

-1 – Element not found

c)

0 – Always returns first element

d)

Compilation error

3.

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]);

    }

}

a)

Bubble sort – swaps adjacent elements repeatedly

b)

Selection sort – selects minimum at each iteration

c)

Insertion sort – inserts elements in order

d)

Quick sort – divides array recursively

4.

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;

}

a)

3 – Array is sorted, key at index 3

b)

2 – Middle index returned

c)

1 – Incorrect search

d)

0 – Indexing error

5.

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;

a)

150 – All elements included

b)

100 – Partial sum

c)

120 – Some elements skipped

d)

200 – Incorrect sum

6.

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] << " ";

a)

Rotates array left by 1

b)

Rotates array right by 1

c)

Reverses array

d)

Shifts elements incorrectly

7.

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;

a)

9 – Maximum element

b)

7

c)

5

d)

1

8.

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] << " ";

a)

Insertion sort – inserts elements into sorted subarray

b)

Bubble sort

c)

Selection sort

d)

Merge sort

9.

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;

a)

5 key not found

b)

0

c)

4

d)

3

10.

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] << " ";

}

a)

1 3 3 5 5 7 – Even numbers incremented

b)

2 3 4 5 6 7

c)

1 2 3 4 5 6

d)

1 3 3 5 5 6

11.

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] << " ";

}

a)

1 4 9 16

b)

1 2 3 4

c)

1 8 27 64

d)

1 2 6 12

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;

a)

3

b)

2

c)

5

d)

0

13.

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]<<" ";

a)

Selection sort – selects minimum in each iteration

b)

Bubble sort

c)

Insertion sort

d)

Quick sort

14.

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]<<" ";

a)

2 4 6 8 10

b)

1 2 3 4 5

c)

1 4 9 16 25

d)

5 10 15 20 25

15.

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;

a)

3

b)

2

c)

5

d)

0

16.

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;

}

a)

20 10

b)

10 20

c)

0 10

d)

Compilation error

17.

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;

}

a)

15 – Removes top element

b)

5 – Removes bottom element

c)

10 – Middle element

d)

Compilation error

18.

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;

}

a)

Prevents writing beyond stack size

b)

Ensures correct output

c)

Optimizes memory

d)


D. Compilation

19.

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;

}

a)

3 2 1 – Top to bottom

b)

1 2 3 – Bottom to top

c)

3 1 2 – Random

d)

Compilation error

20.

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;

}

a)

100 – Top element after pop

b)

200 – Popped element

c)

0 – Empty stack

d)

Compilation error

21.

Why is the LIFO property important in this stack implementation?

(a)  

22.

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;

}

a)

Prevents popping from an empty stack

b)

Prevents pushing beyond capacity

c)

Ensures correct output

d)

Compilation error

23.

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;

}

a)

2 4 6 8 – Bottom to top

b)

8 6 4 2 – Top to bottom

c)

2 4 6 – Only first three

d)

Compilation error

24.

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;

}

a)

Prevents adding beyond stack size

b)

Prevents popping empty stack

c)

Traverses stack

d)

. Compilation error

25.

Why would you use a stack in expression evaluation?

a)

To reverse order of operations (LIFO)

b)

To sort numbers

c)

To traverse arrays

d)

To store files

26.

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;

}

a)

10 20

b)

. 20 10

c)

0 10

d)

Compilation error

27.

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)  

28.

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)  

29.

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)  

30.

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)  

31.

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;

}

a)

Wraps around to reuse empty spaces

b)

Stops enqueueing after one element

c)

Deletes elements automatically

d)

Compilation error

32.

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;

}

a)

Prevents dequeue from an empty queue

b)

Prevents enqueue beyond capacity

c)

Traverses queue

d)

Compilation error

33.

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;

}

a)

Supports enqueue at rear and dequeue at front

b)

Only supports front enqueue

c)

Only supports rear dequeue

d)

Compilation error

34.

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)  

35.

Why would you use a queue for process scheduling?

(a)  

36.

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)  

37.

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)  

38.

Why is a binary tree used instead of a linear structure here?

(a)  

39.

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)  

40.

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;

}

a)

15 – Right child of root

b)

10 – Root

c)

. 5 – Left child

d)

Compilation error

41.

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;

}

a)

2 – Maximum depth from root to leaf

b)

3 – Incorrect calculation

c)

1 – Only root counted

d)

0 – Empty tree

42.

Why is recursion used in tree traversals?

(a)  

43.

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;

}

a)

1 2 3

b)

3 1 2

c)

2 1 3

d)

1 3 2

44.

How can a binary search tree be used for searching?

a)

Compare target with root, recursively traverse left/right

b)

Traverse all nodes randomly

c)

Only check root node

d)

. Traverse left only

45.

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;

}

a)

3 1 2

b)

1 2 3

c)

3 2 1

d)

Compilation error

46.

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)  

47.

How does binary search improve search efficiency?

(a)  

48.

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)  

49.

How many comparisons will bubble sort make on this array?

int arr[4]={4,3,2,1};

(a)  

50.

Why is insertion sort efficient for nearly sorted arrays?

(a)  

51.

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]<<" ";

a)

12 22 25 64

b)

64 25 12 22

c)

22 12 25 64

d)

Compilation error

52.

How many swaps will selection sort make in the best case?

a)

n-1 – Minimum swaps for n elements

b)

0

c)

n/2

d)

n

53.

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]<<" ";

a)

10 20 30 40 50 60

b)

50 10 20 30 40 60

c)

10 20 30 50 40 60

d)

Compilation error

54.

How is quicksort different from bubble sort?

(a)  

55.

What will be printed by merge sort on this array?

int arr[5]={5,2,4,1,3};

// Assume mergeSort is implemented correctly

(a)  

56.

Why is hashing efficient for search operations?

(a)  

57.

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)  

58.

How does the complexity of insertion, selection, and bubble sort compare in worst-case scenario?

a)

All O(n²) – Quadratic time

b)

Insertion O(n), others O(n²)

c)

Bubble O(n), others O(log n)

d)

All O(n log n)

59.

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)  

60.

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)  

61.

What is the best-case complexity of bubble sort?

a)

O(n) – Already sorted

b)

O(n²)

c)

O(log n)

d)

O(1)

62.

What is the worst-case complexity of quicksort?

(a)  

63.

How many comparisons are needed for linear search in worst-case?

(a)  

64.

How many comparisons are needed for binary search in worst-case?

(a)  

65.

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)