Monday, October 23, 2017

Python Interview Questions and answers

In this post we will learn frequently asked Python Interview Questions and answers for freshers and also for experienced developers.In previous post we had discussed about why Python so powerful for Data Science, one of the frequently raised question that one so go through that post as well.

1) What is Python?

Ans: Python is a high level ,Object oriented,most successful interpreted language. It is a portable language,since it can run on different platforms such as Windows,Linux,Unix,Mac...It is a interpreted language that means when write a Python script it doesn't need to get compiled before execution.

2) What are the Benefits of Python?

Ans:

  1. Python is a Object Oriented Programming language as you can define classes along with the composition and Inheritance. It doesn't use any access specifier like Public or private
  2. It provides very high level dynamic data types and supports dynamic type checking
  3. It can be use as Scripting language or can be compiled to byte-code for building large applications.
  4. We can use Python for developing Web applications,automation,Scientific Modeling,Big Data applications etc..
  5. It can be easily integrated with C,C++,JAVA,COBRA,COM .
  6. It is extensible
3) What are the Data types in Python?

Ans: Python has 5 Standard Data types:

  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictiornary
4) Is Multithreading supports in Python?

Ans: YES,Python has multi-threading package. It has a Construct called Global Interpreter Lock(GIL). This GIL makes you feeling only one thread is 'executing' at any one time.This happens very quickly so to the human eye it may seem like your threads are executing in parallel, but they are really just taking turns using the same CPU core.This means that if you want to make your code run faster then using the threading package often isn’t a good idea.

5)What is the output of print str if str = ‘Hello World!’?


Ans: Hello World!.

6) How to overload methods in Python?

Ans: Python's constructor: 
_init__ () is a first method of a class. Whenever we try to instantiate a object __init__() is automatically invoked by python to initialize members of an object.

7)How To Find Bugs Or Perform Static Analysis In A Python Application?

Ans: You can use PyChecker, which is a static analyzer. It identifies the bugs in Python project and also reveals the style and complexity related bugs.Another tool is Pylint, which checks whether the Python module satisfies the coding standard.

8) How to achieve Inheritance in Python?

Ans: Inheritance allows One class to gain all the members(say attributes and methods) of another class. Inheritance provides code re-usability, makes it easier to create and maintain an application. The class from which we are inheriting is called super-class and the class that is inherited is called a derived / child class.

9) What is the usage of help() and dir() function in Python?

Ans: Help() and dir() both functions are accessible from the Python interpreter and used for viewing a consolidated dump of built-in functions. 

Help() function: The help() function is used to display the documentation string and also facilitates you to see the help related to modules, keywords, attributes, etc.
Dir() function: The dir() function is used to display the defined symbols.

10)How is memory managed in Python?

Ans: Python memory is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have an access to this private heap and interpreter takes care of this Python private heap. 
The allocation of Python heap space for Python objects is done by Python memory manager. The core API gives access to some tools for the programmer to code.
Python also have an inbuilt garbage collector, which recycle all the unused memory and frees the memory and makes it available to the heap space.

11)What is dictionary in Python?

Ans: The built-in datatypes in Python is called dictionary. It defines one-to-one relationship between keys and values. Dictionaries contain pair of keys and their corresponding values. Dictionaries are indexed by keys.

Let’s take an example:

The following example contains some keys. Country, State & Captain. Their corresponding values are India, Andhra and Kohili respectively.


dict={'Country':'India','State':'Andhra','Captain':'kohli'}
print dict[Country]
India
print dict[State]
Andhra
print dict[Captian]
Kohli

12)What is pickling and unpickling in Python?

Ans: Pickling is a process in which a pickle module accepts any Python object, converts it into a string representation and dumps it into a file by using dump() function.
Unpickling is a process of retrieving original Python object from the stored string representation for use.

13) How Python does Compile-time and Run-time code checking?

