Question 1: Consider the
following code snippet:
def foo(n):
if n <= 0:
return
print(n)
foo(n-1)
print(n)
foo(3)
What is the output of the
above code?
Answer 1: The output of the above code is:
3
2
1
1
2
3
Question 2: Consider a
binary tree as shown below:
1
/ \
2 3
/ \ /
\
4 5 6
7
Perform an in-order
traversal on the above binary tree.
What is the result?
Answer 2: The in-order traversal of
the given binary tree is: 4 2 5 1 6 3 7.
Question
3: Consider a simple CPU with an instruction
set consisting of 50 different instructions. Each instruction is stored as a
12-bit value. If the CPU has a 4 KB instruction cache, what is the maximum
number of instructions that can be stored in the cache?
Answer 3: Since each instruction is stored as a 12-bit value, the
number of instructions that can be stored in the cache can be calculated as
follows:
Number of
instructions = Cache size / Instruction size
= (4 KB * 8) / 12 bits
= 2 KB
Therefore,
the maximum number of instructions that can be stored in the cache is 2 KB.
Question 4: Consider the following C program:
#include <stdio.h>
int main() {
int a = 10, b = 5;
int *p, *q;
p = &a;
q = &b;
*p = *q + 5;
printf("%d %d", a, b);
return 0;
}
What will be the output of
the program?
Answer 4: The output of the program will be: 10 5
Since p points to a and q points to b, the statement *p = *q + 5; assigns the value of b + 5 (10) to the variable a. Therefore, a will be 10 and b remains unchanged at 5.
Question 5: Consider a directed graph with five nodes and the following adjacency matrix representation:
0 1 0 1 0
0 0 1 0 0
0 0 0 0 1
0 1 0 0 0
0 0 0 0 0
Which of the following traversal orders will visit the nodes of the graph in a topological order:
A) 2, 3, 4, 1, 0
B) 3, 2, 1, 4, 0
C) 4, 3, 2, 1, 0
D) 2, 3, 1, 4, 0
Answer 5: The correct traversal
order that visits the nodes of the graph in a topological order is:
C) 4, 3, 2, 1, 0
In a topological order, a directed edge from node A to node B implies that A comes before B. The given traversal order satisfies this condition, making it a valid topological order.
Question 6: Consider the
following code snippet:
def
func(n):
if n > 0:
return n + func(n-1)
else:
return 0
result = func(5)
print(result)
What is
the output of the above code?
Answer 6: The output of the above code is: 15
The func() function is a recursive function that calculates the sum
of positive integers from n to 1. In
this case, the sum of integers from 5 to 1 is 15.
Question
7: Consider the following SQL query:
SELECT A.name, B.salary
FROM Employees A JOIN Salaries B ON A.id =
B.id
WHERE B.salary > 50000;
What does
the above query do?
Answer 7: The above query selects the name column from the Employees
table and the salary column from the Salaries table for employees whose salary
is greater than 50000. It performs an inner join between the two tables based
on the id column.
Question
8: In the context of computer networks, what is the purpose of the Address
Resolution Protocol (ARP)?
Answer 8: The Address Resolution Protocol (ARP) is used to map an
IP address to a physical (MAC) address on a local network. It resolves IP
addresses to MAC addresses to enable communication between devices within the
same network.
Question
9: Which data structure is typically used to implement a LIFO (Last-In,
First-Out) behavior?
Answer 9: A stack is typically used to implement a LIFO behavior.
In a stack, the last element that was inserted is the first one to be removed.
Question
10: Which of the following sorting algorithms has a worst-case time complexity
of O(n^2)?
A) Merge sort
B) Quick
sort
C) Heap
sort
D)
Insertion sort
Answer 10: The sorting algorithm with a worst-case time
complexity of O(n^2) is: D) Insertion sort
Insertion sort has a quadratic worst-case
time complexity, meaning its performance degrades rapidly as the input size
increases.
Question 11: Consider the
following code snippet:
#include
<stdio.h>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int *p = arr;
printf("%d", *(p + 2));
return 0;
}
What will
be the output of the program?
Answer
11: The output of the program will be: 3
The pointer p is initialized to the base address of the arr array. (p + 2) moves
the pointer two elements ahead, pointing to the third element of the array. *(p
+ 2) dereferences the pointer and prints the
value, which is 3.
Question
12: Which of the following is not an example of a non-preemptive scheduling
algorithm?
A) First-Come, First-Served (FCFS) B) Round
Robin (RR) C) Shortest Job Next (SJN) D) Priority Scheduling
Answer
12: B) Round Robin (RR)
Round Robin is an example of a preemptive
scheduling algorithm, as it allocates a fixed time slice to each process and
allows preemption after the time slice expires.
Question
13: What is the purpose of the SELECT statement in SQL?
Answer
13: The SELECT statement is used in SQL to
retrieve data from one or more tables in a database. It allows you to specify
the columns to be retrieved and apply conditions using various clauses like
WHERE, GROUP BY, HAVING, etc.
Question
14: Which of the following is not a property of a binary search tree (BST)?
A) The left child of a node has a smaller key
than the node itself. B) The right child of a node has a larger key than the
node itself. C) The left and right subtrees of a node are also BSTs. D) The
maximum number of children for a node is two.
Answer 14: D) The maximum number of children for a node
is two.
Question
15: In computer networks, what is the purpose of the Domain Name System (DNS)?
Answer15: The Domain Name System (DNS) is used to translate domain names (e.g., www.example.com) into IP addresses (e.g., 192.168.0.1) on the internet. It acts as a distributed database that maps human-readable domain names to their corresponding IP
addresses.
Question
16: Which of the following is an example of a dynamic programming problem?
A) Finding the shortest path in a graph using Dijkstra's algorithm.
B) Calculating the factorial of a number using recursion.
C) Sorting an array using the bubble sort algorithm.
D) Finding the maximum subarray sum using Kadane's algorithm.
Answer
16: The correct answer is: D) Finding the maximum subarray sum using Kadane's algorithm.
This is an example of a dynamic programming
problem, where the solution can be efficiently computed by breaking it down
into smaller overlapping subproblems.
Question
17: What is the time complexity of the binary search algorithm?
Answer 17: The time complexity of the binary search algorithm is O(log n), where n is the number of elements in the sorted array being searched. Binary search repeatedly divides the search space in half, resulting in a logarithmic time complexity.
Question
18: Consider the following code snippet:
def
power(x, n):
if n == 0:
return 1
elif n % 2 == 0:
return power(x, n/2) * power(x, n/2)
else:
return x * power(x, n-1)
print(result)
What is
the output of the above code?
Answer 18: The output of the above code is: 32
The power()
function calculates the exponentiation of x raised to the power of n
using a recursive approach. In this case, 2 raised to the power of 5
equals 32.
Question 19: Which of the following is
not a valid HTTP status code?
A) 200 OK
B) 404 Not
Found
C) 502 Bad
Gateway
D) 100
Continue
Answer 19: The correct answer is: D) 100 Continue
100 Continue is a valid HTTP status code. It is used as a
provisional response to indicate that the client should continue with the
request.
Question
20: In the context of databases, what is the purpose of the COMMIT statement?
Answer 20: The COMMIT statement in a database is used
to permanently save the changes made during a transaction. It marks the
successful completion of a transaction and makes the changes visible to other
users.