Top 50+ Python Interview Questions with Answers, Lecture notes of Computer Science

This document includes explanation of the Top 50+ Python Interview Questions with Answers.

Typology: Lecture notes

2019/2020

Uploaded on 02/19/2020

Sikander_Iqbal
Sikander_Iqbal 🇵🇰

5

(1)

7 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Top 50+ Python Interview
Questions with Answers
We have 100+ questions on Python Programming
basics which will help you with different expertise
levels to reap the maximum benefit from our blog. Our
Python Interview Questions is the one-stop resource
from where you can boost your interview preparation.
Q.1- What is Python? What are the benefits of using Python?
Ans: Python is a programming language with objects, modules, threads, exceptions and
automatic memory management. The benefits of pythons are that it is simple and easy,
portable, extensible, build-in data structure and it is an open source.
Q.2-What is PEP 8?
pf3
pf4
pf5
pf8

Partial preview of the text

Download Top 50+ Python Interview Questions with Answers and more Lecture notes Computer Science in PDF only on Docsity!

Top 50+ Python Interview

Questions with Answers

We have 100+ questions on Python Programming

basics which will help you with different expertise

levels to reap the maximum benefit from our blog. Our

Python Interview Questions is the one-stop resource

from where you can boost your interview preparation.

Q.1- What is Python? What are the benefits of using Python?

Ans: Python is a programming language with objects, modules, threads, exceptions and automatic memory management. The benefits of pythons are that it is simple and easy, portable, extensible, build-in data structure and it is an open source.

Q.2-What is PEP 8?

Ans: PEP 8 is a coding convention, a set of recommendation, about how to write your Python code more readable.

Q.3- What is pickling and unpickling?

Ans: Pickle module accepts any Python object and converts it into a string representation and dumps it into a file by using dump function, this process is called pickling. While the process of retrieving original Python objects from the stored string representation is called unpickling.

Q.4- How Python is interpreted?

Ans: 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.

Q.5- How memory is 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.

Q.6- What are the tools that help to find bugs or perform static

analysis?

Ans: PyChecker is a static analysis tool that detects the bugs in Python source code and warns about the style and complexity of the bug. Pylint is another tool that verifies whether the module meets the coding standard.

Q.7- What are Python decorators?

Ans: A Python decorator is a specific change that we make in Python syntax to alter functions easily.

Q.8- What is the difference between list and tuple?

Ans: The difference between list and tuple is that list is mutable while tuple is not. Tuple can be hashed for e.g. as a key for dictionaries.

Q.9- How are arguments passed by value or by reference?

Ans: Everything in Python is an object and all variables hold references to the objects. The references values are according to the functions; as a result, you cannot change the value of the references. However, you can change the objects if it is mutable.

Ans : It is a single expression anonymous function often used as inline function.

Q.19- Why lambda forms in python does not have statements?

Ans: A lambda form in python does not have statements as it is used to make new function object and then return them at runtime.

Q.20- What is a pass in Python?

Ans: Pass means, no-operation Python statement, or in other words it is a place holder in compound statement, where there should be a blank left and nothing has to be written there.

Q.21- What is a module and package in Python?

Ans: In Python, module is the way to structure program. Each Python program file is a module, which imports other modules like objects and attributes. The folder of Python program is a package of modules. A package can have modules or subfolders.

Q.22- Mention what are the rules for local and global

variables in Python?

Ans: 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.

Q.23- How can you share global variables across modules?

Ans: 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.

Q.24- Explain how can you make a Python Script executable

on Unix?

Ans: To make a Python Script executable on Unix, you need to do two things,  Script file's mode must be executable and  the first line must begin with # (#!/usr/local/bin/python)

Q.25- Explain how to delete a file in Python?

Ans: By using a command os.remove (filename) or os.unlink(filename)

Q.26- Explain how can you generate random numbers in

Python?

Ans: To generate random numbers in Python, you need to import command as import random random.random() This returns a random floating-point number in the range [0,1)

Q.27- How can you copy an object in Python?

Ans: To copy an object in Python, you can try copy. Copy () or copy.deepcopy() for the general case. You cannot copy all objects but most of them.

