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.

251.

What is MRO in python?

Answer»

Method resolution order or MRO refers to when one class inherits from MULTIPLE CLASSES. The class that GETS inherited is the parent class and the class that inherits is the child class. It also refers to the order where the BASE class is searched while executing the method.

252.

How do you call a superclass method in Python?

Answer»

Here’s how to call a superclass METHOD in PYTHON:

Example

class PARENT:
    def show(self):
        PRINT("Inside Parent class")
 
class Child(Parent):
      
    def display(self):
        print("Inside Child class")

obj = Child()
obj.display()
obj.show()

Output

Inside Child class
Inside Parent class

253.

What happens when you import a module Python?

Answer»

When a module is imported in Python, the following happens behind the scenes:
It starts with searching for mod.py in a list of directories which have been gathered from the following sources:

  • The original directory from where the input script was actually being run or in the current list if the interpreter is being run interactively side by side.
  • List of the directories within the PYTHONPATH environment variable, if it is actually set.
  • A directory list from the installed directories would have been configured at the time of installation of Python itself.

Note: After learning the basics of Python, if you are looking for what more to learn, you can start with meta-programming, buffering protocols, iterator protocols, and much more. We have created a list of Python INTERVIEW Questions for Experienced professionals to help them USE this language to solve complex problems.

27. How do you use range in Python?

The range() is an in-built function in Python, which is used to repeat an action for a specific number of times.

Let US give you an example to demonstrate how the range() function works:

Example

SUM = 0
for i in range(1, 11):
    sum = sum + i
print("Sum of first 10 number :", sum)

Output:

Sum of first 10 number: 55

254.

How do you create a null object in Python?

Answer»

In Python, to display a NULL OBJECT, the NONE statement is used. Here's the SYNTAX to check for if the object is null:

255.

Which of the keyword is used to display a customized error message to the user in Python?

Answer»

You should use a try-EXCEPT keyword to capture the error and use the raise keyword to display the error message of your CHOICE. Here's an example demonstrating the same:

try:
    a = INT(input())
except:
    raise Exception('An error is being RAISED in the system')

256.

How to check the prime number in Python?

Answer»

Here's a program to check whether a number is prime or not.

Note: PYTHON is an interpreted bytecode-complied language. Our list of Python CODING Interview QUESTIONS will clear the basic as well as complex concepts of this high-level programming language.

Example

