Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

## Python MCQ Set 1

1. What is the output of the following expression:

`print(4.00/(2.0+2.0))`

a) Error
b) 1.0
c) 1.00
d) 1

### View Answer

Answer: b [Reason:] The result of the expression shown above is 1.0 because print rounds off digits.

2. Consider the expression given below. The value of X is:

`X = 2+9*((3*12)-8)/10`

a) 30.0
b) 30.8
c) 28.4
d) 27.2

### View Answer

Answer: d [Reason:] The expression shown above is evaluated as: 2+9*(36-8)/10, which simplifies to give 2+9*(2.8), which is equal to 2+25.2 = 27.2. Hence the result of this expression is 27.2.

3. Which of the following expressions involves coercion when evaluated in Python?
a) 4.7 – 1.5
b) 7.9 * 6.3
c) 1.7 % 2
d) 3.4 + 4.6

### View Answer

Answer: c [Reason:] Coercion is the implicit (automatic) conversion of operands to a common type. Coercion is automatically performed on mixed-type expressions. The expression 1.7 % 2 is evaluated as 1.7 % 2.0 (that is, automatic conversion of int to float).

4. What is the value of the following expression:

`24//6%3, 24//4//2`

a) (1,3)
b) (0,3)
c) (1,0)
d) (3,1)

### View Answer

Answer: a [Reason:] The expressions are evaluated as: 4%3 and 6//2 respectively. This results in the answer (1,3). This is because the associativity of both of the expressions shown above is left to right.

5. Which among the following list of operators has the highest precedence?

` +, -, **, %, /, <<, >>, |`

a) <<, >>
b) **
c) |
d) %

### View Answer

Answer: b [Reason:] The highest precedence is that of the exponentiation operator, that is of **.

6. What is the value of the expression:

`float(4+int(2.39)%2)`

a) 5.0
b) 5
c) 4.0
d) 4

### View Answer

Answer: c [Reason:] The above expression is an example of explicit conversion. It is evaluated as: float(4+int(2.39)%2) = float(4+2%2) = float(4+0) = 4.0. Hence the result of this expression is 4.0.

7. Which of the following expressions is an example of type conversion?
a) 4.0 + float(3)
b) 5.3 + 6.3
c) 5.0 + 3
d) 3 + 7

### View Answer

Answer: a [Reason:] Type conversion is nothing but explicit conversion of operands to a specific type. Options ‘b’ and ‘c’ are examples of implicit conversion whereas option ‘a’ is an example of explicit conversion or type conversion.

8. Which of the following expressions results in an error?
a) float(‘10’)
b) int(‘10’)
c) float(’10.8’)
d) int(’10.8’)

### View Answer

Answer: d [Reason:] All of the above examples show explicit conversion. However the expression int(’10.8’) results in an error.

9. What is the value of the expression:

`4+2**5//10`

a) 3
b) 7
c) 77
d) 0

### View Answer

Answer: b [Reason:] The order of precedence is: **, //, +. The expression 4+2**5//10 is evaluated as 4+32//10, which is equal to 4+3 = 7. Hence the result of the expression shown above is 7.

10. The expression 2**2**3 is evaluates as: (2**2)**3. State whether this statement is true or false.
a) True
b) False

### View Answer

Answer: b [Reason:] The value of the expression (2**2)**3 = 4**3 = 64. When the expression 2**2**3 is evaluated in python, we get the result as 256, because this expression is evaluated as 2**(2**3). This is because the associativity of exponentiation operator (**) is from right to left and not from left to right.

## Python MCQ Set 2

1. To include the use of functions which are present in the random library, we must use the option:
a) import random
b) random.h
c) import.random
d) random.random

### View Answer

Answer: a [Reason:] The command import random is used to import the random module, which enables us to use the functions which are present in the random library.

2. The output of the following snippet of code is either 1 or 2. State whether this statement is true or false.

```import random
random.randint(1,2)```

a) True
b) False

### View Answer

Answer: a [Reason:] The function random.randint(a,b) helps us to generate an integer between ‘a’ and ‘b’, including ‘a’ and ‘b’. In this case, since there are no integers between 1 and 2 , the output will necessarily be either 1 or 2’.

3. What is the output of the code shown below?

```import random
random.choice(2,3,4)```

