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) 

Cross
c, b and a abracad0
Cross
a, b and c abracadabra
Cross
a, b and c abracadcad
Tick
c, b and a abracadabra


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) 

Tick
2.0000 2 -8 3.77
Cross
2 3.77 -8 3.77
Cross
2.000 3 -8 3.77
Cross
2.000 2 8 3.77


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)	 

Cross
L*oo frph e| wkhq1
Tick
L*oo#frph#e|#wkhq1
Cross
l*oo@frph@e|$wkhq1
Cross
O*oo#Frph#E|#wKhq1


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=' ') 

Cross
[‘What’, ‘will’, ‘have’, ‘so’, ‘will’]
Tick
Wh t will h ve so will
Cross
What will have so will
Cross
[‘Wh’, ‘t will h’, ‘ve so will’]


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)
Cross
True 2
Tick
True
Cross
False
Cross
False 2


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 =" ") 

Cross
geeksforgeeks
Tick
No Output
Cross
i i i i i i …
Cross
g e e k s f o r g e e k s


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) 

Cross
0 1 2 3 … 12
Cross
geeksforgeeks
Cross
No Output
Tick
Error


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) 
Cross
GEEKSFORGEEKS
Tick
geeksforgeeks
Cross
Error
Cross
No Output


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'
Cross
aaaaaaaaaaaa
Tick
geeksforgeeks a a a a a a a a a a a a
Cross
Error
Cross
No Output


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=" ") 
Cross
1 2 3
Cross
1 1 1
Tick
Error
Cross
No Output


Question 10-Explanation: 
Objects of type int are not iterable instead a list, dictionary or a tuple should be used.
There are 21 questions to complete.


  • Last Updated : 28 Sep, 2023

Share your thoughts in the comments
Similar Reads