Week-1, Practice
This document has questions.
Question-1
Statement
What will be the output of the following statement?
1 print('15 % 3 * 2 + 2')
Options
(a)
1 12
(b)
1 2
(c)
1 15 % 3 * 2 + 2
(d)
1 '15 % 3 * 2 + 2'
Answer
(c)
Feedback
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?
1 print("a + 'b' + c + '' + d")
Options
(a)
1 a + 'b' + c + '' + d
(b)
1 "a + 'b' + c + '' + d"
(c)
1 a + b + c + '' + d
(d)
1 a + ''b'' + c + '' + d
Answer
(a)
Feedback
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 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)
1 print(1 2 3 4 5)
(b)
1 print('1 2 3 4 5') # there is a space between consecutive numbers
(c)
1 print(1)
2 print(2)
3 print(3)
4 print(4)
5 print(5)
(d)
1 print(1, 2, 3, 4, 5)
Answer
(b), (d)
Feedback
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.
Execute the following lines and see what you get:
1 print(1, 2)
2 print('1 2')
3 print('a', 'b')
4 print('a b')
Question-4
Statement
What will be the type of the following expressions?
1 13 % 5 // 2 * 30 ** 5
2 "@Python"
3 20 ** 10 / 2 + 25 - 70
4 (5 > 3) and False
5 20 * 100.0 // 11 % 5
(A) float
(B) int
(C) str
(D) bool
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)
Feedback
The division operation will always produce a float value irrespective of the operands involved. For
example, in the expression a / b , even if a and b are integers such that a is divisible by b , the
result will still be a float. Try out the following in your interpreter:
1 print(10 / 5)
For any arithmetic operation, even if one of the operands is float, the resulting value is a float. For
example, try out the following in your interpreter:
1 print(10 // 5)
2 print(10.0 // 5)
3 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 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)
1 word[0] + word[1]
(b)
1 word[0] + word[-1]
(c)
1 word[0] + word[len(word) - 1]
(d)
1 word[-1] + word[len(word)]
Answer
(b), (c)
Feedback
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:
1 x = int(input())
2 y = int(input())
3 z = int(input())
4 avg = (x + y + z) / 3
5 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.
1 10
2 11
3 12
Options
(a)
1 11
(b)
1 11.0
(c)
1 33
(d)
1 33.0
Answer
(b)
Feedback
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:
1 w1 = input()
2 w2 = input()
3 print(w1 + w2)
What is the output of the code given above for the following input? NAT
1 10
2 20
Answer
1020
Feedback
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:
1 2 ** 3 ** 0
What is the output? What do you think is happening here?
Answer
2
Feedback
There are two ways of thinking about this expression and both of them return different answers.
1 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!