Quiz 2
Registry Synced

May 2026 - Python - Week 4 - Week 4 - Graded Assignment 4

3026 words
15 min read

Week 4 - Graded Assignment 4

Course: May 2026 - Python
Week 4 - Graded Assignment 4 WEEK 4 Due Jul 15, 2026 11:59 PM IST Overdue

Introduction

Week 4 - Graded Assignment 4Week 4
15th Jul

Week 4 - Graded Assignment 4

Week 4
Due Jul 15, 202611:59 PM ISTOverdue

Question 1 / 15

2 marks
12345
plaintext
Execute the following snippet of code and then answer questions 1 and 2.

What is the size of the list rolls? (NAT)
python
Execute the following snippet of code and then answer questions 1 and 2.



What is the size of the list rolls? (NAT)
python
Execute the following snippet of code and then answer questions 1 and 2.



What is the size of the list rolls? (NAT)
Your Answer: (Not answered)

Question 2 / 15

2 marks
Execute the following snippet of code.
1
python
some_var = count / len(rolls)
What does the variable some_var represent?
python
some_var = count / len(rolls)
python
some_var = count / len(rolls)
Answer is Correct Score: 2 / 2
Options:
  • A. It represents the probability that a number chosen at random between 1 and 100000 is a prime.
  • B. It represents the number of primes between 1 and 100000
  • C. It represents the number of prime numbers in the list rolls.
  • D. It represents the probability that a number chosen at random from the list rolls is a prime. ✅
  • E. It represents the probability that a number chosen at random from the list primes is a prime.

Question 3 / 15

2 marks
1234567891011121314151617
plaintext
Common Data for questions (3) and (4)

Assume that L is a non-empty list of positive integers. Also assume that the list is a distinct collection of numbers, i.e., no two numbers are alike. Consider the following code. Answer questions 3 and 4 based on this code.

S = 0
for x in L:
    S += x

flag = False
y = -1
for x in L:
    if x * len(L) == S:
        flag = True
        y = x
        break

If flag is True at the end of execution of the code given above, which of the following statements are true? Note that the options should be true for any list L that satisfies the conditions given in the common data. Multiple options could be correct.
python
Common Data for questions (3) and (4)

Assume that L is a non-empty list of positive integers. Also assume that the list is a distinct collection of numbers, i.e., no two numbers are alike. Consider the following code. Answer questions 3 and 4 based on this code.

S = 0
for x in L:
    S += x

flag = False
y = -1
for x in L:
    if x * len(L) == S:
        flag = True
        y = x
        break

If flag is True at the end of execution of the code given above, which of the following statements are true? Note that the options should be true for any list L that satisfies the conditions given in the common data. Multiple options could be correct.
python
Common Data for questions (3) and (4)

Assume that L is a non-empty list of positive integers. Also assume that the list is a distinct collection of numbers, i.e., no two numbers are alike. Consider the following code. Answer questions 3 and 4 based on this code.

S = 0
for x in L:
    S += x

flag = False
y = -1
for x in L:
    if x * len(L) == S:
        flag = True
        y = x
        break

If flag is True at the end of execution of the code given above, which of the following statements are true? Note that the options should be true for any list L that satisfies the conditions given in the common data. Multiple options could be correct.
Answer is Partially Correct Score: 1 / 2
Options:
  • A. y is an element in the list L ✅
  • B. y is the smallest number in the list
  • C. y is the greatest number in the list
  • D. y is the average (arithmetic mean) of the numbers in the list
  • E. y is the element at index len(L) // 2 in the list L

Question 4 / 15

2 marks
Assume that L is a list of the first nnn positive integers, where n>0n > 0n>0. Under what conditions will the variable flag be True at the end of execution of the code given above?
Answer is Correct Score: 2 / 2
Options:
  • A. n is an odd integer ✅
  • B. n is an even integer

Question 5 / 15

