Python is a general-purpose programming language that is open source, flexible, powerful and easy to use. With Big Data and Analytics being the norm nowadays, Python has become the most important language if you want to succeed in the IT industry.
But as with any job, acing the interview is half the work done. To help you breeze through your next Python interview, I’ve compiled 21 of the most commonly asked python interview questions and answers for experienced. Feel free to add your own in the comments below!
- What is the Python programming language? List out key benefits.
Python is an object-oriented programming language with modules, threads, exceptions and automatic memory management.
Some of the benefits of using Python are as follows:
- High-level language.
- Object Oriented
- Built-in data structure
- Open source
- How are compile-time and run-time code checking done in Python?
Python has a unique way of performing compile-time and run-time code checking. A small portion checking is carried out during compile-time checking, but most of the checks such as type, name, etc are postponed until code execution. If the Python code references a user-defined function that does not exist, the code will compile successfully and the code will fail with an exception only when the code execution path references the function which does not exist.
- How is Python executed?
Python files are first compiled to bytecode and are then executed by the host. “OR” Type python.pv at the command line.
- State how arguments are passed by value or by reference.
Python is an object-oriented language so all variables hold references to the objects. The references values are according to the functions and as a result, the value of references cannot be changed. However, the objects can be changed if it is mutable.
- What is a module and package in Python?
The module is the way to structure a program. Each Python program file is a module, which imports other modules like objects and attributes.
The folder of Python program is a package of modules. A package can have modules or subfolders.
- How does exception handling in Python differ from Java? Also, list the optional clauses for a <try-except> block In Python.
Python has its own method of implementing exception handling. <try-except> is the block which can be used by the programmer to see the error details without having to terminate the program. Also, in some cases, this <try-except> statement offers a solution to deal with the error.
- What is the difference between .py and .pyc files?
.py files are Python source files. .pyc files are the compiled bytecode files that is generated by the Python compiler
- Explain the use of try: except raise, and finally.
Python makes use of try, except and finally blocks for error handling. Try block is used to execute the code until an error occurs. We can make use of an except block to receive control which will receive control after all errors, or one can use specific exception handling blocks for various error types. Control is transferred to the appropriate except block. In all cases, the final block is executed. Raise may be used to raise your own exceptions.
- How are instance variables different from class variables?
Instance variables are variables that are created locally within a class to refer to an object of the class. A class variable is one that is created globally in a class and is accessible within all instance of that class.
Class variables are declared with keyword static and Instance variables are declared without static keyword.
Class variables can be accessed anywhere within that class whereas an instance variable can only be accessed within the particular object of the class.
As class variables are common to all objects of a class, changes made to these variables through one object will reflect in another. As each object will have its own copy of instance variables, changes made to these variables through one object will not reflect in another object.
Class variables can be accessed using either class name or object reference. Instance variables can be accessed only through an object reference.
- What is namespace in Python?
In Python, every name introduced has a place where resides and can be found. This space is known as a namespace. It is an address location where a variable name is mapped to the object placed. Whenever the variable is searched out, this address location will be searched, to get the corresponding object.
- Explain inheritance in Python with an example.
Inheritance is a feature by which one class can gain all the attributes and methods of another class. This facilitates code reusability and a program one written can be reused by inheriting the parent class attributes without having to rewrite them every time. The class from which we are inheriting is called super-class and the class that is inherited is called a sub-class / derived class.
Single Inheritance – where a derived class acquires the members of a single superclass.
multi-level inheritance – a derived class d1 is inherited from base class base1, and d2 is inherited from base2.
hierarchical inheritance – from one base class you can inherit any number of child classes
multiple inheritances – a derived class is inherited from more than one base class.
v1 = “from SuperClass – v1”
v2 = “from SuperClass – v2″class SubClass(SuperClass):
passc = SubClass() print(c.v1) print(c.v2)
12.How to find undefined g++ symbols __builtin_new or __pure_virtual?
To load g++ extension modules dynamically, we have to:
Re-link it using g++ (change LINKCC in the python Modules Makefile)
Link the extension module using g++ (e.g., “g++ -shared -o mymodule.so mymodule.o”).
- What is the function to randomize the items of a list in-place?
Python has a built-in module called <random>. It exports a public method <shuffle(<list>)> which can randomize any input sequence.
list = [2, 18, 8, 4]
print “Prior Shuffling – 0”, list
print “After Shuffling – 1”, list
print “After Shuffling – 2”, list
- Is it valid if a function doesn’t have a return statement?
It is valid at the end of a function is defined by the block of code being executed and not by any explicit keyword. The function will return a None object.
- Illustrate the proper use of Python error handling.
…# error handling code
…# code that will be executed regardless of exception handling goes here.
- Explain the difference between local and global namespaces.
Local namespaces are created within a function when that function is called. Global namespaces are created when the program starts.
- When would you use a continue statement in a for loop?
When processing a particular item is complete, to move on to the next, without executing further processing in the block continue statement is used. The continue statement states, the current item is done processing, move on to the next item.
- When would you use a break statement in a for loop?
The break statement states that the function of the loop is over and to move on to the next block of code. For example, when the item being searched is found, there is no need to keep looping. The break statement comes into play here and the loop terminates and the execution moves on to the next section of the code.
- Can you describe what’s wrong with the below code?
testProc([1, 2, 3]) # Explicitly passing in a list
testProc() # Using a default empty list
def testProc(n = ):
# Do something with n
The above code would throw a <NameError>.
The variable n is local to the function <testProc> and can’t be accessed outside.
So, printing it won’t be possible.
- Given the string “Hello World” print out each character except for the spaces, using a for loop.
inyString = “Hello World”
for myCizar in myString:
fmyChar == ‘’ ‘’:
I hope these Python Advanced Questions and Answers are of help. Can you think of any other questions from your interview experiences? Let us know in the comments below!