Python Multiple Choice Quizs Question– String

Q 1. The output of executing string.ascii_letters can also be achieved by:
A. string.ascii_lowercase_string.digits
B. string.lowercase_string.uppercase
C. string.letters
D. string.ascii_lowercase+string.ascii_uppercase

Show Answer Answer:-D. string.ascii_lowercase+string.ascii_uppercase
Explanation Execute in shell and check.

Q 2 . What will be the output of the following Python code?

  1. >>> str1 = ‘hello’
  2. >>> str2 = ‘,’
  3. >>> str3 = ‘world’
  4. >>> str1[-1:]

A. olleh
B. hello
C. h
D. o

Show Answer Answer:-D. o
Explanation 1 corresponds to the last index.

Q 3. What arithmetic operators cannot be used with strings?
A. +
B. *
C. –
D. All of the mentioned

Show Answer Answer:-C. –
Explanation + is used to concatenate and * is used to multiply strings.

Q 4. What will be the output of the following Python code?

  1. >>>print (r”\nhello”)

A. a new line and hello
B. \nhello
C. the letter r and then hello
D. error

Show Answer Answer:-B. \nhello
Explanation When prefixed with the letter ‘r’ or ‘R’ a string literal becomes a raw string and the escape sequences such as \n are not converted.

Q 5. What will be the output of the following Python statement?

  1. >>>print(‘new’ ‘line’)

A. Error
B. Output equivalent to print ‘new\nline’
C. new line
D. newline

Show Answer Answer:-D. newline
Explanation String literal separated by whitespace are allowed. They are concatenated.

Q 6. What will be the output of the following Python statement?

  1. >>> print(‘x\97\x98‘)

A. Error
B. 97 98

C. \x97\x98
D. x\97

Show Answer Answer:-D. x\97
Explanation \x is an escape sequence that means the following 2 digits are a hexadecimal number encoding a character.

Q 7. What will be the output of the following Python code?

  1. >>>str1=”helloworld”
  2. >>>str1[::-1]

A. world
B. hello
C. dlrowolleh
D. helloworld

Show Answer Answer:-C. dlrowolleh
Explanation Execute in shell to verify.

Q 8. What will be the output of the following Python code?

print(0xA + 0xB + 0xC)

A. 0xA0xB0xC
B. Error
C. 0x22
D. 33

Show Answer Answer:-D. 33
Explanation 0xA and 0xB and 0xC are hexadecimal integer literals representing the decimal values 10, 11 and 12 respectively. There sum is 33.

Q 9. What will be the output of the following Python statement?

  1. >>>”abcd”[2:]

A. a
B. ab
C. cd
D. dc

Show Answer Answer:-C. cd
Explanation Slice operation is performed on string.

Q 10. What will be the output of the following Python statement?

  1. >>>”a”+”bc”

A. a
B. bc
C. bca
D. abc

Show Answer Answer:-D. abc
Explanation + operator is concatenation operator.

Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page