Python MCQs on String: SET 7

61. What will be the output of the following Python code?

print(“ab\tcd\tef”.expandtabs())

a) ab  cd  ef

b) abcdef

c) ab\tcd\tef

d) ab cd ef

Check Answer

 

Answer: a
Each \t is converted to 8 blank spaces by default.

 

 

62. What will be the output of the following Python code?

print(“ab\tcd\tef”.expandtabs(4))

a) ab  cd  ef

b) abcdef

c) ab\tcd\tef

d) ab cd ef

Check Answer

Answer: d
Each \t is converted to 4 blank spaces.

 

 

63. What will be the output of the following Python code?

print(“ab\tcd\tef”.expandtabs(‘+’))

a) ab+cd+ef

b) ab++++++++cd++++++++ef

c) ab cd ef

d) none of the mentioned

Check Answer

Answer: d
TypeError, an integer should be passed as an argument.

 

 

64. What will be the output of the following Python code?

print(“abcdef”.find(“cd”) == “cd” in “abcdef”)

a) True

b) False

c) Error

d) None of the mentioned

Check Answer

Answer: b
The function find() returns the position of the sunstring in the given string whereas the in keyword returns a value of Boolean type.

 

 

65. What will be the output of the following Python code?

print(“abcdef”.find(“cd”))

a) True

b) 2

c) 3

d) None of the mentioned

Check Answer

Answer: b
The first position in the given string at which the substring can be found is returned.

 

 

66. What will be the output of the following Python code?

print(“ccdcddcd”.find(“c”))

a) 4

b) 0

c) Error

d) True

Check Answer

 

Answer: b
The first position in the given string at which the substring can be found is returned.

 

67. What will be the output of the following Python code?

print(“Hello {0} and {1}”.format(‘foo’, ‘bin’))

a) Hello foo and bin

b) Hello {0} and {1} foo bin

c) Error

d) Hello 0 and 1

Check Answer

 

Answer: a
The numbers 0 and 1 represent the position at which the strings are present.

 

 

68. What will be the output of the following Python code?

print(“Hello {1} and {0}”.format(‘bin’, ‘foo’))

a) Hello foo and bin

b) Hello bin and foo

c) Error

d) None of the mentioned

Check Answer

 

Answer: a
The numbers 0 and 1 represent the position at which the strings are present.

 

 

69. What will be the output of the following Python code?

print(“Hello {} and {}”.format(‘foo’, ‘bin’))

a) Hello foo and bin

b) Hello {} and {}

c) Error

d) Hello and

 

Check Answer

 

Answer: a
It is the same as Hello {0} and {1}.

 

70. What will be the output of the following Python code?

print(“Hello {name1} and {name2}”.format(‘foo’, ‘bin’))

a) Hello foo and bin

b) Hello {name1} and {name2}

c) Error

d) Hello and

Check Answer

 

Answer: c
The arguments passed to the function format aren’t keyword arguments.

Pages  1  2  3  4  5  6  7  8  9  10  11  12  13

 

 

 

 

Leave a Comment