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
25
Explanation
Discuss four string methods.
1. s.upper(): This function returns a copy of a string s to uppercase. As strings are immutable, the original string s will remain same.
>>> st= “hello”
>>> st1=st.upper()
>>> print(st1)
'HELLO'
>>> print( st) #no change in original string
'hello'
2. s.lower(): This method is used to convert a string s to lowercase. It returns a copy of original string after conversion, and original string is intact.
>>> st='HELLO'
>>> st1=st.lower()
>>> print(st1)
hello
>>> print(st) #no change in original string
HELLO
3. s.find(s1) : The find() function is used to search for a substring s1 in the string s. If found, the index position of first occurrence of s1 in s, is returned. If s1 is not found in s, then -1 is returned.
>>> st='hello'
>>> i=st.find('l')
>>> print(i) #output is 2
>>> i=st.find('lo')
>>> print(i) #output is 3
>>> print(st.find(‘x’)) #output is -1
4. s.strip(): Returns a copy of string s by removing leading and trailing white spaces.
>>> st=" hello world "
>>> st1 = st.strip()
>>> print(st1)
hello world
Question
26
Explanation
Discuss some list methods.
1. append(): This method is used to add a new element at the end of a list.
>>> ls=[1,2,3]
>>> ls.append(‘hi’)
>>> ls.append(10)
>>> print(ls)
[1, 2, 3, ‘hi’, 10]
2. extend(): This method takes a list as an argument and all the elements in this list
are added at the end of invoking list.
>>> ls1=[1,2,3]
>>> ls2=[5,6]
>>> ls2.extend(ls1)
>>> print(ls2)
[5, 6, 1, 2, 3]
3. sort(): This method is used to sort the contents of the list. By default, the function will sort the items in ascending order.
>>> ls=[3,10,5, 16,-2]
>>> ls.sort()
>>> print(ls)
[-2, 3, 5, 10, 16]
4. reverse(): This method can be used to reverse the given list.
>>> ls=[4,3,1,6]
>>> ls.reverse()
>>> print(ls)
[6, 1, 3, 4]
Question
27
Explanation
Why indendation is necessary in python?
Indentation is necessary for Python. It specifies a block of code. All code within loops, classes, functions, etc is specified within an indented block.
It is usually done using four space characters. If your code is not indented necessarily, it will not execute accurately and will throw errors as well.
Question
28
Explanation
What is pass statement in python?
The pass keyword represents a null operation in Python. It is generally used for the purpose of filling up empty blocks of code which may execute during runtime but has yet to be written.
Without the pass statement in the following code, we may run into some errors during code execution.
The difference between a comment and a pass statement in Python is that while the interpreter ignores a comment entirely, pass is not ignored.
EXAMPLE
def myEmptyFunc():
# do nothing
pass
myEmptyFunc() # nothing happens
## Without the pass keyword
# File "<stdin>", line 3
# IndentationError: expected an indented block



.png)