Python Interview Questions For Freshers - Docx 1
Python Interview Questions For Freshers - Docx 1
If you want an ordered container of constant elements use tuple as tuples are immutable objects
and list are mutable in nature.
Python allows to open files in one of the three modes. They are:
• read-only mode,
• write-only mode,
• read-write mode,
• Append mode.
A text file can be opened in any one of the above said modes by specifying the option “t” along
with“r”, “w”, “rw”, and “a”, so that the preceding modes become “rt”, “wt”, “rwt”, and “at”.
A binary file can be opened in any one of the above said modes by specifying the option “b”
along with “r”, “w”, “rw”, and “a” so that the preceding modes become “rb”, “wb”, “rwb”, “ab”.
Dictionaries – are best suited when the data is labelled, i.e., the data is a record with field names.
Lists – are better option to store collections of un-labelled items say all the files and sub
directories in a folder.
Generally Search operation on dictionary object is faster than searching a list object.
4. Differentiate between append() and extend() methods ?
Both append() and extend() methods are the methods of list. These methods are used to add the
elements at the end of the list.
append(element) – adds the given element at the end of the list which has called this method.
extend(another-list) – adds the elements of another-list at the end of the list which is called the
extend method.
No it is not, because the objects that are referenced from global namespaces of Python modules
are not always de-allocated when Python exits.
repr() compute the “official” string representation of an object i.e. a representation that has all
information about the object and str() is used to compute the “informal” string representation of
an object (a representation that is useful for printing the object)
A function is called Higher Order Function if it contains other functions as a parameter or returns
a function as an output i.e., the functions that operate with another function are known as Higher
order Functions
Use = operator to create a copy of an object. It creates a new variable that shares the reference of
the original object.
Array in Python can be created by importing array module. array(data_type, value_list) is used to
create an array with data type and value list specified in its arguments.
Exceptions can be handled using a try statement. The critical operation which can raise an
exception is placed inside the try clause. The code that handles the exceptions is written in the
except clause. We can then choose what operations to perform once we have caught the
exception.
11. What is static method, class method and instance method?
Instance methods need a class instance and can access the instance through self. Class methods
don't need a class instance. They can't access the instance ( self ) but they have access to the class
itself via cls . Static methods don't have access to cls or self
Iterator in python is an object that is used to iterate over iterable objects like lists, tuples, dicts,
and sets. The iterator object is initialized using the iter() method. It uses the next() method for
iteration. Generators in Python are used to create iterators and return a traversal object. It helps in
traversing all the items one at a time with the help of the keyword yield.
The exec() function executes the specified Python code. The exec() function accepts large blocks
of code, unlike the eval() function which only accepts a single expression.
Python descriptors are a way to create managed attributes. Among their many advantages,
managed attributes are used to protect an attribute from changes or to automatically update the
values of a dependant attribute. Descriptors increase an understanding of Python, and improve
coding skills.
Method Resolution Order (MRO) denotes the way a programming language resolves a method or
attribute. Python supports classes inheriting from other classes. The class being inherited is
called the Parent or Superclass, while the class that inherits is called the Child or Subclass.
A metaclass in Python is a class of a class that defines how a class behaves. A class is itself an
instance of a metaclass. A class in Python defines how the instance of the class will behave. In
order to understand metaclasses well, one needs to have prior experience working with Python
classes.
17. Differentiate mutable vs immutable
Mutable Immutable
Objects cannot be modified after the creation of
The objects can be modified after the creation as well. the objects.
Classes that are mutable are not considered final. Classes that are immutable are considered final.
Thread unsafe. Thread-safe.
Classes are not made final for the mutable objects. Classes are made final for the immutable objects.
Example: Lists, Dicts, Sets, User-Defined Classes, Example: int, float, bool, string, Unicode, tuple,
Dictionaries, etc. Numbers, etc.
List Array
consist of elements belonging to different data Only consists of elements belonging to the same data
types type
No need to explicitly import a module for
declaration Need to explicitly import a module for declaration
Cannot directly handle arithmetic operations Can directly handle arithmetic operations
Can be nested to contain different type of
elements Must contain either all nested elements of same size
Preferred for shorter sequence of data items Preferred for longer sequence of data items
Method Description
items() Returns a list containing a tuple for each key value pair
Python sequences can be index in positive and negative numbers. For positive index, 0 is the first
index, 1 is the second index and so forth. For negative index, (-1) is the last index and (-2) is the
second last index and so forth.
21. Mention what are the rules for local and global variables in Python?
Local variables: If a variable is assigned a new value anywhere within the function’s body, it’s
assumed to be local.
Global variables: Those variables that are only referenced inside a function are implicitly global.
To share global variables across modules within a single program, create a special module.
Import the config module in all modules of your application. The module will be available as a
global variable across modules.
• Python comprises of a huge standard library for most Internet platforms like Email,
HTML, etc.
• Python does not require explicit memory management as the interpreter itself allocates
the memory to new variables and free them automatically
• Having the built-in data types saves programming time and effort from declaring
variables
Python language is an interpreted language. Python program runs directly from the source code.
It converts the source code that is written by the programmer into an intermediate language,
which is again translated into machine language that has to be executed.
25. What is Scope in Python?
A scope defines the hierarchical order in which the namespaces have to be searched in order to
obtain the mappings of name-to-object(variables). It is a context in which variables exist and
from which they are referenced. It defines the accessibility and the lifetime of a variable.
26.Explain isinstance()?
The isinstance() function returns True if the specified object is of the specified type, otherwise
False . If the type parameter is a tuple, this function will return True if the object is one of the
types in the tuple.
Python is a dynamic, high level, free open source and interpreted programming language. It
supports object-oriented programming as well as procedural oriented programming. In Python,
we don't need to declare the type of variable because it is a dynamically typed language.
Indexing refers to an element of an iterable by its position within the iterable. Slicing operation
helps in getting a subset of elements from an iterable based on their indices.
repr() compute the “official” string representation of an object (a representation that has all
information about the object) and str() is used to compute the “informal” string representation of
an object (a representation that is useful for printing the object).