Previous Year Derived Questions
Previous Year Derived Questions
Section B
Question 1:
Describe the concept of "Inheritance" in Object-Oriented Programming. Give a Python example to
illustrate simple inheritance. (2 marks)
Correct Answer: Inheritance is a fundamental concept in object-oriented programming (OOP)
that allows a class (child class) to inherit attributes and methods from another class (parent class).
This mechanism promotes code reusability and a hierarchical classification of classes. For
example:
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def bark(self):
print("Dog barks")
This example shows a Dog class inheriting from the Animal class, allowing it to use the speak method
defined in Animal, in addition to its own bark method.
Question 2:
Write an SQL query to select all records from a table Students where the Grade is 'A'. Assume the
table has columns StudentID, Name, and Grade. (2 marks)
Correct Answer:
SELECT * FROM Students WHERE Grade = 'A';
This SQL query selects all columns for students who have achieved a grade of 'A' in the Students table.
Question 3:
Explain the difference between "Stack" and "Queue" data structures with respect to how data is
added and removed in each. (2 marks)
Correct Answer: Stack and Queue are two fundamental data structures that differ primarily in
how data is added and removed:
Stack: Follows the Last In, First Out (LIFO) principle. This means the last element added
to the stack will be the first to be removed. Operations are typically named push (to add
an item) and pop (to remove an item).
Queue: Follows the First In, First Out (FIFO) principle. This means the first element
added to the queue will be the first to be removed. Operations are commonly referred to
as enqueue (to add an item) and dequeue (to remove an item).
Question 4:
Consider the following Python code snippet:
nums = [10, 20, 30, 40, 50]
nums[2] = nums[2] + 10
print(nums)
What will be the output of the above code, and why? (2 marks)
Correct Answer: The output of the code will be [10, 20, 40, 40, 50]. This is because the element
at index 2 (which is 30, as indexing starts at 0 in Python) is modified to be 30 + 10, making it 40.
Therefore, the updated list becomes [10, 20, 40, 40, 50], reflecting this change.
5. Write a Python function merge_dictionaries(dict1, dict2) that combines two dictionaries passed as
parameters and returns a new dictionary that contains all the items from both dictionaries. If there is
an overlap, values from dict2 should override those from dict1.
Correct Answer:
def merge_dictionaries(dict1, dict2):
result = dict1.copy() # Copy all items from dict1 into the result
result.update(dict2) # Update with items from dict2 (overrides if exists)
return result
6. Given the SQL table Orders with columns OrderID, CustomerID, and OrderDate, write an
SQL query to find the total number of orders made by each customer. Sort the result by
CustomerID in ascending order.
Correct Answer:
SELECT CustomerID, COUNT(OrderID) AS TotalOrders
FROM Orders
GROUP BY CustomerID
ORDER BY CustomerID ASC;
7. Explain the difference between 'Class' and 'Object' in the context of Object-Oriented
Programming (OOP). Provide an example to support your explanation.
Correct Answer: In OOP, a 'Class' is a blueprint or template for creating objects, defining the
properties (attributes) and behaviors (methods) that the objects created from the class can
have. An 'Object' is an instance of a class, embodying the defined attributes and methods. For
example, if Car is a class with attributes like color and brand, and methods like drive(), an
object of this class could be myCar with color = 'red' and brand = 'Toyota' that can
perform the drive() operation.
8. Describe the term "Normalization" in databases. Why is it important? Provide an example of
converting a table to the First Normal Form (1NF).
Correct Answer: Normalization is the process of organizing data in a database to reduce
redundancy and improve data integrity. It involves dividing large tables into smaller tables
and defining relationships between them. Normalization is important because it minimizes
duplicate data, simplifies data management, and enhances data consistency. For a table to be
in 1NF, it must have atomic values, meaning each cell contains a single value, not a list or set
of values. Example: Splitting a Students table that contains a Name and Courses (with
multiple courses listed in a single cell) into two tables where Courses are listed in individual
rows for each student.
9. What is the significance of the OSI model in networking? Briefly describe the function of any
two layers of the OSI model.
Correct Answer: The OSI (Open Systems Interconnection) model is a conceptual framework
used to understand network interactions in seven layers. Each layer serves a specific function
in the process of communicating over a network. For example, the Transport Layer ensures
reliable data transfer between end systems, and the Application Layer provides network
services to end-user applications. This model helps standardize network communications and
allows different network hardware and software to communicate.
10. In Python, how would you handle an exception raised during file operations, such as attempting
to read a file that does not exist? Provide a code snippet.
Correct Answer:
try:
with open('nonexistent_file.txt', 'r') as file:
data = file.read()
except FileNotFoundError:
print("The file was not found.")