Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What is the default value of encoding in encode()?(a) ascii(b) qwerty(c) utf-8(d) utf-16This question was posed to me in semester exam.This question is from Strings topic in section Strings of Python

Answer»

Correct OPTION is (c) utf-8

The BEST EXPLANATION: The DEFAULT VALUE of encoding is utf-8.

2.

Suppose x is 345.3546, what is format(x, “10.3f”) (_ indicates space).(a) __345.355(b) ___345.355(c) ____345.355(d) _____345.354The question was posed to me by my college professor while I was bunking the class.This intriguing question comes from Strings topic in chapter Strings of Python

Answer»

Right ANSWER is (b) ___345.355

The EXPLANATION is: EXECUTE in the SHELL to VERIFY.

3.

What function do you use to read a string?(a) input(“Enter a string”)(b) eval(input(“Enter a string”))(c) enter(“Enter a string”)(d) eval(enter(“Enter a string”))I have been asked this question in unit test.Enquiry is from Strings topic in section Strings of Python

Answer» CORRECT option is (a) INPUT(“Enter a string”)

The best I can explain: EXECUTE in SHELL to verify.
4.

Suppose i is 5 and j is 4, i + j is same as ________(a) i.__add(j)(b) i.__add__(j)(c) i.__Add(j)(d) i.__ADD(j)The question was posed to me by my school principal while I was bunking the class.I need to ask this question from Strings topic in portion Strings of Python

Answer» RIGHT ANSWER is (b) i.__add__(J)

To explain I WOULD SAY: Execute in shell to verify.
5.

To check whether string s1 contains another string s2, use ________(a) s1.__contains__(s2)(b) s2 in s1(c) s1.contains(s2)(d) si.in(s2)The question was asked in an interview for job.Question is taken from Strings in section Strings of Python

Answer»

Right choice is (a) s1.__contains__(S2)

BEST EXPLANATION: s2 in s1 works in the same WAY as calling the SPECIAL function __contains__ .

6.

If a class defines the __str__(self) method, for an object obj for the class, you can use which command to invoke the __str__ method.(a) obj.__str__()(b) str(obj)(c) print obj(d) all of the mentionedI had been asked this question in my homework.Question is from Strings topic in section Strings of Python

Answer» RIGHT answer is (d) all of the mentioned

Easiest EXPLANATION - EXECUTE in shell to verify.
7.

To return the length of string s what command do we execute?(a) s.__len__()(b) len(s)(c) size(s)(d) s.size()This question was posed to me in an international level competition.Asked question is from Strings in section Strings of Python

Answer»

Right choice is (a) s.__len__()

Best EXPLANATION: Execute in SHELL to VERIFY.

8.

To retrieve the character at index 3 from string s=”Hello” what command do we execute (multiple answers allowed)?(a) s[](b) s.getitem(3)(c) s.__getitem__(3)(d) s.getItem(3)I got this question in an international level competition.Query is from Strings topic in section Strings of Python

Answer»

The CORRECT option is (C) s.__getitem__(3)

The best explanation: __getitem(..) can be used to get character at index SPECIFIED as parameter.

9.

What is “Hello”.replace(“l”, “e”)?(a) Heeeo(b) Heelo(c) Heleo(d) NoneI have been asked this question in an internship interview.I'm obligated to ask this question of Strings topic in division Strings of Python

Answer» CORRECT OPTION is (a) Heeeo

Explanation: EXECUTE in SHELL to VERIFY.
10.

Say s=”hello” what will be the return value of type(s)?(a) int(b) bool(c) str(d) StringThe question was asked in my homework.I'd like to ask this question from Strings in chapter Strings of Python

Answer»

Right ANSWER is (c) STR

Best explanation: str is USED to represent STRINGS in python.

11.

What will be displayed by print(ord(‘b’) – ord(‘a’))?(a) 0(b) 1(c) -1(d) 2The question was asked in an interview.Question is from Strings in portion Strings of Python

Answer»

Correct OPTION is (b) 1

Easy explanation - ASCII value of b is one more than a. HENCE the output of this code is 98-97, which is EQUAL to 1.

12.

What will be the output of the “hello” +1+2+3?(a) hello123(b) hello(c) Error(d) hello6I have been asked this question during an interview for a job.I want to ask this question from Strings topic in division Strings of Python

Answer»

The CORRECT option is (c) Error

The EXPLANATION is: Cannot CONCATENATE STR and int OBJECTS.

13.

The format function, when applied on a string returns ___________(a) Error(b) int(c) bool(d) strI got this question in an interview for internship.I want to ask this question from Strings in chapter Strings of Python

Answer»

The correct ANSWER is (d) str

Explanation: FORMAT FUNCTION returns a STRING.

14.

Suppose s is “\t\tWorld\n”, what is s.strip()?(a) \t\tWorld\n(b) \t\tWorld\n(c) \t\tWORLD\n(d) WorldThe question was asked during an online exam.My question is based upon Strings in division Strings of Python

Answer» RIGHT CHOICE is (d) World

The EXPLANATION: Execute help(string.strip) to FIND details.
15.

Which of the following statement prints hello\example\test.txt?(a) print(“hello\example\test.txt”)(b) print(“hello\\example\\test.txt”)(c) print(“hello\”example\”test.txt”)(d) print(“hello”\example”\test.txt”)The question was asked by my college professor while I was bunking the class.This is a very interesting question from Strings in portion Strings of Python

Answer»

Right OPTION is (b) print(“hello\\example\\test.txt”)

The BEST EXPLANATION: \is used to indicate that the NEXT \ is not an escape SEQUENCE.

16.

To concatenate two strings to a third what statements are applicable?(a) s3 = s1 . s2(b) s3 = s1.add(s2)(c) s3 = s1.__add__(s2)(d) s3 = s1 * s2This question was addressed to me in my homework.This is a very interesting question from Strings in section Strings of Python

Answer»

The correct OPTION is (C) S3 = s1.__add__(s2)

The best I can explain: __add__ is ANOTHER METHOD that can be used for concatenation.

17.

Given a string example=”hello” what is the output of example.count(‘l’)?(a) 2(b) 1(c) None(d) 0I had been asked this question in semester exam.This interesting question is from Strings in division Strings of Python

Answer» RIGHT answer is (a) 2

Explanation: L occurs twice in HELLO.
18.

The output of executing string.ascii_letters can also be achieved by:(a) string.ascii_lowercase_string.digits(b) string.ascii_lowercase+string.ascii_uppercase(c) string.letters(d) string.lowercase_string.uppercaseThis question was addressed to me by my school teacher while I was bunking the class.The above asked question is from Strings in chapter Strings of Python

Answer» RIGHT ANSWER is (B) string.ascii_lowercase+string.ascii_uppercase

The explanation is: EXECUTE in SHELL and check.