Python Programming Bits
1. How many except
statements can a try-except block have?
a) zero
b) one
c) more than one
d) more than zero
2. When will the else part of try-except-else be executed?
a) always
b) when an exception occurs
c) when no exception occurs
d) when an exception occurs in to except block
3. Is the following code valid?
a) zero
b) one
c) more than one
d) more than zero
2. When will the else part of try-except-else be executed?
a) always
b) when an exception occurs
c) when no exception occurs
d) when an exception occurs in to except block
3. Is the following code valid?
try:
# Do
something
except:
# Do
something
finally:
# Do
something
a) no, there is no
such thing as finally
b) no, finally cannot be used with except
c) no, finally must come before except
d) yes
4. Is the following code valid?
b) no, finally cannot be used with except
c) no, finally must come before except
d) yes
4. Is the following code valid?
try:
# Do
something
except:
# Do
something
else:
# Do
something
a) no, there is no
such thing as else
b) no, else cannot be used with except
c) no, else must come before except
d) yes
5. Can one block of except statements handle multiple exception?
a) yes, like except TypeError, SyntaxError [,…] b) yes, like except [TypeError, SyntaxError] c) no
d) none of the mentioned
6. What is the output of the following?
b) no, else cannot be used with except
c) no, else must come before except
d) yes
5. Can one block of except statements handle multiple exception?
a) yes, like except TypeError, SyntaxError [,…] b) yes, like except [TypeError, SyntaxError] c) no
d) none of the mentioned
6. What is the output of the following?
x
= ['ab', 'cd']
for i in x:
i.upper()
print(x)
a) [‘ab’, ‘cd’].
b) [‘AB’, ‘CD’].
c) [None, None].
d) none of the mentioned
7. What is the output of the following?
b) [‘AB’, ‘CD’].
c) [None, None].
d) none of the mentioned
7. What is the output of the following?
x
= ['ab', 'cd']
for i in x:
x.append(i.upper())
print(x)
a) [‘AB’, ‘CD’].
b) [‘ab’, ‘cd’, ‘AB’, ‘CD’].
c) [‘ab’, ‘cd’].
d) none of the mentioned
8. What is the output of the following?
b) [‘ab’, ‘cd’, ‘AB’, ‘CD’].
c) [‘ab’, ‘cd’].
d) none of the mentioned
8. What is the output of the following?
i
= 1
while True:
if i%3 == 0:
break
print(i)
i
+ = 1
a) 1 2
b) 1 2 3
c) error
d) none of the mentioned
9. What is the output of the following?
b) 1 2 3
c) error
d) none of the mentioned
9. What is the output of the following?
i
= 1
while True:
if i%0O7 == 0:
break
print(i)
i
+= 1
a) 1 2 3 4 5 6
b) 1 2 3 4 5 6 7
c) error
d) none of the mentioned
View Answer
b) 1 2 3 4 5 6 7
c) error
d) none of the mentioned
View Answer
Answer: a
10. What is the output of the following?
10. What is the output of the following?
i
= 5
while True:
if i%0O11 == 0:
break
print(i)
i
+= 1
a) 5 6 7 8 9 10
b) 5 6 7 8
c) 5 6
d) error
11. What is the type of each element in sys.argv?
a) set
b) list
c) tuple
d) string
12. What is the length of sys.argv?
a) number of arguments
b) number of arguments + 1
c) number of arguments – 1
d) none of the mentioned
13. How are keyword arguments specified in the function heading?
a) one star followed by a valid identifier
b) one underscore followed by a valid identifier
c) two stars followed by a valid identifier
d) two underscores followed by a valid identifier
14. How many keyword arguments can be passed to a function in a single function call?
a) zero
b) one
c) zero or more
d) one or more
15. What is the output of the following code?
b) 5 6 7 8
c) 5 6
d) error
11. What is the type of each element in sys.argv?
a) set
b) list
c) tuple
d) string
12. What is the length of sys.argv?
a) number of arguments
b) number of arguments + 1
c) number of arguments – 1
d) none of the mentioned
13. How are keyword arguments specified in the function heading?
a) one star followed by a valid identifier
b) one underscore followed by a valid identifier
c) two stars followed by a valid identifier
d) two underscores followed by a valid identifier
14. How many keyword arguments can be passed to a function in a single function call?
a) zero
b) one
c) zero or more
d) one or more
15. What is the output of the following code?
def foo(fname, val):
print(fname(val))
foo(max, [1, 2, 3])
foo(min, [1, 2, 3])
a) 3 1
b) 1 3
c) error
d) none of the mentioned
16. What is the output of the following code?
b) 1 3
c) error
d) none of the mentioned
16. What is the output of the following code?
def foo():
return total + 1
total
= 0
print(foo())
a) 0
b) 1
c) error
d) none of the mentioned
17. What is the output of the following code?
b) 1
c) error
d) none of the mentioned
17. What is the output of the following code?
def foo():
total
+= 1
return total
total
= 0
print(foo())
a) 0
b) 1
c) error
d) none of the mentioned
View Answer
b) 1
c) error
d) none of the mentioned
View Answer
18. What is the output
of the following code?
def foo(x):
x
= ['def', 'abc']
return id(x)
q
= ['abc', 'def']
print(id(q) == foo(q))
a) True
b) False
c) None
d) error
19. What is the output of the following code?
b) False
c) None
d) error
19. What is the output of the following code?
def foo(i, x=[]):
x.append(i)
return x
for i in range(3):
print(foo(i))
a) [0] [1] [2] b) [0] [0, 1] [0, 1, 2] c) [1] [2] [3]
d) [1] [1, 2] [1, 2, 3]
20. Which function is
called when the following code is executed?
f
=
foo()
format(f)
a) format()
b) __format__()
c) str()
d) __str__()
21. Which of the following will print True?
b) __format__()
c) str()
d) __str__()
21. 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
22. Which function
overloads the >> operator?
a) __more__()
b) __gt__()
c) __ge__()
d) none of the mentioned
23. Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?
a) __add__(), __str__()
b) __str__(), __add__()
c) __sum__(), __str__()
d) __str__(), __sum__()
24. Which operator is overloaded by the __or__() function?
a) ||
b) |
c) //
d) /
24. Which function overloads the // operator?
a) __div__()
b) __ceildiv__()
c) __floordiv__()
d) __truediv__()
25. 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
26. 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
27. What does os.getlogin() return?
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
28. 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
29. 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
30. Which of the following functions can be used to read data from a file using a file descriptor?
a) os.reader()
b) os.read()
c) os.quick_read()
d) os.scan()
31. 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()
32. What does os.link() do?
a) create a symbolic link
b) create a hard link
c) create a soft link
d) none of the mentioned
33. 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()
34. Which of the following can be used to create a symbolic link?
a) os.symlink()
b) os.symb_link()
c) os.symblin()
d) os.ln()
a) __more__()
b) __gt__()
c) __ge__()
d) none of the mentioned
23. Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?
a) __add__(), __str__()
b) __str__(), __add__()
c) __sum__(), __str__()
d) __str__(), __sum__()
24. Which operator is overloaded by the __or__() function?
a) ||
b) |
c) //
d) /
24. Which function overloads the // operator?
a) __div__()
b) __ceildiv__()
c) __floordiv__()
d) __truediv__()
25. 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
26. 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
27. What does os.getlogin() return?
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
28. 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
29. 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
30. Which of the following functions can be used to read data from a file using a file descriptor?
a) os.reader()
b) os.read()
c) os.quick_read()
d) os.scan()
31. 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()
32. What does os.link() do?
a) create a symbolic link
b) create a hard link
c) create a soft link
d) none of the mentioned
33. 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()
34. Which of the following can be used to create a symbolic link?
a) os.symlink()
b) os.symb_link()
c) os.symblin()
d) os.ln()