Python Interview Questions Pdf – 2019 Updated

1. What is the purpose pass statement in python?

Answer:

The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.

2. What Is %S In Python?

Answer:

Python has support for formatting any value into a string. It may contain quite complex expressions.

One of the common usages is to push values into a string with the %s format specifier. The formatting operation in Python has the comparable syntax as the C function printf() has.

3. What Is A Function Call Or A Callable Object In Python?

Answer:

A function in Python gets treated as a callable object. It can allow some arguments and also return a value or multiple values in the form of a tuple. Apart from the function, Python has other constructs, such as classes or the class instances which fits in the same category.

4. What Does The Continue Do In Python?

Answer:

The continue is a jump statement in Python which moves the control to execute the next iteration in a loop leaving all the remaining instructions in the block unexecuted.

The continue statement is applicable for both the “while” and “for” loops.

5. Does Python Have A Main() Method?

Answer:

The main() is the entry point function which happens to be called first in most programming languages.

Since Python is interpreter-based, so it sequentially executes the lines of the code one-by-one.

Python also does have a Main() method. But it gets executed whenever we run our Python script either by directly clicking it or starts it from the command line.

We can also override the Python default main() function using the Python if statement. Please see the below code.

6. When Should You Use The “Break” In Python?

Answer:

Python provides a break statement to exit from a loop. Whenever the break hits in the code, the control of the program immediately exits from the body of the loop.

The break statement in a nested loop causes the control to exit from the inner iterative block. (e Learning Portal)

7. What Is A Dictionary In Python Programming?

Answer:

A dictionary is a data structure known as an associative array in Python which stores a collection of objects.

The collection is a set of keys having a single associated value. We can call it a hash, a map, or a hashmap as it gets called in other programming languages. (python interview questions pdf)

8. What Is Class In Python?

Answer:

Python supports object-oriented programming and provides almost all OOP features to use in programs.

A Python class is a blueprint for creating the objects. It defines member variables and gets their behavior associated with them.

We can make it by using the keyword “class.” An object gets created from the constructor. This object represents the instance of the class.

9. What Is Composition In Python?

Answer:

The composition is also a type of inheritance in Python. It intends to inherit from the base class but a little differently, i.e., by using an instance variable of the base class acting as a member of the derived class.

10. How Do You Handle Exceptions With Try/Except/Finally In Python?

Answer:

Python lay down Try, Except, Finally constructs to handle errors as well as Exceptions. We enclose the unsafe code indented under the try block. And we can keep our fall-back code inside the except block. Any instructions intended for execution last should come under the finally block.

11. What Do You Know About The Python Enumerate?

Answer:

While using the iterators, sometimes we might have a use case to store the count of iterations. Python gets this task quite easy for us by giving a built-in method known as the enumerate().

The enumerate() function attaches a counter variable to an iterable and returns it as the “enumerated” object.

We can use this object directly in the “for” loops or transform it into a list of tuples by calling the list() method.

12. What Is The Use Of Globals() Function In Python?

Answer:

The globals() function in Python returns the current global symbol table as a dictionary object.

Python maintains a symbol table to keep all necessary information about a program. This info includes the names of variables, methods, and classes used by the program.

All the information in this table remains in the global scope of the program and Python allows us to retrieve it using the globals() method. (Online Training Institute)

13. How Do You Monitor The Code Flow Of A Program In Python?

Answer:

In Python, we can use the sys module’s settrace() method to setup trace hooks and monitor the functions inside a program.

You need to define a trace callback method and pass it to the settrace() function. The callback should specify three arguments as shown below.

14. What Does The Yield Keyword Do In Python?

Answer:

The yield keyword can turn any function into a generator. It works like a standard return keyword. But it’ll always return a generator object. Also, a method can have multiple calls to the yield keyword. (python interview questions pdf)

15. What is the purpose of Pythonpath environment variable?

Answer:

The yield keyword can turn any function into a generator. It works like a standard return keyword. But it’ll always return a generator object. Also, a method can have multiple calls to the yield keyword.

16. What is pickling and unpickling?

Answer:

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.

python training online

 

17. Explain the use of session in Django framework?

Answer:

Django provides session that lets you store and retrieve data on a per-site-visitor basis. Django abstracts the process of sending and receiving cookies, by placing a session ID cookie on the client side, and storing all the related data on the server side.

18. What advantages do NumPy arrays offer over (nested) Python lists?

Answer:

Python’s lists are efficient general-purpose containers. They support (fairly) efficient insertion, deletion, appending, and concatenation, and Python’s list comprehensions make them easy to construct and manipulate.
They have certain limitations: they don’t support “vectorized” operations like elementwise addition and multiplication, and the fact that they can contain objects of differing types mean that Python must store type information for every element, and must execute type dispatching code when operating on each element.
NumPy is not just more efficient; it is also more convenient. You get a lot of vector and matrix operations for free, which sometimes allow one to avoid unnecessary work. And they are also efficiently implemented.
NumPy array is faster and You get a lot built in with NumPy, FFTs, convolutions, fast searching, basic statistics, linear algebra, histograms, etc.

19. What Do You Think Is The Output Of The Following Code Fragment?

Answer:

The result of the above lines of code is []. There won’t be any error like an IndexError.

