Top 10 Most Asked Python Interview Questions With Answers Part 14
By Muhammad Umair
Q.1 What do you mean by overriding methods?
Ans. Suppose class B inherits from class A. Both have the method sayhello()- to each, their own version. B overrides the sayhello() of class A. So, when we create an object of class B, it calls the version that class B has.
class A:
def sayhello(self):
print("Hello, I'm A")
class B(A):
def sayhello(self):
print("Hello, I'm B")
a=A()
b=B()
a.sayhello()
Hello, I’m A
b.sayhello()
Hello, I’m B
Q.2 What is JSON? Describe in brief how you’d convert JSON data into Python data?
Ans. JSON stands for JavaScript Object Notation. It is a highly popular data format, and it stores data in NoSQL databases. JSON is generally built on the following two structures:
A collection of <name,value> pairsAn ordered list of values.
Python supports JSON parsers. In fact, JSON-based data is internally represented as a dictionary in Python. To convert JSON data into Python data, we use the load() function from the JSON module.
Q.3 How do you execute a Python Script?
From the command line, type python .py or pythonx.y.py where the x.y is the version of the Python interpreter desired. Learn how to use Python, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Python Training Demo!
Q.4 Explain the use of try: except raise, and finally:
Try, except and finally blocks are used in Python error handling. Code is executed in the try block until an error occurs. One can use a generic except block, 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.
Q.5 Illustrate the proper use of Python error handling.
Code Example:
try:
….#This can be any codeexcept:
…# error handling code goes herefinally:
…# code that will be executed regardless of exception handling goes here.
Q.6 What is a namespace in Python?
In Python, every name introduced has a place where it lives and can be hooked for. This is known as a namespace. It is like a box where a variable name is mapped to the object placed. Whenever the variable is searched out, this box will be searched, to get the corresponding object.
A namespace is a collection of names. It maps names to corresponding objects. When different namespaces contain objects with the same names, this avoids any name collisions. Internally, a namespace is implemented as a Python dictionary.
On starting the interpreter, it creates a namespace for as long as we don’t exit. We have local namespaces, global namespaces, and a built-in namespace.
Q.7 Explain the differences 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.
Q.8 Name the four main types of namespaces in Python.
Global, Local, Module, and Class namespaces.
Q.9 When would you use triple quotes as a delimiter?
Triple quotes ‘’”” or ‘“ are string delimiters that can span multiple lines in Python. Triple quotes are usually used when spanning multiple lines, or enclosing a string that has a mix of single and double quotes contained therein.
Q.10 How to use GUI that comes with Python to test your code?
That is just an editor and a graphical version of the interactive shell. You write or load code and run it, or type it into the shell. There is no automated testing.