Quiz: String

To View Tricks: Login Required

Number of Questions: 10

Question: 1 -

What will be the output of below Python code?

str1="Information"
print(str1[2:8])

Options:
  1. formatio

  2. format

  3. orma

  4. ormat

  5. Answer:

    format

    Solution:

    Concept of slicing is used in this question. In string slicing,the output is the substring starting from the first given index position i.e 2 to one less than the second given index position i.e.(8-1=7) of the given string str1. Hence, the output will be "format".


Question: 2 -

Which of the following will result in an error?

str1="python"

Options:
  1. str1[1]="x"

  2. None of these

  3. print(str1[0:9])

  4. print(str1[2])

  5. Answer:

    str1[1]="x"

    Solution:

    Strings are immutable. So,new values cannot be assigned at any index position in a string.


Question: 3 -

What will be the output of below Python code?

str1="Aplication"
str2=str1.replace('a','A')
print(str2)

Options:
  1. Application

  2. applicAtion

  3. application

  4. ApplicAtion

  5. Answer:

    ApplicAtion

    Solution:

    replace() function in string is used here to replace all the existing "a" by "A" in the given string.


Question: 4 -

Which of the following is False?

Options:
  1. lower() function in string is used to return a string by converting the whole given string into lowercase.

  2. String is immutable.

  3. None of these.

  4. capitalize() function in string is used to return a string by converting the whole given string into uppercase.

  5. Answer:

    capitalize() function in string is used to return a string by converting the whole given string into uppercase.

    Solution not available.

Question: 5 -

What will be the output of above Python code?

str1="6/4"
print("str1")

Options:
  1. str1

  2. 1

  3. 1.5

  4. 6/4

  5. Answer:

    str1

    Solution:

    Since in print statement, str1 is written inside double quotes so it will simply print str1 directly.