Ans: In Python some amount of coding is done at compile time, but most of the checking such as type, name, etc. are postponed until code execution. Consequently, if the Python code references a user -defined function that does not exist, the code will compile successfully. The Python code will fail only with an exception when the code execution path does not exist.

14)Which command is used to exit help window or help command prompt?

Ans: "quit" command is used to exit help window or help command prompt.

15)What Is The Key Difference Between A List And The Tuple?
Ans: 
The major difference between a list and the tuple is that the list is mutable while tuple is not. A tuple is allowed to be hashed, for example, using it as a key for dictionaries.

16)Is There A Switch Or Case Statement In Python? If Not Then What Is The Reason For The Same?

Ans:
No, Python does not have a Switch statement, but you can write a Switch function and then use it.

 17)What Is A Built-In Function That Python Uses To Iterate Over A Number Sequence?

Ans: 
range() generates a list of numbers, which is used to iterate over for loops.
Example:
for i in range(5):
    print(i)

The range() function accompanies two sets of parameters.

range(stop)
stop: It is the no. of integers to generate and starts from zero. eg. range(4) == [0, 1, 2,3].
range([start], stop[, step])
start: It is the starting no. of the sequence.
stop: It specifies the upper limit of the sequence.
step: It is the incrementing factor for generating the sequence.

Points to note:
Only integer arguments are allowed.
Parameters can be positive or negative.

18)What are the Popular Frameworks of Python.

Ans: Major Frameworks: Django and Pyramid

Minor Frameworks: Bottle and Flask

19) How to take input from the User in Python?

Python provides a built-in method to accept input from the user.
It is as follows: input(“Enter the Input”)
However, in order to store the input in a variable, you must write a variable name before the input() method.
It can done as follows: var1=input(“Enter the input”)

20)What are the various Exceptions identified by Python?

Ans: The various exceptions identified by Python Environment are as follows:
1. IOError
2. IndexError
3. KeyError
4. NameError
5. SyntaxError
6. ValueError
7. TypeError

21)What is a Frame in Python GUI?

Ans: A Frame in Python can be related as a storage holder for other Graphical User Interface or GUI elements such as Label, Text Entry, Text Box, Check Button, RadioButton, etc.

22)What is the difference between input() method and raw_input() method?

Ans: raw_input() method returns string values whereas input() method returns integer vaues.
Input() method was used in Python 2.x versions whereas Python 3.x and later versions use raw_input() method. However, input()method has been replaced by raw_input() method in Python 3.x.

24)What is range() method in Python?

Ans: Range() method is Python is used as a Looping construct. It takes in 2 mandatory parameters and 1 optional parameter.

Example: range(1,20,2)
This method prints numbers after every alternate iterations between 1 and 10. It prints 1 3 5 7 9,11,13,15,17,19

25)Which method is used to find out the location of the pointer in a file?
Ans: The tell() method is used to return the current location or position of the read/write pointer within the file. This method doesn’t require any parameter to be passed in it.

Syntax:
FileVariableName.tell()

26)Why is Finally Block used in Python Exception Handling?

Ans: A Finally Block is generally used in association with try and catch blocks in Python. A Finally Block executes itself no matter if an error occurs at run time or not. It is the default execution block in Python Exception Handling technique.

27)How do you print the sum of digits from 1 to 100 in Python?

Ans:   print(sum(range(1,101))
This command would print in the sum of digits from 1 to 100.

28) How do you copy an object in Python?

Ans. Using the functions copy.copy() and copy.deepcopy()

29)How Do I Generate Random Numbers In Python?

Ans: The standard module random implements a random number generator.
Usage is simple:
import random
random.random()
This returns a random floating point number in the range [0, 1).

30)How Do I Interface To C++ Objects From Python?

Ans: Depending on your requirements, there are many approaches. To do this manually, begin by reading the "Extending and Embedding" document. Realize that for the Python run-time system, there isn't a whole lot of difference between C and C++ -- so the strategy of building a new Python type around a C structure (pointer) type will also work for C++ objects.






No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...