wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

QUIZ SEC-H

Total questions: 70

Worksheet time: 35mins

Name
Class
Date
1.

What is the time complexity of in-order traversal in a binary tree?

a)

O(1)

b)

O(n)

c)

O(n^2)

d)

O(log n)

2.

Describe the pre-order traversal method for a binary tree.

a)

Left, Root, Right

b)

In pre-order traversal, the order of visiting nodes is: Root, Left, Right.

c)

Right, Root, Left

d)

Left, Right, Root

3.

How do you implement a binary tree in Java?

a)

class TreeNode { int value; }

b)

class TreeNode { int value; TreeNode left; TreeNode right; TreeNode(int value) { this.value = value; } } class BinaryTree { TreeNode root; void insert(int value) { // insertion logic } void traverseInOrder(TreeNode node) { // in-order traversal logic } }

c)

void traversePreOrder(TreeNode node) { }

d)

class BinaryTree { void addNode(int value) { } }

4.

What is the difference between a binary tree and a binary search tree?

a)

Both trees are identical in structure and function.

b)

A binary tree can only have two children, while a binary search tree can have more.

c)

A binary search tree allows duplicate values, while a binary tree does not.

d)

A binary tree has no specific ordering of nodes, while a binary search tree has an ordering property that facilitates efficient searching.

5.

Explain how to insert a node in a binary search tree.

a)

Insert the node at the root of the tree regardless of its value.

b)

Delete the smallest node in the tree before inserting the new node.

c)

Insert the node by traversing the tree based on comparisons until an empty position is found.

d)

Always place the new node as the left child of the current node.

6.

What are the properties of a balanced binary tree?

a)

A balanced binary tree has no nodes.

b)

A balanced binary tree must be a complete binary tree.

c)

A balanced binary tree can have any height difference between subtrees.

d)

A balanced binary tree has the property that the height of the left and right subtrees of any node differ by at most one.

7.

How can you determine if a binary tree is balanced?

a)

A binary tree is balanced if the height difference between left and right subtrees is no more than 1 for all nodes.

b)

A binary tree is balanced if it has a maximum depth of 3.

c)

A binary tree is balanced if it is a complete binary tree.

d)

A binary tree is balanced if all nodes have the same number of children.

8.

What is the maximum height of a binary tree with n nodes?

a)

n

b)

n/2

c)

log(n)

d)

n^2

9.

How do you calculate the height of a binary tree in Java?

a)

Use a recursive function to find the maximum height of left and right subtrees, returning max(leftHeight, rightHeight) + 1.

b)

Use an iterative approach with a queue to find the height.

c)

Calculate the height by summing the values of all nodes.

d)

Count the number of nodes in the tree.

10.

What is the process for deleting a node from a binary search tree?

a)

The process involves locating the node, handling three cases: leaf, one child, or two children.

b)

Only removing nodes with two children

c)

Deleting nodes without checking their children

d)

Removing the root node only

11.

What happens when you delete a node with two children in a binary search tree?

a)

The node is simply removed without any replacement.

b)

The entire tree is deleted when a node is removed.

c)

The node is replaced with a random node from the tree.

d)

Replace the node with its in-order predecessor or successor.

12.

How can you perform a level-order traversal of a binary tree?

a)

Traverse the tree using a stack to visit nodes in reverse order.

b)

Process all nodes in a single pass without using any data structure.

c)

Visit each node recursively before its children to achieve level-order.

d)

Use a queue to traverse the tree level by level, processing each node from left to right.

13.

What is the significance of the root node in a binary tree?

a)

The root node can be any node in the tree.

b)

The root node is the starting point of a binary tree and defines its structure.

c)

The root node is only used for traversal purposes.

d)

The root node is the last node in a binary tree.

14.

How do you find the minimum value in a binary search tree?

a)

The minimum value is found at the leftmost node of the binary search tree.

b)

The minimum value is found at the rightmost node of the binary search tree.

c)

The minimum value is the root node of the binary search tree.

d)

You can find the minimum value by averaging all the node values.

15.

What is a complete binary tree?

a)

A complete binary tree is a binary tree where all levels are filled equally.

b)

A complete binary tree is a binary tree where all levels are fully filled except possibly the last, which is filled from left to right.

c)

A complete binary tree is a tree with only one level.

d)

A complete binary tree has all nodes with two children.

16.

Explain the concept of a full binary tree.

a)

A full binary tree is a binary tree with at least one child for every node.

b)

A full binary tree is a binary tree where every node has 1 or 3 children.

c)

A full binary tree is a binary tree where every node has 0 or 2 children.

d)

A full binary tree is a tree with only leaf nodes.

17.

How can you convert a binary tree into a binary search tree?

a)