a) An integer other than 2, 3 and 4
b) Either 2, 3 or 4
c) Error
d) 3 only

### View Answer

Answer: c [Reason:] The code shown above displays the incorrect syntax of the function random.choice(). This functions takes its numeric parameter in the form of a list. Hence the correct syntax world be: random.choice([2,3,4]).

4. What is the output of the code shown below?

```import random
random.choice([10.4, 56.99, 76])```

a) Error
b) Either 10.4, 56.99 or 76
c) Any number other than 10.4, 56.99 and 76
c) 56.99 only

### View Answer

Answer: b [Reason:] The function random.choice(a,b,c,d) returns a random number which is selected from a, b, c and d. The output can be either a, b, c or d. Hence the output of the snippet of code shown above can be either 10.4, 56.99 or 76.

5. What is the output of the function shown below (random module has already been imported)?

`random.choice('sun')`

a) sun
b) u
c) either s, u or n
d) error

### View Answer

Answer: c [Reason:] The above function works with alphabets just as it does with numbers. The output of thes expression will be either s, u or n.

6. What is the output of the following funtion, assuming that the random module has already been imported?

`random.uniform(3,4)`

a) Error
b) Either 3 or 4
c) Any integer other than 3 and 4
d) Any decimal value between 3 and 4

### View Answer

Answer: d [Reason:] This question depicts the basic difference between the functions random.randint(a, b) and random.uniform(a, b). While random.randint(a,b) generates an integer between ‘a’ and ‘b’, including ‘a’ and ‘b’, the function random.uniform(a,b) generates a decimal value between ‘a’ and ‘b’.

7. What is the output of the function shown below if the random module has already been imported?

`random.randint(3.5,7)`

a) Error
b) Any integer between 3.5 and 7, including 7
c) Any integer between 3.5 and 7, excluding 7
d) The integer closest to the mean of 3.5 and 7

### View Answer

Answer: a [Reason:] The function random.randint() does not accept a decimal value as a parameter. Hence the function shown above will throw an error.

8. Which of the following functions helps us to randomize the items of a list?
a) seed
b) randomise
c) shuffle
d) uniform

### View Answer

Answer: c [Reason:] The function shuffle, which is included in the random module, helps us to randomize the items of a list. This function takes the list as a parameter.

9. What is the output of the code shown below?

```random.seed(3)
random.randint(1,5)
2
random.seed(3)
random.randint(1,5)```

a) 3
b) 2
c) Any integer between 1 and 5, including 1 and 5
d) Any integer between 1 and 5, excluding 1 and 5

### View Answer

Answer: b [Reason:] We use the seed function when we want to use the same random umber once again in our program. Hence the output of the code shown above will be 2, since 2 was generated previously following which we used the seed function.

10. What is the interval of the value generated by the function random.random(), assuming that the random module has already been imported?
a) (0,1)
b) (0,1]
c) [0,1]
d) [0,1)

### View Answer

Answer: d [Reason:] The function random.random() generates a random value in the interval [0,1), that is, including zero but excluding one.

11. Which of the following is a possible outcome of the function shown below?

`random.randrange(0,91,5)`

a) 10
b) 18
c) 79
d) 95

### View Answer

Answer: a [Reason:] The function shown above will generate an output which is a multiple of 5 and is between 0 and 91. The only option which satisfies these criteria is 10. Hence the only possible output of this function is 10.

12. Both the functions randint and uniform accept ____________ parameters.
a) 0
b) 1
c) 3
d) 2

### View Answer

Answer: c [Reason:] Both of these functions, that is, randint and uniform are included in the random module and both of these functions accept 3 parameters. For example: random.uniform(self,a,b) where ‘a’ and ‘b’ specify the range and self is an imaginary parameter.

13. The randrange function returns only an integer value. Sate whether true or false.
a) True
b) False

### View Answer

Answer: a [Reason:] The function randrange returns only an integer value. Hence this statement is true.

14. Which of the following options is the possible outcome of the function shown below?

`random.randrange(1,100,10)`

a) 32
b) 67
d) 91
d) 80

### View Answer

Answer: c [Reason:] The output of this function can be any value which is a multiple of 10, plus 1. Hence a value like 11, 21, 31, 41…91 can be the output. Also, the value should necessarily be between 1 and 100. The only option which satisfies this criteria is 91.

