0% found this document useful (0 votes)
97 views10 pages

Answer Key

This document outlines the Continuous Internal Assessment for the Python Programming course at Sri Eshwar College of Engineering. It includes various questions covering Python concepts, programming tasks, and theoretical knowledge, structured into two parts with specific marks allocated. The assessment aims to evaluate students' understanding and application of Python programming principles and practices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views10 pages

Answer Key

This document outlines the Continuous Internal Assessment for the Python Programming course at Sri Eshwar College of Engineering. It includes various questions covering Python concepts, programming tasks, and theoretical knowledge, structured into two parts with specific marks allocated. The assessment aims to evaluate students' understanding and application of Python programming principles and practices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Set A

Register Number

Sri Eshwar College of Engineering


(An Autonomous Institution)
Approved by AICTE, New Delhi and Affiliated to Anna University, Chennai
Kondampatti (Post), Kinathukadavu (Tk), Coimbatore - 641 202

Continuous Internal Assessment – I :: Apr/May 2025

First Semester
B.E. – Mechanical Engineering
(Common to B.E. – CSE, ECE & B.Tech. – IT, AI&DS)
U23CS382: Python Programming

(Regulations 2019)

Duration: 1 Hour 45 mins Maximum Marks: 60

CO1 : Pls fil it.. from the gvn doc I got only course objective, not outcome

CO2 :

Answer ALL Questions

Part A – (9 X 2 = 18 Marks)

Q. No. Question M CO# KL

1 What type of typing does Python use, and what does it imply 2 CO1 K4
about type conversions? Provide an example to support your
answer.
Answer: Python uses strong typing, which means that it
does not perform implicit type conversions. For example,
adding an integer and a string will raise a TypeError.
2 Consider the following code snippet: 2 CO1 K4
y = 2.0
y = int(y) + 1.0
print(y, type(y))
What is the output of this code?
Answer: 3.0 <class 'float'>
3 Given the following code snippet: 2 CO1 K3
str_var = "A string"
count = 0
for c in str_var:
count += 1
print(count)
What is the program's output?
Answer: 8
Page 1 of 10
SECE/CoE/CIA/007/Rev. 01/24.03.2022
4 How is each expression evaluated? Predict the answer and 2 CO1 K3
give reasons.
a. 2 * 2 ** 2 ** 3
b. 10 + 3 * 2 / 4
Answer:

● Expression (a): 512

● Expression (b): 11.5

5 Write a program that takes user inputs in a while loop until the 2 CO1 K3
user enters "begin". Test the code with the given input values
to check that the loop does not terminate until the input is
"begin".
Once the input "begin" is received, print "The while loop
condition has been met.". Enter different input words to see
when the while loop condition is met.
Answer:
# Program to take user input until "begin" is entered
while True:
user_input = input("Enter a word (type 'begin' to exit): ")
if user_input.lower() == "begin":
print("The while loop condition has been met.")
break # Exit the loop when "begin" is entered
else:
print(f"You entered: {user_input}. Continue entering
words.")

# Test the code with different input values


6 What are lambda functions in Python? Discuss their syntax and 2 CO1 K2
use cases. How do they differ from regular functions? Provide
examples to illustrate their application.
7 Create a list of sports played on a college campus. The sports 2 CO2 K3
to be included are baseball, football, tennis, and table tennis.
Next, add volleyball to the list. Next, remove "football" from
the list and add "soccer" to the list. With the help of simple
python code demonstrate the list contents after each
modification.
Answer:
['Jamie', 'Vicky', 'DeShawn', 'Tae']
['Jamie', 'Vicky', 'DeShawn', 'Tae', 'Ming']
['Vicky', 'DeShawn', 'Tae', 'Ming']
['Vicky', 'DeShawn', 'Tae']
8 Consider the list my_list = [[7, 4, 5, 12], [24, 3, 9, 16], [12, 8, 2 CO2 K4
91, -5]]
Page 2 of 10
SECE/CoE/CIA/007/Rev. 01/24.03.2022
a. Analyze which code [Code X or Y] prints each number in
my_list starting from 7, then 4, and so on ending with-
5?
Code X: Code Y:
for row in my_list: for elem in my_list:
for elem in row: print(elem)
print(elem)

b. Apart from the above simple code, give an alternative


code that prints each number in my_list starting from 7,
then 4, and so on, ending with-5, by using counting for
loops?

9 Predict the output of the following code: 2 CO2 K3


# Given code
t = (1, 2, 3, 4, 5, 6, 7, 8, 9)
new_t = t[1:4] + (t[-1],) * 3 + tuple(t[i] for i in range(0, len(t),
2))
print(new_t)
Answers:
Output: (2, 3, 4, 9, 9, 9, 1, 3, 5, 7, 9)

Part B – (3 X 14 = 42 Marks)

10 a) i. Explain the difference between Python 2 and Python 3. [2m] 14 CO1 K1


