Programming, Data Structures and Algorithms
using Python – Assignment and Exam Questions
• Week 1 – Assignments:
• July–Dec 2025 (Assignment 1):
1. Consider the function g(y) given below. What is the value of g(728) ?
python
def g(y):
b = 0
while y >= 3:
(y,b) = (y/3,b+1)
return(b) 1
2. Given
def f(n):
s = 0
for i in range(2, n):
if n % i == 0 and i % 2 == 1:
s = s + 1
return(s)
What is f(90) - f(89) ? 2
3. For the function below, h(n) returns False for a positive n if and only if:
def h(n):
s = True
for i in range(1, n+1):
if i*i == n:
s = False
return(s)
(Options: n is odd; n is prime; n is a perfect square; n is composite.) 3
4. For the recursive function foo(m) below, which statement is correct (assume m is a
nonnegative integer)?
def foo(m):
if m == 0:
return(0)
else:
return(m + foo(m-1))
(Options involve foo(n) = factorial(n) or foo(n) = n(n+1)/2 .) 4
1
• Jan–Apr 2025 (Assignment 1):
1. Given
def f(x):
d = 0
y = 1
while y <= x:
d = d + 1
y = y * 3
return d
What is the value of f(8538) ? 5
2. Given
def h(n):
s = 0
for i in range(1, n + 1):
if n % i > 0:
s = s + 1
return s
What is h(61) - h(60) ? 6
3. For what integer n does the function below return 12?
python
def g(m, n):
res = 0
while m >= n:
res = res + 1
m = m - n
return res 7
4. Consider the function mys(m) below. Which statement correctly describes its behavior
(assume m is an integer argument)?
python
def mys(m):
if m == 1:
return 1
else:
return m * mys(m - 1) 8
• July–Dec 2024 (Assignment 1):
1. For the function below, what does h(27993) return?
python
def h(x):
(d,n) = (1,0)
while d <= x:
(d,n) = (d*3,n+1)
return(n) 9
2
2. Given
def g(n):
s = 0
for i in range(2, n):
if n % i == 0:
s = s + 1
return(s)
What is g(60) - g(48) ? 10
3. Consider the function f(n) below. It returns True iff which property holds?
def f(n):
s = 0
for i in range(1, n+1):
if n//i == i and n % i == 0:
s = 1
return (s % 2 == 1)
(Options include “n is odd”, “n is prime”, “n is a perfect square”, “n is composite”.) 11
4. For foo(m) as above, which statement is correct (same options)? 12
• Week 2 – Assignments:
• July–Dec 2025 (Assignment 2):
1. One of the following 10 Python statements causes an error. Which one? (Each statement
involves list slicing or mutation; the statements are listed in the source.) 13
2. After executing:
b = [23,44,87,100]
a = b[1:]
d = b[2:]
c = b
d[0] = 97
c[2] = 77
which of the following holds at the end? (Four choices involving values of a[1] , b[2] ,
c[2] , d[0] .) 14
3. Given:
startmsg = "python"
endmsg = ""
for i in range(1, 1+len(startmsg)):
endmsg = startmsg[-i] + endmsg
What is the value of endmsg after this? 15
4. Given:
3
def mystery(l):
l = l[1:]
return()
mylist = [7,11,13]
mystery(mylist)
What is the value of mylist after this code? 16
• Jan–Apr 2025 (Assignment 2):
1. (Same “error statement” question with different statements; image provided in source.)
17
2. After executing:
x = ['super', 397, 'king', 43]
y[1] = 357
z[3] = [723]
which of the following is correct? (Four options relating x , y , z .) 18
3. Given:
first = "pterodactyl"
second = ""
for i in range(len(first) - 1, -1, -1):
second = first[i] + second
What is the value of second after this? 19
4. Given:
def mystery(l):
return ()
list1 = mystery(list1)
What is the value of list1 after this? 20
• July–Dec 2024 (Assignment 2):
1. One of the following 10 statements causes an error. Which one? (Statements are listed in
source.) 21
2. After executing:
b = [43,99,65,105,4]
a = b[2:]
d = b[1:]
c = b
d[1] = 95
4
b[2] = 47
c[3] = 73
which of the following holds? (Options provided in source.) 22
3. Given:
startmsg = "anaconda"
endmsg = ""
for i in range(1,1+len(startmsg)):
endmsg = endmsg + startmsg[-i]
What is the value of endmsg ? 23
4. Given:
def mystery(l):
l = l[2:]
return(l)
mylist = [7,11,13,17,19,21]
mystery(mylist)
What is the value of mylist ? 24
• July–Dec 2023 (Assignment 2):
1. One of the 10 statements below causes an error. Which one? (Statements listed in source.)
25
2. After executing:
b = [23,44,87,100]
a = b[1:]
d = b[2:]
c = b
d[0] = 97
c[2] = 77
which of the following holds? (Options in source.) 26
3. Given:
startmsg = "python"
endmsg = ""
for i in range(1,1+len(startmsg)):
endmsg = startmsg[-i] + endmsg
What is endmsg ? 15
4. Given:
5
def mystery(l):
l = l[1:]
return()
mylist = [7,11,13]
mystery(mylist)
What is mylist ? 16
• Week 4 – Assignments:
• Jan–Apr 2025 (Assignment 4):
1. Consider:
def mystery(l):
if (l == []):
return l
else:
mid = len(l)//2
if (len(l) % 2 == 0):
return l[mid-1:mid+1] + mystery(l[:mid-1] + l[mid+1:])
else:
return l[mid:mid+1] + mystery(l[:mid] + l[mid+1:])
What does mystery([22,14,19,65,82,55]) return? 27
2. What is the value of triples after:
python
triples = [ (x,y,z)
for x in range(1,4)
for y in range(2,5)
for z in range(5,8)
if x+y > z ] 28
3. Given the dictionary
marks = {
"Quizzes": {"Mahesh":[3,5,7,8], "Suresh":[9,4,8,8], "Uma":[9,
9,7,6]},
"Exams": {"Mahesh":[37], "Uma":[36]}
}
Which of the following statements does not generate an error? (Options listed in source.)
29
4. Starting with an empty dictionary inventory = {} , which of the following
assignments generates an error? (Options in source, e.g.
inventory[["Amul","Mystic Mocha"]] = 55 .) 30
• July–Dec 2024 (Assignment 4):
1. Consider:
6
def mystery(l):
if l == []:
return l
else:
return mystery(l[1:]) + l[:1]
What does mystery([22,14,19,65,82,55]) return? 31
2. What is the value of pairs after:
python
pairs = [ (x,y)
for x in range(4,1,-1)
for y in range(5,1,-1)
if (x+y)%3 == 0 ] 32
3. Given
wickets = {"Tests":{"Bumrah":[3,5,2,3],"Shami":[4,4,1,0],"Ashwin":
[2,1,7,4]},
"ODI": {"Bumrah":[2,0],"Shami":[1,2]}}
Which statement does not generate an error? (Choices in source.) 33
4. With hundreds = {} , which assignment generates an error? (Options listed in source.)
34
• Week 5 – Assignments (Programming questions):
• July–Dec 2025 (Assignment 5): The assignment is a programming problem. It describes reading
Hogwarts grade data (courses, students, grades) from input and outputting each student’s GPA
in the format Roll~Name~GPA . (Full problem statement in source.) 35 36
• July–Dec 2023 (Assignment 5): (Same Hogwarts GPA problem as above.) 35 36
• Jan–Apr 2023 (Assignment 5): A different programming problem. It involves reading Hogwarts
library data (Books, Borrowers, Checkouts) and outputting each checked-out book’s details
(DueDate, Borrower Name, Accession No, Title) sorted by due date. (See full statement in
source.) 37 38
• Previous-Year (Proctored) Test Questions (Hackademic, Sept 2017):
• Test 2–5 PM (Sep 17, 2017) 39 40 :
1. A function maxbad(l) is given. Provide an input list for which maxbad produces an
incorrect output (the function is supposed to find the maximum in a list). 41
2. A (unstable) sort function stablesortbad(l) is given. Provide an input list (list of
integer pairs) for which this sort is not stable. 42
3. (Code-completion) Fill in the missing lines for a function that computes the third-smallest
element in a list of distinct integers. (Partial code shown; complete it.) 40
4. (Recursive code) Fill in the missing recursive argument for a function
evenpositions(l) that returns the elements at even indices in list l . 43
7
5. Define “sum of three squares”. Write a function sumof3squares(n) that returns True
if n can be written as i^2+j^2+k^2 with i,j,k ≥ 1 , else False . (Examples
given.) 44
6. Write a function uncommon(l1,l2) that takes two sorted lists and returns a sorted list
of elements that appear in exactly one of the lists (union minus intersection). (Examples
given.) 45
7. Program: Read lines of text until a blank line. The first line is a pattern string. Print the
last input line (among the following lines) that contains this pattern. (If none match, print
empty line.) (Example illustrated.) 46 47
• Test 9–12 AM (Sep 17, 2017) 48 49 :
1. A function maxbad(l) (another version) is given. Provide a list input for which it is
incorrect. 48
2. A (flawed) quicksort implementation is given. Provide a list input for which it produces
an incorrect sort. 50
3. Complete the missing lines in a function min3(x,y,z) that returns the smallest of
three integers (all below 1,000,000). 51
4. Complete the missing recursive call for myreverse(l) , which reverses a list. (Base case
given.) 52
5. Define “square-free” integer. Write a function squarefree(n) that returns True if n
is not divisible by any square >1 (i.e. no factor i^2 with i>1 divides n ), else False .
(Example: 5,10,21 are square-free; 4,48 are not.) 53
6. Write a function disjointlist(l1,l2) returning True if two lists share no common
element (i.e. no element appears in both), else False . (Definition given.) 54
7. Program: Read an even number of input lines (terminated by blank line). Suppose there
are 2n lines. Print the last n lines followed by the first n lines (essentially swap halves).
(Example given.) 49
8. Write a function maxcount(l) that returns the maximum frequency of any value in list
l . E.g. maxcount([1,17,31,17,22,17]) returns 3 (since 17 occurs 3 times).
(Another example given.) 55
• Quiz Questions:
• Week 4 Quiz (NPTEL) 56 57 :
1. Given
def mystery(l):
if l == []:
return l
else:
return mystery(l[1:]) + l[:1]
What does mystery([22,14,19,65,82,55]) return? 56
2. After
8
pairs = [ (x,y) for x in range(4,1,-1)
for y in range(5,1,-1)
if (x+y)%3 == 0 ]
what is the value of pairs ? 58
3. Given
wickets = {"Tests":{"Kumble":[3,5,2,3],"Srinath":[4,4,1,
0],"Prasad":[2,1,7,4]},
"ODI": {"Kumble":[2,0],"Srinath":[1,2]}}
Which of the following does not raise an error? (Answer was wickets["ODI"]
["Prasad"] = [4,4] .) 59
4. With hundreds = {} initially, which of the following assignments generates an error?
(Answer was hundreds[["Tendulkar","international"]] = 100 .) 60
• Week 6 Quiz (NPTEL) 61 62 :
1. Suppose u and v are Python sets. What is the most general condition that guarantees
u - (v - u) == u ? 61
2. Suppose u and v are Python sets. What is the most general condition that guarantees
u | v == u ^ v (union equals symmetric difference)? 61
3. Starting from the min-heap [17,25,42,67,38,89,54,98,89] , insert 19. What is the
resulting heap (in array form)? 62
4. Given the min-heap [13,29,24,67,52,89,45,98,79,58] , perform delete-min twice.
What is the resulting heap? 62
All the above questions were sourced from publicly available NPTEL course materials and solution
repositories 1 46 . Each question is cited with the corresponding source. (For answers or detailed
discussion, refer to the original sources.)
Sources: Aggregated from NPTEL assignment solution blogs and archives 1 46 61 . Each question
above is quoted from the cited source.
1 2 3 4 5 6 7 8 9 10 11 12 Programming Data Structures And Algorithms Using Python
Week 1 Nptel
[Link]
13 14 15 16 17 18 19 20 21 22 23 24 25 26 Programming DSA Using Python Week 2 Assignment
Answers
[Link]
27 28 29 30 31 32 33 34 Programming DSA Using Python Week 4 Assignment Answers
[Link]
35 36 37 38 Programming Data Structures And Algorithms Using Python Week 5
[Link]
9
39 40 41 42 43 44 45 46 47 ONLINE TEST 2-5 PM | Solution for NPTEL Programming, Data
Structures and Algorithms using Python | Hackademic
[Link]
48 49 50 51 52 53ONLINE TEST 9-12 AM | Solution for NPTEL Programming, Data Structures
54 55
and Algorithms using Python | Hackademic
[Link]
56 57 58 59 60 Programming, Data Structures And Algorithms Using Python WEEK 4 QUIZ · GitHub
[Link]
61 62 Programming, Data Structures And Algorithms Using Python WEEK 6 QUIZ · GitHub
[Link]
10