2 marks
1234567891011121314151617
plaintext
L = [90, 47, 8, 18, 10, 7]
S = [L[0]]    # list containing just one element
for i in range(1, len(L)):
    flag = True
    for j in range(len(S)):
        if L[i] < S[j]:
            before_j = S[: j]    # elements in S before index j
            new_j = [L[i]]       # list containing just one element
            after_j = S[j: ]     # elements in S starting from index j
            # what is the size of S now?
            S = before_j + new_j + after_j
            # what is the size of S now?
            flag = False
            break
    if flag:
        S.append(L[i])
print(S)
python
L = [90, 47, 8, 18, 10, 7]
S = [L[0]]    # list containing just one element
for i in range(1, len(L)):
    flag = True
    for j in range(len(S)):
        if L[i] < S[j]:
            before_j = S[: j]    # elements in S before index j
            new_j = [L[i]]       # list containing just one element
            after_j = S[j: ]     # elements in S starting from index j
            # what is the size of S now?
            S = before_j + new_j + after_j
            # what is the size of S now?
            flag = False
            break
    if flag:
        S.append(L[i])
print(S)
python
L = [90, 47, 8, 18, 10, 7]
S = [L[0]]    # list containing just one element
for i in range(1, len(L)):
    flag = True
    for j in range(len(S)):
        if L[i] < S[j]:
            before_j = S[: j]    # elements in S before index j
            new_j = [L[i]]       # list containing just one element
            after_j = S[j: ]     # elements in S starting from index j
            # what is the size of S now?
            S = before_j + new_j + after_j
            # what is the size of S now?
            flag = False
            break
    if flag:
        S.append(L[i])
print(S)
Answer is Correct Score: 2 / 2
Options:
  • A. [90, 47, 8, 18, 10, 7]
  • B. [7, 10, 18, 8, 47, 90]
  • C. [7, 8, 10, 18, 47, 90] ✅
  • D. [90, 47, 18, 10, 8, 7]
  • E. [90, 7, 8, 10, 18 47]

Question 6 / 15

2 marks
Consider the following grid of integer points in the plane: Pij=(i,j), 0≤i,j≤4P_{ij} = (i, j),\ 0 \leq i, j \leq 4Pij​=(i,j), 0≤i,j≤4.
image
We wish to represent this grid of points as a list of tuples. The name of the list should be points. Each element of the list should be a tuple of the form (x, y). The first element of the tuple is the x-coordinate (horizontal) of the point, the second is the y-coordinate (vertical).
We have employed the standard Cartesian coordinate system. Select all the correct implementations of this program. (MSQ)
Answer is Correct Score: 2 / 2
Options:
  • A. 12345points = []