Q.28- What is the negative index in Python?

Ans: Python sequences can be an 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.

Q.29- How you can convert a number to a string?

Ans: In order to convert a number into a string, use the inbuilt function str(). If you want a octal or hexadecimal representation, use the inbuilt function oct() or hex().

Q.30- What is the difference between Xrange and range?

Ans: Xrange returns the xrange object while range returns the list, and uses the same memory and no matter what the range size is.

Q.31- Explain how can you access a module written in Python

from C?

Ans: You can access a module written in Python from C by following method, Module = =PyImport_ImportModule("");

Q.32- Mention the use of // operator in Python?

Ans: It is a Floor Division operator , which is used for dividing two operands with the result as quotient showing only digits before the decimal point. For instance, 10//5 = 2 and 10.0//5. = 2.0.

Q.33- Explain what is Flask & its benefits?

Ans: Flask is a web micro framework for Python based on "Werkzeug, Jinja 2 and good intentions" BSD licensed. Werkzeug and jingja are two of its dependencies. Flask is part of the micro-framework. Which means it will have little to no dependencies on external libraries. It makes the framework light while there is little dependency to update and less security bugs.

Q.39- Explain how you can access sessions in Flask?

Ans: A session basically allows you to remember information from one request to another. In a flask, it uses a signed cookie so the user can look at the session contents and modify. The user can modify the session if only it has the secret key Flask.secret_key.

Q.40- Explain the database connection in Python Flask?

Ans: Flask supports database powered application (RDBS). Such system requires creating a schema, which requires piping the shema.sql file into a sqlite3 command. So you need to install sqlite3 command in order to create or initiate the database in Flask. Flask allows to request a database in three ways  before_request() : They are called before a request and pass no arguments  after_request() : They are called after a request and pass the response that will be sent to the client  teardown_request(): They are called in situa Ans: tion when exception is raised, and response are not guaranteed. They are called after the response been constructed. They are not allowed to modify the request, and their values are ignored.

Q.41- You are having multiple Memcache servers running

Python, in which one of the memcacher server fails, and it has

your data, will it ever try to get key data from that one failed

server?

Ans: The data in the failed server won't get removed, but there is a provision for auto-failure, which you can configure for multiple nodes. Fail-over can be triggered during any kind of socket or Memcached server level errors and not during normal client errors like adding an existing key, etc.

Q.42- Explain how you can minimize the Memcached server

outages in your Python Development?

 When one instance fails, several of them goes down, this will put larger load on the database server when lost data is reloaded as client make a request. To avoid this, if your code has been written to minimize cache stampedes then it will leave a minimal impact  Another way is to bring up an instance of Memcached on a new machine using the lost machines IP address  Code is another option to minimize server outages as it gives you the liberty to change the Memcached server list with minimal work  Setting timeout value is another option that some Memcached clients implement for Memcached server outage. When your Memcached server goes down, the client will keep trying to send a request till the time-out limit is reached

Q.43- Explain what is Dogpile effect? How can you prevent

this effect?

Ans: Dogpile effect is referred to the event when cache expires, and websites are hit by the multiple requests made by the client at the same time. This effect can be prevented by using semaphore lock. In this system when value expires, first process acquires the lock and starts generating new value.

Q.44- Explain how Memcached should not be used in your

Python project?

 Memcached common misuse is to use it as a data store, and not as a cache  Never use Memcached as the only source of the information you need to run your application. Data should always be available through another source as well  Memcached is just a key or value store and cannot perform query over the data or iterate over the contents to extract information  Memcached does not offer any form of security either in encryption or authentication

Q.45-What are the built-in types of python?

Ans: Built-in types in Python are as follows –  Integers  Floating-point  Complex numbers  Strings  Boolean  Built-in functions

Q.46- What are Python packages?

Ans: Python packages are namespaces containing multiple modules.

Q.47- How can files be deleted in Python?

Ans: To delete a file in Python, you need to import the OS Module. After that, you need to use the os.remove() function. Example: 1 2 import os os.remove("xyz.txt")