Use breadth-first traversal to collect values and sort them before rebuilding the tree.

b)

Convert the binary tree to a linked list and then back to a binary search tree.

c)

Perform pre-order traversal to collect values, then rebuild the tree using random values.

d)

Perform in-order traversal to collect values, then rebuild the tree using sorted values.

18.

What is the difference between iterative and recursive tree traversals?

a)

Iterative traversal does not use any data structures.

b)

Iterative traversal uses loops and explicit stacks; recursive traversal uses function calls and the call stack.

c)

Recursive traversal uses loops and explicit stacks.

d)

Iterative traversal is faster than recursive traversal.

19.

How do you handle duplicate values in a binary search tree?

a)

Ignore duplicates and keep the first occurrence only.

b)

Store duplicates in a linked list at each node.

c)

Delete all duplicate values from the tree.

d)

Replace duplicates with a special marker.

20.

What is the role of pointers in a binary tree implementation?

a)

Pointers store data values in a binary tree.

b)

Pointers are used only for memory allocation in trees.

c)

Pointers connect nodes in a binary tree, enabling traversal and manipulation.

d)

Pointers are not necessary for binary tree operations.

21.

What is the purpose of a base case in recursion?

a)

To enhance the performance of recursive functions.

b)

The purpose of a base case in recursion is to provide a stopping condition that prevents infinite recursion.

c)

To provide additional parameters for recursive calls.

d)

To define the maximum depth of recursion allowed.

22.

How do you design a recursive function in Java?

a)

You can create a recursive function by defining multiple base cases without a recursive case.

b)

A recursive function in Java should only call itself once without any updated arguments.

c)

A recursive function in Java is designed by defining a base case and a recursive case that calls the function with updated arguments.

d)

A recursive function in Java is designed by using only loops without any base case.

23.

What are the key components of a backtracking algorithm?

a)

Graph traversal, node expansion, heuristic evaluation, pruning

b)

Decision space, choice making, constraint checking, backtracking mechanism

c)

Sorting, searching, data structure manipulation, algorithm efficiency

d)

Dynamic programming, greedy choice, optimal substructure, solution space

24.

Explain the subset sum problem in the context of recursion.

a)

The subset sum problem is about finding the maximum element in a set.

b)

The subset sum problem is a method for calculating the average of a set of numbers.

c)

The subset sum problem is a decision problem that asks if a subset of a given set of integers can sum to a specific target value.

d)

The subset sum problem involves sorting a list of integers.

25.

How can recursion be used to solve the subset sum problem?

a)

Recursion can only be used for sorting numbers.

b)

Recursion simplifies the problem by eliminating numbers from consideration.

c)

Recursion is not applicable to the subset sum problem.

d)

Recursion can be used to explore all combinations of numbers to find a subset that sums to the target.

26.

What is the time complexity of generating all permutations of a set?

a)

O(n * n!)

b)

O(n^3)

c)

O(n!)

d)

O(n^2)

27.

Describe a scenario where backtracking is more efficient than brute force.

a)

Sudoku puzzle solving.

b)

Finding the shortest path in a graph with Dijkstra's algorithm.

c)

Generating all possible combinations of a set of numbers.

d)

Solving a simple arithmetic equation.

28.

How do you implement a recursive function to generate permutations in Java?

a)

Implement a sorting algorithm for permutations

b)

Create a static method without recursion

c)

public class Permutations { public static void permute(char[] arr, int index) { if (index == arr.length) { System.out.println(new String(arr)); return; } for (int i = index; i < arr.length; i++) { swap(arr, index, i); permute(arr, index + 1); swap(arr, index, i); } } private static void swap(char[] arr, int i, int j) { char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static void main(String[] args) { char[] arr = {'a', 'b', 'c'}; permute(arr, 0); } }

d)

Use a loop to generate permutations

29.

What is the difference between recursion and iteration?

a)

Recursion uses self-calls; iteration uses loops.

b)

Recursion is faster than iteration.

c)

Iteration uses self-calls; recursion uses loops.

d)

Recursion is a type of iteration.

30.

Can you provide an example of a problem that can be solved using backtracking?

a)

Traveling Salesman Problem

b)

Graph Coloring Problem

c)

Sudoku Solver

d)

N-Queens problem

31.

What is the primary data structure used to implement a stack in Java?

a)

TreeSet

b)

ArrayList or LinkedList

c)

Queue

d)

HashMap

32.

How do you push an element onto a stack in Java?

a)

stack.add(element);

b)

stack.insert(element);

c)

stack.pushTo(element);

d)

stack.push(element);

33.

What method would you use to pop an element from a stack?

a)

delete()

b)

shift()

c)

remove()

d)

pop()

34.

Explain the difference between a stack and a queue.

