Strings in Python - Set-1
1) What will be the output of the following Python statement?
print("a"+"bc")
a) a) a
b) b) bc
c) c) bca
d) d) abc
2) What will be the output of the following Python statement?
print("abcd"[2:])
a) a) a
VS
b) b) ab
c) c) cd
d) d) dc
3) What will be the output of the following Python code?
str1 = 'hello'
str2 = ','
RA str3 = 'world'
print(str1[-1:])
a) a) olleh
b) b) hello
c) c) h
d) d) o
U
4) What arithmetic operators cannot be used with strings?
a) a) +
b) b) *
SO
c) c) –
d) d) All of the mentioned
5) What will be the output of the following Python code?
print(r"\nhello")
a) a) a new line and hello
b) b) \nhello
c) c) the letter r and then hello
d) d) error
6) What will be the output of the following Python statement?
print('new' 'line')
a) a) Error
b) b) Output equivalent to print ‘new\nline’
c) c) newline
d) d) new line
7) What will be the output of the following Python code?
str1="helloworld"
print(str1[::-1])
a) a) dlrowolleh
b) b) hello
c) c) world
d) d) helloworld
8) What will be the output of the following Python code?
print(0xA + 0xB + 0xC)
a) a) 0xA0xB0xC
b) b) Error
c) c) 0x22
d) d) 33
VS
Set-2
9) What will be the output of the following Python code?
example = "snow world"
print("%s" % example[4:7])
RA a) a) ” wo”
b) b) ” world”
c) c) “sn ”
d) d) ” rl”
10) What will be the output of the following Python code?
example = "snow world"
U
print(example[3] = 's')
a) a) snow
b) b) snow world
c) c) Error
SO
d) d) snos world
11) What will be the output of the following Python code?
print(max("what are you"))
a) a) error
b) b) u
c) c) t
d) d) y
12) Given a string example=”hello” what is the output of example.count(‘l’)?
a) a) 2
b) b) 1
c) c) None
d) d) 0
13) What will be the output of the following Python code?
example = "helle"
print(example.find("e"))
a) a) Error
b) b) -1
c) c) 1
d) d) 0
14) What will be the output of the following Python code?
example = "helle"
print(example.rfind("e"))
a) a) -1
b) b) 4
c) c) 3
d) d) 1
VS
15)What will be the output of the following Python code?
example="helloworld"
print(example[::-1].startswith("d"))
a) a) dlrowolleh
b) b) True
c) c) -1
d) d) None
RA
16)To concatenate two strings to a third what statements are applicable?
a) a) s3 = s1 . s2
b) b) s3 = s1.add(s2)
c) c) s3 = s1.__add__(s2)
d) d) s3 = s1 * s2
Set - 3
U
17) Which of the following statements prints hello\example\test.txt?
a) a) print(“hello\example\test.txt”)
SO
b) b) print(“hello\\example\\test.txt”)
c) c) print(“hello\”example\”test.txt”)
d) d) print(“hello”\example”\test.txt”)
18)Suppose s is “\t\tWorld\n”, what is s.strip()?
a) a) \t\tWorld\n
b) b) \t\tWorld\n
c) c) \t\tWORLD\n d) World
19)What will be the output of the “hello” +1+2+3?
a) a) hello123
b) b) hello
c) c) Error
d) d) hello6
20)What will be the output of the following Python code?
print("D", end = ' ')
print("C", end = ' ')
print("B", end = ' ')
print("A", end = ' ')
a) a) DCBA
b) b) A, B, C, D
c) c) D C B A
d) d) D, C, B, A will be displayed on four lines
21)Say s=”hello” what will be the return value of type(s)?
a) a) int
b) b) bool
c) c) str
d) d) String
Set - 4
VS
22) What is “Hello”.replace(“l”, “e”)?
a) a) Heeeo
b) b) Heelo
c) c) Heleo
d) d) None
RA
23)To return the length of string s what command do we execute?
a) a) len(s) OR s.__len__()
b) b) len(s) only
c) c) size(s)
d) d) s.size()
U
24)What function do you use to read a string?
a) a) input(“Enter a string”)
b) b) eval(input(“Enter a string”))
c) c) enter(“Enter a string”)
SO
d) d) eval(enter(“Enter a string”))
Set - 5
25)What will be the output of the following Python code?
print("abc DEF".capitalize())
a) a) abc def
b) b) ABC DEF
c) c) Abc def
d) d) Abc Def
26)What will be the output of the following Python code?
print("abc. DEF".capitalize())
a) a) abc. def
b) b) ABC. DEF
c) c) Abc. def
d) d) Abc. Def
Set - 6
27)What will be the output of the following Python code?
print("xyyzxyzxzxyy".count('yy'))
a) a) 2
b) b) 0
c) c) error
d) d) none of the mentioned
28)What will be the output of the following Python code?
print("xyyzxyzxzxyy".count('yy', 1))
a) a) 2
b) b) 0
VS
c) c) 1
d) d) none of the mentioned
29)What will be the output of the following Python code?
print("xyyzxyzxzxyy".count('yy', 2))
a) a) 2
b) b) 0
RA c) c) 1 d) none of the mentioned
30)What will be the output of the following Python code?
print("xyyzxyzxzxyy".count('xyy', 0, 100))
a) a) 2
b) b) 0
c) c) 1
U
d) d) error
31)What will be the output of the following Python code?
print("xyyzxyzxzxyy".count('xyy', 2, 11))
SO
a) a) 2
b) b) 0
c) c) 1
d) d) error
32)What will be the output of the following Python code?
print("xyyzxyzxzxyy".count('xyy', -10, -1))
a) a) 2
b) b) 0
c) c) 1
d) d) error
33)What will be the output of the following Python code?
print("xyyzxyzxzxyy".endswith("xyy"))
a) a) 1
b) b) True
c) c) 3
d) d) 2
34)What will be the output of the following Python code?
print("xyyzxyzxzxyy".endswith("xyy", 0, 2))
a) a) 0
b) b) 1
c) c) True
d) d) False
Set - 7
35)What will be the output of the following Python code?
print("abcdef".find("cd") == "cd" in "abcdef")
VS
a) a) True
b) b) False
c) c) Error
d) d) None of the mentioned
RA
36)What will be the output of the following Python code?
print("abcdef".find("cd"))
a) a) True
b) b) 2
c) c) 3
d) d) None of the mentioned
U
37)What will be the output of the following Python code?
a) print("ccdcddcd".find("c"))
b) a) 4
SO
c) b) 0
d) c) Error
e) d) True
38)What will be the output of the following Python code?
print("Hello {0} and {1}".format('foo', 'bin'))
a) a) Hello foo and bin
b) b) Hello {0} and {1} foo bin
c) c) Error
d) d) Hello 0 and 1
39)What will be the output of the following Python code?
print("Hello {0} and {1}".format('bin', 'foo'))
a) a) Hello foo and bin
b) b) Hello bin and foo
c) c) Error
d) d) None of the mentioned
40)What will be the output of the following Python code?
print("Hello {} and {}".format('foo', 'bin'))
a) a) Hello foo and bin
b) b) Hello {} and {}
c) c) Error
d) d) Hello and
41)What will be the output of the following Python code?
print("Hello {name1} and {name2}".format('foo', 'bin'))
a) a) Hello foo and bin
b) b) Hello {name1} and {name2}
c) c) Error
VS
d) d) Hello and
RA
Set - 9
42)What will be the output of the following Python code?
print('ab12'.isalnum())
a) a) True
b) b) False
U
c) c) None
d) d) Error
43)What will be the output of the following Python code?
SO
print('ab,12'.isalnum())
a) a) True
b) b) False
c) c) None
d) d) Error
44)What will be the output of the following Python code?
print('ab'.isalpha())
a) a) True
b) b) False
c) c) None
d) d) Error
45)What will be the output of the following Python code?
print('a B'.isalpha())
a) a) True
b) b) False
c) c) None
d) d) Error
46)What will be the output of the following Python code snippet?
print('0xa'.isdigit())
a) a) True
b) b) False
c) c) None
d) d) Error
47)What will be the output of the following Python code snippet?
print(' '.isdigit())
a) a) True
VS
b) b) False
c) c) None
d) d) Error
Set - 10
RA
48)What will be the output of the following Python code snippet?
print('abc'.islower())
a) a) True
b) b) False
c) c) None
U
d) d) Error
49)What will be the output of the following Python code snippet?
print('a@ 1,'.islower())
SO
a) a) True
b) b) False
c) c) None
d) d) Error
50)What will be the output of the following Python code snippet?
print(''''''.isspace())
a) a) True
b) b) False
c) c) None
d) d) Error
51)What will be the output of the following Python code snippet?
print('\t'.isspace())
a) a) True
b) b) False
c) c) None
d) d) Error
52)What will be the output of the following Python code snippet?
print('HelloWorld'.istitle())
a) a) True
b) b) False
c) c) None
d) d) Error
53)What will be the output of the following Python code snippet?
print('Hello World'.istitle())
a) a) True
b) b) False
VS
c) c) None
d) d) Error
RA
U
SO