0% found this document useful (0 votes)
31 views

Mcq-String 1.: What Is The Output of The Following Program?

This document contains 11 multiple choice questions about Python string operations and functions. Key concepts covered include string slicing, concatenation, escaping characters, converting between hexadecimal and decimal, and built-in string methods like split() and ascii_letters. Correct answers and short explanations are provided for each question.

Uploaded by

Aldrin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Mcq-String 1.: What Is The Output of The Following Program?

This document contains 11 multiple choice questions about Python string operations and functions. Key concepts covered include string slicing, concatenation, escaping characters, converting between hexadecimal and decimal, and built-in string methods like split() and ascii_letters. Correct answers and short explanations are provided for each question.

Uploaded by

Aldrin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MCQ-STRING

1.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’]
Ans. (b)
Explanation: split() will use ‘a’ as the delimiter. It’ll create parition 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.

2. What is the output of the following code ?


>>>example = "snow world"
>>>print("%s" % example[4:7])
a) wo
b) world
c) sn
d) rl
Answer: a

3.What is the output when following statement is executed ?


>>>"a"+"bc"
a)a
b)bc
c)bca
d) abc
Answer: d
Explanation: + operator is concatenation operator.

4. What is the output when following statement is executed ?


>>>"abcd"[2:]
a) a
b) ab
c) cd
d) dc
Answer: c
Explanation: Slice operation is performed on string.

5. The output of executing string.ascii_letters can also be achieved by:


a) string.ascii_lowercase_string.digits
b) string.ascii_lowercase+string.ascii_upercase
c) string.letters
d) string.lowercase_string.upercase

Answer: b

6. What is the output when following code is executed ?


>>> str1 = 'hello'
>>> str2 = ','
>>> str3 = 'world'
>>> str1[-1:]
a) olleh
b) hello
c) h
d) o
Answer: d
Explanation: -1 corresponds to the last index.
7. What arithmetic operators cannot be used with strings ?
a) +
b) *
c) –
d) All of the mentioned

Answer: c
Explanation: + is used to concatenate and * is used to multiply strings.

8. What is the output when following code is executed ?


>>>print (r"\nhello")
a) new line and hello
b) \nhello
c) the letter r and then hello
d) error
Answer: b
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.

9. What is the output when following statement is executed ?


>>>print('new' 'line')
a) Error
b) Output equivalent to print ‘new\nline’
c) newline
d) new line
Answer: c
Explanation: String literal separated by whitespace are allowed. They are concatenated

10. What is the output when following code is executed ?


>>>str1="helloworld"
>>>str1[::-1]
a) dlrowolleh
b) hello
c) world
d) helloworld
Answer: a

11. print(0xA + 0xB + 0xC)


a) 0xA0xB0xC
b) Error
c) 0x22
d) 33

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

Minu singh
PGT-CS
KVNO1 AFS HINDAN

You might also like