Week-1 Python Practice Assignment 2- IITM BS Degree

Week-1, Practice

This Worksheet contains 8 questions.

Question 1

Statement

What will be the output of the following statement?

print('15 % 3 * 2 + 2')

Options

(a) 12

(b) 2

(c) 15 % 3 * 2 + 2

(d) '15 % 3 * 2 + 2'

Answer

(c)

Hint

The content within the quotes will be printed. What if you want to print '15 % 3 * 2 + 2'? Think about it.

Question 2

Statement

What will be the output of the following statement?

print("a + 'b' + c + '' + d")

Options

(a) a + 'b' + c + '' + d

(b) "a + 'b' + c + '' + d"

(c) a + b + c + '' + d

(d) a + ''b'' + c + '' + d

Answer

(a)

Hint

Similar to question-1. But the new concept here is the use of different types of quotes. If you want to print single quotes, then enclose it within double quotes.

Question 3

Statement

A snippet of code gives the following output when executed:

1 2 3 4 5

There is exactly one space between any two consecutive integers. Which of the following options correspond to the correct code snippet? (MSQ).

Options

(a) print(1 2 3 4 5)

(b) print('1 2 3 4 5') # there is a space between consecutive numbers

(c) print(1)
print(2)
print(3)
print(4)
print(5)

(d) print(1, 2, 3, 4, 5)

Answer

(b), (d)

Hint

In option-(d), we see that multiple items can be printed using a single print statement. By default, when printing multiple items in this manner, a space is added between consecutive items. Try executing the following:

print(1, 2)
print('1 2')
print('a', 'b')
print('a b')

Question 4

Statement

What will be the type of the following expressions?

13 % 5 // 2 * 30 ** 5
"@Python"
20 ** 10 / 2 + 25 - 70
(5 > 3) and False
20 * 100.0 // 11 % 5

Options

(a) 1 - (A), 2 - (C), 3 - (B), 4 - (D), 5 - (A)

(b) 1 - (B), 2 - (C), 3 - (A), 4 - (D), 5 - (A)

(c) 1 - (A), 2 - (C), 3 - (B), 4 - (D), 5 - (B)

(d) 1 - (B), 2 - (C), 3 - (A), 4 - (D), 5 - (B)

Answer

(b)

Hint

The division operation will always produce a float value irrespective of the operands involved. Try the following:

print(10 / 5)
print(10 // 5)
print(10.0 // 5)
print(10 // 5.0)

Question 5

Statement

Given a string variable word that stores some word in the English language, we wish to create a new string with just two characters:

  • The first character in the new string is the first letter in word.
  • The second character in the new string is the last letter in word.

Assume that word has at least three characters. Which of the following lines of code can be used to create the new string? (MSQ).

Options

(a) word[0] + word[1]

(b) word[0] + word[-1]

(c) word[0] + word[len(word) - 1]

(d) word[-1] + word[len(word)]

Answer

(b), (c)

Hint

The last character in the string word can be accessed either using the index -1 or the index len(word) - 1.

Question 6

Statement

Consider the following snippet of code:

x = int(input())
y = int(input())
z = int(input())
avg = (x + y + z) / 3
print(avg)

What is the output of the code given above for the following input? Try to think about the answer without executing the code in an interpreter.

10
11
12

Options

(a) 11

(b) 11.0

(c) 33

(d) 33.0

Answer

(b)

Hint

The only catch here is that division operation always results in a float. So, the answer is 11.0 and not 11. Such small things matter in Python!

Question 7

Statement

Consider the following snippet of code:

w1 = input()
w2 = input()
print(w1 + w2)

What is the output of the code given above for the following input? NAT

10
20

Answer

1020

Hint

Have we converted the string to an integer at any point? String concatenation is the way to go here and not integer addition!

Question 8

Statement

Exploratory problem: This problem is meant to encourage you to try certain things outside the lectures

Run this piece of code in the Replit console:

2 ** 3 ** 0

Answer

2

Hint

There are two ways of thinking about this expression and both of them return different answers:

2 ** 3 ** 0

- (2 ** 3) ** 0
- 2 ** (3 ** 0)

The interpreter is following the second way, i.e., the statement is being executed from right to left. This kind of execution happens only in the case of the exponentiation operator and the assignment operator. It is good to know that this behavior is a part of the language.

However, it is even more important to understand the importance of parentheses while writing mathematical expressions. As a good practice, we encourage learners to use parentheses so as to make expressions completely unambiguous and not leave it to the capricious ways of the interpreter to decipher your intentions!

Post a Comment (0)
Previous Post Next Post