You should know that trying to fetch a member from the list using an index that exceeds the member count (for example, attempting to access list[10] as given in the question) would yield an IndexError. By the way, retrieving only a slice at the starting index that surpasses the no. of items in the list won’t result in an IndexError. It will just return an empty list.

20. What Is The Purpose Of “End” In Python?

Answer:

Python’s print() function always prints a newline in the end. The print() function accepts an optional parameter known as the ‘end.’ Its value is ‘\n’ by default. We can change the end character in a print statement with the value of our choice using this parameter.

21. What Is Whitespace In Python?

Answer:

Whitespace represents the characters that we use for spacing and separation.

They possess an “empty” representation. In Python, it could be a tab or space. (python interview questions pdf)

22. What Are Attributes And Methods In A Python Class?

Answer:

A class is useless if it has not defined any functionality. We can do so by adding attributes. They work as containers for data and functions. We can add an attribute directly specifying inside the class body.

23. What Are Closures In Python?

Answer:

Python closures are function objects returned by another function. We use them to eliminate code redundancy.

24. What Is The Syntax For Dictionary Comprehension In Python?

Answer:

A dictionary has the same syntax as was for the list comprehension but the difference is that it uses curly braces

25. Which Python Function Will You Use To Convert A Number To A String?

Answer:

For converting a number into a string, you can use the built-in function str(). If you want an octal or hexadecimal representation, use the inbuilt function oct() or hex().

26. Explain the use of with statement?

Answer:

In python generally “with” statement is used to open a file, process the data present in the file, and also to close the file without calling a close() method. “with” statement makes the exception handling simpler by providing cleanup activities.

27. Name few Python modules for Statistical, Numerical and scientific computations?

Answer:

numPy – this module provides an array/matrix type, and it is useful for doing computations on arrays. scipy – this module provides methods for doing numeric integrals, solving differential equations, etc pylab – is a module for generating and saving plots.

python training online

28. How do you get the last value in a list or a tuple?

Answer:

When we pass -1 to the index operator of the list or tuple, it returns the last value. If -2 is passed, it returns the last but one value. (python interview questions pdf)

29. What happens if an error occurs that is not handled in the except block?

Answer:

The program tenuinates. and an execution trace is sent to sys.stder.

30. What is the lambda operator?

Answer:

The lambda operator is used to create anonymous functions. It is mostly used in cases where one wishes to pass functions as parameters. or assign them to variable names.

31. Under what circumstances would von use a while statement rather than for?

Answer:

The while statement is used for simple repetitive looping and the for statement is used when one wishes to iterate through a list of items, such as database records, characters in a string, etc.

32. When would you use a break statement in a for loop?

Answer:

When the loop has served its purpose. As an example. after finding the item in a list searched for, there is no need to keep looping. The break statement says, I’m done in this loop; move on to the next block of code.” (Interview Questions on Python – Best 47 Freshers and Experienced)

33. What is the structure of a while loop?

Answer:

The ellipsis represents a code block to be executed. until the condition becomes false. The condition is an expression that is considered true unless it evaluates to o, null or false.

34. Control flow statements?

Answer:

By default, python program execution starts from the first line, execute each and every statement only once and transactions the program if the last statement of the program execution is over.
Control flow statements are used to disturb the normal flow of the execution of the program.

35. What is File Handling?

Answer:

File is a named location on the disk, which stores the data in permanent manner.
Python language provides various functions and methods to provide the communication between python programs and files.
Python programs can open the file, perform the read or write operations on the file and close the file
We can open the files by calling open function of built-in-modules
At the time of opening the file, we have to specify the mode of the file
Mode of the file indicates for what purpose the file is going to be opened(r,w,a,b)… (python interview questions pdf)

36. What is Try Block?

Answer:

A block which is preceded by the try keyword is known as a try block
Syntax:
statements that may cause an exception
The statements which cause to runtime errors and other statements which depends on the execution of runtime errors statements are recommended to represent in a try block
While executing try block statement if any exception is raised then immediately try block identifies that exception, receive that exception and forward that exception to except block without executing remaining statements to try block.

37. What is the Encapsulation?

Answer:

The concept of binding or grouping related data members along with its related functionalities is known as an Encapsulation.

python training online

38. Executing DML Commands Through Python Programs?

Answer:

DML Commands are used to modify the data of the database objects

Whenever we execute DML Commands the records are going to be modified temporarily
Whenever we run “rollback” command the modified records will come back to its original state
To modify the records of the database objects permanently we use “commit” command
After executing the commit command even though we execute “rollback” command, the modified records will not come back to its original state
Create the emp1 table in the database by using following command
Create table emp1 as select * from emp;
Whenever we run the DML commands through the python program, then the no.of records which are modified because of that command will be stored into the rowcount attribute of cursor object
After executing the DML Command through the python program we have to call commit method of cursor object.

39. What is scheduling?

Answer:

Among multiple threads which thread as to start the execution first, how much time the thread as to execute after allocated time is over, which thread, as to continue the execution next this, comes under scheduling Scheduling is highly dynamic. (python interview questions pdf)

40. What is OS Module?

Answer:

OS Module is a predefined module and which provides various functions and methods to perform the operating system related activities, such as creating the files, removing the files, creating the directories removing the directories, executing the operating system related commands, etc.