15. What is the output of this function, assuming that the random library has already been included?

`random.shuffle[1,2,24]`

a) Randomized list containing the same numbers in any order
b) The same list, that is [1,2,24].
c) A list containing any random numbers between 1 and 24
d) Error

### View Answer

Answer: d [Reason:] The function shown above will result in an error because this is the incorrect syntax for the usage of the function shuffle(). The list should be previously declared and then passed to this function to get an output. An example of the correct syntax: >>> l=[‘a’,’b’,’c’,’d’]. >>> random.shuffle(l) >>> print(l)

## Python MCQ Set 3

1. Which is the most appropriate definition for recursion?
a) A function that calls itself
b) A function execution instance that calls another execution instance of the same function
c) A class method that calls another class method
d) An in-built method that is automatically called

### View Answer

Answer: b [Reason:] The appropriate definition for a recursive function is a function execution instance that calls another execution instance of the same function either directly or indirectly.

2. Only problems that are recursively defined can be solved using recursion. True or False?
a) True
b) False

### View Answer

Answer: b [Reason:] There are many other problems can also be solved using recursion.

3. Which of these is false about recursion?
a) Recursive function can be replaced by a non-recursive function
b) Recursive functions usually take more memory space than non-recursive function
c) Recursive functions run faster than non-recursive function
d) Recursion makes programs easier to understand

### View Answer

Answer: c [Reason:] The speed of a program using recursion is slower than the speed of its non-recursive equivalent.

4. Fill in the line of code for calculating the factorial of a number.

```def fact(num):
if num == 0:
return 1
else:
return _____________________```

a) num*fact(num-1)

b) (num-1)*(num-2)

c) num*(num-1)

d) fact(num)*fact(num-1)

### View Answer

Answer: a [Reason:] Suppose n=5 then, 5*4*3*2*1 is returned which is the factorial of 5.

5. What is the output of the following piece of code?

```def test(i,j):
if(i==0):
return j
else:
return test(i-1,i+j)
print(test(4,7))```

a) 13
b) 7
c) Infinite loop
d) 17

### View Answer

Answer: a [Reason:] The test(i-1,i+j) part of the function keeps calling the function until the base condition of the function is satisfied.

6. What is the output of the following code?

```l=[]
def convert(b):
if(b==0):
return l
dig=b%2
l.append(dig)
convert(b//2)
convert(6)
l.reverse()
for i in l:
print(i,end="")```

a) 011
b) 110
c) 3
d) Infinite loop

### View Answer

Answer: b [Reason:] The above code gives the binary equivalent of the number.

7. What is tail recursion?
a) A recursive function that has two base cases
b) A function where the recursive functions leads to an infinite loop
c) A recursive function where the function doesn’t return anything and just prints the values
d) A function where the recursive call is the last thing executed by the function

### View Answer

Answer: d [Reason:] A recursive function is tail recursive when recursive call is executed by the function in the last.

8. Observe the following piece of code?

```def a(n):
if n == 0:
return 0
else:
return n*a(n - 1)
def b(n, tot):
if n == 0:
return tot
else:
return b(n-2, tot-2)```

a) Both a() and b() aren’t tail recursive
b) Both a() and b() are tail recursive
c) b() is tail recursive but a() isn’t
d) a() is tail recursive but b() isn’t

### View Answer

Answer: c [Reason:] A recursive function is tail recursive when recursive call is executed by the function in the last.

9. Which of the following statements is false about recursion?
a) Every recursive function must have a base case
b) Infinite recursion can occur if the base case isn’t properly mentioned
c) A recursive function makes the code easier to understand
d) Every recursive function must have a return value

### View Answer

Answer: d [Reason:] A recursive function needn’t have a return value.

10. What is the output of the following piece of code?

```def fun(n):
if (n > 100):
return n - 5
return fun(fun(n+11));

print(fun(45))```

a) 50
b) 100
c) 74
d) Infinite loop

### View Answer

Answer: b [Reason:] The fun(fun(n+11)) part of the code keeps executing until the value of n becomes greater than 100, after which n-5 is returned and printed.

11. Recursion and iteration are the same programming approach. True or False?
a) True
b) False

### View Answer

Answer: b [Reason:] In recursion, the function calls itself till the base condition is reached whereas iteration means repetition of process for example in for-loops.