a)

A stack uses FIFO, while a queue uses LIFO.

b)

A stack uses LIFO, while a queue uses FIFO.

c)

Both stack and queue use LIFO.

d)

A stack allows random access, while a queue does not.

35.

What is the time complexity of accessing the top element of a stack?

a)

O(n)

b)

O(log n)

c)

O(1)

d)

O(n^2)

36.

Describe a real-world application of a stack.

a)

Task scheduling in operating systems

b)

Undo functionality in text editors

c)

Web browser history management.

d)

File storage management

37.

How do you implement a queue using two stacks in Java?

a)

Implement the queue using an array instead of stacks.

b)

Use three stacks to manage the queue operations.

c)

Use two stacks: one for enqueueing and one for dequeueing. Transfer elements from the first stack to the second when needed.

d)

Use a single stack for both enqueueing and dequeueing.

38.

What method is used to enqueue an element in a queue?

a)

add

b)

push

c)

enqueue

d)

insert

39.

What is the time complexity of dequeueing an element from a queue?

a)

O(1) for simple array

b)

O(1) for linked list or circular array, O(n) for simple array

c)

O(log n) for linked list

d)

O(n) for circular array

40.

List one application of a queue in computer science.

a)

Task scheduling in operating systems

b)

Network packet routing

c)

Data storage in databases

d)

Image processing in graphics

41.

What is a circular queue and how does it differ from a regular queue?

a)

A circular queue is a type of stack that allows elements to be added only at one end.

b)

A circular queue can only hold a fixed number of elements and cannot grow dynamically.

c)

A circular queue allows efficient use of space by wrapping around, while a regular queue can waste space and requires shifting elements.

d)

A regular queue is more efficient than a circular queue in terms of memory usage.

42.

How can you detect if a stack is empty in Java?

a)

Use stack.clear() to determine if a stack is empty.

b)

Check if stack.peek() returns null.

c)

Use stack.isEmpty() to check if a stack is empty.

d)

Use stack.size() to check if a stack is empty.

43.

What is the significance of the LIFO principle in stacks?

a)

The LIFO principle allows items to be removed in the order they were added.

b)

The LIFO principle ensures that the most recently added item is the first to be removed, facilitating efficient data management.

c)

The LIFO principle is used to prioritize older items over newer ones.

d)

The LIFO principle ensures that all items are removed simultaneously.

44.

Explain how a circular queue can help in resource management.

a)

A circular queue helps in resource management by efficiently utilizing a fixed-size buffer for resource allocation and deallocation.

b)

A circular queue limits resource allocation to a single user at a time.

c)

A circular queue is primarily used for sorting data in ascending order.

d)

A circular queue requires dynamic memory allocation for each resource request.

45.

What are the advantages of using a queue over a stack?

a)

Stacks allow for faster access to the most recently added item.

b)

Queues are less efficient for managing memory compared to stacks.

c)

Queues provide ordered processing, fair access to resources, and are ideal for task scheduling.

d)

Stacks are better for task scheduling than queues.

46.

What is the index of the first element in a Java array?

a)

5

b)

0

c)

1

d)

-1

47.

How do you declare an array of integers in Java?

a)

myArray int[];

b)

array int myArray;

c)

int[] myArray;

d)

int myArray[];

48.

What is the default value of an uninitialized integer array in Java?

a)

-1

b)

null

c)

undefined

d)

0

49.

How can you find the length of a string in Java?

a)

Call str.getLength() to find the string length.

b)

Use str.size() to determine the length of a string.

c)

Use str.lengthOf() to retrieve the length of a string.

d)

Use str.length() to get the length of a string.

50.

What method would you use to convert a string to lowercase in Java?

a)

myString.convertToLower()

b)

myString.lowercase()

c)

myString.toLowerCase()

d)

myString.toLower()

51.

How do you access the third element of a multidimensional array in Java?

a)

arrayName[2][0]

b)

arrayName[0][2]

c)

arrayName[0][1]

d)

arrayName[1][2]

52.

What is the output of the following code: String str = "Hello"; System.out.println(str.charAt(1));?

a)

H

b)

e

c)

l

d)

o

53.

Which method is used to sort an array in Java?

a)

List.sort()

b)

ArrayList.sort()

c)

Arrays.sort()

d)

Collections.sortArray()

54.

How can you concatenate two strings in Java?

a)

You can concatenate two strings in Java using the '+' operator or the String.concat() method.

b)

Using the StringBuilder.append() method

c)

Using the '-' operator

d)

Using the String.append() method

55.

What is the purpose of the StringBuilder class in Java?

a)

The purpose of the StringBuilder class in Java is to provide a mutable sequence of characters for efficient string manipulation.

b)

