Python -- interview questions Flashcards

1
Q

Q: How Python is interpreted?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q: What is the difference between list and tuple?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q: How are arguments passed by value or by reference?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q: What are Dict and List comprehension?

A

They are syntax constructions to ease the creation of a Dictionary or List based on existing iterable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q: What are the built-in type does python provides?

A
A: There are mutable and Immutable types of Pythons built-in types Mutable built-in types
List
Sets
Dictionaries
Immutable built-in types
Strings
Tuples
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q: What is namespace in Python?

A

A: In Python, every name introduced has a place where it lives and can be hooked for. This is known as a namespace. It is like a box where a variable name is mapped to the object placed. Whenever the variable is searched out, this box will be searched, to get the corresponding object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q: What is lambda in Python?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q: Why lambda forms in python do not have statements?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q: What are the generators in Python?

A

A: The way of implementing iterators are known as generators. It is a normal function except that it yields expression in the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q: How can you copy an object in Python?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q: What is a negative index in Python?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q: How you can convert a number to a string?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q: What is the difference between Xrange and range?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Q: What is the module and package in Python?

A

A: In Python, the module is the way to structure the program. Each Python program file is a module, which imports other modules like objects and attributes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Q: What are the rules for local and global variables in Python?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Q: how many Data structures are available In pandas?

A

A: There are two data structures supported by pandas library, Series and DataFrames. Both of the data structures are built on top of Numpy. Series is a one-dimensional data structure in pandas and DataFrame is the two-dimensional data structure in pandas. There is one more axis label known as Panel which is a three-dimensional data structure and it includes items, major_axis, and minor_axis.

17
Q

Q: What Are A pandas DataFrame? How Will You Create An Empty DataFrame In pandas?

A

A: Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). It consists of three principal components, the data, rows, and columns. pandas DataFrame can be created from the lists, dictionary, and from a list of the dictionary, etc.
To create an empty DataFrame in pandas, type
import pandas as pd
df = pd.DataFrame()

18
Q

Q:Suppose you want to join train and test dataset (both are two numpy arrays train_set and test_set) into a resulting array (resulting_set) to do data processing on it simultaneously. This is as follows:

A

A: train_set = np.array([1, 2, 3])
test_set = np.array([[0, 1, 2], [1, 2, 3]])
resulting_set –> [[1, 2, 3], [0, 1, 2], [1, 2, 3]]

19
Q

Q: Can we create a DataFrame with multiple data types in Python? If yes, how can you do it?

A
20
Q

Q: Write the code to sort an array in NumPy by the nth column?

A

A: Using argsort () function this can be achieved. If there is an array X and you would like to sort the nth column then code for this will be x[x [: n-1].argsort ()]

21
Q

Q: Explain the usage of decorators?

A

A: decorator is a function that takes another function and extends the behavior of the latter function without explicitly modifying it. They are used to modify the code in classes and functions. With the help of decorators, a piece of code can be executed before or after the execution of the original code.

22
Q

Q: What is __init__ in Python?

A

A: __init__ is a reserved method in Python classes which is equivalent to constructors in OOP terminology. The __init__ method is called automatically whenever a new object is initiated. The __init__ method allocates memory for the new object as soon as it is created. This method can also be used to initialize variables.

23
Q

Q: What is self-keyword in Python?

A

A: Self-keyword is used as the first parameter of a function inside a class that represents the instance of the class. The object or the instance of the class is automatically passed to the method that it belongs to and is received in the ‘self-keyword’. Users can use another name for the first parameter of the function that catches the object of the class, but it is recommended to use the self-keyword only as it is more of a Python convention.

24
Q

What does yield do in python?

A

yield is an keyword expression used in generators. They suspend the iterator at a certain point and return a value, the generator only goes forward with a next statement.

Co-routines also use this function but use yield from or the variable before it.

25
Q

What is GIL

A

Global interpreter lock used so only one thread has control (or lock) on the program at a time. https://realpython.com/python-gil/