# question

QUESTION: Which type of Programming does Python support?
EXPLANATION:
Python is an interpreted programming language, which supports object-oriented, structured, and functional programming.


QUESTION: Which of the following is the correct extension of the Python file?
EXPLANATION:
`.py` is the correct extension of the Python file. Python programs can be written in any text editor. To save these programs we need to save in files with file extension `py`.


QUESTION: Is Python code compiled or interpreted?
EXPLANATION:
Many languages have been implemented using both compilers and interpreters, including C, Pascal, and Python.


QUESTION: What will be the value of the following Python expression?
EXPLANATION:
The order of precedence is: %, +. Hence the expression above, on simplification results in 4 + 3 = 7. 


QUESTION: Which of the following is used to define a block of code in Python language?
EXPLANATION:
In Python, to define a block of code we use indentation. Indentation refers to whitespaces at the beginning of the line.


QUESTION: Which of the following character is used to give single-line comments in Python?
EXPLANATION:
To write single-line comments in Python use the Hash character (#) at the beginning of the line. It is also called number sign or pound sign. To write multi-line comments, close the text between triple quotes.
Example:
```
""" 
comment text 
"""
```


EXPLANATION: SyntaxError, there shouldn’t be a space between + and = in +=.
QUESTION:
What will be the output of the following Python code?

```
i = 1
while True:
    if i%3 == 0:
        break
    print(i)
 
    i + = 1
```


QUESTION:
Which of the following functions can help us to find the version of python that we are currently working on?

EXPLANATION:
The function sys.version can help us to find the version of python that we are currently working on. It also contains information on the build number and compiler used. For example, 3.5.2, 2.7.3 etc. this function also returns the current date, time, bits etc along with the version.


QUESTION: Python supports the creation of anonymous functions at runtime, using a construct called __________
EXPLANATION:
Python supports the creation of anonymous functions (i.e. functions that are not bound to a name) at runtime, using a construct called lambda. Lambda functions are restricted to a single expression. They can be used wherever normal functions can be used.


QUESTION: What will be the output of the following Python code snippet if x=1?
EXPLANATION:
The binary form of `1` is `0001`. The expression `x<<2` implies we are performing bitwise left shift on `x`. This shift yields the value: `0100`, which is the binary form of the number `4`.


QUESTION: What are the values of the following Python expressions? ``` 2**(3**2) (2**3)**2 2**3**2 ```
EXPLANATION:
Expression 1 is evaluated as: `2**9`, which is equal to `512`. Expression 2 is evaluated as `8**2`, which is equal to `64`. The last expression is evaluated as `2**(3**2)`. This is because the associativity of `**` operator is from right to left. Hence the result of the third expression is `512`.


QUESTION: Which of the following is the truncation division operator in Python?
EXPLANATION:
`//` is the operator for truncation division. It is called so because it returns only the integer part of the quotient, truncating the decimal part. For example: `20//3 = 6`.


QUESTION:
What will be the output of the following Python code?

```
l=[1, 0, 2, 0, 'hello', '', []]
list(filter(bool, l))
```

EXPLANATION:
The code shown above returns a new list containing only those elements of the list l which do not amount to zero. Hence the output is: `[1, 2, ‘hello’]`.


QUESTION: Which of the following functions is a built-in function in python?
EXPLANATION:
The function seed is a function which is present in the random module. The functions sqrt and factorial are a part of the math module. The print function is a built-in function which prints a value directly to the system output.


EXPLANATION: The code shown above shows a general decorator which can work with any number of arguments.
QUESTION:
The following python program can work with ____ parameters.

```
def f(x):
    def f1(*args, **kwargs):
           print("Sanfoundry")
           return x(*args, **kwargs)
    return f1
```


QUESTION: What will be the output of the following Python function? ```min(max(False,-3,-4), 2,7)```
EXPLANATION:
The function max() is being used to find the maximum value from among -3, -4 and false. Since false amounts to the value zero, hence we are left with `min(0, 2, 7)` Hence the output is 0 (false).


QUESTION: What will be the output of the following Python expression `if x=56.236`? ```print("%.2f"%x)```
EXPLANATION:
The expression shown above rounds off the given number to the number of decimal places specified. Since the expression given specifies rounding off to two decimal places, the output of this expression will be 56.24. Had the value been `x=56.234` (last digit being any number less than 5), the output would have been 56.23.


QUESTION: What will be the output of the following Python function? ```len(["hello",2, 4, 6])```
EXPLANATION:
The function `len()` returns the length of the number of elements in the iterable. Therefore the output of the function shown above is 4.


EXPLANATION: The instance of the string returned by `upper()` is being printed.
QUESTION:
What will be the output of the following Python code?

```
x = 'abcd'
for i in x:
    print(i.upper())
```


EXPLANATION: `[::-1]` reverses the list.
QUESTION:
What will be the output of the following Python code snippet?

```
for i in [1, 2, 3, 4][::-1]:
    print (i)
```


EXPLANATION: Both `str(f)` and `format(f)` call `f.__str__()`.
QUESTION:
Which function is called when the following Python program is executed?

```
f = foo()
format(f)
```


EXPLANATION: Id in this case will be the attribute of the instance.
QUESTION:
What will be the output of the following Python code?

```
class tester:
    def __init__(self, id):
        self.id = str(id)
        id="224"
 
>>>temp = tester(12)
>>>print(temp.id)
```


EXPLANATION: The same object is modified in the function.
QUESTION:
What will be the output of the following Python program?

```
def foo(x):
    x[0] = ['def']
    x[1] = ['abc']
    return id(x)
q = ['abc', 'def']
print(id(q) == foo(q))
```


QUESTION:
What will be the output of the following Python program?

```
z=set('abc')
z.add('san')
z.update(set(['p', 'q']))
z
```

EXPLANATION:
The code shown first adds the element `‘san’` to the set `z`. The set `z` is then updated and two more elements, namely, `‘p’` and `‘q’` are added to it. Hence the output is: `{‘a’, ‘b’, ‘c’, ‘p’, ‘q’, ‘san’}`


QUESTION:
What will be the value of ‘result’ in following Python program?

```
list1 = [1,2,3,4]
list2 = [2,4,5,6]
list3 = [2,6,7,8]
result = list()
result.extend(i for i in list1 if i not in (list2+list3) and i not in result)
result.extend(i for i in list2 if i not in (list1+list3) and i not in result)
result.extend(i for i in list3 if i not in (list1+list2) and i not in result)
```

EXPLANATION:
Here, ‘result’ is a list which is extending three times. When first time ‘extend’ function is called for ‘result’, the inner code generates a generator object, which is further used in ‘extend’ function. This generator object contains the values which are in ‘list1’ only (not in ‘list2’ and ‘list3’).
Same is happening in second and third call of ‘extend’ function in these generator object contains values only in ‘list2’ and ‘list3’ respectively.
So, ‘result’ variable will contain elements which are only in one list (not more than 1 list).


EXPLANATION: Padding is done towards the right-hand-side first when the final string is of even length.
QUESTION:
What will be the output of the following Python code?

```print('*', "abcde".center(6), '*', sep='')```


EXPLANATION: Lists should be copied by executing [:] operation.
QUESTION:
What will be the output of the following Python code?

```
>>>list1 = [1, 3]
>>>list2 = list1
>>>list1[0] = 4
>>>print(list2)
```


QUESTION: Which one of the following is the use of function in python?
EXPLANATION:
Functions are reusable pieces of programs. They allow you to give a name to a block of statements, allowing you to run that block using the specified name anywhere in your program and any number of times.


QUESTION:
Which of the following Python statements will result in the output: 6?

```
A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
```

EXPLANATION:
The output that is required is 6, that is, row 2, item 3. This position is represented by the statement: `A[1][2]`.


EXPLANATION: The else part is not executed if control breaks out of the loop.
QUESTION:
 What will be the output of the following Python program?

```
i = 0
while i < 5:
    print(i)
    i += 1
    if i == 3:
        break
else:
    print(0)
```


EXPLANATION: `i` takes values 0, 1, 2 and 3.
QUESTION:
What will be the output of the following Python code?

```
x = 'abcd'
for i in range(len(x)):
    print(i)
```


QUESTION: What are the two main types of functions in Python?
EXPLANATION:
Built-in functions and user defined ones. The built-in functions are part of the Python language. Examples are: `dir()`, `len()` or `abs()`. The user defined functions are functions created with the def keyword.


EXPLANATION: `+` will append the element to the list.
QUESTION:
What will be the output of the following Python program?

```
def addItem(listParam):
    listParam += [1]
 
mylist = [1, 2, 3, 4]
addItem(mylist)
print(len(mylist))
```


QUESTION:
What will be the output of the following Python code snippet?

```
z=set('abc$de')
'a' in z
```

EXPLANATION:
The code shown above is used to check whether a particular item is a part of a given set or not. Since ‘a’ is a part of the set z, the output is true. Note that this code would result in an error in the absence of the quotes.


QUESTION: What will be the output of the following Python expression? ```round(4.576)```
EXPLANATION:
This is a built-in function which rounds a number to give precision in decimal digits. In the above case, since the number of decimal places has not been specified, the decimal number is rounded off to a whole number. Hence the output will be 5.


QUESTION: Which of the following is a feature of Python DocString?
EXPLANATION:
Python has a nifty feature called documentation strings, usually referred to by its shorter name docstrings. DocStrings are an important tool that you should make use of since it helps to document the program better and makes it easier to understand.


EXPLANATION: The elements of the tuple are accessed by their indices.
QUESTION:
What will be the output of the following Python code?

```print("Hello {0[0]} and {0[1]}".format(('foo', 'bin')))```


EXPLANATION: `(element,)` is not the same as element. It is a tuple with one item.
QUESTION:
What will be the output of the following Python code?

```
x = [[0], [1]]
print((' '.join(list(map(str, x))),))
```


QUESTION: The process of pickling in Python includes ____________
EXPLANATION:
Pickling is the process of serializing a Python object, that is, conversion of a Python object hierarchy into a byte stream. The reverse of this process is known as unpickling.


EXPLANATION: The finally block is executed even there is a return statement in the try block.
QUESTION:
What will be the output of the following Python code?

```
def foo():
    try:
        return 1
    finally:
        return 2
k = foo()
print(k)
```


# answer

ANSWER_TEXT:
a) Python first searches the built-in namespace, then the global namespace and finally the local namespace


ANSWER_TEXT:
b) Python first searches the built-in namespace, then the local namespace and finally the global namespace


ANSWER_TEXT:
c) Python first searches the local namespace, then the global namespace and finally the built-in namespace


ANSWER_TEXT:
d) Python first searches the global namespace, then the local namespace and finally the built-in namespace


ANSWER_TEXT:
c) It provides a convenient way of associating documentation with Python modules, functions, classes, and methods


