Python String Quiz

  • Last Updated : 18 Sep, 2020

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) 

A
c, b and a abracad0
B
a, b and c abracadabra
C
a, b and c abracadcad
D
c, b and a abracadabra
Python String Quiz    
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) 

A
2.0000 2 -8 3.77
B
2 3.77 -8 3.77
C
2.000 3 -8 3.77
D
2.000 2 8 3.77
Python String Quiz    
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)	 

A
L*oo frph e| wkhq1
B
L*oo#frph#e|#wkhq1
C
l*oo@frph@e|$wkhq1
D
O*oo#Frph#E|#wKhq1
Python String Quiz    
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=' ') 

A
[‘What’, ‘will’, ‘have’, ‘so’, ‘will’]
B
Wh t will h ve so will
C
What will have so will
D
[‘Wh’, ‘t will h’, ‘ve so will’]
Python String Quiz    
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)
A
True 2
B
True
C
False
D
False 2
Python String Quiz    
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 =" ") 

A
geeksforgeeks
B
No Output
C
i i i i i i …
D
g e e k s f o r g e e k s
Python String Quiz    
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) 

A
0 1 2 3 … 12
B
geeksforgeeks
C
No Output
D
Error
Python String Quiz    
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) 
A
GEEKSFORGEEKS
B
geeksforgeeks
C
Error
D
No Output
Python String Quiz    
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'
A
aaaaaaaaaaaa
B
geeksforgeeks a a a a a a a a a a a a
C
Error
D
No Output
Python String Quiz    
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=" ") 
A
1 2 3
B
1 1 1
C
Error
D
No Output
Python String Quiz    
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.
My Personal Notes arrow_drop_up


Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.