1.
What does the following code do?
def a(b, c, d): pass
Correct Answer
B. Defines a function, which does nothing
Explanation
The given code defines a function named "a" with three parameters, "b", "c", and "d". However, the function does not contain any code or instructions, so it essentially does nothing.
2.
What gets printed? Assuming python version 2.x
print type(1/2)
Correct Answer
A.
Explanation
The code will print "". In Python 2.x, the division operator ("/") performs integer division when both operands are integers. So, 1/2 will result in 0. The type() function is used to determine the type of an object, and in this case, it will return the type "int" for the result of the division.
3.
What is the output of the following code?
print type([1,2])
Correct Answer
E.
Explanation
The code will output "". The "type()" function is used to determine the type of an object in Python. In this case, the object is a list containing the elements 1 and 2. The "type()" function returns the type of the object, which is "list" in this case.
4.
What gets printed?
def f(): pass
print type(f())
Correct Answer
C.
Explanation
The code defines a function called "f" that does not have any code inside it. Then, the code calls the function "f" and tries to print the type of its return value. Since the function "f" does not have a return statement, it will return None by default. Therefore, the code will print "".
5.
What should the below code print?
print type(1J)
Correct Answer
A.
Explanation
The code should print "complex" because the function `type()` is used to determine the type of an object, and `1J` represents a complex number in Python.
6.
What is the output of the following code?
print type(lambda:None)
Correct Answer
D.
Explanation
The output of the code is "". The code is using the "type()" function to determine the type of the lambda function. In this case, the lambda function is defined as "lambda:None", which means it takes no arguments and returns None. The "type()" function returns the type of the object passed to it, so it returns "".
7.
What is the output of the below program?
a = [1,2,3,None,(),[],]
print len(a)
Correct Answer
D. 6
Explanation
The given program creates a list 'a' with 6 elements - 1, 2, 3, None, (), and []. The 'len' function is then used to find the length of the list, which is the number of elements in it. Since the list 'a' has 6 elements, the output of the program will be 6.
8.
What gets printed? Assuming python version 3.x
print (type(1/2))
Correct Answer
C.
Explanation
The code will print the type of the result of the division operation, which is a float. In Python 3.x, when dividing two integers, the result will always be a float, regardless of whether the division is exact or not.
9.
What gets printed?
d = lambda p: p * 2
t = lambda p: p * 3
x = 2
x = d(x)
x = t(x)
x = d(x)
print x
Correct Answer
C. 24
Explanation
The code defines two lambda functions, d and t, which multiply their input by 2 and 3 respectively. The variable x is initially set to 2. The code then updates the value of x by passing it to the d function, which doubles it to 4. Next, x is passed to the t function, which triples it to 12. Finally, x is passed to the d function again, resulting in a value of 24. Therefore, the value of x that gets printed is 24.
10.
What gets printed?
x = 4.5
y = 2
print x//y
Correct Answer
A. 2.0
Explanation
The code snippet is using the floor division operator "//" to divide the value of x (4.5) by the value of y (2). Floor division returns the largest integer less than or equal to the result of the division. In this case, 4.5 divided by 2 equals 2.25, and the largest integer less than or equal to 2.25 is 2. Therefore, the code will print 2.0.
11.
What gets printed?
nums = set([1,1,2,3,3,3,4])
print len(nums)
Correct Answer
C. 4
Explanation
The code creates a set called "nums" with the values [1, 2, 3, 4]. Since sets only store unique values, any duplicates are automatically removed. The len() function is then used to find the length of the set, which is 4. Therefore, the code will print 4.
12.
What gets printed?
x = True
y = False
z = False
if x or y and z:
print "yes"
else:
print "no"
Correct Answer
A. Yes
Explanation
The code will print "yes" because the condition in the if statement is x or (y and z). Since x is True, the entire condition evaluates to True and the code inside the if block is executed.
13.
What gets printed?
x = True
y = False
z = False
if not x or y:
print 1
elif not x or not y and z:
print 2
elif not x or y or not y and x:
print 3
else:
print 4
Correct Answer
C. 3
Explanation
The correct answer is 3 because the condition "not x or y or not y and x" evaluates to True. This is because the "not x" part is True, the "y" part is True, and the "not y and x" part is also True. Therefore, the code block under this condition will be executed, which is to print 3.
14.
If PYTHONPATH is set in the environment, which directories are searched for modules?
A) PYTHONPATH directory
B) current directory
C) home directory
D) installation dependent default path
Correct Answer
D. A, B, and D
Explanation
When PYTHONPATH is set in the environment, the directories that are searched for modules include the PYTHONPATH directory, the current directory, and the installation dependent default path. This means that modules can be located in any of these directories and will be accessible when PYTHONPATH is set.
15.
In python 2.6 or earlier, the code will print error type 1 if accessSecureSystem raises an exception of either AccessError type or SecurityError type
try:
accessSecureSystem()
except AccessError, SecurityError:
print "error type 1"
continueWork()
Correct Answer
B. False
Explanation
The given code will not print "error type 1" if accessSecureSystem raises an exception of either AccessError type or SecurityError type. This is because the except statement is not written correctly. In Python 2.6 or earlier versions, multiple exceptions should be enclosed in parentheses and separated by commas. Therefore, the correct syntax should be except (AccessError, SecurityError):. Since the except statement is not written correctly, the code will raise a syntax error.