Select Page
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 does the function math.frexp(x) return?
a) a tuple containing of the mantissa and the exponent of x
b) a list containing of the mantissa and the exponent of x
c) a tuple containing of the mantissa of x
d) a list containing of the exponent of x

Answer: a [Reason:] It returns a tuple with two elements. The first element is the mantissa and the second element is the exponent.

2. What is the result of math.fsum([.1 for i in range(20)])?
a) 2.0
b) 20
c) 2
d) 2.0000000000000004

Answer: a [Reason:] The function fsum returns an accurate floating point sum of the elements of its argument.

3. What is the result of sum([.1 for i in range(20)])?
a) 2.0
b) 20
c) 2
d) 2.0000000000000004

Answer: d [Reason:] There is some loss of accuracy when we use sum with floating point numbers. Hence the function fsum is preferable.

4. What is returned by math.isfinite(float(‘inf’))?
a) True
b) False
c) None
d) error

Answer: b [Reason:] float(‘inf’) is not a finite number.

5. What is returned by math.isfinite(float(‘nan’))?
a) True
b) False
c) None
d) error

Answer: b [Reason:] float(‘nan’) is not a finite number.

6. What is x if x = math.isfinite(float(‘0.0’))?
a) True
b) False
c) None
d) error

Answer: a [Reason:] float(‘0.0’) is a finite number.

7. What is the result of the following?

`>>> -float('inf') + float('inf')`

a) inf
b) nan
c) 0
d) 0.0

Answer: b [Reason:] The result of float(‘inf’)-float(‘inf’) is undefined.

8. What is the output of the following?

`print(math.isinf(float('-inf')))`

a) error, the minus sign shouldn’t havve been inside the brackets
b) error, there is no function called isinf
c) True
d) False

Answer: c [Reason:] -float(‘inf’) is the same as float(‘-inf’).

9. What is the value of x if x = math.ldexp(0.5, 1)?
a) 1
b) 2.0
c) 0.5
d) none of the mentioned

Answer: d [Reason:] The value returned by ldexp(x, y) is x * (2 ** y). In the current case x is 1.0.

10. What is returned by math.modf(1.0)?
a) (0.0, 1.0)
b) (1.0, 0.0)
c) (0.5, 1)
d) (0.5, 1.0)

Answer: a [Reason:] The first element is the fractional part and the second element is the integral part of the argument.

## Python MCQ Set 2

1. What does os.name contain?
a) the name of the operating system dependent module imported
b) the address of the module os
c) error, it should’ve been os.name()
d) none of the mentioned

Answer: a [Reason:] It contains the name of the operating system dependent module imported such as ‘posix’, ‘java’ etc.

2. What does print(os.geteuid()) print?
a) the group id of the current process
b) the user id of the current process
c) both the group id and the user of the current process
d) none of the mentioned

Answer: b [Reason:] os.geteuid() gives the user id while the os.getegid() gives the group id.

a) name of the current user logged in
b) name of the superuser
c) gets a form to login as a different user
d) all of the above

Answer: a [Reason:] It returns the name of the user who is currently logged in and is running the script.

4. What does os.close(f) do?
a) terminate the process f
b) terminate the process f if f is not responding
c) close the file descriptor f
d) return an integer telling how close the file pointer is to the end of file

Answer: c [Reason:] When a file descriptor is passed as an argument to os.close() it will be closed.

5. What does os.fchmod(fd, mode) do?
a) change permission bits of the file
b) change permission bits of the directory
c) change permission bits of either the file or the directory
d) none of the mentioned

Answer: a [Reason:] The arguments to the function are a file descriptor and the new mode.

6. Which of the following functions can be used to read data from a file using a file descriptor?
d) os.scan()

Answer: b [Reason:] None of the other functions exist.

7. Which of the following returns a string that represents the present working directory?
a) os.getcwd()
b) os.cwd()
c) os.getpwd()
d) os.pwd()

Answer: a [Reason:] The function getcwd() (get current working directory) returns a string that represents th present working directory.

d) none of the mentioned

9. Which of the following can be used to create a directory?
a) os.mkdir()
b) os.creat_dir()
c) os.create_dir()
d) os.make_dir()

Answer: a [Reason:] The function mkdir() creates a directory in the path specified.

10. Which of the following can be used to create a symbolic link?
c) os.symblin()
d) os.ln()

