Python interview questions for freshers

Python interview questions for freshers




Now a days Python is a very good platform for MNC company.So here in interview questions answers provides you most expected interview questions in python for freshers, frequently asked questions in python for experienced with examples.Apart from that interview questions and answers provides you here like advanced python interview questions and solutions, Python developer interview questions and answers.

What are the benefits of using Python?

Python is widely used Programming language which is having objects,modules, exceptions, threads are the key features.The benefits of pythons are
1) it is simple and easy
2) portable
3) extensible
4) automatic memory management
5) build-in data structure
6) it is an open source.

What are the basic features of Python.

  1. Dynamically-typed
  2. Object oriented
  3. Interpreted
  4. Concise and simple
  5. Free and large library.

What are negative indices in python?

For accessing elements from a list or tuple of python from end of the list called as negative index.

Example:

If arr= ['S','O','N']

Output ::

arr[-1] then output will N

arr[-2] then output will O

Is Python case-sensitive?

Yes Python is case-sensitive language, See the below example.

>>>var=2
>>>Var
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
Var
NameError: name 'Var' is not defined
>>>

How memory is managed in Python?

  • All Python objects and data structures are located in a private heap. Memory is managed by Python private heap space.
  •  Private heap take care by interpreter, Programmer don’t have any role to manage Private Heap.
  • The allocation of Python heap space for Python objects is done by Python memory manager.
  • Python  recycle all the unused memory and frees the memory and makes it available to the heap space it self. Its a in-built features in Python.

What is a closure in Python?

A closure is said to occur when a nested function references a value in its enclosing scope. The whole point here is that it remembers the value.

>>> def X(x):
def Y():
print(x)
return Y
>>> X(7)()

How is Python different from Java or C++?

Python uses white space indentation to delimit code, so you don’t need to use curly braces for that. Also, semicolons are optional. Both following syntax are correct:
x = 10
print(x)
x = 10;
print(x)
While Java and C++ are statically-typed, Python is dynamically-typed. You also don’t need to declare the type of a variable; you assign it:
life=42
Java is faster by a few seconds.

What are the data type provides by python?

Mutable built-in types

  • List
  • Sets
  • Dictionaries

Immutable built-in types

  • Strings
  • Tuples
  • Numbers

How Python is interpreted?

Python is compiled your source code (.py) file extension to bytecode and saved in (.pyc) file extension.

Now (.pyc) file will interpreted by machine level language and execute the code one by one. So Python can’t directly interpreted.

Python code is compiled to bytecode which is then interpreted.

How to pass optional or keyword arguments in Python?

Optional Arguments

  • These are also called Positional Argument.
  • They have no keywords before argument.
  • The position or order is very important.
func(1,2,3, "cool")

Keyword Arguments

  • They have keywords in the front for argument.
  • They can be placed in any order or position.
func(cool="sam", zoo=5, some="hey")

Explain indexing and slicing in Python?

1. Indexing and Slicing are are important features of sequence.
2. Indexing helps get a particular item from a list or tuple, where as slicing get the some part of the sequence.
3. Slicing operation is done by sequence name followed by square bracket with start_no and stop_no like SN[start_no.:stop_no]. where SN is the sequence name.
4.Start number means from which position the sequence will be slice and stop_no means upto where will be slice.
5. If Missing of strat_no will consider from first position of sequence and missing of stop_no will by default take end of sequence.
Example-

>>> SN=['PHP','JAVA','PYTHON','ORACLE']
>>> print "Slicing operation :",SN[2:]
Slicing operation :['PYTHON','ORACLE']
>>>print "Indexing operation :",SN[0]
Indexing operation :PHP

What are differences between str() and repr() in python?

str() gives output as human readable where as repr() gives output as compiler readable.

Example:-

import datetime
today = datetime.datetime.now()

print str(today)

print repr(today)

output:-
2018-04-27 09:46:22.218108
datetime.datetime(2018, 4, 27, 9, 46, 22, 218108)

Explain isinstance().

Python provides a function to check the passed argument is instance or subclass, is know as isinstance().
It usually check the first argument is instance or subclass.It’s return type is true or false.

isinstance(object, classinfo)

How to generate random numbers?

Like other language python also have the built-in function to generate random number between two number range.

Example-
randint(start, end)

Disadvantageous of python.

  • Python isn’t the good for memory accelerated tasks.
  • high-graphic 3d game  and mobile development not supported by python.
  • It is bit slower than Java and C++.
  • Python is not good for multi-processor.
  • More Memory Consumption.that lead to more CPU workload.
  • It has limitations database access.

What is the Difference between range() and xrange()?

In python-2 range() returns list ,where xrange() returns objects. Both are one kind of iteration functions which can be iterate by mentioned time as parameter.

But in Python-3 xrange() function has deprecated only range() function works like xrange();

What is PEP8?

PEP8 is a one kind of document which provides how to code and style the Python program.

What is virtualenv?

  • It is called virtual environment.
  • It is very useful for python for development.
  • It makes isolated every individual project.
  • It makes developer not to be worried for affecting other project when they are working in specific project in same environment.