for x in range(0, 5): for y in range(0, 5): points.append(x, y)
  • B. ```html 12345points = []
for x in range(0, 5): for y in range(0, 5): points.append([x, y])
  • C. 12345points = ()
for x in range(0, 5): for y in range(0, 5): points.append((x, y))
  • D. 12345points = []
for x in range(0, 5): for y in range(0, 5): points.append((x, y)) ✅

Question 7 / 15

2 marks
123
plaintext
L = [y - x for x in [1, 2, 3] for y in [3, 4, 5] if y > x]

Which of the following codes are equivalent to the above code? [MSQ]
python
L = [y - x for x in [1, 2, 3] for y in [3, 4, 5] if y > x]

Which of the following codes are equivalent to the above code? [MSQ]
python
L = [y - x for x in [1, 2, 3] for y in [3, 4, 5] if y > x]

Which of the following codes are equivalent to the above code? [MSQ]
Answer is Correct Score: 2 / 2
Options:
  • A. 12345L = [ ] for x in [1, 2, 3]: for y in [3, 4, 5]: if y > x: L.append(y - x) ✅
  • B. 12345L = [ ] for y in [3, 4, 5]: for x in [1, 2, 3]: if y > x: L.append(y - x)
  • C. 12345L = [ ] for x in [1, 2, 3]: for y in [3, 4, 5]: if y > x: L += [y - x] ✅
  • D. 12345L = [] for y in [3, 4, 5]: for x in [1, 2, 3]: if y > x: L += [y -x]

Question 8 / 15

2 marks
1234
plaintext
We wish to find all integer triplets (x, y, z) such that:
x^2 + y^2 = z^2, and 0 < x < y < z < 100

Select all snippets of code that create a list called triplets and store each triplet as a tuple. [MSQ]
python
We wish to find all integer triplets (x, y, z) such that:
x^2 + y^2 = z^2, and 0 < x < y < z < 100

Select all snippets of code that create a list called triplets and store each triplet as a tuple. [MSQ]
python
We wish to find all integer triplets (x, y, z) such that:
x^2 + y^2 = z^2, and 0 < x < y < z < 100

Select all snippets of code that create a list called triplets and store each triplet as a tuple. [MSQ]
Answer is Correct Score: 2 / 2
Options:
  • A. 1234triplets = [(x, y, z) for x in range(1, 100) for y in range(x + 1, 100) for z in range(y + 1, 100) if x ** 2 + y ** 2 == z ** 2] ✅
  • B. 123456triplets = [ ] for x in range(1, 100): for y in range(x + 1, 100): for z in range(y + 1, 100): if x ** 2 + y ** 2 == z ** 2: triplets.append((x, y, z)) ✅
  • C. 1234triplets = [(x, y, z) for x in range(1, 100) for y in range(1, 100) for z in range(1, 100) if x ** 2 + y ** 2 == z ** 2 and x < y < z] ✅

Question 9 / 15

2 marks
1
plaintext
L is a list of names. Create a list P of names that contain only those names in L that begin with a capital letter. Select all correct implementations. [MSQ]
python
L is a list of names. Create a list P of names that contain only those names in L that begin with a capital letter. Select all correct implementations. [MSQ]
python
L is a list of names. Create a list P of names that contain only those names in L that begin with a capital letter. Select all correct implementations. [MSQ]
Answer is Correct Score: 2 / 2
Options:
  • A. 1P = [name for name in L if 'a' <= name[0] <= 'z']
  • B. 1P = [name for name in L if 'A' <= name[0] <= 'Z'] ✅
  • C. 1234P = [ ] for name in L: if 'A' <= name[0] <= 'Z': P.append(name) ✅

Question 10 / 15

2 marks
Accept a sequence of comma-separated strings as input from the user and populate a list of strings that do not have the letter 'e' in them. Print this list as output to the console. Select all snippets of code that achieve this. [MSQ]
Answer is Correct Score: 2 / 2
Options:
  • A. 123456P = input().split(',') L = [ ] for word in P: if 'e' not in word: L.append(word) print(L) ✅
  • B. 12L = [word for word in input().split(',') if 'e' not in word] print(L) ✅
  • C. 1print([word for word in input().split(',') if 'e' not in word]) ✅

Question 11 / 15

2 marks
12345678
plaintext
Consider the following snippet of code.

def minmax(a, b):
    if a <= b:
        return a, b
    return b, a

x is a real number. When minmax(x, x) is called, which return statement in the function is executed?
python
Consider the following snippet of code.

def minmax(a, b):
    if a <= b:
        return a, b
    return b, a

x is a real number. When minmax(x, x) is called, which return statement in the function is executed?
python
Consider the following snippet of code.

def minmax(a, b):
    if a <= b:
        return a, b
    return b, a

x is a real number. When minmax(x, x) is called, which return statement in the function is executed?
Answer is Correct Score: 2 / 2
Options:
  • A. The return statement in line-3 which is inside the if-block. ✅
  • B. The return statement in line-4 which is outside the if-block.
  • C. Both the return statements are executed.
  • D. Neither return statement is executed.

Question 12 / 15

2 marks
Select all correct implementations of a function named unique that accepts a non-empty list L of integers as an argument. The function should remove all duplicate elements from the list L. Specifically, It should return a list L_uniq that retains the first occurrence (from the left) of each distinct element in the input list L.
A few instances of the input-output behaviour of the function is given below. Note that these are just sample test cases. Your function should work for any non-empty list L of integers. (MSQ)
L unique(L)
[1, 1, 2, 3, 5, 5] [1, 2, 3, 5]
[1, 2, 3, 4, 5, 7, 7] [1, 2, 3, 4, 5, 7]
Answer is Correct Score: 2 / 2
Options:
  • A. 123456def unique(L): L_uniq = [ ] for elem in L: if elem not in L_uniq: L_uniq.append(elem) return L_uniq ✅
  • B. 123456def unique(L): L_uniq = [ ] for elem in L: if elem in L_uniq: L_uniq.append(elem) return L_uniq
  • C. 123456def unique(L): L_uniq = [L[0]] for i in range(1, len(L)): if not(L[i] in L[:i]): L_uniq.append(L[i]) return L_uniq ✅
  • D. 123456def unique(L): L_uniq = [ ] for i in range(1, len(L)): if not(L[i] in L[:i]): L_uniq.append(L[i]) return L_uniq
  • E. 123456def unique(L): L_uniq = [ ] for i in range(0, len(L)): if not(L[i] in L[i + 1: ]): L_uniq.append(L[i]) return L_uniq

Question 13 / 15

2 marks
Given a Python list of the coefficients of a polynomial — L = [a_0, a_1, a_2,. . ., a_n] — write a function poly that accepts the list of coefficients L and a real number x_0 as arguments. It should return the polynomial evaluated at the value x_0.
For example poly([1, 2, 3], 5) should return the value 1+2×5+3×52=861 + 2 \times 5 + 3 \times 5^2 = 861+2×5+3×52=86. Select the correct implementation of this function.
Answer is Correct Score: 2 / 2
Options:
  • A. 12345def poly(L, x_0): n = len(L) for i in range(n): psum = psum + L[i] * (x_0 ** i) return psum
  • B. 123456def poly(L, x_0): psum = 0 n = len(L) for i in range(1, n): psum = psum + L[i] * (x_0 ** i) return psum
  • C. 123456def poly(L, x_0): psum = 0 n = len(L) for i in range(n): psum = psum + L[i] * (x_0 ** i) return psum ✅
  • D. 123456def poly(L, x_0): psum = 0 n = len(L) for i in range(n): psum = psum + x_0 * (L[i] ** i) return psum

Question 14 / 15

2 marks
Write a function named poly_zeros that accepts the list of coefficients L, and two integers a and b as arguments. It should return a list named zeros, that consists of all integer-zeros of the polynomial in the range [a,b][a, b][a,b], endpoints inclusive, in ascending order. For example, poly_zeros([2, -3, 1], 0, 4) should return the list [1, 2].
Assume that you have access to the function poly that was defined in question (7). Select the correct implementation of this function.
Note: The returned list should not have any repeating elements.
Answer is Correct Score: 2 / 2
Options:
  • A. 123456def poly_zeros(L, a, b): zeros = [ ] for x in range(a, b + 1): if poly(L, x) != 0: zeros.append(x) return zeros
  • B. 12345678def poly_zeros(L, a, b): zeros = [ ] for x in range(a, b + 1): if poly(L, x) == 0: zeros.append(x) else: return zeros return zeros
  • C. 12345def poly_zeros(L, a, b): zeros = [ ] for x in range(a, b + 1): if poly(L, x) == 0: zeros.append(x)
  • D. 123456def poly_zeros(L, a, b): zeros = [ ] for x in range(a, b + 1): if poly(L, x) == 0: zeros.append(x) return zeros ✅

Question 15 / 15

2 marks
Using the two functions defined above, find the number of integer-zeros of the polynomial f(x)=x6−4x5−18x4+52x3+101x2−144x−180f(x) = x^6 - 4 x^5 - 18 x^4 + 52 x^3 + 101 x^2 - 144 x - 180f(x)=x6−4x5−18x4+52x3+101x2−144x−180 in the range [0,4][0, 4][0,4], endpoints inclusive. You must enter a non-negative integer. Note that we are asking for the number of integer-zeros and not their values. (NAT)
Your Answer: (Not answered)

Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.