QUESTION 1 (B)
Differentiate between a run-time error, semantic error and syntax error. Give one example of each.
ANSWER
QUESTION 1 (C)
Explain the use of Lambdas in Python with an example
ANSWER
In Python, lambdas are anonymous functions that can be defined using the lambda keyword. They are
often used for short, one-time operations where a full function definition is unnecessary. Here's a
concise explanation along with an example:
Use of Lambdas in Python:
Lambdas provide a way to create small, unnamed functions on the fly.
They are particularly useful for short, simple operations where a full function definition would be overly
verbose.
EXAMPLE
INPUT
x = lambda a : a + 10
print(x(5))
OUTPUT
15
QUESTION 1 (D)
What is the difference between deep and shallow copy?
QUESTION 1 (E)
What do you mean by indentation in python? Is indentation required in python?
ANSWER
Indentation refers to the spaces at the beginning of a code line. Where in other programming languages
the indentation in code is for readability only, the indentation in Python is very important. Python uses
indentation to indicate a block of code.
QUESTION 2 (A)
What are the counter controlled and sentinel-controlled loops?
ANSWER
Counter-controlled loops are also known as "for" loops.
They are used when the number of iterations is known or can be predetermined.
A loop variable (counter) is initialized before the loop begins, and the loop executes a fixed number of
times based on the counter.
Sentinel-controlled loops are also known as "while" loops.
They are used when the number of iterations is not known beforehand and is determined by a
condition.
A sentinel value is used to control the loop; the loop continues until a specific value (sentinel) is
encountered.
QUESTION 2 (B)
What does *args, **kwargs mean? Why would we use them?
ANSWER Explanation (1 Mark):
*args and **kwargs are used in Python to allow functions to accept a variable number of arguments.
They enable the definition of functions without specifying the exact number of parameters.
Usage (1 Mark):
*args is used to pass a variable number of non-keyword (positional) arguments to a function.
**kwargs is used to pass a variable number of keyword arguments (or named arguments) to a function.
QUESTION 2 (C)
Assume, you are given two lists: a = [1,2,3,4,5] b = [6,7,8,9] Write a command to create a list which has
all the elements of a and b in one dimension.
ANSWER
Code
combined_list = a + b
In this command:
• a + b concatenates the two lists, creating a new list that contains all the elements of a
followed by all the elements of b in one dimension.
QUESTION 2(D)
Write a programme that takes a positive integer n and produce in line of output as shown below:
23
456
789
ANSWER
x=1
for i in range(1,5): #number of lines
for j in range(i): #number of values in that line
print(x,end=" ")
x+=1
if x==10:
break
print("") #this is used to changed to next line
QUESTION 2(F)
What is the importance of user defined module? Discuss different in-built modules of Python.
ANSWER
Importance of User-Defined Module (1 Mark):
• User-defined modules in Python enhance code organization, promote reusability, and
simplify maintenance by encapsulating related functionality.
• Math Module: Provides mathematical functions and constants.
• Random Module: Offers functions for generating random numbers.
• statistics Module: The statistics module in Python's standard library provides
functions for basic statistical operations on numeric data.
QUESTION 3(A)
Looking at the below code, write down the final values of A0, A1, …An.
(a) A0 = dict(zip(('a','b','c','d','e'),(1,2,3,4,5)))
(b) A1 = range(10)
(c) A2 = sorted([i for i in A1 if i in A0])
(d) A3 = sorted([A0[s] for s in A0])
(e) A4 = {i:i*i for i in A1}
(f) A5 = [[i,i*i] for i in A1]
print(A0,A1,A2,A3,A4,A5)
ANSWER
(a) {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
(b) range(0, 10)
(c) []
(d) [1, 2, 3, 4, 5]
(e) {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
(f) [[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81]]
QUESTION 3(B)
(b) Observe the following Python code carefully and obtain the output, which will appear on the screen
after execution of it.
i , j , k , n = 5,6,7,3
print(i + j * k - k % n)
print(i // n)
ANSWER
46 (it will first apply * and then % and then +and then -)
QUESTION 3(C)
QUESTION 3(D)
What do you understand by local and global scope of variables? How can you access a global variable
inside the function, if function has a variable with same name?
ANSWER
QUESTION 3(E)
What will be the output of the following Python code?
ANSWER
3 1
QUESTION 4 (A)
ANSWER
QUESTION 4 (B)
Define a class Employee. Display the personal salary details of three employees using single inheritance.
ANSWER
QUESTION 4(C)
What is the utility of indexing and slicing in tuple?
ANSWER
Indexing in Tuple (1.25 Marks):
• Indexing in a tuple is the ability to access individual elements using their position (index)
in the tuple.
• Tuple indexing starts at 0 for the first element.
• It provides a way to retrieve specific elements quickly.
Slicing in Tuple (1.25 Marks):
• Slicing allows you to extract a subset or a range of elements from a tuple.
• The syntax for slicing is tuple[start:stop:step].
• It provides a concise way to work with a portion of the tuple without modifying the
original tuple.
• Slicing is helpful in creating sub-tuples and performing operations on specific parts of
the tuple.
EXAMPLE
QUESTION 5(B)
Discuss at-least 4 different ways to create a dictionary.
ANSWER
QUESTION 5(C)
ANSWER
(i) Colon is missing in function header
(ii) Errors are :
• define in place of def
• colon is missing in function header
• N must be numeric so that we can perform arithmetic operations.
QUESTION 5(D)
ANSWER
• stRecord[3]
• stRecord[2][4]
• max(stRecord[2])
• stRecord[1]
• stRecord[0]=”Aavya”
QUESTION 5(E)
The __init__ method in Python is crucial for initializing object attributes when an instance of a
class is created. It helps set up the initial state of an object. Here's a concise example to illustrate
its significance:
QUESTION 5(F)
What does PIP stands for? Why is it utilized?
ANSWER
PIP stands for “Preferred Installer Program” or PIP Installs Packages. It is a command-
line utility that installs, reinstalls, or uninstalls PyPI packages with one simple command:
pip
QUESTION 6 (C)
Write short notes on:
i) Keyword Arguments
ii) ii) Default Arguments
iii) iii) Variable length Arguments
ANSWER
i) Keyword Arguments:
• Definition: In Python, keyword arguments allow you to pass values to a function by
specifying the parameter name along with the value. This provides clarity and flexibility
in function calls.
ii) Default Arguments:
• Definition: Default arguments in Python allow you to specify a default value for a
function parameter. If the caller does not provide a value for that parameter, the default
value is used.
iii) Variable Length Arguments:
• Definition: Variable length arguments in Python allow a function to accept a variable
number of arguments. There are two types: *args (non-keyword variable arguments) and
*kwargs (keyword variable arguments).
• Example:
QUESTION 6 (D)
What do you mean by right shift and left shift operator? Write binary equivalent of the following
numbers and perform the right and left shift on both of them.(i) 23 (ii) 40
ANSWER
Right Shift Operator (>>): The right shift operator (>>) shifts the bits of a binary
number to the right by a specified number of positions. It's equivalent to dividing the
number by 2 raised to the power of the specified shift count.
Left Shift Operator (<<): The left shift operator (<<) shifts the bits of a binary number
to the left by a specified number of positions. It's equivalent to multiplying the number
by 2 raised to the power of the specified shift count.
Let's perform right and left shifts for the given numbers:
(i) Number: 23
• Binary Equivalent: 10111
• Right Shift (>> 2): 00010 (Binary equivalent of 2)
• Left Shift (<< 2): 11100 (Binary equivalent of 28)
(ii) Number: 40
• Binary Equivalent: 101000
• Right Shift (>> 3): 000101 (Binary equivalent of 5)
• Left Shift (<< 3): 10100000 (Binary equivalent of 320)
QUESTION 6(A)
(a) Create a class called library with data attributes like acc_number, publisher, title and
author. The methods of the class should include i). read() – acc_number, title, author. ii).
compute() - to accept the number of days late, calculate and display the fine charged at
the rate of $1.50 per day. iii). display the data.
ANSWER
(b) (b) Write a program to input your friends' names and their Phone Numbers and store
them in the dictionary as the key-value pair. Perform the following operations on the
dictionary: i) Display the name and phone number of all your friends ii) Add a new key-
value pair in this dictionary and display the modified dictionary iii) Delete a particular
friend from the dictionary iv) Modify the phone number of an existing friend v) Check if
a friend is present in the dictionary or not vi) Display the dictionary in sorted order of
names