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 body is empty, indicated by the "pass" statement. Therefore, the function does nothing when called.
2.
All keywords in Python are in:
Correct Answer
D. None
Explanation
In Python, keywords are predefined reserved words that cannot be used as identifiers (variable names, function names, etc.). These keywords are written in lowercase, and there are no keywords that are written in uppercase or capitalized. Therefore, the correct answer is "none."
3.
What gets printed?
x = 4.5
y = 2
print x//y
Correct Answer
A. 2.0
Explanation
The given code snippet calculates the floor division of x by y and prints the result. Floor division is a division operation that rounds the quotient down to the nearest integer. In this case, x is 4.5 and y is 2. When we perform floor division (//) on 4.5 and 2, the result is 2.0 because 4.5 divided by 2 is 2.25, and floor division rounds it down to the nearest integer which is 2. Therefore, the output of the code is 2.0.
4.
What is the output of the below program?
a = [1,2,3,None,(),[],]
print len(a)
Correct Answer
D. 6
Explanation
The program creates a list "a" with 6 elements: 1, 2, 3, None, (), and []. The "len(a)" function returns the number of elements in the list, which is 6.
5.
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 correct answer is "yes" because the condition in the if statement is evaluating the logical operators in a specific order. Since the "and" operator has higher precedence than the "or" operator, it will be evaluated first. In this case, y and z both evaluate to False, so the expression y and z is False. Then, the x or False expression evaluates to True. Therefore, the if statement condition is True, and the code will print "yes".
6.
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 are the PYTHONPATH directory, the current directory, and the installation dependent default path. This means that when importing modules, Python will first look in the directory specified by the PYTHONPATH variable, then in the current directory, and finally in the default path that is determined by the Python installation.
7.
In python 2.6 or earlier, the code will print error type 1 if access secure system 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
In Python 2.6 or earlier, the code will not print "error type 1" if accessSecureSystem() raises an exception of either AccessError type or SecurityError type. The correct syntax to catch multiple exceptions in Python is to enclose them in parentheses and separate them with commas. So, the correct code would be:
try:
accessSecureSystem()
except (AccessError, SecurityError):
print "error type 1"
continueWork()
Since the exceptions are not caught correctly in the given code, the correct answer is False.
8.
What gets printed?
print r"\nwoow"
Correct Answer
C. The text like exactly like this: \nwoow
Explanation
The given answer is incorrect. The correct answer is "the text like exactly like this: woow". This is because the string "r"woow"" is printed exactly as it is, including the characters "r", "", and "woow".
9.
What gets printed?
print "\x48\x49!"
Correct Answer
E. HI!
Explanation
The given code prints "HI!" because "\x48" represents the ASCII value for the letter 'H' and "\x49" represents the ASCII value for the letter 'I'. Therefore, when these values are printed together, it forms the string "HI!".
10.
What gets printed?
class parent:
def __init__(self, param):
self.v1 = param
class child(parent):
def __init__(self, param):
self.v2 = param
obj = child(11)
print "%d %d" % (obj.v1, obj.v2)
Correct Answer
C. Error is generated by program
Explanation
The given code will generate an error because the child class does not call the constructor of the parent class. As a result, the parent class's attributes, including v1, are not initialized. Therefore, when trying to print obj.v1, it will result in an error.
11.
What sequence of numbers is printed?
values = [2, 3, 2, 4]
def my_transformation(num):
return num ** 2
for i in map(my_transformation, values):
print i
Correct Answer
B. 4 9 4 16
Explanation
The given code defines a function called my_transformation that squares a given number. The map function is then used to apply this transformation to each element in the values list. The resulting sequence of numbers that is printed is the squared values of the original list, which are 4, 9, 4, and 16.
12.
What does the code below do?
sys.path.append('/root/mods')
Correct Answer
D. Adds a new directory to seach for python modules that are imported
Explanation
The code `sys.path.append('/root/mods')` adds a new directory to search for Python modules that are imported.
13.
Assuming the filename for the code below is /usr/lib/python/person.py
and the program is run as:
python /usr/lib/python/person.py
What gets printed?
class Person:
def __init__(self):
pass
def getAge(self):
print __name__
p = Person()
p.getAge()
Correct Answer
A. __main__
Explanation
When the program is run, it creates an instance of the Person class and calls the getAge method on that instance. Inside the getAge method, it prints the value of __name__. In this case, since the program is being run directly as the main module, the value of __name__ is "__main__". Therefore, when the program is run, "__main__" gets printed.
14.
Which of the following data structures can be used with the "in" operator to check if an item is in the data structure?
Correct Answer
D. All of the above
Explanation
The "in" operator can be used with all of the given data structures (list, set, and dictionary) to check if an item is present in them. In a list, the "in" operator checks if the item is one of the elements in the list. In a set, it checks if the item is one of the values in the set. In a dictionary, it checks if the item is one of the keys in the dictionary. Therefore, all of the given data structures can be used with the "in" operator to perform the desired check.
15.
What gets printed?
class A:
def __init__(self, a, b, c):
self.x = a + b + c
a = A(1,2,3)
b = getattr(a, 'x')
setattr(a, 'x', b+1)
print a.x
Correct Answer
D. 7
Explanation
The code defines a class A with an initializer that takes three arguments and assigns their sum to the instance variable x. Then, an instance of class A is created with arguments 1, 2, and 3. The variable b is assigned the value of the attribute x of the instance a. Then, the attribute x of the instance a is set to the value of b plus 1. Finally, the value of the attribute x of the instance a is printed, which is 7.
16.
Which of the following statements is NOT true about Python?
Correct Answer
A. Python's syntax is much like pHP
Explanation
Python's syntax is not much like PHP. Python has its own unique syntax and is known for its simplicity and readability. While both Python and PHP are popular programming languages, they have distinct syntax and are used for different purposes.
17.
If you have a variable "example", how do you check to see what type of variable you are working with?
Correct Answer
C. type(example)
Explanation
To check the type of variable "example", the correct way is to use the "type()" function. The "type(example)" statement will return the type of the variable "example".
18.
If you had a statement like, "f = open("test.txt","w")", what would happen to the file as soon as that statement is executed?
Correct Answer
B. The file's contents will be erased
Explanation
When the statement "f = open("test.txt","w")" is executed, it opens the file "test.txt" in write mode. In write mode, if the file already exists, it will be truncated, meaning its contents will be erased. Therefore, the correct answer is that the file's contents will be erased.
19.
Given a function that does not return any value, What value is thrown by it by default when executed in a shell.
Correct Answer
D. None
Explanation
When a function does not return any value, it is considered to have a return type of "void". In programming languages like C, C++, and Java, "void" is used to indicate that a function does not return any value. However, in a shell environment, when a function with a void return type is executed, it does not throw any specific value. Therefore, the default value thrown by a void function when executed in a shell is "None".
20.
Which of the following will run without errors?
Correct Answer(s)
A. Round(45.8)
B. Round(6352.894,2)
Explanation
The first option, round(45.8), will run without errors because it is using the round() function with only one argument, which is the number to be rounded. The second option, round(6352.894,2), will also run without errors because it is using the round() function with two arguments, the number to be rounded and the number of decimal places to round to.
21.
Which of the following results in a SyntaxError?
Correct Answer(s)
B. '3\'
D. "He said, "Yes!""
Explanation
The given answer '3\',"He said, "Yes!""' results in a SyntaxError because it contains multiple quotation marks within the string without escaping them properly. In Python, when using quotation marks within a string, they need to be escaped by using a backslash (\) before each quotation mark. In this case, the quotation marks around "Yes!" are not escaped, causing a syntax error. Additionally, the backslash before the comma (,) is not necessary and also contributes to the syntax error.
22.
Is the following code valid?
try:
# Do something
except:
# Do something
finally:
# Do something
Correct Answer
B. no, finally cannot be used with except
Explanation
The given code is not valid because the "finally" block cannot be used together with the "except" block. The "finally" block is used to specify code that will be executed regardless of whether an exception occurs or not. On the other hand, the "except" block is used to handle specific exceptions that may occur within the try block. These two blocks cannot be used together in the same try-except statement.
23.
How many except statements can a try-except block have?
Correct Answer
A. More than zero
Explanation
A try-except block can have more than zero except statements. The purpose of a try-except block is to handle exceptions that may occur within the try block. Each except statement specifies the type of exception it can handle. By having multiple except statements, we can handle different types of exceptions separately and provide appropriate error handling for each. This allows for more robust and specific exception handling within the try-except block.
24.
Can one block of except statements handle multiple exception?
Correct Answer
A. Yes, like except TypeError, SyntaxError [,…]
Explanation
Yes, one block of except statements can handle multiple exceptions by listing them within the parentheses after the "except" keyword. In this case, the exceptions being handled are TypeError and SyntaxError. This means that if either of these exceptions occur in the code, the block of code within the except statement will be executed.
25.
Which of the following will print True?a = foo(2)
b = foo(3)
print(a < b)
Correct Answer
C. Class foo:
def __init__(self, x):
self.x = x
def __lt__(self, other):
if self.x < other.x:
return True
else:
return False
Explanation
The correct answer is the class foo with the __lt__ method that returns True if self.x is less than other.x, and False otherwise. This is because the expression a < b will call the __lt__ method of the foo class for the objects a and b, and the method will compare their x attributes and return the appropriate boolean value.
26.
Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?
Correct Answer
A. __add__(), __str__()
Explanation
When print(A + B) is executed, the __add__() function is called to perform the addition of objects A and B. Then, the __str__() function is called to convert the result of the addition into a string representation, which is then printed.
27.
What is returned by math.expm1(p)?
Correct Answer
A. (math.e ** p) – 1
Explanation
The expression math.expm1(p) returns the value of e raised to the power of p, subtracted by 1. This is equivalent to (math.e ** p) - 1.
28.
What is output of print(math.pow(3, 2))?
Correct Answer
B. 9.0
Explanation
The output of the given code is 9.0. The math.pow() function in Python is used to calculate the power of a number. In this case, it calculates 3 raised to the power of 2, which is 9. The output is a floating-point number because the math.pow() function always returns a float value.
29.
What is the difference between r+ and w+ modes?
Correct Answer
B. In r+ the pointer is initially placed at the beginning of the file and the pointer is at the end for w+
Explanation
The difference between r+ and w+ modes is that in r+ mode, the pointer is initially placed at the beginning of the file, allowing both reading and writing operations. On the other hand, in w+ mode, the pointer is also initially placed at the beginning of the file, but it is set to the end of the file as soon as any write operation is performed. Therefore, in w+ mode, only writing operations are allowed.
30.
How do you get the current position within the file?
Correct Answer
B. Fp.tell()
Explanation
The correct answer is fp.tell(). The fp.tell() function is used to get the current position within the file. It returns an integer representing the current position in the file, which can be used for various file operations such as reading or writing data at a specific position. This function is commonly used in file handling operations to keep track of the file pointer's position.
31.
What happens if no arguments are passed to the seek function?
Correct Answer
D. Error
Explanation
If no arguments are passed to the seek function, an error occurs. This is because the seek function requires at least one argument to specify the position in the file where the file pointer should be moved. Without any arguments, the function does not know where to move the file pointer, resulting in an error.
32.
Which of the following returns a string that represents the present working directory?
Correct Answer
A. Os.getcwd()
Explanation
The correct answer is os.getcwd(). This function is used in the os module in Python to get the current working directory as a string. It returns a string that represents the present working directory. The other options, os.cwd(), os.getpwd(), and os.pwd(), do not exist in the os module and therefore are not valid functions to use for getting the current working directory.
33.
Python supports the creation of anonymous functions at runtime, using a construct called __________
Correct Answer
A. Lambda
Explanation
Python supports the creation of anonymous functions at runtime using a construct called "Lambda". Lambda functions are small, one-line functions that do not have a name and can be used wherever function objects are required. They are commonly used in scenarios where a small function is needed for a short period of time and does not need to be defined separately.
34.
What is the output of this program?
y = 6
z = lambda x: x * y
print z(8)
Correct Answer
A. 48
Explanation
The program defines a lambda function called "z" that takes an argument "x" and multiplies it by the value of "y", which is 6. The program then calls the lambda function with an argument of 8, resulting in 8 * 6 = 48. Therefore, the output of the program is 48.
35.
What is the output of below program? def writer(): title = 'Sir' name = (lambda x:title + ' ' + x) return name who = writer()who('Arthur')
Correct Answer
B. Sir Arthur
Explanation
The program defines a function named "writer" which returns a lambda function. This lambda function takes an argument "x" and concatenates it with the variable "title" (which is set to "Sir"). The function "writer" is then called and assigned to the variable "who". Finally, the lambda function stored in "who" is called with the argument "Arthur". Therefore, the output of the program is "Sir Arthur".
36.
What is the output of the below program? def C2F(c): return c * 9/5 + 32print C2F(100)print C2F(0)
Correct Answer
A. 212
32
Explanation
The given program defines a function named C2F that converts a temperature in Celsius to Fahrenheit. The function takes a parameter c, which represents the temperature in Celsius. It then applies the formula c * 9/5 + 32 to convert the temperature to Fahrenheit.
In the first print statement, the function is called with an argument of 100, so it will calculate 100 * 9/5 + 32, which equals 212. Therefore, the output of the first print statement is 212.
In the second print statement, the function is called with an argument of 0, so it will calculate 0 * 9/5 + 32, which equals 32. Therefore, the output of the second print statement is 32.
37.
What is the output of the below program?
def power(x, y=2):
r = 1
for i in range(y):
r = r * x
return r
print power(3)
print power(3, 3)
Correct Answer
B. 9
27
Explanation
The program defines a function called "power" that takes two parameters, "x" and "y". The default value for "y" is 2. Inside the function, it initializes a variable "r" to 1. It then enters a loop that iterates "y" times. In each iteration, it multiplies "r" by "x". Finally, it returns the value of "r".
In the first print statement, the function is called with the argument 3. Since the default value of "y" is 2, the function calculates 3^2 which is 9.
In the second print statement, the function is called with the arguments 3 and 3. The function calculates 3^3 which is 27.
Therefore, the output of the program is 9 and 27.
38.
What is the order of precedence in python?
i) Parentheses
ii) Exponential
iii) Division
iv) Multiplication
v) Addition
vi) Subtraction
Correct Answer
E. I,ii,iv,iii,v,vi
Explanation
The order of precedence in Python determines the sequence in which different operators are evaluated in an expression. In this case, the correct answer is "i,ii,iv,iii,v,vi". This means that parentheses have the highest precedence, followed by exponential operations, then multiplication and division, and finally addition and subtraction. This order ensures that calculations are performed correctly according to the rules of mathematics.
39.
Operators with the same precedence are evaluated in which manner?
Correct Answer
A. Left to Right
Explanation
Operators with the same precedence are evaluated from left to right. This means that when there are multiple operators with the same level of precedence in an expression, they are evaluated in the order they appear from left to right. This ensures that the mathematical operations are performed in a consistent and predictable manner.
40.
Which of the following is incorrect?
Correct Answer
D. X = 03964
Explanation
The given statement x = 03964 is incorrect because the leading zero indicates an octal number in Python. However, the octal number system only uses digits 0-7. Therefore, the digit 9 is not valid in octal, making the statement incorrect.