12. What happens if the base condition isn’t defined in recursive programs?
a) Program gets into an infinite loop
b) Program runs once
c) Program runs n number of times where n is the argument given to the function
d) An exception is thrown

### View Answer

Answer: a [Reason:] The program will run until the system gets out of memory.

13. Which of these is not true about recursion?
a) Making the code look clean
b) A complex task can be broken into sub-problems
c) Recursive calls take up less memory
d) Sequence generation is easier than a nested iteration

### View Answer

Answer: c [Reason:] Recursive calls take up a lot of memory and time as memory is taken up each time the function is called.

14. Which of these is not true about recursion?
a) The logic behind recursion may be hard to follow
b) Recursive functions are easy to debug
c) Recursive calls take up a lot of memory
d) Programs using recursion take longer time than their non-recursive equivalent

### View Answer

Answer: b [Reason:] Recursive functions may be hard to debug as the logic behind recursion may be hard to follow.

15. What is the output of the following piece of code?

```def a(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return a(n-1)+a(n-2)
for i in range(0,4):
print(a(i),end=" ")```

a) 0 1 2 3
b) An exception is thrown
c) 0 1 1 2 3
d) 0 1 1 2

### View Answer

Answer: d [Reason:] The above piece of code prints the Fibonacci series.

## Python MCQ Set 4

1. Which module in Python supports regular expressions?
a) re
b) regex
c) pyregex
d) none of the mentioned

### View Answer

Answer: a [Reason:] re is a part of the standard library and can be imported using: import re.

2. Which of the following creates a pattern object?
a) re.create(str)
b) re.regex(str)
c) re.compile(str)
d) re.assemble(str)

### View Answer

Answer: c [Reason:] It converts a given string into a pattern object.

3. What does the function re.match do?
a) matches a pattern at the start of the string
b) matches a pattern at any position in the string
c) such a function does not exist
d) none of the mentioned

### View Answer

Answer: a [Reason:] It will look for the pattern at the beginning and return None if it isn’t found.

4. What does the function re.search do?
a) matches a pattern at the start of the string
b) matches a pattern at any position in the string
c) such a function does not exist
d) none of the mentioned

### View Answer

Answer: b [Reason:] It will look for the pattern at any position in the string.

5. What is the output of the following?

```sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.groups())```

a) (‘we’, ‘are’, ‘humans’)
b) (we, are, humans)
c) (‘we’, ‘humans’)
d) ‘we are humans’

### View Answer

Answer: a [Reason:] This function returns all the subgroups that have been matched.

6. What is the output of the following?

```sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.group())```

a) (‘we’, ‘are’, ‘humans’)
b) (we, are, humans)
c) (‘we’, ‘humans’)
d) ‘we are humans’

### View Answer

Answer: d [Reason:] This function returns the entire match.

7. What is the output of the following?

```sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.group(2))```

a) ‘are’
b) ‘we’
c) ‘humans’
d) ‘we are humans’

### View Answer

Answer: c [Reason:] This function returns the particular subgroup.

8. What is the output of the following?

```sentence = 'horses are fast'
regex = re.compile('(?P<animal>w+) (?P<verb>w+) (?P<adjective>w+)')
matched = re.search(regex, sentence)
print(matched.groupdict())```

a) {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
b) (‘horses’, ‘are’, ‘fast’)
c) ‘horses are fast’
d) ‘are’

### View Answer

Answer: a [Reason:] This function returns a dictionary that contains all the mathches.

9. What is the output of the following?

```sentence = 'horses are fast'
regex = re.compile('(?P<animal>w+) (?P<verb>w+) (?P<adjective>w+)')
matched = re.search(regex, sentence)
print(matched.groups())```

a) {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
b) (‘horses’, ‘are’, ‘fast’)
c) ‘horses are fast’
d) ‘are’

### View Answer

Answer: b [Reason:] This function returns all the subgroups that have been matched.

10. What is the output of the following?

```sentence = 'horses are fast'
regex = re.compile('(?P<animal>w+) (?P<verb>w+) (?P<adjective>w+)')
matched = re.search(regex, sentence)
print(matched.group(2))```

a) {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
b) (‘horses’, ‘are’, ‘fast’)
c) ‘horses are fast’
d) ‘are’