Answer: a [Reason:] It is the function that allows you to create a symbolic link.

## Python MCQ Set 3

1. What is the output of the following?

```elements = [0, 1, 2]
def incr(x):
return x+1
print(list(map(elements, incr)))```

a) [1, 2, 3].
b) [0, 1, 2].
c) error
d) none of the mentioned

Answer: c [Reason:] The list should be the second parameter to the mapping function.

2. What is the output of the following?

```elements = [0, 1, 2]
def incr(x):
return x+1
print(list(map(incr, elements)))```

a) [1, 2, 3].
b) [0, 1, 2].
c) error
d) none of the mentioned

Answer: a [Reason:] Each element of the list is incremented.

3. What is the output of the following?

```x = ['ab', 'cd']
print(list(map(upper, x)))```

a) [‘AB’, ‘CD’].
b) [‘ab’, ‘cd’].
c) error
d) none of the mentioned

Answer: c [Reason:] A NameError occurs because upper is a class method.

4. What is the output of the following?

```def to_upper(k):
return k.upper()
x = ['ab', 'cd']
print(list(map(upper, x)))```

a) [‘AB’, ‘CD’].
b) [‘ab’, ‘cd’].
c) none of the mentioned
d) error

Answer: d [Reason:] A NameError occurs because upper is a class method.

5. What is the output of the following?

```def to_upper(k):
return k.upper()
x = ['ab', 'cd']
print(list(map(to_upper, x)))```

a) [‘AB’, ‘CD’].
b) [‘ab’, ‘cd’].
c) none of the mentioned
d) error

Answer: a [Reason:] Each element of the list is converted to uppercase.

6. What is the output of the following?

```def to_upper(k):
k.upper()
x = ['ab', 'cd']
print(list(map(to_upper, x)))```

a) [‘AB’, ‘CD’].
b) [‘ab’, ‘cd’].
c) none of the mentioned
d) error

Answer: c [Reason:] A list of Nones is printed as to_upper() returns None.

7. What is the output of the following?

```x = ['ab', 'cd']
print(map(len, x))```

a) [‘ab’, ‘cd’].
b) [2, 2].
c) [‘2’, ‘2’].
d) none of the mentioned

Answer: d [Reason:] A map object is generated by map(). We must convert this to a list to be able to print it in a human readable form.

8. What is the output of the following?

```x = ['ab', 'cd']
print(list(map(len, x)))```

a) [‘ab’, ‘cd’].
b) [2, 2].
c) [‘2’, ‘2’].
d) none of the mentioned

Answer: b [Reason:] The length of each string is 2.

9. What is the output of the following?

```x = ['ab', 'cd']
print(len(map(list, x)))```

a) [2, 2].
b) 2
c) 4
d) none of the mentioned

Answer: d [Reason:] A TypeError occurs as map has no len().

10. What is the output of the following?

```x = ['ab', 'cd']
print(len(list(map(list, x))))```

a) 2
b) 4
c) error
d) none of the mentioned

Answer: a [Reason:] The outer list has two lists in it. So it’s length is 2.

## Python MCQ Set 4

1. Which function is called when the following code is executed?

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

a) format()
b) __format__()
c) str()
d) __str__()

Answer: d [Reason:] Both str(f) and format(f) call f.__str__().

2. Which of the following will print True?

```a = foo(2)
b = foo(3)
print(a < b)```

a)

```class foo:
def __init__(self, x):
self.x = x
def __lt__(self, other):
if self.x < other.x:
return False
else:
return True```

b)

```class foo:
def __init__(self, x):
self.x = x
def __less__(self, other):
if self.x > other.x:
return False
else:
return True```

c)

```class foo:
def __init__(self, x):
self.x = x
def __lt__(self, other):
if self.x < other.x:
return True
else:
return False```

d)

```class foo:
def __init__(self, x):
self.x = x
def __less__(self, other):
if self.x < other.x:
return False
else:
return True```

3. Which function overloads the + operator?
b) __plus__()
c) __sum__()
d) none of the mentioned

4. Which operator is overloaded by __invert__()?
a) !
b) ~
c) ^
d) –

5. Which function overloads the == operator?
a) __eq__()
b) __equ__()
c) __isequal__()
d) none of the mentioned

Answer: a [Reason:] The other two do not exist.