ii. Can a program written in Python 2 run in Python 3? [2m]
iii. Comment on the following special characters with example:
" " & ''' ''' [2m]
iv. Write a program that displays “Welcome to
Python” five times. [2]
v. What sorting algorithm is employed by the built-in sort()
method in Python lists, and what are its key characteristics
and advantages? [3]
v. How does Python fixes to the principles of Object Oriented
Programming [3]
(Or)
10 b) (i) Write a program that prompts the user to enter an 7 CO1 K3
integer and checks whether the number is divisible by
both 5 and 6, divisible by 5 or 6, or just one of them (but
not both). Here is a sample run:

Page 3 of 10
SECE/CoE/CIA/007/Rev. 01/24.03.2022
Enter an integer:10
Is 10 divisible by 5 and 6? False
Is 10 divisible by 5 or 6? True
Is 10 divisible by 5 or 6, but not both? True
Answers:
# Program to check divisibility by 5 and 6
# Prompt user for an integer input
num = int(input("Enter an integer: "))

# Check divisibility
divisible_by_5_and_6 = (num % 5 == 0) and (num % 6
== 0)
divisible_by_5_or_6 = (num % 5 == 0) or (num % 6 == 0)
divisible_by_5_or_6_not_both = (divisible_by_5_or_6) and
not divisible_by_5_and_6

# Output results
print(f"Is {num} divisible by 5 and 6?
{divisible_by_5_and_6}")
print(f"Is {num} divisible by 5 or 6?
{divisible_by_5_or_6}")
print(f"Is {num} divisible by 5 or 6, but not both?
{divisible_by_5_or_6_not_both}")
(ii) Explain how Python handles memory management. 7 CO1 K3
Discuss the concepts of reference counting and garbage
collection, providing code examples to illustrate each
concept.
Answers:
Reference Counting:
● Each object in Python maintains a count of
references pointing to it. When the reference count
drops to zero, the memory occupied by the object
is deallocated.
Garbage Collection:
● Python also has a garbage collector that helps
reclaim memory by identifying and disposing of
objects that are no longer in use, particularly those
involved in circular references.

11 a) Having a mobile device can be a lifesaver on long road trips. 14 CO1 K3


Programs like Google Maps find the shortest route and
estimate the time of arrival. The time of arrival is based on the
Page 4 of 10
SECE/CoE/CIA/007/Rev. 01/24.03.2022
current time plus how long the trip will take.
Write a program that
(1) inputs the current time and estimated length of a trip,
(2) calculates the time of arrival, and
(3) outputs the results in hours and minutes.
Your program should use the following prompts (user input in
bold):
Current hour (0-23)? 13
Current minute (0-59)? 25
Trip time (in minutes)? 340

In this example, the current time is 13:25 (1:25pm). The trip


time is 340 minutes (5 hours and 40 minutes). 340 minutes
after 13:25 is 19:05 (7:05pm).
Your program should output the result in this format:
Arrival hour is 19
Arrival minute is 5

Conditions:
The arrival hour must be between 0 and 23. Ex: Adding 120
minutes to 23:00 should be 1:00, not 25:00. The arrival minute
must be between 0 and 59. Ex: Adding 20 minutes to 8:55
should be 9:15, not 8:75.
Your code must not use Python keywords, such as if or while.
The solution requires Expressions only addition, multiplication,
division, and modulo.
# Input current time and trip duration
current_hour = int(input("Current hour (0-23)? ")) # e.g., 13
current_minute = int(input("Current minute (0-59)? ")) # e.g., 25
trip_time = int(input("Trip time (in minutes)? ")) # e.g., 340

# Calculate total minutes from current time


total_current_minutes = current_hour * 60 + current_minute

# Calculate arrival time in total minutes


total_arrival_minutes = total_current_minutes + trip_time

# Calculate arrival hour and minute


arrival_hour = (total_arrival_minutes // 60) % 24 # Ensure hour is between 0-23
arrival_minute = total_arrival_minutes % 60 # Ensure minute is between 0-59

# Output results
print("Arrival hour is", arrival_hour)
print("Arrival minute is", arrival_minute)

Page 5 of 10
SECE/CoE/CIA/007/Rev. 01/24.03.2022
(Or)
11 b) (i) Complete the following steps to calculate a triangle's area 8 CO1 K3
and print the result of each step. The area of a triangle is
(b*h)/2 where b is the base and h is the height.

1. Calculate the area of a triangle with base = 7 and


height = 3.5.
2. Round the triangle's area to one decimal place.
3. Round the triangle's area to the nearest integer value.
Answer:
# Step 1: Define base and height
base = 7
height = 3.5

# Step 2: Calculate the area of the triangle


area = (base * height) / 2
print("Area of the triangle:", area)

# Step 3: Round the area to one decimal place


rounded_area_one_decimal = round(area, 1)
print("Rounded area (one decimal place):",
rounded_area_one_decimal)

# Step 4: Round the area to the nearest integer value


rounded_area_integer = round(area)
print("Rounded area (nearest integer):",
rounded_area_integer)
(ii) Write a program that reads two integer values, n1 and 6 CO1 K3
n2.
1. Using a while loop, calculate the product of all
the numbers between n1 and n2 (inclusive
of n1 and n2).
2. Using a for loop, perform the same calculation to
find the product of all the numbers
between n1 and n2 (inclusive of n1 and n2).
# Input two integer values
n1 = int(input("Enter the first integer (n1): "))
n2 = int(input("Enter the second integer (n2): "))

# Using a while loop to calculate the product of numbers


product_while = 1
current = n1

while current <= n2:


product_while *= current
current += 1

print("Product of numbers (while loop):", product_while)

Page 6 of 10
SECE/CoE/CIA/007/Rev. 01/24.03.2022
# Using a for loop to calculate the product of numbers
product_for = 1

for number in range(n1, n2 + 1):


product_for *= number

print("Product of numbers (for loop):", product_for)

12 a) (i) Given two list of integers nums and index. Your task is 7 CO2 K3
to create target list under the following rules:

● Initially target list is empty.

● From left to right read nums[i] and index[i], insert at

index index[i] the value nums[i] in target list.

● Repeat the previous step until there are no elements

to read in nums and index.


Return the target list.
Sample Output:
Example 1:
Input: nums = [0,1,2,3,4], index = [0,1,2,2,1]
Output: [0,4,1,3,2]
Explanation:
nums index target
0 0 [0]
1 1 [0,1]
2 2 [0,1,2]
3 2 [0,1,3,2]
4 1 [0,4,1,3,2]
Example 2:
Input: nums = [1,2,3,4,0], index = [0,1,2,3,0]
Output: [0,1,2,3,4]
Explanation:
nums index target
1 0 [1]
2 1 [1,2]
3 2 [1,2,3]
4 3 [1,2,3,4]
0 0 [0,1,2,3,4]
Answer:
target = [] # Initialize an empty target list
for i in range(len(nums)):
target.insert(index[i], nums[i]) # Insert nums[i] at
index[i]
return target
(ii) Write a program that reads in numbers separated by a 7 CO2 K3
space in one line and displays distinct numbers (i.e., if a
number appears multiple times, it is displayed only once).
You should implement this using List operations and
Loopings in python.
Page 7 of 10
SECE/CoE/CIA/007/Rev. 01/24.03.2022
Enter ten numbers: 1 2 3 2 1 6 3 4 5 2
The distinct numbers are: 1 2 3 6 4 5
Answers:
for number in number_list:
# Check if the number is not already in the
distinct_numbers list
if number not in distinct_numbers:
distinct_numbers.append(number)
(Or)
12 b) (i) You are given an array nums consisting of distinct 9 CO2 K3
positive integers. Your task is to determine the number of
unique tuples (a,b,c,d) such that the product of a and b
equals the product of c and d, where a, b, c, and d are
elements of nums. Additionally, each element must be
distinct, meaning a≠b≠c≠d.
Write a function count_tuples(nums) that takes the list
nums as input and returns the number of valid tuples
(a,b,c,d).
Sample Input Output:
nums = [2, 3, 4, 6]
There are 8 valid tuples that satisfy the condition
a×b=c×d:

