The Joy Of Computing Using Python Week 3 : Assignment 1 | NPTEL | [Jan 2023]

The Joy of Computing using Python Week 3 - Assignment 1 | NPTEL | Answer with Explanation

The Joy of Computing using Python Week 2 Assignment 1  NPTEL   [Jan 2023]
"Discover the Excitement of Computing with Python | Week 3 Multiple Choice Questions - Get ready to enhance your programming skills and deepen your understanding of the Python language with this week 3 MCQ on 'The Joy of Computing using Python'. Test your knowledge and boost your confidence as a Python programmer today!"

___________ is the method to insert an item into a specified position in a list.

a. Push
b. Write
c. Insert
d. All of the above
Answer

c. Insert
- The method to insert an item into a specified position in a list in Python is the insert method.

Example:
fruits = ["apple", "banana", "cherry"]
fruits.insert(1, "mango")
print(fruits)

Output:
["apple", "mango", "banana", "cherry"]

Which method returns the number of occurrences of an element in a list.

a. Number of
b. Total
c. Count
d. Length
Answer

c. Count
- The method that returns the number of occurrences of an element in a list is the count() method.
For example, consider the following list:
list1 = [1, 2, 3, 1, 2, 1, 3, 4]
To find the number of occurrences of the number 1 in

the list, we use the count() method as follows:
count_of_one = list1.count(1)
print(count_of_one)

Output: 3

Note: The len() function returns the total number of elements in a list, while the count() method returns the number of occurrences of a specific element in a list.

The function random.randint(1,100) in python generates.

a. A random integer between 1 to 100 with 1 and 100 both inclusive
b. A random integer between 1 to 100 with 1 and 100 both exclusive
c. A random integer between 1 to 100 with only 100 inclusive
d. None of the above
Answer

a. A random integer between 1 to 100 with 1 and 100 both inclusive
Example

import random
random_integer = random.randint(1, 100)
print(random_integer)

This code will generate a random integer between 1 and 100 (including 1 and 100). The output may be something like: 57.

The method open(“file1.txt”, r+) opens the file file1.txt in __________________.


a. Read only mode
b. Write only mode
c. Read Write mode
d. None of the above
Answer

c. Read Write mode
open() Syntax:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
- r Opens a file for reading only.
-rb Opens a file for reading only in binary format.
-r+ Opens a file for both reading and writing.
-rb+ Opens a file for both reading and writing in binary format.

Consider the list L= [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]. What will be the output of the statement L [3:6]?

a. [2, 3, 5]
b. [0, 1, 1]
c. [1, 2, 3]
d. none
Answer

a. [2, 3, 5]
L[3:6] is a slice notation in Python. It represents the sub-list of elements from the list "L" starting from the index 3 up to but not including the index 6.
In other words, it returns a new list containing the elements of the original list "L" at the indices 3, 4, and 5. If "L" is [a, b, c, d, e, f, g], then L[3:6] would be [d, e, f].

What is the output of this code?

a,b=1,0
a=a^b
b=a^b
a=a^b
print(a)
a. 0
b. 1
c. 2
d. This code will raise a runtime error
Answer

a. 0
The code swaps the values of two variables a and b using the bitwise XOR operator ^. The line a,b=1,0 assigns the values 1 and 0 to variables a and b, respectively.
The next three lines perform the swap: a=a^b b=a^b a=a^b

The first line a=a^b computes the bitwise XOR of a and b, and stores the result in a.

The second line b=a^b takes the value of a (which was updated in the first line), XORs it with b, and stores the result in b.

The third line a=a^b takes the updated value of b (which now contains the original value of a), XORs it with a, and stores the result in a.

Finally, the line print(a) outputs the value of a, which is the original value of b (i.e. 0).

What is the output of the following code?

def foo(l):
    a = l[0]
    for i in l:
        if i < a:
           a = i
    return a
print(foo([2, 3, 5, 1, 7, 6]))
Answer

1
The function foo takes a single argument l, which is a list.
Inside the function, a variable a is initialized with the first value of the list l by using a = l[0].
The for loop iterates over the elements in the list l.
In each iteration, the code if i < a: checks if the current element i is less than the value stored in a. If i is less than a, then a is updated with the value of i.
After the for loop is completed, the function returns the minimum value found in the list l, which is stored in a.
Finally, the function is called with the list [2, 3, 5, 1, 7, 6] and the minimum value, 1, is printed as the result.

What is the output of the following code?

def bar(string):
    left = 0
    right = len(string) - 1
    while(left < right):
             if (string[left] != string[right]):
                       return False
             left += 1
             right -= 1
    return True

print(bar("telugu"))
print(bar("malayalam"))
a. False True
b. True False
c. True True
d. False False
Answer

a. False True

Explain what the output will be when the code given below is executed.

def func(list1):
   while (len(list1) > 2 ):
      k=list1[0]
       for i in list1:
          if k < i:
             k = i
         list1.remove(k)

          j=list1[0]
    for i in list1:
    if j>i:
             j=i
     list1.remove(j)
         return list1

list2=func([1,4,3,6,5,3,7,8,9,4])
#sum is a function which returns the sum of all of the values of all the elements of a list
print(sum(list2)/len(list2))
a. The program throws an error
b. 5
c. 5.5
d. 4.5
Answer

a. The program throws an error
Output:
ValueError: list.remove(x): x not in list

Which among the following statements is True with respect to the code given below?

count = 0
for i in range(10):
    for j in range(5):
        count += i * j
print(count)

a. count=50
b. The following code throws up an error.
c. count=550
d. count=450
Answer

d. count=450
The code is a nested loop that calculates a value for the variable "count". The outer loop (for i in range(10)) runs 10 times and the inner loop (for j in range(5)) runs 5 times per iteration of the outer loop. This results in a total of 10 * 5 = 50 iterations of the inner loop.
In each iteration of the inner loop, the value of "i * j" is calculated and added to "count". The final value of "count" is printed after both loops have completed.

A variable 'count' is initialized with the value 0.
The outer for loop is executed 10 times.
For each iteration of the outer loop, the inner for loop is executed 5 times.
The inner loop calculates the value of 'i * j' and adds it to 'count' in each iteration.
After the inner loop finishes executing, the outer loop continues to the next iteration.
The process of the inner loop executing 5 times and the outer loop 10 times continues until all iterations have been completed.
After both loops have finished executing, the final value of 'count' is printed.

Example:
Suppose the first iteration of the outer loop is with i=2.
Then, the inner loop will execute 5 times, each time with a different value of j (0 to 4). The calculation of i * j will be performed 5 times, each time with a different value of j, and the result will be added to 'count' each time.

The inner loop for i=2:
i * j (with j=0) = 2 * 0 = 0, count = count + 0 = 0
i * j (with j=1) = 2 * 1 = 2, count = count + 2 = 2
i * j (with j=2) = 2 * 2 = 4, count = count + 4 = 6
i * j (with j=3) = 2 * 3 = 6, count = count + 6 = 12
i * j (with j=4) = 2 * 4 = 8, count = count + 8 = 20

This process is repeated for each iteration of the outer loop, with a different value of i, until the final value of count is obtained.

Disclaimer:
"This page contains multiple choice questions (MCQs) related to The Joy of Computing using Python . The answers to these questions are provided for educational and informational purposes only.These answers are provided only for the purpose to help students to take references. This website does not claim any surety of 100% correct answers. So, this website urges you to complete your assignment yourself."

The Joy of Computing using Python Week 3,NPTEL ,,Assignment 1 [Jan 2023],noc23_cs20

Post a Comment

0 Comments