Q1. if x = [1, 2, 5, 3, 4], what will be the output of print(x[0:3:2])?
[1, 5, 4]
[1, 5]
[1, 2, 5, 3, 4]
List index out of range exception!
Answer: 2 Explanation: Slice in Python list here refer to list[::]. So x[0:3:2] will start at index 0, stop at index 3 at take a step of 2.
Q2. What is the difference between a method and a function in Python?
Functions can be called only by its name but methods can not be called by its name only.
we don’t need a class to define a function but methods are defined inside a class.
Functions are independent entities in a program while methods are dependent on the class they belong to.
All of the above.
Answer: 4 Explanation: All the given three options are correct.
Q3. What will be the output of following code:
1
2
3
x=[10,20,30,40]
x+=[10]
print(x)
[20, 30, 40, 50]
[10, 20, 30, 40, 10]
Answer: 2 Explanation: In Python language, + operator append the elements to the original array.
Q4. What will be the output of following code:
1
2
3
def fun(x=3,y):
print(x,y)
fun(2)
3 2
2 2
2 3
SyntaxError
Answer: 4 Explanation: It will raise an error. In function definition, non-default arguments should be written before the default arguments.
Q5. Which of the following is true when you open a file using with keyword?
File reading and writing are faster using the with statement.
The file is automatically closed after leaving the block, and all the resources that are tied up with the file are released.
You need not to handle exception anymore in the whole script.
Answer: 2 Explanation: When you open a file using with keyword, the file is automatically closed after leaving the block, and all the resources that are tied up with the file are released.
Q6. A class can serve as base class for many derived classes.
True
False
Answer: 1 Explanation: yes a class can serve as base class for many derived classes. It is also call as Hierarchical inheritance.
Q7. The following code will throw an error.
1
2
3
def fun(**x):
print(x[a])
fun(a=1,b=2,c=3)
True
False
Answer: 1 Explanation: **kwargs are used as arbitrary keyword arguments. If we do not know how many keyword arguments that will be passed into our function then we can add two asterisk ** before the parameter name in the function definition. In the given function definition x will be a dictinary of arguments. That’s why print(x[a]) will raise an error.
Q8. Which of the following statement will create a variable of string type?
a = ”’AILearner”’
a = “AILearner”
a = ‘AILearner’
All of the above.
Answer: 4 Explanation: In Python language, a string can be created by using any of the single, double or triple quotation marks. Usually three quotes are used to create a multiline string.
Q1. In Python, which of the following is the super class of all created classes?
Super class
Object class
Integer class
Base class
Answer: 2 Explanation: Object class is the super class of all created classes.
Q2. What will be the output of following code:
1
2
3
4
x=[1,2,3]
y=x
y.pop()
print(x,y)
[1, 2] [1, 2, 3]
[1, 2, 3] [1, 2]
[1, 2, 3] [1, 2, 3]
[1, 2] [1, 2]
Answer: 4 Explanation: pop(index) method remove the item of a list at specified index. If index is not specified it will remove the first element of the list. Also as x is assigned to variable y and both refer to the same object in the memory, value of x and y will be same.
Q3. What will be the output of following code:
1
2
3
4
5
x={1:'a',2:'b',3:'c'}
delx[1]
x[1]=5
delx[2]
print(len(x))
2
1
4
3
Answer: 1 Explanation: del x[1] => x = {2: ‘b’, 3: ‘c’} x[1] = 5 => x = {2: ‘b’, 3: ‘c’, 1: 5} del x[2] => x = {3: ‘c’, 1: 5}
Q4. What will be the output of following code:
1
2
3
4
5
6
def func(**kwargs):
print(type(kwargs),end=" ")
foriinkwargs:
print(i,end=" ")
func(x=1,y=2)
x y
1 2
x y
1 2
Answer: 3 Explanation: **kwargs are used as arbitrary keyword arguments. If we do not know how many keyword arguments that will be passed into our function then we can add two asterisk ** before the parameter name in the function definition. Arbitrary Keyword Arguments (**kwargs) make the way for the function to receive the dictionary of arguments.
Q5. In Python, which of the following is a numeric data type?
int
float
complex
All of the above.
Answer: 4 Explanation: In Python language, there are three types of numeric data. 1. int (Example : 0, 2, -1, etc.) 2.float (Example : 0.0, 2.3, -1.5, etc.) 3.complex(Example : 0j, 2j, -1.5j, etc.)
Q6. What will be the output of following code:
1
2
3
x1={1,2,3}
x2=x1.add(4)
print(x1,x2)
{1, 2, 3} {1, 2, 3, 4}
{1, 2, 3, 4} None
{1, 2, 3, 4} {1, 2, 3, 4}
Error
Answer: 2 Explanation: add() method does not return any value, that’s why x2 will be None.
Q7. What will be the output of following code”
1
2
3
4
5
foriinrange(5):
with open("a.txt","r")asfile:
ifi>2:
break
print(file.closed,i)
False 5
True 5
False 3
True 3
Answer: 4 Explanation: When you open a file using with keyword, the file is automatically closed after leaving the block, and all the resources that are tied up with the file are released.
Q8. What is the difference between a global variable and a local variable?
Global variable can be used inside a function only while local variable can be used both inside and outside the function.
Local variable can be used inside a function only while global variable can be used both inside and outside the function.
Both are same.
Both are different name for same variables.
Answer: 2 Explanation: As stated, Local variable can be used inside a function only while global variable can be used both inside and outside the function.
Answer: 3 Explanation: To build a class/object as an iterator, we need the methods __iter__() and __next__(). The __next__() method return the next item in the sequence. myiter = iter(a) => Creates an iterator. next(myiter) will execute the __next__() method of class A. Python Iterators
Q2. The following code will throw an error.
1
2
3
4
def fun(*x):
print(x[2])
fun("a","b","c")
True
False
Answer: 2 Explanation: *args are used as arbitrary Arguments. If we do not know how many arguments that will be passed into our function then we can add a * before the parameter name in the function definition.
Q3. What will be the output of following code:
1
print(len(" TheAI "),len(" TheAI ".strip()))
8 5
8 6
7 6
7 5
Answer: 4 Explanation: strip() method removes any leading and trailing characters from a string.
Q4. What will be the output of following code:
1
print(('AILearner')*3)
(‘AILearner’,’AILearner’,’AILearner’)
AILearnerAILearnerAILearner
(‘AILearner3’)
Error
Answer: 2 Explanation: Here, * operator will multiply the content of string ‘AILearner’ 3 times.
Q5. What will be the output of following code:
1
2
3
4
5
classLanguage:
def get_lang(self,line='Python'):
print(line)
obj=Language()
obj.get_lang('Java')
Python
Java
Java Python
Python Java
Answer: 2 Explanation: Java is the correct answer.
Q6. What will be the output of following code:
1
2
3
x=[1,2]
x[3:4]=[3]
print(x)
[1, 2, 3]
[1, 3]
[1, 2]
Throws an error!
Answer: 1 Explanation: As the len(x) = 2, x[3:4] will add the data at the end of the list. Extending a list in Python
Q7. Which of the following data types can have only two values (either True or False)?
integer
Strings
Boolean
Float
Answer: 3 Explanation: Booleans can only have one of two values: True of False.
Q8. What will be the output of print(2 * 3 ** 3 * 2)?
108
432
1458
36
Answer: 1 Explanation: ** is a power operator and it has higher precedence than * multiplication operator. So the equation will be solved as follows: => 2 * 3 ** 3 * 2 => 2 * 27 * 2 => 54 * 2 => 108
Q1. Which of the following statement will read entire content of a text file as string?
txtfile = open(‘a.txt’, ‘r’) txtfile.read()
txtfile = open(‘a.txt’, ‘r’) txtfile.readline()
txtfile = open(‘a.txt’, ‘r’) txtfile.readlines()
txtfile = open(‘a.txt’, ‘r’) txtfile.reads()
Answer: 1 Explanation: txtfile.read() Read at most n characters from stream. This method will read from underlying buffer until we have n characters or we hit End of file. If n is negative or omitted, read until End of file.
Q2. Which of the following creates iterable elements?
continue
range
break
pass
Answer: 2 Explanation: range(stop) -> range object range(start, stop[, step]) -> range object Return an object that produces a sequence of integers from start (inclusive)to stop (exclusive) by step. range(i, j) produces i, i+1, i+2, …, j-1. start defaults to 0, and stop is omitted! range(4) produces 0, 1, 2, 3. These are exactly the valid indices for a list of 4 elements. When step is given, it specifies the increment (or decrement).
Q3. Which of the following is default mode in open() function?
‘a’ – append
‘w’ – write
‘r’ – read
‘x’ – create
Answer: 3 Explanation: ‘r’ => open for reading (default).
Q4. In s1 = “theai” and s2 = “learner”. Output of the print(2*s1[:2]+s2) will be:
Q6. In Python, which of the following rule is used to decide the order in which the namespaces are to be searched for scope resolution?
LEGB rule (Local, Enclosed, General, Built-in)
LEGB rule (Local, Enclosed, Global, Built-in)
ELGB rule (Enclosed, Local, Global, Built-in)
ELGB rule (Enclosed, Local, Genral, Built-in)
Answer: 1 Explanation: Since x1 and x2 refers to the same object in the memory, adding the items in the x2 will also add items in x1.
Q8. if x = [2, 4, 6, 8], which of the following operation will change x to [2, 4, 8]?
del x[2]
x.remove(6)
x[2:3] = []
All of the above!
Answer: 4 Explanation: del x[2] => will delete the item at index 2. x.remove(6) => will remove the first occurance of value 6. x[2:3] = [] => will replace the value at index 2 to nothing.
Answer: 3 Explanation: continue keyword will skip the rest of the statements after it in the loop and make the loop to run for the next iteration.
Q2. What will be the output of following code:
1
2
3
4
5
6
7
def func(x,y=2,z=3):
def in_fun(a,b):
x=a+b
returnx
returnx+in_fun(y,z)
print(func(3,6))
18
12
9
Error
Answer: 2 Explanation: function which is defined inside another function is known as inner function or nested function. Nested functions are able to access variables of the enclosing scope. Inner functions are used so that they can be protected from everything happening outside the function. This process is also known as Encapsulation. Reference: Inner Functions Default arguments in Python
Q3. What are the dunder(magic) methods in Python?
Methods that start with single underscore.
Methods that start with double underscore and ends with double underscore.
Methods that start with double underscore.
Methods that ends with single underscore.
Answer: 2 Explanation: Dunder or magic methods in Python are the methods having two prefix and suffix underscores in the method name. Dunder here means “Double Under (Underscores)”. These are commonly used for operator overloading. Few examples for magic methods are: __init__, __add__, __len__, __repr__ etc. Reference: Dunder or magic methods in Python
Q4. Which of the following option gives the value of rating2 from the code?
Answer: 3 Explanation: A function which is defined inside another function is known as inner function or nested function. Nested functions are able to access variables of the enclosing scope. Inner functions are used so that they can be protected from everything happening outside the function. This process is also known as Encapsulation. Reference: Inner Functions
Q2. Which of the following function is used inside a for loop in Python?
Answer: 2 Explanation: If “if” condition is not True then only “elif” condition will execute.
Q4. What will be the output of following code:
1
2
x=("a",2,3,2,2.3,[1,2,3])
print(len(x))
5
8
6
TypeError!
Answer: 3 Explanation: len() function gives the length of the tuple.
Q5. Which of the following statement will return False?
bool(-1)
bool(1)
bool(0)
bool(12E5)
Answer: 3 Explanation: Any number is True, except 0.
Q6. Which of the following brackets can be used to create lists in Python?
[]
{}
()
<>
Answer: 1 Explanation: list1 = [] will create a list in Python.
Q7. In Python, what is the correct way of casting an integer value to a string?
x = str(6)
x = Integer.toString(6);
Both of the above.
None of the above.
Answer: 1 Explanation: In Python language, you can cast an integer to a string by using ‘str’ keyword.
Q8. What will be the output of following code:
1
"TheAILearner".count("a",0,5)
0
1
2
Throws an error!
Answer: 1 Explanation: count() method returns the number of times a specified value occurs in a string. “a” occurs one time in “”TheAILearner”. But in count method start (0) and end (5) index of the string is given where it will search for “a”.
Q1. Which of the following is true for a lambda function in Python?
A lambda function is a small anonymous function.
A lambda function can take any number of arguments, but can only have one expression.
Lambda functions are used for functional programming.
All of the above.
Answer: 4 Explanation: All of the statements regarding the Python lambda functions are correct.
Q2. Which of the following is the recommended way to ensure that a file object is properly closed after usage?
Use close() function after the use of file object.
Use try/finally block
Use with statement
All of the above.
Answer: 3 Explanation: When you open a file using with keyword, the file is automatically closed after leaving the block, and all the resources that are tied up with the file are released.
Answer: 3 Explanation: sys.getsizeof(object, default) return the size of object in bytes.
Q4. Which of the following list assignment in Python will throw an exception?
x = [1, 2, ‘python’]
x = [1, 2, [1, 2]]
x = [‘a’, ‘b’, ‘c’, ‘a’, ‘4.5’, int(3.4)]
None of the above.
Answer: 4 Explanation: in Python language, list items can be of any data types and also a list can contain different data types.
Q5. What will be the output of following code:
1
2
x=("TheAILearner")
print(type(x))
<class ‘tuple’>
<class ‘str’>
<class ‘list’>
<class ‘int’>
Answer: 2 Explanation: To create a tuple with single item, we need to add comma(,) at the end of the item.
Q6. If str = “AILearner”, what will be the output of print(str == ‘AILearner’, str is “AILearner”)?
False False
True False
False True
True True
Answer: 4 Explanation: In Python language, “is” keyword is used to test if the two variables refers to the same object.
Q7. What will be the output of print(13//2)?
6.5
6
7
Throws an error.
Answer: 2 Explanation: In Python language, // is a floor division operator. This operator will divide the two numbers and round up it to the previous lower integer.
Q8. What will be the output of following code:
1
2
x=(1120,'a',-1120)
print(max(x))
1120
‘a’
-1120
TypeError
Answer: 4 Explanation: max() does not support comparison between instances of ‘str’ and ‘int’.
Answer: 1 Explanation: This is a Sobel filter. Refer to this link to know more.
Q2. Which of the following OpenCV functions can be used to threshold an image?
cv2.threshold()
cv2.thresh()
cv2.Thresh()
cv2.Threshold()
Answer: 1 Explanation: In OpenCV, cv2.threshold() can be used to threshold an image. Refer to this link to know more.
Q3. In a high contrast image, _________?
everything looks nearly of the same intensity (dull, washed-out grey look)
everything looks clear to us
Answer: 2 Explanation: In a low contrast image, it is difficult to identify details because everything looks nearly of the same intensity (dull, washed-out grey look) as compared to a high contrast image where everything looks crisp clear. Refer to this link that has examples of both low and high contrast images.
Q4. Which technique is used to construct a color image from a Bayer image?
Sampling
Interpolation
Segmentation
Quantization
Answer: 2 Explanation: Because Bayer filter uses 1 channel containing 25%-Red, 50%-Green, and 25%-Blue pixels instead of 3 channels, so to form a color image we use interpolation to find the missing information. To know more about Bayer filter, refer to this link.
Q5. Which of the following OpenCV functions can be used to create a structuring element?
cv2.getStructuringElement()
cv2.createStructEl()
cv2.createStructuringElement()
cv2.getStructEl()
Answer: 1 Explanation: In OpenCV, cv2.getStructuringElement() can be used to create a structuring element. Refer to this link to know more.
Q6. How to calculate the threshold value for adaptive Thresholding?
Mean of the pixel neighborhood
Weighted Mean of the pixel neighborhood
Median of the pixel neighborhood
Can be any of the above three
Answer: 4 Explanation: We can use any of the above mentioned statistics to calculate the threshold value for adaptive Thresholding.
Q7. In the expression g(x,y) = (k+1)*f(x,y) – k*b(x,y), if we set k=1 what is this technique called? Suppose f(x,y) is the input image, b(x,y) is its blurred version and g(x,y) is the output image.
Unsharp Masking
Highboost filtering
Laplacian of Gaussian
Difference of Gaussian
Answer: 1 Explanation: This is known as Unsharp Masking. Refer to this link to know more.
Q8. Which of the following set operations are generally used in Morphological image processing?
Union
Intersection
Difference
All of the above
Answer: 4 Explanation: All of the above set operations can be used in Morphological image processing.
Q1. Difference between the closing and the input image is known as ________?
Black top-hat transform
White top-hat transform
Hit or Miss
Closing
Answer: 1 Explanation: Difference between the closing and the input image is known as Black top-hat transform. Refer to this link to know more.
Q2. Which of the following Morphological operations output the object boundaries?
Erosion
Dilation
Morphological Gradient
Closing
Answer: 3 Explanation: As we know Morphological Gradient is the difference between the dilation and erosion of an image. Because dilation and erosion mostly affect the pixels that are close to the boundary between the foreground and background, their difference generally yields the boundary and thus this is used for edge detection and segmentation tasks.
Q3. What type of filters can be used for noise removal?
Low Pass filters
High Pass filters
Answer: 1 Explanation: Because low pass filters blocks the high-frequency parts of an image, and noise is a high frequency component so Low Pass filters can be used for noise removal.
Q4. Does using a separable filter reduces computational cost?
Yes
No
Answer: 1 Explanation: For instance, the Gaussian kernel is linearly separable. Because of this, the computational complexity is reduced from O(n^2) to O(n).
Q5. In contrast stretching, by changing the shape of the transformation function we can obtain/perform ________?
Min-Max stretching
Percentile stretching
Thresholding function
All of the above
Answer: 4 Explanation: Because Contrast Stretching uses a Piecewise Linear function so by changing the location of points we can obtain all the above mentioned functions.
Q6. Which technique expands the range of intensity levels in an image so that it spans the full intensity range of the display?
Contrast Stretching
Gamma Correction
Intensity Level Slicing
None of the above
Answer: 1 Explanation: Out of the above mentioned techniques, Contrast Stretching expands the range of intensity levels in an image.
Q7. Which of the following is an example of Affine Transformation?
Translation
Rotation
Scaling
All of the above
Answer: 4 Explanation: All of the above are examples of Affine Transformation as they preserves collinearity, parallelism as well as the ratio of distances between the points.
Q8. Which of the following is an additive color model?
RGB
CMYK
Both of the above
None of the above
Answer: 1 Explanation: In additive model the colors present in the light add to form new colors. For instance, in RGB color model, Red, Green, and Blue are added together in varying proportions to produce an extensive range of colors. To know more about additive models, refer to this link.
Q1. A color image is formed by a combination of ________ colors?
Red, Green, and Blue
Black, and White
Red, Green, and White
All colors
Answer: 1 Explanation: A color image has 3 channels that contains the intensity information corresponding to three wavelengths red, green, and blue (RGB) collectively called primary colors or channels.
Q2. In spatial filtering, we convolve the neighborhood of a pixel with a subimage. What we generally call this subimage?
mask
kernel
filter
All of the above
Answer: 4 Explanation: We refer to this subimage by all of the above mentioned names.
Q3. Which of the following interpolation algorithms results in a blocky or pixelated image?
Nearest Neighbor
Bilinear
Bicubic
None of the above
Answer: 1 Explanation: Because in Nearest Neighbor we are only replicating the nearest pixel value so this results in a blocky or pixelated image. To know more about Nearest Neighbor, refer to this link.
Q4. Does Sobel filter have some smoothing effect?
Yes
No
Answer: 1 Explanation: Because Sobel filter is obtained by multiplying the x, and y-derivative filters with some smoothing filter(1D) so this has some smoothing effect.
Q5. Which of the following arithmetic operations can be used for Image Enhancement?
Image Averaging
Image Subtraction
Image Multiplication
All of the above
Answer: 4 Explanation: All the arithmetic operations such as Averaging, subtraction or multiplication can be used for image enhancement. For instance, Averaging can be used for noise removal, subtraction can be used for detecting blockage or moving objects and multiplication can be used for extracting region of interest (roi) using masking etc.
Q6. Suppose we plot the 2D color histogram for Red and Green channels. So, what does each point within this histogram represents?
Frequency corresponding to each Red and Green pair
Pixel location corresponding to each Red and Green pair
Intensity value of the Red color
Intensity value of the Green color
Answer: 1 Explanation: For the above case, Y and X-axis correspond to the Red and Green channel ranges( for 8-bit, [0,255]) and each point within the histogram shows the frequency corresponding to each Red and Green pair. To know more about 2D Image Histograms, refer to this link.
Q7. A 0 degree hue results in which color?
Red
Green
Blue
White
Answer: 1 Explanation: A 0 degree hue results in Red color. Refer to this link to know more.
Q8. What response does a first order derivative filter will give for flat regions in an image?
Zero
positive
negative
Answer: 1 Explanation: As we know that the differentiation of a constant is 0. Since the flat region has nearly constant intensity so the first order derivative filter will give 0 response.