Top 10 Most Asked Python Interview Questions With Answers Part 08

By Muhammad Umair

Muhammad Umair
2 min readSep 19, 2022
Top 10 Most Asked Python Interview Questions With Answers Part 05 By Muhammad Umair

Question no 1: What is the output of the following?

x = ['ab', 'cd']
print(len(map(list, x)))

A TypeError occurs as the map has no len().

Question no 2: What is the output of the following?

x = ['ab', 'cd']
print(len(list(map(list, x))))

The length of each string is 2.

Question no 3: Which of the following is not the correct syntax for creating a set?

a) set([[1,2],[3,4]])b) set([1,2,2,3,4])c) set((1,2,3,4))d) {1,2,3,4}

Question no 4: Explain a few methods to implement Functionally Oriented Programming in Python.

Sometimes, when we want to iterate over a list, a few methods come in handy.

filter(): Filter lets us filter in some values based on conditional logic.

list(filter(lambda x:x>5,range(8)))

Ans: [6, 7]

map(): Map applies a function to every element in an iterable.

list(map(lambda x:x**2,range(8)))

Ans: [0, 1, 4, 9, 16, 25, 36, 49]

reduce(): Reduce repeatedly reduces a sequence pair-wise until we reach a single value

from functools import reducereduce(lambda x,y:x-y,[1,2,3,4,5])

Ans: -13

Question no 5: Write a Python function that checks whether a passed string is palindrome Or not.

Note: A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam, saas, nun.

def isPalindrome(string):
left_pos = 0
right_pos = len(string) – 1
while right_pos >= left_pos:
if not string[left_pos] == string[right_pos]:
return False
left_pos += 1
right_pos -= 1
return Trueprint(isPalindrome('aza'))

Question no 6: Write a Python program to calculate the sum of a list of numbers.

def list_sum(num_List):    
if len(num_List) == 1:
return num_List[0]
else:
return num_List[0] + list_sum(num_List[1:])
print(list_sum([2, 4, 5, 6, 7]))

Sample Output: 24

Question no 7: How to retrieve data from a table in MySQL database through Python code? Explain.

#import MySQLdb module as :
import MySQLdb#establish a connection to the
database.db = MySQLdb.connect("host"="local host", "database-user"="user-name", "password"="password","database-name"="database")
#initialize the cursor variable upon the established connection:
c1 = db.cursor()#retrieve the information by defining a required query string.s = "Select * from dept"#fetch the data using fetch() methods and print it.data = c1.fetch(s)#close the database connection.db.close()

Question no 8: Write a Python program to read a random line from a file.

import randomdef random_line(fname):    
lines = open(fname).read().splitlines()
return random.choice(lines)
print(random_line('test.txt'))

Question no 9: Write a Python program to count the number of lines in a text file.

def file_lengthy(fname):    
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
print("Number of lines in the file: ",file_lengthy("test.txt"))

Question no 10: What are the key features of Python?

If it makes for an introductory language to programming, Python must mean something. These are its qualities:

  • Interpreted.
  • Dynamically-typed.
  • Object-oriented
  • Concise and simple
  • Free
  • Has a large community

--

--

Muhammad Umair
Muhammad Umair

Written by Muhammad Umair

MERN Stack Developer | Software Engineer| Frontend & Backend Developer | Javascript, React JS, Express JS, Node JS, MongoDB, SQL, and Python