### View Answer

Answer: d [Reason:] This function returns the particular subgroup.

## Python MCQ Set 5

1. The character Dot (that is, ‘.’) in the default mode, matches any character other than _____________
a) caret
b) ampersand
c) percentage symbol
d) newline

### View Answer

Answer: d [Reason:] The character Dot (that is, ‘,’) in the default mode, matches any character other than newline. If DOTALL flag is used, then it matches any character other than newline.

2. The expression a{5} will match _____________ characters with the previous regular expression.
a) 5 or less
b) exactly 5
c) 5 or more
d) exactly 4

### View Answer

Answer: b [Reason:] The character {m} is used to match exactly m characters to the previous regular expression. Hence the expression a{5} will match exactly 5 characters and not less than that.

3. ________ matches the start of the string.
________ matches the end of the string.
a) ‘^’, ‘\$’
b) ‘\$’, ‘^’
c) ‘\$’, ‘?’
d) ‘?’, ‘^’

### View Answer

Answer: a [Reason:] ‘^’ (carat) matches the start of the string. ‘\$’ (dollar sign) matches the end of the string.

4. Which of the following will result in an error?
a) >>> p = re.compile(“d”)
>>> p.search(“door”)
b) >>> p = re.escape(‘hello’)
c) >>> p = re.subn()
d) >>> p = re.purge()

### View Answer

Answer: c [Reason:] The function re.subn() will result in an error. This is because subn() requires 3 positional arguments while we have entered none.

5. What is the output of the line of code shown below?

`re.split('W+', 'Hello, hello, hello.')`

a) [‘Hello’, ‘hello’, ‘hello.’]
b) [‘Hello, ‘hello’, ‘hello’]
c) [‘Hello’, ‘hello’, ‘hello’, ‘.’]
d) [‘Hello’, ‘hello’, ‘hello’, ”]

### View Answer

Answer: d [Reason:] In the code shown above, the function split() splits the string based on the pattern given as an argument in the parenthesis. Note: split will never split a string on an empty pattern match. Hence the output of this code is: [‘Hello’, ‘hello’, ‘hello’, ”].

6. What is the output of the following function?

`re.findall("hello world", "hello", 1)`

a) [“hello”]
b) [ ]
c) hello
d) hello world

### View Answer

Answer: b [Reason:] The function findall returns the word matched if and only if both the pattern and the string match completely, that is, they are exactly the same. Observe the example shown below: >>> re.findall(“hello”, “hello”, 1) The output is: [‘hello’] Hence the output of the code shown in this question is [].

7. Choose the function whose output can be: <_sre.SRE_Match object; span=(4, 8), match='aaaa'>.
a) >>> re.search(‘aaaa’, “alohaaaa”, 0)
b) >>> re.match(‘aaaa’, “alohaaaa”, 0)
c) >>> re.match(‘aaa’, “alohaaa”, 0)
d) >>> re.search(‘aaa’, “alohaaa”, 0)

### View Answer

Answer: a [Reason:] The output shown above is that of a search function, whose pattern is ‘aaaa’ and the string is that of 8 characters. The only option which matches all these criteria is: >>> re.search(‘aaaa’, “alohaaaa”, 0)

8. Which of the following functions clears the regular expression cache?
a) re.sub()
b) re.pos()
c) re.purge()
d) re.subn()

### View Answer

Answer: c [Reason:] The function which clears the regular expression cache is re.purge(). Note that this function takes zero positional arguments.

9. What is the output of the code shown?

```import re
re.ASCII```

a) 8
b) 32
c) 64
d) 256

### View Answer

Answer: d [Reason:] The expression re.ASCII returns the total number of ASCII characters that are present, that is 256. This can also be abbreviated as re.A, which results in the same output (that is, 256).

10. Which of the following functions results in case in-sensitive matching?
a) re.A
b) re.U
c) re.I
d) re.X

### View Answer

Answer: c [Reason:] The function re.I (that is, re.IGNORECASE) results in case-insensitive matching. That is, expressions such as [A-Z] will match lowercase characters too.

### Synopsis and Project Report

You can buy synopsis and project from distpub.com. Just visit https://distpub.com/product-category/projects/ and buy your university/institute project from distpub.com .woocommerce-message { background-color: #98C391 !important; }