top of page
  • Facebook
  • Twitter
  • Instagram

Top Python Interview Questions

Mobiprep handbooks are free downloadable placement preparation resources that can help you ace any company placement process. We have curated a list of the 40 most important MCQ questions which are asked in most companies such as Infosys, TCS, Wipro, Accenture, etc. The placement handbooks also have detailed explanations for every question ensuring you revise all the questions with detailed answers and explanations.

Question

29

Explanation

"Strings are immutable". Comment.

The objects of string class are immutable. That is, once the strings are created (or initialized), they cannot be modified. No character in the string can be
edited/deleted/added. Instead, one can create a new string using an existing string by imposing any modification required.
Try to attempt following assignment –
>>> st= “Hello World”
>>> st[3]='t'
TypeError: 'str' object does not support item assignment
Here, we are trying to change the 4th character (index 3 means, 4th character as the first index is 0) to t. The error message clearly states that an assignment of new item (a string) is not possible on string object. So, to achieve our requirement, we can create a new string using slices of existing string as below –
>>> st= “Hello World”
>>> st1= st[:3]+ 't' + st[4:]
>>> print(st1)
Helto World #l is replaced by t in new string st1

Question

30

Explanation

What is a function and how is it defined in python ?

A function is a block of code which is executed only when it is called. To define a Python function, the def keyword is used.

Example:
def func():
print("Hi, Welcome to ROOTWORKZ")
func(); #calling the function

Question

31

Explanation

Python program that display lines that do not contain number in a text file.

import re
fname=input("Enter file name:")
try:
fhand=open(fname)
except:
print("File cannot be opened")
exit(0)
for line in fhand:
line=line.rstrip()

if re.search('^([^0-9]*)$',line):
print(line)

Question

32

Explanation

Write a program to search and print all the lines starting with a specific word (taken as keyboard input) in a file.

fname=input(‘Enter a file name:’)
word=input(‘Enter a word to be searched:’)
fhand=open(fname)
for line in fhand:
if line.startswith(word):
print(line)
fhand.close()

bottom of page