num = 11
if num > 1:     
   for i in range(2, num//2):

   if (num % i) == 0:
           PRINT(num, "is not a prime number")
           break
   else:
       print(num, "is a prime number")
else:
   print(num, "is not a prime number")
OUTPUT
11 is a prime number

257.

Why multithreading is not possible in python?

Answer»

One of the many confusing QUESTIONS in Python, yes, Python does support THREADING, but, DUE to the PRESENCE of GIL multi-threading is not supported. The GIL basically does not support the running of MULTIPLE CPU cores parallelly, hence, multithreading is not supported in Python.

258.

What is pass in Python? What are the differences between pass and continue?

Answer»

PASS means where there is a no-operation PYTHON statement. It is just a placeholder in a compound statement where nothing NEEDS can be WRITTEN. The continue makes the loop to resume from the next iteration.

259.

What is a negative index in Python?

Answer»

In Python, the negative index is used to index by starting from the last element in a list, tuple, or any other container class which supports INDEXING. Here, (-1) POINTS to the previous index, -2 to the second last index and SIMILARLY.

260.

What is a slice object in Python?

Answer»

The Slicing() object in Python allows users to access PARTS and sequences of data types such as strings, tuples, and lists. Slicing can also be used to modify or even DELETE items that have mutable sequences such as lists. Besides that, slices can also be integrated with third-party apps like NumPy ARRAYS, data frames, and Panda series.

Syntax: SLICE(start, stop, step)

261.

What is the use of __ init __ in Python?

Answer»

The "init" is an example of a reserved METHOD in python classes. It is actually KNOWN as a constructor in the object-oriented CONCEPTS and techniques. It is CALLED when an object is CREATED within a class, and then it allows the same class to initialize the attributes within.

262.

What is the use of Xrange in Python?

Answer»

In Python, the use of the xrange() FUNCTION is to generate a SEQUENCE of numbers that are similar to the RANGE() function. But, the xrange() function is used only in Python 2. xx WHEREAS the range() is used in Python 3.

263.

How to create an empty class in Python?

Answer»

In Python, an empty class can be created by using the “pass” command. This can be done only after the DEFINING of the class object because at least one line of CODE is mandatory for creating a class. Here’s an example of how to create an empty class:

Example

class CUSTOMER:
    pass

customer1 = customer()

customer1.first_name = 'Jason'
customer1.last_name = 'Doe'

264.

What is lambda? Why do lambda forms not have statements?

Answer»

Lambda is an ANONYMOUS expression function that is often used as an inline function. Its form does not have a STATEMENT as it is only used to MAKE NEW functional objects and then return them at the RUNTIME.

265.

How can you access a session in Flask?

Answer»

A session allows the programmer to REMEMBER information from one request to another. In a flask, a session uses a SIGNED cookie so that the user can look at the contents and modify them. The programmer will be able to modify the session only if it has the SECRET KEY Flask.secret_key.

266.

How we can copy an object in Python?

Answer»

In PYTHON, we can USE TRY COPY.copy () or copy.deepcopy() for copy an OBJECT.

267.

How to print the first 5 elements of list python?

Answer»

To GENERATE the first 5 elements from a list in Python, use the isclice function as follows:Example

From itertools import islice
L = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
iterator = islice(l, 5)
for item in iterator:
    print item

Output

1
2
3
4
5

268.

How is Python interpreted?

Answer»

PYTHON is an INTERPRETED language. It runs DIRECTLY from the source CODE and converts the source code into an INTERMEDIATE language. This intermediate language is translated into machine language and has to be executed.

269.

What is the difference between list and tuples?

Answer»
TuplesLists
Items in a TUPLE are SURROUNDED by a parenthesis ()Items are surrounded in square brackets [ ]
They are IMMUTABLE in natureLists are by nature immutable
There are 33 available methods in it.There are 46 methods here.
Keys can be created using Tuples.No, keys can’t be created using these
270.

Why is flask used in Python?

Answer»

A Flask is a micro web framework for Python based on the "Werkzeug, Jinja 2 and good intentions". Werkzeug and jingja are its dependencies. Because a Flask is PART of the micro-framework, it has little or no dependencies on the external libraries. A Flask ALSO MAKES the framework light while TAKING little dependency and gives FEWER security bugs.

Note: These python programming interview questions have been designed specially to get you familiar with the nature of questions.

271.

How is it possible to share global variables across various modules?

Answer»

In order to SHARE GLOBAL variables ACROSS different MODULES within a single program, you need to create a special module. After that, just import the config module in all of the modules of your application. This will make the module available as a global variable across all the modules.

272.

What is PEP 8?

Answer»

PEP in Python stands for Python Enhancement Proposal. The PEP 8 is basically Python’s STYLE guide. It helps in writing CODE to specific rules making it helpful for large codebases having MULTIPLE writers by BRINGING a uniform and predictive writing style.

273.

What is monkey patching in Python with an example?

Answer»

In Python, the term monkey PATCHING REFERS to the dynamic/run-time changes taking place within a class or module. Here's an example:

Note: Being one of the most sought after languages, Python is chosen by small and large organizations equally to help them TACKLE issues. Our list of Python Coding Interview QUESTIONS shall help you crack an interview in organizations using Python while making you a better Python Developer.

Example

IMPORT monk
def monkey_f(self):
     print "monkey_f() is being called"  
monk.A.func = monkey_f
obj = monk.A()
obj.func()

Output

monkey_f() is being called

274.

What is self variable in Python?

Answer»

In Python, a self variable is used for binding the instance WITHIN the class to the instance inside the METHOD. In this, to access the instance variables and METHODS, we have to explicitly DECLARE it as the first method argument.

Example

class Dog:
    def __init__(self, breed):
        self.breed = breed
    def bark(self):
        print(F'{self.breed} is continuously barking.')
d = Dog('German Shepherd')
d.bark()

Output
German Shepherd is continuously barking.

275.

What are the main features of Python?

Answer»

Here are some important features of Python:

  • Being easy to learn, it is CONSIDERED as the best LANGUAGE for beginner developers.
  • It is an interpreted language.
  • It is cross-platform in nature.
  • Free and Open source
  • It is based on an Object-Oriented Programming Language (OOPS)
  • It has EXTENSIVE in-built libraries
276.

Why are dictionaries useful in Python?

Answer»

In Python, dictionaries are essential as they are INCREDIBLY flexible, and they allow any data which is given to be STORED as a value. It COULD be anything such as primitive types LIKE strings and decimals like floats to even more complex types like objects.