Python String Quiz
Question 1 |
Question 1: What is the output of the following program?
str1 = '{2}, {1} and {0}'.format('a', 'b', 'c') str2 = '{0}{1}{0}'.format('abra', 'cad') print(str1, str2)
c, b and a abracad0 | |
a, b and c abracadabra | |
a, b and c abracadcad | |
c, b and a abracadabra |
Discuss it
Question 1 Explanation:
String function format takes a format string and an arbitrary set of positional and keyword arguments. For str1 ‘a’ has index 2, ‘b’ index 1 and ‘c’ index 0. str2 has only two indices 0 and 1. Index 0 is used twice at 1st and 3rd time.
Question 2 |
Question 2: What is the output of the following program?
a = 2 b = '3.77' c = -8 str1 = '{0:.4f} {0:3d} {2} {1}'.format(a, b, c) print(str1)
2.0000 2 -8 3.77 | |
2 3.77 -8 3.77 | |
2.000 3 -8 3.77 | |
2.000 2 8 3.77 |
Discuss it
Question 2 Explanation:
At Index 0, integer a is formatted into a float with 4 decimal points, thus 2.0000. At Index 0, a = 2 is formatted into an integer, thus it remains to 2. Index 2 and 1 values are picked next, which are -8 and 3.77 respectively.
Question 3 |
Question 3: What is the output of the following program?
line = "I'll come by then." eline = "" for i in line: eline += chr(ord(i)+3) print(eline)
L*oo frph e| wkhq1 | |
L*oo#frph#e|#wkhq1 | |
l*oo@frph@e|$wkhq1 | |
O*oo#Frph#E|#wKhq1 |
Discuss it
Question 3 Explanation:
This piece of code ciphers the plain text. Each character is moved to its 3rd next character by increasing the ASCII value. ‘I’ becomes ‘L’, thus option (c) and (d) are ruled out. ‘ ‘ has an ASCII value of 32, thus it’ll become 35(‘#’), thus option (a) is ruled out as, ‘ ‘ cannot remain to be ‘ ‘ in the ciphered text.
Question 4 |
Question 4: What is the output of the following program?
line = "What will have so will" L = line.split('a') for i in L: print(i, end=' ')
[‘What’, ‘will’, ‘have’, ‘so’, ‘will’] | |
Wh t will h ve so will | |
What will have so will | |
[‘Wh’, ‘t will h’, ‘ve so will’] |
Discuss it
Question 4 Explanation:
split() will use ‘a’ as the delimiter. It’ll create partition at ‘a’, thus split() return an array L, which is in [‘Wh’, ‘t will h’, ‘ve so will’]. For loop will print the elements of the list.
Question 5 |
Question 5: What is the output of the following program?
import string Line1 = "And Then There Were None" Line2 = "Famous In Love" Line3 = "Famous Were The Kol And Klaus" Line4 = Line1 + Line2 + Line3 print("And" in Line4)
True 2 | |
True | |
False | |
False 2 |
Discuss it
Question 5 Explanation:
The "in" operator returns True if the string contains the substring (ie, And), else returns False.
Question 6 |
Question 6: What is the output of the following program?
my_string = "geeksforgeeks" i = "i" while i in my_string: print(i, end =" ")
geeksforgeeks | |
No Output | |
i i i i i i … | |
g e e k s f o r g e e k s |
Discuss it
Question 6 Explanation:
‘i’ is not present in string ‘geeksforgeeks’
Question 7 |
Question 7: What is the output of the following program?
my_string = 'geeksforgeeks' for i in range(my_string): print(i)
0 1 2 3 … 12 | |
geeksforgeeks | |
No Output | |
Error |
Discuss it
Question 7 Explanation:
range(str) is not allowed.
Question 8 |
Question 8: What is the output of the following program?
my_string = 'geeksforgeeks' for i in range(len(my_string)): my_string[i].upper() print (my_string)
GEEKSFORGEEKS | |
geeksforgeeks | |
Error | |
No Output |
Discuss it
Question 8 Explanation:
Changes do not happen in-place, rather it will return a new instance of the string.
Question 9 |
Question 9: What is the output of the following program?
my_string = 'geeksforgeeks' for i in range(len(my_string)): print (my_string, end=" ") my_string = 'a'
aaaaaaaaaaaa | |
geeksforgeeks a a a a a a a a a a a a | |
Error | |
No Output |
Discuss it
Question 9 Explanation:
String is modified only after ‘geeksforgeeks’ has been printed once.
Question 10 |
Question 10: What is the output of the following program?
x = 123 for i in x: print(i, end=" ")
1 2 3 | |
1 1 1 | |
Error | |
No Output |
Discuss it
Question 10 Explanation:
Objects of type int are not iterable instead a list, dictionary or a tuple should be used.
There are 19 questions to complete.