Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
131 views

Python Interview Questions For Freshers - Docx 1

This document contains 28 Python interview questions for freshers. The questions cover a range of Python topics including data structures like lists, tuples, dictionaries and sets; file processing modes; when to use dictionaries vs lists; list and dictionary methods; exceptions handling; classes and objects; iterators and generators; global and local variables; modules; and key Python features.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Python Interview Questions For Freshers - Docx 1

This document contains 28 Python interview questions for freshers. The questions cover a range of Python topics including data structures like lists, tuples, dictionaries and sets; file processing modes; when to use dictionaries vs lists; list and dictionary methods; exceptions handling; classes and objects; iterators and generators; global and local variables; modules; and key Python features.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Python interview questions for freshers

1. When do you use list vs. Tuplevs. Dictionaryvs. Set?

List and Tuple are both ordered containers.

If you want an ordered container of constant elements use tuple as tuples are immutable objects
and list are mutable in nature.

2. Explain all the file processing modes supported by Python?

Python allows to open files in one of the three modes. They are:

• read-only mode,

• write-only mode,

• read-write mode,

• Append mode.

By specifying the flags “r”, “w”, “rw”, “a” respectively.

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”.

3. When does a dictionary is used instead of a list?

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.

List comprehension is used to construct lists in a natural way.

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.

5. Is all the memory freed when Python exits?

No it is not, because the objects that are referenced from global namespaces of Python modules
are not always de-allocated when Python exits.

6. Difference between str() and repr()?

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)

7. What are higher order functions?

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

8. How to copy object in python?

Use = operator to create a copy of an object. It creates a new variable that shares the reference of
the original object.

9. How to make array in python

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.

10. How to handle exceptions?

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

12. What are iterators and generators?

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.

13. What is exec() and eval () in python

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.

14. What is descriptor?

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.

15. What is MRO?

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.

16. What are Metaclasses?

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.

18. Differentiate between list and Python array?

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

19. What are the methods of dict?

Method Description

clear() Removes all the elements from the dictionary

copy() Returns a copy of the dictionary

fromkeys() Returns a dictionary with the specified keys and value

get() Returns the value of the specified key

items() Returns a list containing a tuple for each key value pair

keys() Returns a list containing the dictionary's keys


20. What is negative index in Python?

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.

22 .How can you share global variables across modules?

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.

23.Mention the benefits of using Python?

• 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

• Provide easy readability due to use of square brackets

• Easy-to-learn for beginners

• Having the built-in data types saves programming time and effort from declaring
variables

24. How Python is interpreted?

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.

27. Differentiate between List, Set, and Tuple

List Set Tuple


Lists is Mutable Set is Mutable Tuple is Immutable
It is Ordered collection of items It is Unordered collection of items It is Ordered collection of items
Items in list can be replaced or Items in set cannot be changed or Items in tuple cannot be changed or
changed replaced replaced

28.Explain the key features of python

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.

29.Explain indexing and slicing ?

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.

30. Difference between str() and repr()?

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).

You might also like