Python Interview Question & Answers
1. Compare Java & Python
|
Criteria
|
Java
|
Python
|
|
Ease of use
|
Good
|
Very Good
|
|
Speed of coding
|
Average
|
Excellent
|
|
Data types
|
Static typed
|
Dynamically typed
|
|
Data Science & machine
learning applications
|
Average
|
Very Good
|
2. What is Python?
Python is a high-level, interpreted,
interactive and object-oriented scripting language. Python is designed to be
highly readable. It uses English keywords frequently where as other languages
use punctuation, and it has fewer syntactical constructions than other
languages
.
3. What is the purpose of PYTHONPATH
environment variable?
PYTHONPATH − It has a role similar
to PATH. This variable tells the Python interpreter where to locate the module
files imported into a program. It should include the Python source library
directory and the directories containing Python source code. PYTHONPATH is
sometimes preset by the Python installer.
4. What is the purpose of
Pythonstartup, Pythoncaseok, Pythonhome, Pythonstartup environment variables?
Pythonstartup − It contains the path
of an initialization file containing Python source code. It is executed everytime you start the interpreter. It is named as .pythonrc.py in Unix and it
contains commands that load utilities or modify Pythonpath
Pythoncaseok − It is used in Windows
to instruct Python to find the first case-insensitive match in an import
statement. Set this variable to any value to activate it.
Pythonhome − It is an alternative
module search path. It is usually embedded in the Pythonstartup or Pythonpath
directories to make switching module libraries easy.
5. What are the supported data types
in Python?
Python has five standard data types
−
- Numbers
- String
- List
- Tuple
- Dictionary
6. What is the difference between
list and tuples?
|
LIST
|
TUPLES
|
|
Lists are mutable i.e they can be
edited.
|
Tuples are immutable (tuples are
lists which can’t be edited).
|
|
Lists are slower than tuples.
|
Tuples are faster than list.
|
|
Syntax: list_1 = [10, ‘Chelsea’,
20]
|
Syntax: tup_1 = (10, ‘Chelsea’ ,
20)
|
7. How is memory managed in Python?
- 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.
8. Explain Inheritance in Python
with an example.
Inheritance allows One class to gain
all the members(say attributes and methods) of another class. Inheritance
provides code reusability, 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.
They are different types of
inheritance supported by Python:
·
Single Inheritance – where a derived
class acquires the members of a single super class.
·
Multi-level inheritance – a derived
class d1 in 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 inheritance – a derived
class is inherited from more than one base class.
9. Whenever Python exits, why isn’t
all the memory de-allocated?
·
Whenever Python exits, especially
those Python modules which are having circular references to other objects or
the objects that are referenced from the global namespaces are not always
de-allocated or freed.
·
It is impossible to de-allocate
those portions of memory that are reserved by the C library.
·
On exit, because of having its own
efficient clean up mechanism, Python would try to de-allocate/destroy every
other object.
10. What is dictionary in Python?
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, Capital & PM. Their corresponding values are India, Delhi
and Modi respectively.
dict={‘Country’:’India’,’Capital’:’Delhi’,’PM’:’Modi’}
print dict[Country]
Thanks
& Regards
Sky
InfoTech Pvt. Ltd.
A -50,
Sector-64, Noida (UP)
Ph. 0120 -
4242224
Noida: 9717292598 / 9717292599
Delhi: 9717292601 / 9717292602
Gurgaon: 9810866624 / 9810866642

Comments
Post a Comment