To store a fixed number of characters.

c)

To create immutable strings in Java.

d)

To convert strings to integers efficiently.

56.

How do you format a string to include a number in Java?

a)

Use StringBuilder.append('The number is: ', number) to format a string.

b)

Use String.format("The number is: %d", number) to format a string with a number.

c)

Use String.concat(number) to append a number to a string.

d)

Use number.toString() + ' is the number' to format a string.

57.

What is the difference between an array and an ArrayList in Java?

a)

The main difference is that arrays have a fixed size, whereas ArrayLists can change size dynamically.

b)

Arrays can store only primitive data types, while ArrayLists can store objects.

c)

ArrayLists are faster than arrays in all operations.

d)

Arrays can only be used in static contexts, while ArrayLists can be used in dynamic contexts.

58.

How do you initialize a 2D array in Java?

a)

dataType[][] arrayName = new dataType[rows][columns]; or dataType[][] arrayName = {{value1, value2}, {value3, value4}};

b)

dataType arrayName = new dataType[rows][columns];

c)

dataType[][] arrayName = new dataType[rows];

d)

dataType arrayName = new dataType[rows];

59.

What method would you use to check if a string contains a specific substring?

a)

Use the 'find()' method (Python)

b)

Check the length of the string

c)

Use the 'in' operator (Python) or 'includes()' method (JavaScript).

d)

Use the 'substring()' method (JavaScript)

60.

How can you sort an array of strings in alphabetical order in Java?

a)

sortArray(yourArray);

b)

Arrays.order(yourArray);

c)

Arrays.sort(yourArray);

d)

yourArray.sort();

61.

What is a singly linked list and how does it differ from an array?

a)

A singly linked list is a dynamic data structure with nodes linked sequentially, differing from an array which has a fixed size and allows direct index-based access.

b)

A singly linked list is a static data structure with a fixed size.

c)

A singly linked list stores elements in a contiguous block of memory.

d)

An array allows for dynamic resizing while a linked list does not.

62.

Explain the structure of a doubly linked list and its advantages over a singly linked list.

a)

A doubly linked list is more memory efficient than a singly linked list.

b)

A doubly linked list can only be traversed in one direction.

c)

A doubly linked list has nodes with a single pointer to the next node only.

d)

A doubly linked list has nodes with pointers to both the next and previous nodes, allowing bidirectional traversal, which provides advantages in node deletion and traversal efficiency over a singly linked list.

63.

What is a circular linked list and in what scenarios is it useful?

a)

A circular linked list is a linked list where the last node points to the first node, useful for circular iteration scenarios.

b)

A circular linked list is a data structure that only allows one-way traversal.

c)

A circular linked list is a linked list where nodes are arranged in a grid format.

d)

A circular linked list is a type of array that allows for dynamic resizing.

64.

List and describe at least three common operations performed on linked lists.

a)

Sorting: Arranging nodes in a specific order.

b)

Merging: Combining two linked lists into one.

c)

1) Insertion: Adding a new node to the list. 2) Deletion: Removing a node from the list. 3) Traversal: Accessing each node in the list.

d)

Searching: Finding a specific node based on a value.

65.

How do you insert a new node at the beginning of a singly linked list?

a)

Delete the current head before adding the new node.

b)

Create a new node, set its next to the current head, and update the head to the new node.

c)

Add the new node at the end of the list.

d)

Replace the current head with the new node without linking.

66.

What are the memory management considerations when using linked lists in Java?

a)

Memory overhead from pointers, fragmentation, and garbage collection.

b)

No need for garbage collection

c)

Constant time access to elements

d)

Memory efficiency from arrays

67.

Describe how garbage collection works with linked lists in Java.

a)

Garbage collection in Java requires manual intervention to free linked list nodes.

b)

Linked list nodes are never collected by garbage collection in Java.

c)

Garbage collection in Java identifies unreachable linked list nodes and reclaims their memory automatically.

d)

Garbage collection in Java only works for arrays, not linked lists.

68.

What are some real-world applications of linked lists?

a)

Implementing binary search trees

b)

Storing data in a fixed-size array

c)

Managing user sessions in web applications

d)

Examples of where linked lists are useful include implementing stacks and queues, dynamic memory allocation, maintaining a playlist in media players, representing adjacency lists in graphs, and providing undo functionality in applications.

69.

How would you traverse a circular linked list?

a)

Traverse the list by jumping two nodes at a time.

b)

Visit each node randomly until you decide to stop.

c)

Start at the head, loop through each node until you reach the head again.

d)

Start at the tail and move backwards through each node.

70.

What is the time complexity of searching for an element in a linked list?

a)

O(n^2)

b)

O(log n)

c)

O(1)

d)

O(n)