Special method __add()__ is called when + operator is used.
In Python, same operator may behave differently depending upon operands.
__init__() function is called when a new object is instantiated.
All of the above
Answer: 4 Explanation: All of the given three options are correct.
Q2. In Python, which of the following statement will print a random number between 1 and 10 (both 1 and 10 included)?
print(random.randrange(1, 10))
print(random.random(1, 10))
print(random.randint(1, 10))
None of the above.
Answer: 3 Explanation: 1. print(random.randrange(1, 10)) -> This will print a random integer between 1 and 10 where 10 is not included. 2. print(random.random(1, 10)) -> This will throw an error because random does not take any argument but two are given. 3. print(random.randint(1, 10)) -> This will print a random number between 1 and 10 (both 1 and 10 included).
Q3. Which of the following is not true of global variable in Python?
global keyword can be used to create a global variable inside a local scope.
Accessible only in the block they are defined in.
A variable created in the main body of the Python code is a global variable.
Accessible from anywhere in the script.
Answer: 2 Explanation: Global variables are available from within any scope, either it is global or local.
Q4. If x = 2**4/2+3-1, then what will be the data type of x?
int
float
Answer: 2 Explanation: Division operator is used in x = 2**4/2+3-1 and division operator gives the value including the decimals which will be of float data type.
Q5. Which of the following method can be used to combine strings and numbers?
concatenate()
join()
append()
format()
Answer: 4 Explanation: The format() method format the passed arguments and places them in the string according to the placeholders {}. Fancier Output Formatting
Q6. Suppose x = 6.3, then which of the following statement will throw an error?
y = float(x)
y = int(x)
y = complex(x)
None of the above.
Answer: 4 Explanation: int, float and complex are numeric data types. These will cast ‘x’ into it’s respective data types.
Q7. What will be the output of print(ord(‘the’))?
116 104 101
116 116 116
104 104 104
TypeError
Answer: 4 Explanation: Python ord() function returns the Unicode code from a given character. This function accepts a string of unit length as an argument and returns the Unicode equivalence of the passed argument. Reference: ord() function in Python
Q8. In Python, which of the following character is used to add at the start of a line to make it a comment?
#
“
//
&
Answer: 1 Explanation: In Python language comments start with # and the rest of the line will be rendered as comment.