6. Which operator is overloaded by __lg__()?
a) <
b) >
c) !=
d) none of the mentioned

Answer: d [Reason:] __lg__() is invalid.

7. Which function overloads the >> operator?
a) __more__()
b) __gt__()
c) __ge__()
d) none of the mentioned

8. Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?
c) __sum__(), __str__()
d) __str__(), __sum__()

Answer: a [Reason:] The function __add__() is called first since it is within the bracket. The function __str__() is then called on the object that we recieved after adding A and B.

9. Which operator is overloaded by the __or__() function?
a) ||
b) |
c) //
d) /

Answer: b [Reason:] The function __or__() overloads the bitwise OR operator |.

10. Which function overloads the // operator?
a) __div__()
b) __ceildiv__()
c) __floordiv__()
d) __truediv__()

Answer: c [Reason:] __floordiv__() is for //.

## Python MCQ Set 5

1. What is the output of the following?

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

a) (‘[0] [1]’,)
b) (’01’,)
c) [0] [1].
d) 01

Answer: c [Reason:] (element) is the same as element. It is not a tuple with one item.

2. What is the output of the following?

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

a) (‘[0] [1]’,)
b) (’01’)
c) [0] [1].
d) 01

Answer: a [Reason:] (element,) is not the same as element. It is a tuple with one item.

3. What is the output of the following?

```x = [34, 56]
print((''.join(list(map(str, x))),))```

a) 3456
b) (3456)
c) (‘3456’)
d) (‘3456’,)

Answer: d [Reason:] We have created a tuple with one string in it.

4. What is the output of the following?

```x = [34, 56]
print((''.join(list(map(str, x)))),)```

a) 3456
b) (3456)
c) (‘3456’)
d) (‘3456’,)

Answer: a [Reason:] We have just created a string.

5. What is the output of the following?

```x = [34, 56]
print(len(map(str, x)))```

a) [34, 56].
b) [’34’, ’56’].
c) 34 56
d) error

Answer: d [Reason:] TypeError, map has no len.

6. What is the output of the following?

```x = 'abcd'
print(list(map(list, x)))```

a) [‘a’, ‘b’, ‘c’, ‘d’].
b) [‘abcd’].
c) [[‘a’], [‘b’], [‘c’], [‘d’]].
d) none of the mentioned

Answer: c [Reason:] list() is performed on each character in x.

7. What is the output of the following?

```x = abcd
print(list(map(list, x)))```

a) [‘a’, ‘b’, ‘c’, ‘d’].
b) [‘abcd’].
c) [[‘a’], [‘b’], [‘c’], [‘d’]].
d) none of the mentioned

Answer: d [Reason:] NameError, we have not defined abcd.

8. What is the output of the following?

```x = 1234
print(list(map(list, x)))```

a) [1, 2, 3, 4].
b) [1234].
c) [[1], [2], [3], [4]].
d) none of the mentioned

Answer: d [Reason:] TypeError, int is not iterable.

9. What is the output of the following?

```x = 1234
print(list(map(list, [x])))```

a) [1, 2, 3, 4].
b) [1234].
c) [[1], [2], [3], [4]].
d) none of the mentioned

Answer: d [Reason:] TypeError, int is not iterable.

10. What is the output of the following?

```x = 'abcd'
print(list(map([], x)))```

a) [‘a’, ‘b’, ‘c’, ‘d’].
b) [‘abcd’].
c) [[‘a’], [‘b’], [‘c’], [‘d’]].
d) none of the mentioned

Answer: d [Reason:] TypeError, list object is not callable.

11. Is Python code compiled or interpreted?
a) Python code is only compiled
b) Python code is both compiled and interpreted
c) Python code is only interpreted
d) Python code is neither compiled nor interpreted

Answer: b [Reason:] Many languages have been implemented using both compilers and interpreters, including C, Pascal, and Python.

12. Which of these is the definition for packages in Python?
a) A folder of python modules
b) A set of programs making use of Python modules
c) A set of main modules
d) A number of files containing Python definitions and statements

Answer: a [Reason:] A folder of python programs is called as a package of modules.

13. Which of these is false about a package?
a) A package can have subfolders and modules
b) Each import package need not introduce a namespace
c) import folder.subfolder.mod1 imports packages
d) from folder.subfolder.mod1 import objects imports packages