● (2, 6, 3, 4)

● (2, 6, 4, 3)

● (6, 2, 3, 4)

● (6, 2, 4, 3)

● (3, 4, 2, 6)

● (4, 3, 2, 6)

● (3, 4, 6, 2)

● (4, 3, 6, 2)

Answers:
def count_tuples(nums):
from collections import defaultdict

product_count = defaultdict(int)
n = len(nums)
Page 8 of 10
SECE/CoE/CIA/007/Rev. 01/24.03.2022
for i in range(n):
for j in range(i + 1, n):
product = nums[i] * nums[j]
product_count[product] += 1

count = 0
for product, pair_count in product_count.items():
if pair_count > 1:
count += pair_count * (pair_count - 1) // 2

return count

nums = [2, 3, 4, 6]
result = count_tuples(nums)
print("There are", result, "valid tuples that satisfy the
condition a × b = c × d.")
(ii) Given the Tuple 5 CO2 K3
a = (6, 5, 4, 3, 2, 1, 0), evaluate with necessary steps
1. a[a[−1]+a[3]∗2]
2. a[a[2]∗2−1]
3. a[−(a[4]+2)]
4. a[a[−2]+a[1]∗a[3]]
5. a[a[a[−1]]∗2]

Answers

1. Q1: 0
2. Q2: IndexError (out of range)
3. Q3: 3
4. Q4: 5
5. Q5: 0

K1 – Remember K2 – Understand K3 – Apply K4 – Analyze K5 – Evaluate K6 – Create

Note:
1. Part B questions may have either a single question carrying 14 marks or 2
sub divisions with appropriate marks.
2. Disable the table before taking print.
3. Merge the 2 columns as given below if the question has no sub division
12 a) (i) 7 CO3
(ii) 7 CO3
(Or)

Page 9 of 10
SECE/CoE/CIA/007/Rev. 01/24.03.2022
12 b) Merge here if the question has no sub division 14 CO3

Page 10 of 10
SECE/CoE/CIA/007/Rev. 01/24.03.2022

You might also like