0% found this document useful (0 votes)
32 views2 pages

Python Mid Term Exam Questions 2024

Uploaded by

Manasveer Singh
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)
32 views2 pages

Python Mid Term Exam Questions 2024

Uploaded by

Manasveer Singh
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

Name:

Divgoku
Enrolment No: 28FEloCSE00D63
MANIPAL UNIVERSITY
JAIPUR
Odd Semester Mid Term Examination, Oct-2024
Faculty of Engineering, Schoolof Computer Science and Engineering
Department of Computer Science and Engineering
B. Tech -CSE
Course Code: CSE2122 Course: Object Oriented Programming using Python Semester: 3
Time: 1.5 hrs. Max. Marks: 30
Instructions: All questions are compulsory.
Missing data, if any, may be assumed suitably.
Calculator is not allowed.
SECTION A
[Link]. Marks CO
QA1 What willbe the output of given code snippet?
string = 'FAITH
number='1947
i=0
X="
while i< len(string):
Co1
x=x+ string[i]+number[len(number) -i - 1]
i+=1
print(x)
(a.) FlA419T4H7 (c.) FAITH1947
(b.)F7A417T1H9 (d.) F7A419TIH7

Q A2 What will be the output of the given python code?


2X3=6
def doubleCool(d):
count-0

for k,v in [Link]():


if type(v)dict:
CO2
countt=doubleCool(v)
countFcount+1

returm count

d={'a':{b:{c:1}},;d:2}
print(doubleCool(d)
Write output of the code?
i=l

while i<=10:
if i%3!=0: COI
QA3
jt-2
elifj%3-0:
brcak
it=1
print(i+j)
SECTION B
Q
B1 Explain why tuples can be more efficient in terms of memory and performance compared
to lists. Provide a scenario where using tuple instead of alist would be beneficial. Analyze CO2
the differences between Python's collection types--Lists, Tuples, Sets, and Dictionaries.
Q
B2 Write a python program to construct the below pattern.
1
121
1232 1
12343 21 CO1
1232 1
121
4X 4=
16
QB3 (a.) What is a lambda function in Python, and how does it differ from a regular function?
Write a lambda function to filter out even numbers from a given list.
CO1
(b.)Differentiate iterator with recursion? Write a program to print the 1to 100 Fibonacci
series using recursive function?

Write a Python function named "count_consonants" that takes a string as input and
counts the number of consonants present in the string. The function should use a for loop
Q B4 to iterate over each character in the string and use an if statement to check if the character CO1

is aconsonant. The function should return the total count of consonant found.

SECTION-C
(a.) Give answers of the following:
1. Explain the uses of *args and **kwargs with syntax.
2. CreateaPython class named Circle constructed by a radius and two methods CO1
QCi which willcompute the area and the perimeter of a circle. 4X2=8 CO2
CO3
(b.) What are decorators? Analyze how decorators promote code reusability and
separation of concerns by evaluating a scenario where a logging decorator is applied to
multiple functions in an application.

Common questions

Powered by AI

Object-oriented programming (OOP) in a course titled 'Object Oriented Programming using Python' focuses on organizing code into objects that combine data and behavior, boosting modularity, code reuse, and scalability. OOP concepts like classes, inheritance, and encapsulation help in building complex applications. For example, a simple Circle class may include attributes like radius and methods for computing area and perimeter, allowing representation of circle objects with encapsulated properties and behaviors. Such structure aids clarity, maintainability, and can form foundations for building upon with further related features .

A Python program identifies consonants in a string by iterating over each character and checking if it is a consonant, typically defined as alphabetic characters that are not vowels (a, e, i, o, u). The 'count_consonants' function iterates through the string using a for loop, employing an if condition to verify if each character is an English consonant, incrementing a count variable for each consonant. This logic ensures accurate counting of consonants, regardless of the string content .

Tuples in Python provide memory and performance efficiency because they are immutable, meaning their size and data do not change after creation, allowing Python to optimize memory allocation. This immutability also means operations like insertion or deletion, which require reallocating memory, are avoided, leading to faster access time. A suitable scenario for using a tuple over a list is when representing fixed collections of items, such as the days of the week or a coordinate pair, where the elements should remain constant throughout the program .

Decorators in Python enhance code reusability by allowing the separation of cross-cutting concerns from core functionality. This separation means functionalities like logging, authentication, or caching can be applied across different functions without modifying their codebase. A logging decorator can wrap multiple functions to log their execution without altering their logic. For example, a logging function can be defined as a decorator that logs before and after function execution, and then applied to any function needing such logging, thus promoting DRY (Don't Repeat Yourself) principles .

Lambda functions in Python are anonymous functions defined using the lambda keyword, which can take multiple arguments but can contain only a single expression. Unlike regular functions defined with the def keyword, lambda functions have no name and are typically used for short-term operations. For example, a lambda function to filter out even numbers from a list would be: filtered_even = list(filter(lambda x: x % 2 != 0, [1, 2, 3, 4, 5, 6])). This filters the numbers 1, 3, and 5, which are odd, out of the list .

Lists in Python are ordered and mutable collections, useful for dynamic data storage where modification of contents is needed. Tuples are similar but immutable, making them suited for fixed data. Sets are unordered collections of unique elements, useful for operations like membership tests and eliminations of duplicates. Dictionaries store data as key-value pairs, ideal for quickly accessing data via unique keys. Each structure offers unique strengths: lists for flexibility, tuples for consistency and performance, sets for uniqueness, and dictionaries for key-based data retrieval .

Decorators in Python are a design pattern that allows additional functionality to be wrapped around existing functions. They are applied by prefixing a function definition with a decorator name using the @ syntax, e.g., @log_decorator before a function to ensure logging. A logging decorator can be created to log the input and output or time of function execution, and then applied to multiple functions to standardize logging across the application without altering individual function bodies, thus enforcing consistent logging practices .

The *args and **kwargs in Python enhance function flexibility by allowing variable numbers of positional and keyword arguments, respectively. *args can capture additional positional arguments, while **kwargs captures named arguments as a dictionary. For example, def function(*args): would allow a call like function(1, 2, 3), with args being a tuple (1, 2, 3). Similarly, def function(**kwargs): could handle function(a=1, b=2), with kwargs being {'a': 1, 'b': 2}, allowing dynamic and flexible argument passing in functions .

The recursive approach in Python is advantageous for problems naturally expressed in terms of smaller instances of the same problem, like the Fibonacci sequence, where recursion elegantly expresses the relation F(n) = F(n-1) + F(n-2). For example, a recursive function might return the sum of calls to calculate the previous two terms. However, recursion can be inefficient due to excessive function calls and potential stack overflow. Iterators, on the other hand, provide efficiency by maintaining the state across loop iterations without additional function calls, making them preferred for large data processing. Generating Fibonacci via an iterator avoids overhead by yielding each term sequentially .

Excluding calculators in examinations like the FAITH Odd Semester Mid Term Examination emphasizes cognitive skills such as mental arithmetic, problem-solving, and logical reasoning. This restriction encourages students to develop a deeper understanding of mathematical concepts and enhances their ability to perform calculations manually. Analyzing and verifying results without reliance on digital tools may also improve student's error-checking and estimation abilities, fostering a comprehensive grasp of underlying principles rather than relying on computational shortcuts .

You might also like