0% found this document useful (0 votes)
23 views12 pages

Final Exam Paper Solution

This document outlines the final term examination for the Applied Programming course at the National University of Computer & Emerging Sciences, Karachi. It includes instructions for students, a series of programming and algorithm-related questions, and coding tasks related to data structures such as binary trees, AVL trees, and graphs. The exam is scheduled for December 31, 2024, and consists of 10 questions with a total of 50 marks.

Uploaded by

snsmarch2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views12 pages

Final Exam Paper Solution

This document outlines the final term examination for the Applied Programming course at the National University of Computer & Emerging Sciences, Karachi. It includes instructions for students, a series of programming and algorithm-related questions, and coding tasks related to data structures such as binary trees, AVL trees, and graphs. The exam is scheduled for December 31, 2024, and consists of 10 questions with a total of 50 marks.

Uploaded by

snsmarch2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

National University of Computer & Emerging Sciences, Karachi.

FAST School of Computing,


Final Term Examination, Fall 2024
December 31, 2024, 06:00 pm – 09:00 pm

Course Code: CS-4002 Course Name: Applied Programming


Instructor: Waqar Hussain
Student’s Roll No: Section:

Instructions:
 Read each question completely before answering it. There are 10 questions and 3 pages.
 Students are not allowed to write anything on the question paper except roll number & section.
 Submit the question paper along with the answer script.

Time Allowed: 180 minutes Total Marks: 50


Section: Dry Run

1. Given the following preorder traversal sequence of a binary search tree:


Pre-order Sequence: 30, 20, 10, 15, 25, 23, 39, 35, 42
 Construct the binary search tree (BST) using the given preorder traversal sequence. (3 Marks)

 Find the Post-order traversal of the constructed tree. (2 Marks)

Final Postorder Sequence: 15, 10, 23, 25, 20, 35, 42, 39, 30

2. You are given the following sequence of integers to insert into an AVL tree:
Sequence: 25, 27, 30, 7, 5, 20, 10, 22, 15, 35, 40
Construct the AVL tree by inserting the numbers while ensuring it remains balanced after each
insertion. Also, explain any rotations performed while balancing the AVL tree. (7 Marks)

Insert 25

Insert 27

1
Insert 30

 RR Rotation 

Insert 7

Insert 5

 LL Rotation 

Insert 20

 LR Rotation 

2
Insert 10

Insert 22

Insert 15

 RL Rotation 

3
Insert 35

 RR Rotation 

Insert 40

3. Consider the following BST. Explain, with reasoning, what element will replace if the node to be
deleted is the root node. (3 Marks)

In a Binary Search Tree (BST), when the root node is deleted, it must be replaced by either:

 The inorder successor (smallest value in the right subtree). [8 will replace root node]
 The inorder predecessor (largest value in the left subtree). [3 will replace root node]

4
4. Perform a dry run of the Quick Sort algorithm using the array: [8, 4, 7, 3, 10]. Choose a pivot
element as the last element of the array. (5 Marks)

Summary of Steps
 Partition [8, 4, 7, 3, 10] with pivot 10 → [8, 4, 7, 3, 10].
 Partition [8, 4, 7, 3] with pivot 3 → [3, 4, 7, 8].
 Partition [4, 7, 8] with pivot 8 → [4, 7, 8].
 Partition [4, 7] with pivot 7 → [4, 7].
 Combine partitions → [3, 4, 7, 8, 10].
5. Perform the Breath-First Search – BFS and Depth-First Search – DFS on the following graph. (5 Marks)

Breadth-First Search (BFS)

Initialization:

 Queue: To maintain the nodes to be visited.


 Visited Set: To track the nodes already visited.

Steps:

1. Start with A. Add A to the queue and mark it as visited.


o Queue: [A]
o Visited: {A}
2. Dequeue A and explore its neighbors (B, C).
o Add unvisited neighbors to the queue and mark them as visited.
o Queue: [B, C]
o Visited: {A, B, C}
o Print: A
3. Dequeue B and explore its neighbors (A, E, F).
o Add unvisited neighbors (E, F) to the queue.
o Queue: [C, E, F]
o Visited: {A, B, C, E, F}
o Print: B
4. Dequeue C and explore its neighbors (A, B, D, F).
o Add unvisited neighbors (D) to the queue.
o Queue: [E, F, D]
o Visited: {A, B, C, E, F, D}
o Print: C
5. Dequeue E and explore its neighbors (B, F).
o All neighbors are already visited.
o Queue: [F, D]
o Visited: {A, B, C, E, F, D}

5
oPrint: E
6. Dequeue F and explore its neighbors (B, C, E, G).
o Add unvisited neighbors (G) to the queue.
o Queue: [D, G]
o Visited: {A, B, C, E, F, D, G}
o Print: F
7. Dequeue D and explore its neighbors (C, G).
o All neighbors are already visited.
o Queue: [G]
o Visited: {A, B, C, E, F, D, G}
o Print: D
8. Dequeue G and explore its neighbors (F, D).
o All neighbors are already visited.
o Queue: []
o Visited: {A, B, C, E, F, D, G}
o Print: G

BFS Order: A, B, C, E, F, D, G

Depth-First Search (DFS)

Initialization:

 Stack: To maintain the nodes to be explored.


 Visited Set: To track the nodes already visited.

Steps:

1. Start with A. Push A to the stack and mark it as visited.


