Python Objective PDF
Python Objective PDF
Objective Practise
Questions
P a g e 1 | 37
Q1. How can we create an iterator object from a list?
a) Bypassing the given list to the iter() function
b) By using a for a loop.
c) By using a while loop.
d) You cannot create an iterable object
Choose one
a) An iterable
b) a generator function
c) an anonymous function
d) None of the above
a) 1 3
b) 1 9
c) 1 9 36 100
d) 1
Q4. What are the criteria that must be met to create closure in Python?
a) The program Must have the function inside the function.
b) The nested function must refer to the value defined in the enclosing function.
c) The enclosing function must return the nested
d) All of the above.
P a g e 2 | 37
Q5. What is the output of the code?
1. def Foo(n):
2. def multiplier(x):
3. return x * n
4. return multiplier
5.
6. a = Foo(5)
7. b = Foo(5)
8.
9. print(a(b(2)))
a) 25.
b) 100
c) 10
d) 50
1. def make_pretty(func):
2. def inner():
3. print("I got decorated")
4. func()
5. return inner
6.
7. def ordinary():
8. print("I am ordinary")
9.
10. pretty = make_pretty(ordinary)
11. pretty()
a) I got decorated
b) I am pretty
c) I got decorated
I am ordinary
P a g e 3 | 37
d) I am ordinary
I got decorated
Q7: What is the more pythonic way to use getters and setters?
a) Decorators
b) Generators.
c) Iterators
d) @property
Q10. For the following codes, which of the following statements is true?
1. def printHello():
2. print("Hello")
3. a = printHello()
P a g e 4 | 37
Q11. What is the output of the program?
1. def outerFunction():
2. global a
3. a = 20
4. def innerFunction():
5. global a
6. a = 30
7. print('a =', a)
8. a = 10
9. outerFunction()
10. print('a =', a)
a) a = 10 a = 30
b) a = 10
c) a = 2
d) a = 30
1. class Foo:
2. def printLine(self, line='Python'):
3. print(line)
4.
5. o1 = Foo()
6. o1.printLine('Java')
a) Python
b) Line
c) Java
d) Java
Python
P a g e 5 | 37
Q14. What is the function of the __init__() function in Python?
1. class Point:
2. def __init__(self, x = 0, y = 0):
3. self.x = x+1
4. self.y = y+1
5.
6. p1 = Point()
7. print(p1.x, p1.y)
a) 00
b) 11
c) None None
d) xy
Q17 If you a class is derived from two different classes, it’s called
______
a) Multilevel inheritance
b) Multiple Inheritance
c) Hierarchical Inheritance
d) Python Inheritance
P a g e 7 | 37
Q20. Opening a file in ‘a’ mode
a) opens a file for reading
b) opens a file for writing
c) opens the file for appending, at the end of file
d) opens a file for exclusive creation
occurs?
Choose one
a)
1. with open("test.txt", encoding = 'utf-8') as f:
2. # perform file operation
b)
1. try:
2. f = open("test.txt",encoding = 'utf-8')
3. # perform file operations
4. finally:
5. f.close()
P a g e 8 | 37
Q23. For the following code,
1. f = open('test.txt', 'r', encoding = 'utf-8')
2. f.read()
Which of the following statement is true
a) This program reads the content of the test.txt file.
b) If test.txt contains a newline, read() will return the newline as ‘\n’.
c) You can pass an integer to the read() method
d) All of the above.
Q26. What will happen if we try to open the file that doesn’t exist?
a) A new file is created.
b) Nothing will happen.
c) An exception is raised.
d) None of the above
1. number = 5.0
2. try:
3. r = 10/number
4. print(r)
5. except:
6. print("Oops! Error occurred.")
P a g e 9 | 37
a) Oops! Error occurred
b) 2.0
c) 2.0 Oops! Error occurred.
d) None object
1. try:
2. # code that can raise an error
3. pass
4.
5. except (TypeError, ZeroDivisionError):
6. print("Two")
P a g e 10 | 37
a) Python
b) Python is awesome.
c) Text is awesome.
d) Is awesome.
Q32. If the return statement is not used inside the function, the function
will return:
a) 0
b) None object
c) an arbitrary integer
d) Error! Functions in Python must have a return statement.
1. def greetPerson(*name):
2. print('Hello', name)
3.
4. greetPerson('Frodo', 'Sauron')
a) Hello Frodo
Hello Sauron
c) Hello Frodo
Choose one
a) A function that calls all the functions in the program.
b) A function that calls itself.
c) A function that that calls all the functions in the program except itself.
d) There is no such thing as a recursive function in Python.
P a g e 11 | 37
Q35. What is the output of the program?
1. result = lambda x: x * x
2. print(result(5))
a) lambda x: x*x
b) 10
c) 25
d) 5*5
1. def Foo(x):
2. if (x==1):
3. return 1
4. else:
5. return x+Foo(x-1)
6.
7. print(Foo(4))
a) 10
b) 24
c) 7
d) 1
Q37. Suppose you need to print pi constant defined in the math module.
Which of the following code can do this task?
a) print(math.pi)
b) print(pi)
c)
1. from math import pi
2. print(pi)
d)
1. from math import pi
2. print(math.pi)
P a g e 12 | 37
Q38. Which operator is used in Python to import modules from the
packages?
Choose one
a). operator
b) * operator
c) -> symbol
d) , operator
Q39. What is the output of the code?
1. numbers = [1, 3, 6]
2. newNumbers = tuple(map(lambda x: x , numbers))
3. print(newNumbers)
a) [1, 3, 6]
b) (1, 3, 6)
c) [2, 6, 12]
d) (2, 6, 12)
1. if None:
2. print(“Hello”)
a) False
b) Hello
c) Nothing will be printed
d) Syntax error
Q41. The if-elif-else executes only one block of code among several
blocks.
a) True.
b) False
c) It depends on the expression used.
d) There is no elif statement in Python.
P a g e 13 | 37
Q42. What is the output of the code?
a) 2
1
b) [2, 1]
c) 2
0
d) [2, 0]
Q43. In the Python, for and while loop can have the optional else
statement?
a) Only for loop can have the optional else statement
b) Only while loop can have the optional else statement
c) Both loops can have optional else statement
d) Loops cannot have else statement in Python
1. i = sum = 0
2.
3. while i <= 4:
4. sum += i
5. i = i+1
6.
7. print(sum)
a) 0
P a g e 14 | 37
b) 10
c) 4
d) None of the above
1. while 4 == 4:
2. print('4')
a) 4 is printed once
b) 4 is printed four times
c) 4 is printed infinitely until the program closes
d) Syntax error
Q46. Is it better to use the for loop instead of while if we are iterating
through a sequence?
a) PYTHON
b) PYTHONSTRING
c) PYTHN
d) STRING
Q50. In regards to separated value files such as .csv and .tsv, what is
the delimiter?
Q51. In separated value files such as .csv and .tsv, what does the first
row in the file typically contain?
Q52. Assume you have a file object my_data, which has properly
opened a separated value file that uses the tab character (\t) as the
delimiter.
What is the proper way to open the file using the Python CSV module
and assign it to the variable csv_reader?
P a g e 16 | 37
Assume that csv has already been imported.
a) csv.tab_reader(my_data)
b) csv.reader(my_data, delimiter='\t')
c) csv.reader(my_data)
d) csv.reader(my_data, tab_delimited=True)
writer = csv.DictWriter(
csv_file,
fieldnames=['first_col', 'second_col']
)
writer.writeheader()
P a g e 17 | 37
a) Each key indicates the row index as an integer for where the data should go
b) Each key must match up to the field names (index names) used to identify the row data
c) Each key must match up to the field names (column names) used to identify the column
data
d) Each key indicates the column index as an integer for where the value should go
Q55. Which is the correct way to open the CSV file hrdata.csv for
reading using the pandas package? Assume that the pandas
package has already been imported.
a) pandas.open_csv('hrdata.csv', 'r')
b) pandas.read_table('hrdata.csv')
c) pandas.read_csv('hrdata.csv')
d) pandas.open('hrdata.csv','r')
Q56. By default, pandas uses 0-based indices for indexing rows. Which
for reading and using the 'Name' column as the index row instead?
Below is the contents of hrdata.csv
a) pandas.read_csv('hrdata.csv', index_col='Name')
b) pandas.read_csv('hrdata.csv', index=0, index_col_name='Name')
c) pandas.read_csv('hrdata.csv', index='Name')
d) pandas.read_csv('hrdata.csv', index_col=0)
Q57. Given the file dog_breeds.txt, which of the following is the correct
way to open the file for reading as a text file? Select all that apply.
a) open('dog_breeds.txt', 'w')
b) open('dog_breeds.txt', 'r')
c) open('dog_breeds.txt')
d) open('dog_breeds.txt', 'wb')
e) open('dog_breeds.txt', 'rb')
P a g e 18 | 37
Q58. Given the following directory structure:
animals/
│
├── feline/
│ ├── lions.gif
│ └── tigers.gif
│
├── ursine/
│ └── bears.gif
│
└── animals.csv
Assuming that the cwd is in the root folder where animals reside, what is the full path to
the feline folder?
Q59. Given the file jack_russell.png, which of the following is the correct
way to open the file for reading as a buffered binary file? Select
all that apply.
open('jack_russell.png', 'rb')
open('jack_russell.png', bytes=True)
open('jack_russell.png', 'r')
open('jack_russell.png')
open('jack_russell.png', 'wb')
animals/
│
├── feline/
│ ├── lions.gif
│ └── tigers.gif
P a g e 19 | 37
│
├── ursine/
│ └── bears.gif
│
└── animals.csv
Assuming that the cwd is in the root folder where Animals reside, what is the full path to
the file bears.gif?
a) Making sure that you use the .close() method before the end of the script
b) It doesn’t matter
c) By using the try/finally block
d) By using the with statement
Q62. Using the same directory structure as before:
animals/
│
├── feline/ ← cwd
│ ├── lions.gif
│ └── tigers.gif
│
├── ursine/
│ └── bears.gif
│
└── animals.csv
Assuming that the cwd is in the feline folder, what is the relative path to the
file bears.gif?
Q63. When reading a file using the file object, what method is best for
reading the entire file into a single string?
a) .read_file_to_str()
b) .read()
c) .readlines()
d) .readline()
P a g e 20 | 37
Q64. The value 1.73 rounded to one decimal place using the “rounding
up” strategy is…
a) 1.8
b) 1.7
Q65. The value -2.961 rounded to two decimal places using the
“rounding down” strategy is…
a) -2.96
b) -2.97
Q66. When a value is truncated to 3 decimal places, which of the
following is true?
a) Positive numbers are rounded up, and negative numbers are rounded down.
b) Positive numbers are rounded down, and negative numbers are rounded up.
c) Both positive and negative numbers are rounded up.
d) Both positive and negative numbers are rounded down.
Q67. The value -0.045 rounded to 2 decimal places using the “round half
away from zero” strategy is…
a) -0.05
b) -0.04
Q68. Which rounding strategy does Python’s built-in round() function
use?
a) Round half down
b) Round half away from zero
c) Round half up
d) Round half to even
Q69. The value 4.65 rounded to one decimal place using the “round half
to even” strategy is…
a) 4.6
b) 4.7
P a g e 21 | 37
Q70. Which problem arises due to the multiple inheritances, if
hierarchical inheritance is used previously for its base classes?
a) Diamond
b) Circle
c) Triangle
d) Loop
a) Only 1
b) At least 1
c) At least 3
d) Exactly 3
Q72. If class a inherits class b and class c as “class a: public class b,
public class c {// class body ;}; ”, which class constructor will be
called first?
a) Class a.
b) Class b.
c) Class c.
d) All together.
Q73.If all the members of all base classes are private then,
P a g e 22 | 37
c) Yes, if all the methods are predefined
d) No, since constructors won’t be there
Q75. Which among the following best defines the multilevel inheritance?
a) A
b) B
c) C
d) A and B
Q77. Which Class is having the highest degree of abstraction in
multilevel inheritance of 5 levels?
P a g e 23 | 37
c) Object of lower-level classes must define all the default constructors
d) Only object of first-class can be created, which is first-parent
a) Object
b) Math
c) Errors
d) Exceptions
Q81. What are two exception classes in the hierarchy of java exceptions
class?
Q84. Which class is used to handle the input and output exceptions?
a) InputOutput
b) InputOutputExceptions
c) IOExceptions
d) ExceptionsIO
P a g e 24 | 37
Q85. Which among the following is true for the class exceptions?
Q86. If both base and derived class caught the exceptions, _____.
a) Then catch block of a derived class must be defined before the base class
b) Then catch block of the base class must be defined before the derived class
c) Then catch block of base and derived classes don't matter.
d) catch block of the base and derived classes are not mandatory to be defined
Q87. The catching of base class the exception _________ in java.
a) ClassNotFound
b) NoClassException
c) ClassFoundException
d) ClassNotFoundException
P a g e 25 | 37
Q90. Which condition among the following might result in memory
exception?
a) False if conditions
b) Nested if conditions that are all false
c) Infinite loops
d) The loop that runs exactly 99 times
Q91. Which among the following is the correct definition for static
member functions?
Q93. Which is the correct syntax to access the static member functions
with a class name?
a) className . functionName;
b) className -> functionName;
c) className : functionName;
d) className :: functionName;
Q94. The static members are ______________________
P a g e 26 | 37
c) Created as many times, the class is being used.
d) Created and initialised, only once
a) Scope resolution.
b) Arrow operator.
c) Single colon.
d) Dot operator.
Q98. If static data member are made inline, ______________
a) It can be mutable.
b) Can’t be mutable.
P a g e 27 | 37
c) Can’t be an integer.
d) Can’t be characters.
Q100. We can use the static member functions and static data member
__________________.
a) reshape, resize.
b) resize, reshape.
c) reshape2,resize.
d) all of the mentioned.
Q103. To create sequences of the numbers, NumPy provides a function
__________ analogous to range that returns arrays instead of lists.
a) arrange.
b) aspace.
c) aline.
d) all of the mentioned.
Q104. Point out the correct statement:
P a g e 28 | 37
c) Numpy array class is called ndarray.
d) All of the Mentioned
Q105. Which of the following function stack 1D array as the
columns into the 2D array?
a) row_stack.
b) column_stack.
c) com_stack.
d) all of the mentioned.
Q106. ndarray is also known as an alias array.
a) True
b) False
Q107. Which of the following method creates the new array object that
looks at the same data?
a) view.
b) copy.
c) paste.
d) all of the mentioned.
Q108. Which of the functions can be used to combine the
different vectors to obtain the result for each n-uplet?
a) iid_.
b) ix_.
c) ixd_.
d) all of the mentioned.
Q109. ndarray.dataitemSize is the buffer containing actual elements
of an array.
a) True
b) False
P a g e 29 | 37
Q110. Which of the following is in the NumPy library?
a) The n-dimensional array object
b) The tools for integrating C/C++ and the Fortran code
c) Fourier transform
d) all of the Mentioned
Q111. Which of the following sets the size of the buffer used in ufuncs ?
a) bufsize(size)
b) setsize(size)
c) setbufsize(size)
d) all of the Mentioned
Q112. Point out the wrong statement:
a) .types
b) .type
c) .class
d) all of the Mentioned
Q114. Which of the following returns an array of “ones” with the same
shape and type as a given array?
a) all_like
b) ones_like
c) one_alike
d) all of the Mentioned
Q115. Point out the wrong statement:
a) Each universal function takes an array input and produces array outputs
b) Broadcasting is used throughout NumPy to decide how to handle the disparately
P a g e 30 | 37
shaped arrays
c) The output of the ufunc is necessarily a ndarray, if all the input arguments are
ndarrays
d) All of the Mentioned
Q116. Which of the following set of a floating-point error callback
function or a log object?
a) setter.
b) settercall.
c) setterstack.
d) all of the mentioned.
Q117. Some ufuncs can take output arguments.
a) True
b) False
Q118. ___________ decompose the elements of x into the
mantissa and the two’s exponent.
a) trunc
b) fmod
c) frexp
d) ldexp
Q119. Which of the following function take the only a single value as
input?
a) iscomplex.
b) minimum.
c) fmin.
d) all of the mentioned.
Q120. The array object returned by the _array_prepare_ is passed to
ufunc for computation.
a) True
b) False
P a g e 31 | 37
Q121. All pandas data structures are ___mutable but not always
_______-mutable.
a) size,value.
b) semantic,size.
c) value,size.
d) none of the mentioned.
a) import pandas as pd
b) import panda as py
c) import pandaspy as pd
d) all of the Mentioned
Q124. Which of the following object did we get after reading the CSV
file?
a) DataFrame.
b) Character Vector.
c) Panel.
d) All of the Mentioned
Q125. Point out the wrong statement:
P a g e 32 | 37
Q126. Which of the following library is similar to the pandas?
a) numpy.
b) RPy.
c) OutPy.
d) None of the mentioned.
a) True
b) False
Q128. Which of the following is the prominent python “statistics and
econometrics library”?
a) Bokeh.
b) Seaborn.
c) Statsmodels.
d) None of the mentioned.
Q129. Which of the following is the foundational exploratory visualisation
package for the R language in the pandas ecosystem?
a) yhat.
b) Seaborn.
c) Vincent.
d) None of the mentioned.
Q130. Pandas consist of static and the moving window linear and panel
regression.
a) True
b) False
P a g e 33 | 37
Q131. Quandl API for Python wraps the __ REST API to
returns the pandas DataFrames with time series indexes.
a) Quandl.
b) PyDatastream.
c) PyData.
d) None of the Mentioned.
a) pandaSDMX
b) freedapi
c) Geopandas.
d) All of the Mentioned.
Q134. Which of the following provides the standard API for doing
computations with MongoDB?
a) Blaze.
b) Geopandas.
c) FRED.
d) All of the Mentioned.
Q135. Point out the wrong statement:
P a g e 34 | 37
c) Spyder is a cross-platform Qt-based open-source R IDE
d) None of the Mentioned
Q136. Which of the following makes use of the pandas and returns
data in a Series or DataFrame?
a) pandaSDMX.
b) freedapi.
c) OutPy.
d) none of the mentioned.
Q137. Spyder can introspect and display Pandas DataFrames.
a) True
b) False
Q138. Which of the following is used for machine learning in the python?
a) sci-kit-learn.
b) seaborn-learn.
c) stats-learn.
d) none of the mentioned.
Q139. The ________ project builds on top of the pandas and matplotlib
to provide easy plotting of data.
a) yhat.
b) Seaborn.
c) Vincent.
d) None of the mentioned.
Q140 x-ray brings the labelled data power of pandas to the physical
sciences.
a) True
b) False
P a g e 35 | 37
Q141. Which of the following is the base layer of all of the sparse
has it indexed data structures?
a) SArray.
b) SparseArray.
c) PyArray.
d) None of the mentioned.
Q142. Point out the correct statement.
a) SparseSeries.
b) SparseDataFrame.
c) SparsePanel.
d) None of the mentioned.
Q144. Which of the following list like data structure is used for managing
the dynamic collection of SparseArrays?
a) SparseList.
b) GeoList.
c) SparseSeries.
d) All of the mentioned.
Q145. Point out the wrong statement.
P a g e 36 | 37
Q146. Which of the following method used for transforming the
Sparse-series index by the MultiIndex to a
scipy.sparse.coo_matrix?
a) SparseSeries.to_coo().
b) Series.to_coo().
c) SparseSeries.to_cooser().
d) None of the mentioned.
Q147. The integer format tracks only the locations and the sizes of
blocks of data.
a) True
b) False
Q148. Which of the following is used for the testing for membership in
the list of column names?
a) in.
b) out.
c) else if.
d) none of the mentioned.
Q149. Which of the following indexing capabilities is used as the concise
means of selecting data from a pandas object?
a) In.
b) ix.
c) ipy.
d) none of the mentioned.
Q150. Pandas follow the NumPy convention of raising an error when
you try to convert something to a bool.
a) True
b) False
------------------------------------------------------------------------------------------------------------------------
P a g e 37 | 37