o Stack: [A]
o Visited: {A}
2. Pop A and explore its neighbors (B, C).
o Push unvisited neighbors (B, C) to the stack.
o Stack: [C, B]
o Visited: {A, B, C}
o Print: A
3. Pop B and explore its neighbors (A, E, F).
o Push unvisited neighbors (E, F) to the stack.
o Stack: [C, F, E]
o Visited: {A, B, C, E, F}
o Print: B
4. Pop E and explore its neighbors (B, F).
o All neighbors are already visited.
o Stack: [C, F]
o Visited: {A, B, C, E, F}
o Print: E
5. Pop F and explore its neighbors (B, C, E, G).
o Push unvisited neighbors (G) to the stack.
o Stack: [C, G]
o Visited: {A, B, C, E, F, G}

6
o Print: F
6. Pop G and explore its neighbors (F, D).
o Push unvisited neighbors (D) to the stack.
o Stack: [C, D]
o Visited: {A, B, C, E, F, G, D}
o Print: G
7. Pop D and explore its neighbors (C, G).
o All neighbors are already visited.
o Stack: [C]
o Visited: {A, B, C, E, F, G, D}
o Print: D
8. Pop C and explore its neighbors (A, B, D, F).
o All neighbors are already visited.
o Stack: []
o Visited: {A, B, C, E, F, G, D}
o Print: C

DFS Order: A, B, E, F, G, D, C

6. You are given a sequence of integers to insert the elements into the following 2-3 Tree. Construct the
tree by inserting the numbers one by one. (5 Marks)
Sequence: 42, 44, 11

Insert 42

7
Insert 44

Insert 11

8
Section: Coding

7. If we want to create a binary tree whose nodes contain integer values, we can represent the nodes
using instances of the following Java class.

public class Node {


public int value;
public Node left;
public Node right;
}

Write a function that takes root node as an input and returns the sum of the values contained in all
of the nodes of the binary tree with root n. Hint: Recursion is your friend. (5 Marks)

public int sum(Node n) {


if (n == null) {
return 0;
} else {
return n.value + sum(n.left) + sum(n.right);
}
}

8. Write a pseudocode of an undirected graph using an adjacency matrix representation.


Consider the following vertices have been added {0, 1, 2, 3, 4, 5}. Your implementation should
include:
 A method to add an undirected edge between any two vertices (e.g. draw an edge between
vertex 2 & vertex 3). (3 Marks)
 A method to display the graph as an adjacency matrix. (2 Marks)

InitializeGraph(numVertices):
adjMatrix ← 2D array of size numVertices x numVertices
For i from 0 to numVertices - 1:
For j from 0 to numVertices - 1:
adjMatrix[i][j] ← 0

AddEdge(vertex1, vertex2):
adjMatrix[vertex1][vertex2] ← 1
adjMatrix[vertex2][vertex1] ← 1 # Because the graph is undirected

DisplayGraph():

9
Print "Graph Edges:"
For i from 0 to numVertices - 1:
For j from i + 1 to numVertices - 1: # Avoid duplicate edges
If adjMatrix[i][j] = 1:
Print "Vertex", i, "-> Vertex", j

9. A company has employees of different roles: Manager and Developer. Each employee has a method
called calculateBonus() to calculate their yearly bonus.
 The Manager gets a fixed bonus of PKR 5000.
 The Developer gets a bonus of PKR 2000 plus PKR 10 for every line of code written.

Write the following snippets of code:

 Define the Employee class with a method calculateBonus() and at least one relevant attribute.
(1 Mark)
 Extend the Employee class to create Manager and Developer classes. Override the
calculateBonus() method in each class. (2 Marks)
 Write a main method that:
o Creates an array of Employee objects containing at least one Manager and one
Developer. (1 Mark)
o Demonstrates runtime polymorphism by calling the calculateBonus() method on each
object. (1 Mark)

public class Employee {


protected String name;

public Employee(String name) {


this.name = name;
}

public int calculateBonus() {


return 0;
}
}

class Manager extends Employee {

public Manager(String name) {


super(name);
}

@Override
public int calculateBonus() {
return 5000;
}
}

class Developer extends Employee {


private int linesOfCode;

public Developer(String name, int linesOfCode) {

10
super(name);
this.linesOfCode = linesOfCode;
}

@Override
public int calculateBonus() {
return 2000 + (10 * linesOfCode);
}
}

public class Main {


public static void main(String[] args) {
Employee[] employees = new Employee[2];

employees[0] = new Manager("Alice");


employees[1] = new Developer("Bob", 300);

for (Employee employee : employees) {


System.out.println(employee.name + "'s Bonus: PKR " +
employee.calculateBonus());
}
}
}

10. You are given a singly linked list where only the head node is maintained. The linked list initially
contains the following elements:
Head -> 10 -> 20 -> 30 -> 40 -> null
Implement the deleteNode(int value) method in a LinkedList class. This method should delete the
first occurrence of a node with the specified value. If the value does not exist in the list, the method
should print an appropriate message. (5 Marks)

deleteNode(value):
// Case 1: If the list is empty
if head is null:
Print "The list is empty. Nothing to delete."
return

// Case 2: If the head node contains the value


if head.data == value:
head ← head.next // Move head to the next node
Print "Node with value", value, "deleted."
return

// Case 3: Traverse the list to find the node to delete


current ← head
while current.next is not null AND current.next.data ≠ value:
current ← current.next

// Case 4: If the value is not found


if current.next is null:
Print "Node with value", value, "not found in the list."
11
return

// Case 5: Value found, delete the node


current.next ← current.next.next // Bypass the node to be deleted
Print "Node with value", value, "deleted."

Good Luck

12

You might also like