Professional Documents
Culture Documents
Python MCQ
Python MCQ
a. 16
b. 32
c. 64
d. None of these above
Explanation: The maximum possible length of an identifier is not defined in the python language. It can be of any
number.
a. Zim Den
b. Guido van Rossum
c. Niene Stom
d. Wick van Rossum
Explanation: Python language was developed by Guido van Rossum in the Netherlands.
a. 1995
b. 1972
c. 1981
d. 1989
a. English
b. PHP
c. C
d. All of the above
Answer: (b) C
5) Which one of the following is the correct extension of the Python file?
a. .py
b. .python
c. .p
d. None of these
a. 2008
b. 2000
c. 2010
d. 2005
a. Key
b. Brackets
c. Indentation
d. None of these
a. /
b. //
c. #
d. !
Answer: (c) #
9) Which of the following statements is correct regarding the object-oriented programming concept in Python?
Answer: (b) Objects are real-world entities while classes are not real
Explanation: None
class Name:
def __init__(javatpoint):
javajavatpoint = java
name1=Name("ABC")
name2=name1
a. It will throw the error as multiple references to the same object is not possible
b. id(name1) and id(name2) will have same value
c. Both name1 and name2 will have reference to two different objects of class Name
d. All of the above
Answer: (b) id(name1) and id(name2) will have same value
Explanation: "name1" and "name2" refer to the same object, so id(name1) and id(name2) will have the same value.
a. Object
b. Function
c. Attribute
d. Argument
a. _x = 2
b. __x = 3
c. __xyz__ = 5
d. None of these
Explanation: All declarations will execute successfully but at the expense of low readability.
13) Why does the name of local variables start with an underscore discouraged?
Explanation: Since there is no concept of private variables in Python language, the major underscore is used to
denote variables that cannot be accessed from outside the class.
14) Which of the following is not a keyword in Python language?
a. val
b. raise
c. try
d. with
15) Which of the following statements is correct for variable names in Python language?
Explanation: None
a. xyzp = 5,000,000
b. x y z p = 5000 6000 7000 8000
c. x,y,z,p = 5000, 6000, 7000, 8000
d. x_y_z_p = 5,000,000
a. _val
b. val
c. try
d. _try_
18) Which of the following operators is the correct option for power(ab)?
a. a ^ b
b. a**b
c. a ^ ^ b
d. a ^ * b
20) Which one of the following has the same precedence level?
Explanation: None
21) Which one of the following has the highest precedence in the expression?
a. Division
b. Subtraction
c. Power
d. Parentheses
Answer: (d) Parentheses
a. val()
b. print()
c. print()
d. None of these
Explanation: The print() function is a built-in function in python language that prints a value directly to the system.
round(4.576)
a. 4
b. 5
c. 576
d. 5
Answer: (d) 5
Explanation: The round function is a built-in function in the Python language that round-off the value (like 3.85 is
4), so the output of this function will be 5.
pow(x,y,z)
a. (x**y) / z
b. (x / y) * z
c. (x**y) % z
d. (x / y) / z
Answer: (c) (x**y) % z
all([2,4,0,6])
a. False
b. True
c. 0
d. Invalid code
Explanation: If any element is zero, it returns a false value, and if all elements are non-zero, it returns a true value.
Hence, the output of this "all([2,4,0,6])" function will be false.
x=1
while True:
if x % 5 = = 0:
break
print(x)
x+=1
a. error
b. 2 1
c. 0 3 1
d. None of these
Answer: (a) error
27) Which one of the following syntaxes is the correct syntax to read from a simple text file stored in ''d:\java.txt''?
Explanation: None
x = ['XX', 'YY']
for i in a:
i.lower()
print(a)
a. ['XX', 'YY']
b. ['xx', 'yy']
c. [XX, yy]
d. None of these
Explanation: None
29) Study the following function:
import math
abs(math.sqrt(36))
a. Error
b. -6
c. 6
d. 6.0
a. False
b. Ture
c. Invalid code
d. None of these
Explanation: None
>>>"a"+"bc"
Explanation: In Python, the "+" operator acts as a concatenation operator between two strings.
>>>"javatpoint"[5:]
a. javatpoint
b. java
c. point
d. None of these
a. character
b. ascii_lowercase_string.digits
c. lowercase_string.upercase
d. ascii_lowercase+string.ascii_upercase
Explanation: None
34) Study the following statements:
a. t
b. j
c. point
d. java
Answer: (a) t
Explanation: The correct output of this program is "t" because -1 corresponds to the last index.
a. java
point
b. java point
c. \njavat\npoint
d. Print the letter r and then javat and then point
Explanation: None
36) Study the following statements:
a. 33
b. 63
c. 0xA + 0xB + 0xC
d. None of these
Answer: (a) 33
Explanation: A, B and C are hexadecimal integers with values 10, 11 and 12 respectively, so the sum of A, B and C
is 33.
class book:
def __init__(a, b):
a.o1 = b
class child(book):
def __init__(a, b):
a.o2 = b
obj = page(32)
print "%d %d" % (obj.o1, obj.o2)
a. 32
b. 32 32
c. 32 None
d. Error is generated
Answer: (d) Error is generated
class Std_Name:
def __init__(self, Std_firstName, Std_Phn, Std_lastName):
self.Std_firstName = Std_firstName
self. Std_PhnStd_Phn = Std_Phn
self. Std_lastNameStd_lastName = Std_lastName
Std_firstName = "Wick"
name = Std_Name(Std_firstName, 'F', "Bob")
Std_firstName = "Ann"
name.lastName = "Nick"
print(name.Std_firstName, name.Std_lastName)
a. Ann Bob
b. Ann Nick
c. Wick Bob
d. Wick Nick
Explanation: None
a. 18
b. -18
c. 17
d. -17
Explanation: ASCII value of h is less than the z. Hence the output of this code is 104-122, which is equal to -18.
x = ['xy', 'yz']
for i in a:
i.upper()
print(a)
a. ['xy', 'yz']
b. ['XY', 'YZ']
c. [None, None]
d. None of these
Explanation: None
i = 1:
while True:
if i%3 == 0:
break
print(i)
a. 1 2 3
b. 3 2 1
c. 1 2
d. Invalid syntax
a=1
while True:
if a % 7 = = 0:
break
print(a)
a += 1
a. 1 2 3 4 5
b. 1 2 3 4 5 6
c. 1 2 3 4 5 6 7
d. Invalid syntax
Answer: (b) 1 2 3 4 5 6
Explanation: None
i=0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)
a. 1 2 3
b. 0 1 2 3
c. 0 1 2
d. 3 2 1
Answer: (c) 0 1 2
Explanation: None
i=0
while i < 3:
print(i)
i += 1
else:
print(0)
a. 0 1
b. 0 1 2
c. 0 1 2 0
d. 0 1 2 3
Answer: (c) 0 1 2 0
Explanation: None
a. xyz
b. No output
c. x y z
d. j j j j j j j..
x = 'pqrs'
for i in range(len(x)):
x[i].upper()
print (x)
a. PQRS
b. pqrs
c. qrs
d. None of these
a. a b c
b. 0 1 2
c. 0 a 1b 2c
d. None of these above
Answer: (b) 0 1 2
Explanation: None
d = {0, 1, 2}
for x in d:
print(x)
Answer: (b) 0 1 2
Explanation: None
49) Which of the following option is not a core data type in the python language?
a. Dictionary
b. Lists
c. Class
d. All of the above
Explanation: Class is not a core data type because it is a user-defined data type.
50) What error will occur when you execute the following code?
MANGO = APPLE
a. NameError
b. SyntaxError
c. TypeError
d. ValueError
def example(a):
aa = a + '1'
aa = a*1
return a
>>>example("javatpoint")
a. hello2hello2
b. hello2
c. Cannot perform mathematical operation on strings
d. indentationError
Explanation: None
a. Dictionary
b. Tuple
c. List
d. Stack
a. False
b. Ture
c. ValueError occurs
d. TypeError occurs
a. invalid code
b. JavaTpoint has not exist
c. JavaTpoint has exist
d. none of these above
Explanation: A new exception class must inherit from a BaseException, and there is no such inheritance here.
z = {"x":0, "y":1}
a. x dictionary z is created
b. x and y are the keys of dictionary z
c. 0 and 1 are the values of dictionary z
d. All of the above
Q.21Method which uses a list of well defined instructions to complete a task starting from a given
initial state
(d)A & B
Q.22The chart that contains only function flow and no code is called as
(a)Multiprogramming (b)Multiprocessing
(c)Timesharing
(d)None
Q.26 A structured chart is
(a)Better Programming
(b)Efficient Coding
Q.32Which of the following symbol in a flowchart are used to indicate all arithmetic processes of
adding, subtracting, multiplying and dividing ?
(a)Input/output
(b)terminal
(c)Processing (d)Decision
Q.33 A flowchart that outlines the main segments of program is called as
(a)Micro flowchart (b)Macro flowchart (c)Flowchart
(d)Algorithm
Q.34 A flowchart that outlines with all detail is called as
(c)Hardware Language
(d)Algorithm
(a)Development (b)Coding
(c)Design
(d)Debugging
Q.37 In which of the following pseudo code instructions are written in the order or sequence in
which
they are to be performed?
(a)Selection Logic (b)Sequence Logic (c)Iteration Logic (d)Looping Logic
Q.38 Which of the following logic is used to produce loops in program logic when one or more
instruction may be executed several times depending on some conditions?
(a) Iteration Logic (b) Selection Logic (c) Sequence Logic (d)Decision Logic
Q.39 Selection logic also called as
(a) Decision Logic (b) Iteration Logic
(c) Sequence Logic (d)Looping Logic
Q.40 Which of the following program planning tool allows the programmers to plan program logic
by writing
program instruction in an ordinary language?
(a)Flowchart (b)Pseudo code (c)Program (d)Looping
Q.41Which logic is used to select the proper path out of two or more alternative paths in program
logic
(a)Looping Logic (b)Sequence Logic (c)Iteration Logic (d)Selection Logic
Q.42 Which of the following control structures are used in iteration logic
(a)Draw a flowchart
(i)
(ii) (iii)
(iv) (v)
(a)Connecting
(b)Input/Output
(c)Processing
(d)Terminal
(e)Decision
ANS=i-(d),ii-(e),iii-(c),iv-(a);
Q.45 which of the following file conations the programmer‟s original program code?
(a)Application file (b)Executing (c)Object file (d)Source file
16
Q.46 Algorithm is
(a)Connecter (b)Decision
(c)Statement
(d) All of the above
Q.48 Which of the following is not a characteristic of good algorithm?
(a)Precise
(b)Finite number of steps
(c)Ambiguous
(d)None of above
Q.51 After a programmer plans the logic of a program ,she /he will next____
Q.57structured program can be easily broken down into routines or _______that can be assigned to
any
number of programmers
(a)Segments (b)Modules (c)Units (d)Sequences
1. Python was developed by
A. Guido van Rossum
B. James Gosling
C. Dennis Ritchie
D. Bjarne Stroustrup
View Answer
Ans : A
Explanation: Python file is saved with an extension .py. So, Option B is correc
Explanation: Yes python is object oriented language but not pure. It does not support
strong encapsulation while it is one of the core features of an "object-oriented"
programming language. So, Option A is correct.
Explanation: Python 3.0 was released on December 3rd, 2008. So, Option B is correct.
Explanation: Python uses indentation to define block of code. Indentations are simply
Blank spaces or Tabs which is used as an indicator that indented code is the child part.
As curlybraces are used in C/C++/Java.. So, Option B is correct.
Explanation: # is used for single line is made comment in Python. So, Option C is
correct.
Explanation: python file with .py extension called module. So, Option B is correct.
b=5
x=(a&b)|(a&a)|(a|b)
print("x")
A. 1
B. 5
C. 0
D. None of the above
View Answer
Ans : D
Explanation: Output of the following python code x. Therefore output is none of the
above.
elif True:
else:
A. inside if block
B. inside elif block
C. inside else block
D. Error
View Answer
Ans : B
Explanation: Output of the following python code is inside elif block. Therefore Option B
is correct.
num1=2
num2=0
for i in range(4):
num1+=2
for j in range(len(str1)):
num2=num2+num1
num3=num2%int(str1)
print(num3)
A. 7
B. Infinite Loop
C. 0
D. Error
View Answer
Ans : C
Explanation: The Output for the following code is 0. So, Option C is correct.
16. What will be the result of following Python code snippet after
execution?
str1=""
i=0
var2=1
while(i<3):
var1=1
if str1:
var2=var1*var2+5
else:
var2=var1*var2+1
i=i+1
print(var2)
A. 16
B. 12
C. 11
D. 4
View Answer
Ans : B
Explanation: The Output for the following code is 12. So, Option B is correct.
Explanation: If A is proper subset of B then hen all elements of A are in B but B contains
at least one element that is not in B.
A. i, iii
B. i, iv
C. ii, iv
D. ii, iii
View Answer
Ans : C
Explanation: It returns the index position of first occurance of "m" in the string str1. So
Option D is correct.
Explanation: One of the following is correct way of declaring and initialising a variable, x
with value 5 is x=5.
Explanation: All of the above options are correct regarding variables in Python.
Explanation: There are 33 keywords in Python 3.7 . Keywords are reserved words that
can not be used as variables. So, Option B is correct.
Explanation: All keywords in python are in lowercase except True, False and None. So,
Option C is correct.
Explanation: Variable name can not begin with number it can begin with letter or
underscore. So, Option D is correct.
29. Which of the following is a valid variable?
A. var@
B. 32var
C. class
D. abc_a_c
View Answer
Ans : D
Explanation: Variable name should not be keyword, cannot begin with digit and should
not contain special symbol. Hence D is the correct identifier or variable. So, Option D is
correct.
Explanation: All "in", "is" and "assert" are keywords. So, Option D is correct.
32. What is the output of the following code : print 5//2
A. 2
B. 2.5
C. 2.0
D. Error
View Answer
Ans : A
Explanation: Floor Division "//" - The division of operands where the result is the
quotient in which the digits after the decimal point are removed. So in this case we get 2
as a answer. So, Option A is correct.
SINHGAD COLLEGE OF ENGINEERING
Department of Information Technology
Programming and Problem Solving (110005)
UNIT-II- Decision Control Statements
Multiple choice questions Bank
Content:
Decision Control Statements: Decision control statements, Selection/conditional branching
Statements: if, if-else, nested if, if-elif-else statements. Basic loop Structures/Iterative statements:
while loop, for loop, selecting appropriate loop. Nested loops, The break, continue, pass, else
statement used with loops. Other data types- Tuples, Lists and Dictionary.
A. for loop
B. while loop
C. do-while loop
D. None of the above
Answer: C
list1 = [3 , 2 , 5 , 6 , 0 , 7, 9]
sum = 0
sum1 = 0
for elem in list1:
if (elem % 2 == 0):
sum = sum + elem
continue
if (elem % 3 == 0):
sum1 = sum1 + elem
print(sum , end=" ")
print(sum1)
A. 8 9
B. 8 3
C. 2 3
D. 8 12
Answer: D
A. if a>=2 :
B. if (a >= 2)
C. if (a => 22)
D. if a >= 22
Answer: A
A. else if
B. elseif
C. elif
D. None of the above
Answer: C
A. Yes
B. No
C. if/else not used in python
D. None of the above
Answer: A
A. pq
B. rs
C. pqrs
D. pq12
Answer: A
A. if a = b:
B. if a == b:
C. if a === c:
D. if a == b
Answer: B
A. True
B. False
C. Python has switch statement but we cannot use it.
D. None of the above
Answer: B
A. 2
B. 6 5 2
C. 3
D. 5 2
Answer: A
14. What will be the output of given Python code?
str1="hello"
c=0
for x in str1:
if(x!="l"):
c=c+1
else:
pass
print(c)
A. 2
B. 0
C. 4
D. 3
Answer: D
15. Which of the following Python code will give different output from the others?
A. for i in range(0,5):
print(i)
B. for j in [0,1,2,3,4]:
print(j)
C. for k in [0,1,2,3,4,5]:
print(k)
D. for l in range(0,5,1):
print(l)
Answer: C
A. rnpn ea
B. rnpn ear
C. rnp ea
D. rnp ear
Answer: B
19. Which of the following sequences would be generated bt the given line of code?
range (5, 0, -2)
A. 5 4 3 2 1 0 -1
B. 5 4 3 2 1 0
C. 5 3 1
D. None of the above
Answer: C
A. indefinite
B. discriminant
C. definite
D. indeterminate
Answer: A
21. When does the else statement written after loop executes?
A. a b c d e f
B. abcdef
C. i i i i i.....
D. No Output
Answer: D
23. What will be the output of the following code?
x = "abcd"
for i in range(len(x)):
print(i)
A. abcd
B. 0 1 2 3
C. 1 2 3 4
D. a b c d
Answer: B
A. 12
B. 1 2
C. Error
D. None of the above
Answer: C
A. No output
B. ice cream
C. "ice cream is tasty!"
D. None of the above
Answer: C
A. prrrr
B. ruf
C. prrrrruff
D. None of the above
Answer: B
27. What does the following code print to the console?
if 5 > 10:
print("fan")
elif 8 != 9:
print("glass")
else:
print("cream")
"glass" is printed.
A. fan
B. cream
C. glass
D. None of the above
Answer: C
A. ireland
B. maria
C. usa
D. colombia
Answer: D
A. error
B. cardio
C. No output
D. None of the above
Answer: C
A. 201
B. 101201
C. 101
D. error
Answer: C
A. BMW
B. Ford
C. Nissan
D. Audi
Answer: B
A. huh?
B. 0 is falsy
C. huh? 0 is falsy
D. No Output
Answer: B
if 4 + 5 == 10:
print("TRUE")
else:
print("FALSE")
print("TRUE")
A. TRUE
B.
TRUE
FALSE
C.
FALSE
TRUE
D.
TRUE
FALSE
TRUE
Answer: A
x=3
if (x > 2):
x = x * 2;
if (x > 4):
x = 0;
print(x)
A. x will always equal 0 after this code executes for any value of x
B. if x is greater than 2, the value in x will be doubled after this code executes
C. if x is greater than 2, x will equal 0 after this code executes
D. None of the Above
Answer: C
A. $3.45
B. $3.11
C. $3.105
D. $3.10
Answer: C
37. Given two variables, num1 and num2, which of the following would mean that both num1 and num2
are positive integers?
A. (num1 = num2)
B. (num1 = num2) OR (num1 ≠ num2)
C. (num1 = num2) AND (num1<0)
D. (num1 = num2) AND (num2>0)
Answer: D
A. [‘AB’, ‘CD’]
B. [‘ab’, ‘cd’, ‘AB’, ‘CD’]
C. [‘ab’, ‘cd’]
D. none of the mentioned
Answer: D
A. 1 2
B. 1 2 3
C. error
D. none of the mentioned
Answer: C
A. 1 2 3 4 5 6
B. 1 2 3 4 5 6 7
C. error
D. none of the mentioned
Answer: A
41. What will be the output of the following Python code?
True = False
while True:
print(True)
break
A. True
B. False
C. None
D. none of the mentioned
Answer: D
A. 0 1 2 3 0
B. 0 1 2 0
C. 0 1 2
D. error
Answer: B
A. no output
B. i i i i i i …
C. a a a a a a …
D. a b c d e f
Answer: B
A. i i i i i i
B. a a a a a a
C. a a a a a
D. none of the mentioned
Answer: B
45. What will be the output of the following Python code?
x = "abcdef"
i = "a"
while i in x:
x = x[1:]
print(i, end = " ")
A. a a a a a a
B. a
C. no output
D. error
Answer: B
A. a a a a a a
B. a
C. no output
D. error
Answer: C
A. a B C D
B. a b c d
C. A B C D
D. error
Answer: B
A. a b c d
B. A B C D
C. a B C D
D. error
Answer: B
A. a b c d
B. 0 1 2 3
C. error
D. 1 2 3 4
Answer: B
1. Which of the following is the use of function in python?
a) Functions are reusable pieces of programs
b) Functions don’t provide better modularity for your application
c) you can’t also create your own functions
d) All of the mentioned
Answer: a
2. Which keyword is used for function?
a) Fun
b) Define
c) Def
d) Function
Answer: c
3. What will be the output of the following Python code?
1. def sayHello():
2. print('Hello World!')
3. sayHello()
4. sayHello()
a)
Hello World!
Hello World!
b)
'Hello World!'
'Hello World!'
c)
Hello
Hello
d) None of the mentioned
Answer: a
a)
6
15
b)
6
100
c)
123
12345
d) None of the mentioned
Answer: a
21. Python supports the creation of anonymous functions at runtime, using a construct
called __________
a) lambda
b) pi
c) anonymous
d) none of the mentioned
Answer: a
22. What will be the output of the following Python code?
1. y = 6
2. z = lambda x: x * y
3. print z(8
a) 48
b) 14
c) 64
d) None of the mentioned
Answer: a
23. What will be the output of the following Python code?
1. lamb = lambda x: x ** 3
2. print(lamb(5))
a) 15
b) 555
c) 125
d) None of the mentioned
Answer: c
24. Does Lambda contains return statements?
a) True
b) False
Answer: b
25. Lambda is a statement.
a) True
b) False
Answer: b
26. Lambda contains block of statements.
a) True
b) False
Answer: b
27. What will be the output of the following Python code?
1. def f(x, y, z): return x + y + z
2. f(2, 30, 400
a) 432
b) 24000
c) 430
d) No output
Answer: a
28. What will be the output of the following Python code?
1. def writer():
2. title = 'Sir'
3. name = (lambda x:title + ' ' + x)
4. return name
5.
6. who = writer()
7. who('Arthur')
a) Arthur Sir
b) Sir Arthur
c) Arthur
d) None of the mentioned
Answer: b
29. What will be the output of the following Python code?
1. L = [lambda x: x ** 2,
2. lambda x: x ** 3,
3. lambda x: x ** 4]
4.
5. for f in L:
6. print(f(3))
a)
27
81
343
b)
6
9
12
c)
9
27
81
d) None of the mentioned
Answer: c
30. What will be the output of the following Python code?
1. min = (lambda x, y: x if x < y else y)
2. min(101*99, 102*98)
a) 9997
b) 9999
c) 9996
d) None of the mentioned
Answer: c
31. What is a variable defined outside a function referred to as?
a) A static variable
b) A global variable
c) A local variable
d) An automatic variable
Answer: b
32. What is a variable defined inside a function referred to as?
a) A global variable
b) A volatile variable
c) A local variable
d) An automatic variable
Answer: c
UNIT-IV STRINGS
s = "python rocks"
print(s[1] * s.index("n"))
A. yyyyy
B. 55555
C. n
D. Error, you cannot combine all those things together.
Ans-D
Ans-A
Ans-A
s1 = "HEY"
s2 = s1.lower()
s3 = s2.capitalize()
A. Hey
B. hey
C. HEY
D. HEy
Ans- C
Mali = 5
print("Mali" + " is " + str(Mali))
A. Mali is Mali
B. Mali is 5
C. 5 is Mali
D. 5 is 5
Ans- B
s = "python rocks"
print(s[3])
A. t
B. h
C. c
D. Error, you cannot use the [ ] operator with a string.
Ans- B
s = "python rocks"
print(s[2] + s[-5])
A. tr
B. ps
C. nn
D. Error, you cannot use the [ ] operator with the + operator.
Ans-A
s = "python rocks"
print(len(s))
A. 11
B. 12
C. 10
D. 7
Ans-B
Q-9: What is printed by the following statements:
s = "Rose"
s[1] = "i"
print(s)
A. Rose
B. Rise
C. Error
D. Rsoe
Ans-C
s = "ball"
r = ""
for item in s:
r = item.upper() + r
Print(r)
A. Ball
B. BALL
C. LLAB
D. LAB
Ans-C
s = "python rocks"
print(s[7:11] * 3)
A. rockrockrock
B. rock rock rock
C. rocksrocksrocks
D. Error, you cannot use repetition with slicing.
Ans-A
A. True
B. False
Ans-B
Q-13: In Python, strings are…
A. str objects
B. Incorrect
C. changeable
D. Immutable
Ans-C
>>>"a"+"bc"
a) a
b) bc
c) bca
d) abc
Answer: d
Explanation: + operator is concatenation operator.
>>>"abcd"[2:]
a) a
b) ab
c) cd
d) dc
Answer: c
Answer: b
Explanation: Execute in shell and check.
Q-16: What will be the output of the following Python code?
a) olleh
b) hello
c) h
d) o
Answer: d
Explanation: -1 corresponds to the last index.
Answer: c
Explanation: + is used to concatenate and * is used to multiply strings.
>>>print (r"\nhello")
Answer: b
Explanation: When prefixed with the letter ‘r’ or ‘R’ a string literal becomes a raw
string and the escape sequences such as \n are not converted.
>>>print('new' 'line')
a) Error
b) Output equivalent to print ‘new\nline’
c) newline
d) new line
Answer: c
Explanation: String literal separated by whitespace are allowed. They are
concatenated.
Q-20: What will be the output of the following Python statement?
>>> print('x\97\x98')
a) Error
b)
97
98
c) x\97
d) \x97\x98
Answer: c
Explanation: \x is an escape sequence that means the following 2 digits are a
hexadecimal number encoding a character.
>>>str1="helloworld"
>>>str1[::-1]
a) dlrowolleh
b) hello
c) world
d) helloworld
Answer: a
Explanation: Execute in shell to verify.
Answer: d
Explanation: 0xA and 0xB and 0xC are hexadecimal integer literals representing the
decimal values 10, 11 and 12 respectively. There sum is 33.
Q-23: What will be the output of above Python code?
str1="6/4"
print("str1")
A. 1
B. 6/4
C. 1.5
D. str1
Ans : D
Explanation: Since in print statement, str1 is written inside double quotes so it
will simply print str1 directly.
str1="python"
A. print(str1[2])
B. str1[1]="x"
C. print(str1[0:9])
D. Both (b) and (c)
Ans : B
Explanation: Strings are immutable. So,new values cannot be assigned at any
index position in a string
A. String is immutable.
B. capitalize() function in string is used to return a string by converting the whole
given string into uppercase.
C. lower() function in string is used to return a string by converting the whole given
string into lowercase.
D. None of these.
Ans : B
Explanation: capitalize() function in string gives the output by converting only the
first character of the string into uppercase and rest characters into
lowercase.However, upper() function is used to return the whole string into
uppercase.
Q-26: What will be the output of below Python code?
str1="Information"
print(str1[2:8])
A. format
B. formatio
C. orma
D. ormat
Ans : A
Explanation: Concept of slicing is used in this question. In string slicing,the
output is the substring starting from the first given index position i.e 2 to one less
than the second given index position i.e.(8-1=7) of the given string str1. Hence,
the output will be "format".
str1="Aplication"
str2=str1.replace('a','A')
print(str2)
A. application
B. Application
C. ApplicAtion
D. applicAtion
Ans : C
Explanation: replace() function in string is used here to replace all the existing "a"
by "A" in the given string.
Q-28: What will be the output of below Python code?
str1="poWer"
str1.upper()
print(str1)
A. POWER
B. Power
C. power
D. poWer
Ans : D
Explanation: str1.upper() returns the uppercase of whole string str1. However,it
doesnot change the string str1. So, output will be the original str1.
str1.find("save")
A. It returns the first index position of the first occurance of "save" in the given
string str1.
B. It returns the last index position of the last occurance of "save" in the given
string str1.
C. It returns the last index position of the first occurance of "save" in the given
string str1.
D. It returns the first index position of the first occurance of "save" in the given
string str1.
Ans : A
Explanation: It returns the first index position of the first occurance of "save" in
the given string str1.
Q-30:What will the below Python code will return?
list1=[0,2,5,1]
str1="7"
for i in list1:
str1=str1+i
print(str1)
A. 70251
B. 7
C. 15
D. Error
Ans : D
Explanation: list1 contains integers as its elements. Hence these cannot be
concatenated to string str1 by simple "+" operand. These should be converted to
string first by use of str() function,then only these will get concatenated.
If str1="John,Simon,Aryan"
A. print(str1[-7:-12])
B. print(str1[-11:-7])
C. print(str1[-11:-6])
D. print(str1[-7:-11])
Ans : C
Explanation: Slicing takes place at one index position less than the given second
index position of the string. So,second index position will be -7+1=-6.
Q-32: What will following Python code return?
str1="Stack of books"
print(len(str1))
A. 13
B. 14
C. 15
D. 16
Ans : B
Explanation: len() returns the length of the given string str1, including spaces and
considering " " as a single character.
print r"\nhello"
The output is
A. a new line and hello
B. \nhello
C. the letter r and then hello
D. Error
Ans: B
Answer: Option C
Explanation:
Cannot concantenate str and int objects.
Q-35:What is the output of the following?
print('ab'.isalpha())
A. True
B. False
C. None
D. Error
Answer: Option A
Explanation:
The string has only letters.
A - Hello World!
B-H
C - ello World!
Answer : B
Explanation
H is the correct answer.
Q-37: What is the output of print tinylist * 2 if tinylist = [123, 'john']?
B - [123, 'john'] * 2
C - Error
Answer : A
Explanation
It will print list two times. Output would be [123, 'john', 123, 'john'].
A - repr(x)
B - eval(str)
C - tuple(s)
D - list(s)
Answer : B
Explanation
eval(str) − Evaluates a string and returns an object.
Q-39:Which of the following function convert a single character to its integer
value in python?
A - unichr(x)
B - ord(x)
C - hex(x)
D - oct(x)
Answer : B
Explanation
ord(x) − Converts a single character to its integer value.
A - break
B - continue
C - pass
Answer : C
Explanation
pass statement − The pass statement in Python is used when a statement is
required syntactically but you do not want any command or code to execute.
Q-41 : Which of the following function checks in a string that all characters are in
uppercase?
A - isupper()
B - join(seq)
C - len(string)
D - ljust(width[, fillchar])
Answer : A
Explanation
isupper() − Returns true if string has at least 1 character and all characters are in
uppercase.
Q-42 : Which of the following function returns the min alphabetical character
from the string str?
A - lower()
B - lstrip()
C - max(str)
D - min(str)
Answer : D
Explanation
min(str) − Returns the min alphabetical character from the string str.
Q-43 : What is the output of ['Hi!'] * 4?
B - ['Hi!'] * 4
C - Error
Answer : A
Explanation
['Hi!', 'Hi!', 'Hi!', 'Hi!']
Q-44 : What is the following function removes last object from a list?
A - list.index(obj)
B - list.insert(index, obj)
C - list.pop(obj=list[-1])
D - list.remove(obj)
Answer : C
Explanation
list.pop(obj=list[-1]) − Removes and returns last object or obj from list.
Combined Extra Multiple Choice Questions
Answer : B
2. How to output the string “May the odds favor you” in Python?
Answer : A
Answer : B
Answer : D
Answer : A
Answer : D
Answer : B
Answer : C
Python:
1
2str = "My name is Kevin"
3
A. str[11:16]
B. str(11:16)
C. str[11][16]
D. str[11-16]
Answer : A
10. Which of the following is the correct way to access a specific character. Let’s
say we need to access the character “K” from the following string in Python?
1
2str = "My name is Kevin"
3
A. str(11)
B. str[11]
C. str:9
D. None of the above
Answer : B
11. Which Python module is used to parse dates in almost any string format?
A. datetime module
B. time module
C. calendar module
D. dateutil module
Answer : D
12. Which of the following is the correct way to indicate Hexadecimal Notation in
Python?
A. str = ‘\62’
B. str = ’62’
C. str = “62”
D. str = ‘\x62’
Answer : D
13. To begin slicing from the end of the string, which of the following is used in
Python?
A. Indexing
B. Negative Indexing
C. Begin with 0th index
D. Escape Characters
Answer : B
Answer : A
Answer : B
16. What is the correct way to get maximum value from Tuple in Python?
A. print (max(mytuple));
B. print (maximum(mytuple));
C. print (mytuple.max());
D. print (mytuple.maximum);
Answer : A
17. How to fetch and display only the keys of a Dictionary in Python?
A. print(mystock.keys())
B. print(mystock.key())
C. print(keys(mystock))
D. print(key(mystock))
Answer : A
19. How to access value for key “Price” in the following Python Dictionary:
1
2mystock = {
3"Product": "Earphone",
4"Price": 800,
5"Quantity": 50,
6"InStock" : "Yes"
7}
8
A. mystock[“Product”]
B. mystock(“Product”)
C. mystock[Product]
D. mystock(Product)
Answer : A
Answer : A
21. ___________ uses square brackets for comma-separated values in Python? Fill
Answer : A
Answer : A
23. How to find the index of the first occurrence of a specific value “i”, from string
“This is my website”?
A. str.find(“i”)
B. str.find(i)
C. str.index()
D. str.index(“i”)
Answer : D
Answer : B
25. Which of the following is the correcy way to access a specific element from a
Multi-Dimensional List?
A. list[row_size:column_size]
B. list[row_size][column_size]
C. list[(row_size)(column_size)]
D. None of the above
Answer : B
26. Which of the following Bitwise operators in Python shifts the left operand
value to the right, by the number of bits in the right operand. The rightmost bits
Answer : B
27. How to specify range of index in Python Tuples? Let’s say our tuple name is
“mytuple”
A. print(mytuple[1:4])
B. print(mytuple[1 to 4])
C. print(mytuple[1-4])
D. print(mytuple[].slice[1:4])
Answer : A
Answer : B
Answer : A
Answer : C
31. How to delete an element in a Dictionary with a specific key. Let’s say we need
Answer : B
Answer : A
34. Which of the following is used to empty the Dictionary in Python? Let’s say
Answer : D
Answer : A
36. What is used in Python functions, if you have no idea about the number of
arguments to be passed?
A. Keyword Arguments
B. Default Arguments
C. Required Arguments
D. Arbitrary Arguments
Answer : D
37. What is the correct way to create a Dictionary in Python?
A. mystock = {“Product”: “Earphone”, “Price”: 800, “Quantity”: 50}
B. mystock = {“Product”- “Earphone”, “Price”-800, “Quantity”-50}
C. mystock = {Product[Earphone], Price[800], Quantity[50]}
D. None of the above
Answer : A
38. In Python Functions, what is to be used to skip arguments or even pass them in
Answer : A
Answer : C
Answer : C
Answer : B
42. How to convert the lowercase letters in the string to uppercase in Python?
A. upper()
B. uppercase()
C. capitalize()
D. toUpper()
Answer : A
43. What is the correct way to get minimum value from a List in Python?
A. print (minimum(mylist));
B. print (min(mylist));
C. print (mylist.min());
D. print (mylist.minimum());
Answer : B
Answer : A
Answer : A
Answer : C
47. How to compare two operands in Python and check for equality? Which
operator is to be used?
A. =
B. in operator
C. is operator
D. == (Answer)
Answer : D
48. What is the correct way to get minimum value from Tuple?
A. print (min(mytuple));
B. print (minimum(mytuple));
C. print (mytuple.min());
D. print (mytuple.minimum);
Answer : A
Answer : D
50. How to compare two objects and check whether they have same memory
locations?
A. is operator
B. in operator
C. **
D. Bitwise operators
Answer : A
51. How to fetch and display only the values of a Dictionary in Python?
A. print(mystock.value())
B. print(mystock.values())
C. print(values(mystock))
D. print(value(mystock))
Answer : A
Answer : B
Answer : A
Answer : C
55. Which of the following Bitwise operators sets each bit to 1, if only one of them
Answer : A
56. What is the correct way to get maximum value from a List in Python?
A. print (maximum(mylist));
B. print (mylist.max());
C. print (max(mylist));
D. print (mylist.maximum());
Answer : C
57. An example to correctly begin searching from the end range of index in
Answer : D
58. Which of the following Bitwise operators in Python shifts the left operand
value to the left, by the number of bits in the right operand. The leftmost bits
Answer : C
59. Can we update Tuples or any of its elements in Python after assignment?
A. Yes
B. No
Answer : A
60. How to display only the current month’s name in Python?
A. date.strftime(“%H”)
B. date.strftime(“%B”)
C. date.strftime(“%m”)
D. date.strftime(“%d”)
Answer : B
Answer : B
Answer : A
Answer : B
Answer : B
65. What is the correct way to create a list with type float?
A. mylist = [5.7, 8.2, 9. 3.8, 2.9]
B. mylist = (5.7, 8.2, 9. 3.8, 2.9)
C. mylist = {5.7, 8.2, 9. 3.8, 2.9}
D. None of the above
Answer : A
Answer : A
Answer : A
68. How to fetch last element from a Python List with negative indexing?
A. print(“Last element = “,mylist[0])
B. print(“Last element = “,mylist[])
C. print(“Last element = “,mylist[-1])
D. None of the above
Answer : C
69. What is the correct way to get the length of a List in Python?
A. mylist.count()
B. count(mylist)
C. len(mylist)
D. length(mylist)
Answer : C
70. To get today’s date, which Python module is to be imported?
A. calendar module
B. datetime module
C. dateutil module
D. None of the above
Answer : B
UNIT-V
OBJECT ORIENTED PROGRAMMING
A. Objects are real world entities while classes are not real.
B. Classes are real world entities while objects are not real.
C. Both objects and classes are real world entities.
D. Both object and classes are not real.
Ans : A
Explanation: In OOP, classes are basically the blueprint of the objects. They
doesnot have physical existence.
2. How many objects and reference variables are there for the given Python code?
class A:
print("Inside class")
A()
A()
obj=A()
A. 2 and 1
B. 3 and 3
C. 3 and 1
D. 3 and 2
Ans : C
Explanation: obj is the reference variable here and an object will be created each
time A() is called.So there will be 3 objects created.
class Student:
def __init__(self,id,age):
self.id=id
self.age=age
std=Student(1,20)
class Student:
def __init__(self,name,id):
self.name=name
self.id=id
print(self.id)
std=Student("Simon",1)
std.id=2
print(std.id)
A. 1
1
B. 1
2
C. 2
1
D. 2
2
Ans : B
Explanation: When object with id =1 is created for Student, constructor is invoked
and it prints 1. Later, id has been changed to 2, so next print statement prints 2.
A. 100
100
B. 100
102
C. 102
102
D. Error
Ans : B
Explanation: By default,the value of "count" is 100, so obj1.count=100. For
second object, value of "count" is 102, so obj2.count=102.
class A:
def __init__(self,name):
self.name=name
a1=A("john")
a2=A("john")
class A:
def __init__(self):
self.count=5
self.count=count+1
a=A()
print(a.count)
A. 5
B. 6
C. 0
D. Error
Ans : D
Explanation: It will throw an error as inside constructor, "count" is not defined.
class Book:
def __init__(self,author):
self.author=author
book1=Book("V.M.Shah")
book2=book1
A. Both book1 and book2 will have reference to two different objects of class Book.
B. id(book1) and id(book2) will have same value.
C. It will throw error as multiple references to same object is not possible.
D. None of the above
Ans : B
Explanation: book1 and book2 will reference to the same object. Hence, id(book1)
and id(book2) will have same value.
A. attribute
B. object
C. argument
D. function
Ans : D
Explanation: In OOP of Python, function is known by "method".
10. What will be the output of below Python code?
class A:
def __init__(self,num):
num=3
self.num=num
def change(self):
self.num=7
a=A(5)
print(a.num)
a.change()
print(a.num)
A. 5
7
B. 5
5
C. 3
3
D. 3
7
Ans : D
Explanation: Inside constructor, self.num=3. First print statement prints 3. As,
method change() is invoked, self.num=7 and hence second print statement prints
7.
Answer: c
Explanation: Instantiation simply refers to creation of an instance of class. It is not
a fundamental feature of OOP.
12. Which of the following is the most suitable definition for encapsulation?
a) Ability of a class to derive members of another class as a part of its own definition
b) Means of bundling instance variables and methods in order to restrict access to
certain class members
c) Focuses on variables and passing of variables to functions
d) Allows for implementation of elegant software that is well designed and easily
modified
Answer: b
Explanation: The values assigned by the constructor to the class members is used to
create the object.
class Demo:
def __init__(self):
self.a = 1
self.__b = 1
def display(self):
return self.__b
obj = Demo()print(obj.a)
a) The program has an error because there isn’t any function to return self.a
b) The program has an error because b is private and display(self) is returning a
private member
c) The program runs fine and 1 is printed
d) The program has an error as you can’t name a class member using __b
Answer: c
Explanation: The program has no error because the class member which is public is
printed. 1 is displayed. Execute in python shell to verify.
class Demo:
def __init__(self):
self.a = 1
self.__b = 1
def display(self):
return self.__b
obj = Demo()print(obj.__b)
a) The program has an error because there isn’t any function to return self.a
b) The program has an error because b is private and display(self) is returning a
private member
c) The program has an error because b is private and hence can’t be printed
d) The program runs fine and 1 is printed
Answer: c
Explanation: Variables beginning with two underscores are said to be private
members of the class and they can’t be accessed directly.
15. Methods of a class that provide access to private members of the class are called
as ______ and ______
a) getters/setters
b) __repr__/__str__
c) user-defined functions/in-built functions
d) __init__/__del__
Answer: a
Explanation: The purpose of getters and setters is to get(return) and set(assign)
private instance variables of a class.
Answer: b
Explanation: Variables such as self.__b are private members of the class.
17. What will be the output of the following Python code?
class Demo:
def __init__(self):
self.a = 1
self.__b = 1
def get(self):
return self.__b
obj = Demo()print(obj.get())
a) The program has an error because there isn’t any function to return self.a
b) The program has an error because b is private and display(self) is returning a
private member
c) The program has an error because b is private and hence can’t be printed
d) The program runs fine and 1 is printed
Answer: d
Explanation: Here, get(self) is a member of the class. Hence, it can even return a
private member of the class. Because of this reason, the program runs fine and 1 is
printed.
class Demo:
def __init__(self):
self.a = 1
self.__b = 1
def get(self):
return self.__b
obj = Demo()
obj.a=45print(obj.a)
a) The program runs properly and prints 45
b) The program has an error because the value of members of a class can’t be changed
from outside the class
c) The program runs properly and prints 1
d) The program has an error because the value of members outside a class can only be
changed as self.a=45
Answer: a
Explanation: It is possible to change the values of public class members using the
object of the class.
Answer: b
Explanation: Private members of a class are accessible if written as follows:
obj._Classname__privatemember. Such renaming of identifiers is called as name
mangling.
20. The purpose of name mangling is to avoid unintentional access of private class
members.
a) True
b) False
Answer: a
Explanation: Name mangling prevents unintentional access of private members of a
class, while still allowing access when needed. Unless the variable is accessed with its
mangled name, it will not be found.
class fruits:
def __init__(self):
self.price = 100
self.__bags = 5
def display(self):
print(self.__bags)
obj=fruits()
obj.display()
a) The program has an error because display() is trying to print a private class member
b) The program runs fine but nothing is printed
c) The program runs fine and 5 is printed
d) The program has an error because display() can’t be accessed
Answer: c
Explanation: Private class members can be printed by methods which are members
of the class.
class student:
def __init__(self):
self.marks = 97
self.__cgpa = 8.7
def display(self):
print(self.marks)
obj=student()print(obj._student__cgpa)
a) The program runs fine and 8.7 is printed
b) Error because private class members can’t be accessed
c) Error because the proper syntax for name mangling hasn’t been implemented
d) The program runs fine but nothing is printed
Answer: a
Explanation: Name mangling has been properly implemented in the code given
above and hence the program runs properly.
Answer: c
Explanation: Protected class members can’t be accessed by name mangling.
class objects:
def __init__(self):
self.colour = None
self._shape = "Circle"
def display(self, s):
self._shape = s
obj=objects()print(obj._objects_shape)
a) The program runs fine because name mangling has been properly implemented
b) Error because the member shape is a protected member
c) Error because the proper syntax for name mangling hasn’t been implemented
d) Error because the member shape is a private member
Answer: b
Explanation: Protected members begin with one underscore and they can only be
accessed within a class or by subclasses.
Answer: b
Explanation: Inheritance indicates the code reusability. Encapsulation and
abstraction are meant to hide/group data into one element. Polymorphism is to
indicate different tasks performed by a single entity.
26. If a function can perform more than 1 type of tasks, where the function name
remains same, which feature of OOP is used here?
a) Encapsulation
b) Inheritance
c) Polymorphism
d) Abstraction
Answer: c
Explanation: For the feature given above, the OOP feature used is Polymorphism.
Example of polymorphism in real life is a kid, who can be a student, a son, a
brother depending on where he is.
27. If different properties and functions of a real world entity is grouped or embedded
into a single element, what is it called in OOP language?
a) Inheritance
b) Polymorphism
c) Abstraction
d) Encapsulation
Answer: d
Explanation: It is Encapsulation, which groups different properties and functions of
a real world entity into single element. Abstraction, on other hand, is hiding of
functional or exact working of codes and showing only the things which are
required by the user.
Answer: c
Explanation: Data must be declared using objects. Object usage is mandatory
because it in turn calls its constructors, which in turn must have a class defined. If
object is not used, it is a violation of pure OOP concept.
29. Which among the following doesn’t come under OOP concept?
a) Platform independent
b) Data binding
c) Message passing
d) Data hiding
Answer: a
Explanation: Platform independence is not feature of OOP. C++ supports OOP but
it’s not a platform independent language. Platform independence depends on
programming language.
class student{ int marks; };class topper:public student{ int age; topper(int
age){ this.age=age; } };
a) Inheritance
b) Polymorphism
c) Inheritance and polymorphism
d) Encapsulation and Inheritance
Answer: d
Explanation: Encapsulation is indicated by use of classes. Inheritance is shown by
inheriting the student class into topper class. Polymorphism is not shown here
because we have defined the constructor in the topper class but that doesn’t mean
that default constructor is overloaded.
31. Which feature may be violated if we don’t use classes in a program?
a) Inheritance can’t be implemented
b) Object must be used is violated
c) Encapsulation only is violated
d) Basically all the features of OOP gets violated
Answer: d
Explanation: All the features are violated because Inheritance and Encapsulation
won’t be implemented. Polymorphism and Abstraction are still possible in some
cases, but the main features like data binding, object use and etc won’t be used
hence the use of class is must for OOP concept.
32. How many basic features of OOP are required for a programming language to be
purely OOP?
a) 7
b) 6
c) 5
d) 4
Answer: a
Explanation: There are 7 basic features that define whether a programing language is
pure OOP or not. The 4 basic features are inheritance, polymorphism, encapsulation
and abstraction. Further, one is, object use is must, secondly, message passing and
lastly, Dynamic binding.
33. The feature by which one object can interact with another object is
_____________
a) Data transfer
b) Data Binding
c) Message Passing
d) Message reading
Answer: c
Explanation: The interaction between two object is called the message passing
feature. Data transfer is not a feature of OOP. Also, message reading is not a
feature of OOP.
Answer: d
Explanation: Virtual Functions can be defined in any class using the keyword
virtual. All the classes which inherit the class containing the virtual function, define
the virtual function as required. Redefining the function on all the derived classes
according to class and use represents polymorphism.
Answer: a
Explanation: The feature is operator overloading. There is not a feature named
operator overriding specifically. Function overloading and overriding doesn’t give
addition function to any operator.
Answer: b
Explanation: Function overriding doesn’t illustrate polymorphism because the
functions are actually different and theirs scopes are different. Function and
operator overloading illustrate proper polymorphism. Virtual functions show
polymorphism because all the classes which inherit virtual function, define the
same function in different ways.
Answer: a
Explanation: Exception handling is a feature of OOP as it includes classes concept
in most of the cases. Also it may come handy while using inheritance.
38. Which among the following, for a pure OOP language, is true?
a) The language should follow 3 or more features of OOP
b) The language should follow at least 1 feature of OOP
c) The language must follow only 3 features of OOP
d) The language must follow all the rules of OOP
Answer: d
Explanation: The language must follow all the rules of OOP to be called a purely
OOP language. Even if a single OOP feature is not followed, then it’s known to be
a partially OOP language.
Explanation: It is always true as we have the facility of private and protected access
specifiers. Also, only the public and global data are available globally or else the
program should have proper permission to access the private data.
Answer: a
Explanation: It is actually the ability for a message / data to be processed in more
than one form. The word polymorphism indicates many-forms. So if a single entity
takes more than one form, it is known as polymorphism.
41. What do you call the languages that support classes but not polymorphism?
a) Class based language
b) Procedure Oriented language
c) Object-based language
d) If classes are supported, polymorphism will always be supported
Answer: c
Explanation: The languages which support classes but doesn’t support
polymorphism, are known as object-based languages. Polymorphism is such an
important feature, that is a language doesn’t support this feature, it can’t be called
as a OOP language.
42. Which among the following is the language which supports classes but not
polymorphism?
a) SmallTalk
b) Java
c) C++
d) Ada
Answer: d
Explanation: Ada is the language which supports the concept of classes but doesn’t
support the polymorphism feature. It is an object-based programming language.
Note that it’s not an OOP language.
43. If same message is passed to objects of several different classes and all of those
can respond in a different way, what is this feature called?
a) Inheritance
b) Overloading
c) Polymorphism
d) Overriding
Answer: c
Explanation: The feature defined in question defines polymorphism features. Here
the different objects are capable of responding to the same message in different
ways, hence polymorphism.
44. Which class/set of classes can illustrate polymorphism in the following code?
Answer: c
Explanation: Since Student class is abstract class and class topper and average are
inheriting student, class topper and average must define the function named
calc_grade(); in abstract class. Since both the definition are different in those
classes, calc_grade() will work in different way for same input from different
objects. Hence it shows polymorphism.
Answer: b
Explanation: Only virtual functions among these can show polymorphism. Class
member functions can show polymorphism too but we should be sure that the same
function is being overloaded or is a function of abstract class or something like this,
since we are not sure about all these, we can’t say whether it can show
polymorphism or not.
46. In case of using abstract class or function overloading, which function is supposed
to be called first?
a) Local function
b) Function with highest priority in compiler
c) Global function
d) Function with lowest priority because it might have been halted since long time,
because of low priority
Answer: b
Explanation: Function with highest priority is called. Here, it’s not about the thread
scheduling in CPU, but it focuses on whether the function in local scope is present
or not, or if scope resolution is used in some way, or if the function matches the
argument signature. So all these things define which function has the highest
priority to be called in runtime. Local function could be one of the answer but we
can’t say if someone have used pointer to another function or same function name.
Answer: a
Explanation: Static member functions are not property of any object. Hence it can’t
be considered for overloading/overriding. For polymorphism, function must be
property of object, not only of class.
48. What is output of the following program?
class student{
public : int marks;
void disp()
{
cout<<”its base class”
};
class topper:public student
{
public :
void disp()
{
cout<<”Its derived class”;
}
}
void main() { student s; topper t;
s.disp();
t.disp();}
a) Its base classIts derived class
b) Its base class Its derived class
c) Its derived classIts base class
d) Its derived class Its base class
Answer: a
Explanation: You need to focus on how the output is going to be shown, no space
will be given after first message from base class. And then the message from
derived class will be printed. Function disp() in base class overrides the function of
base class being derived.
class education{
char name[10];
public : disp()
{
cout<<”Its education system”;
}
class school:public education
{
public: void dsip()
{
cout<<”Its school education system”;
}
};
void main()
{
school s;
s.disp();
}}
a) Its school education system
b) Its education system
c) Its school education systemIts education system
d) Its education systemIts school education system
Answer: a
Explanation: Notice that the function name in derived class is different from the
function name in base class. Hence when we call the disp() function, base class
function is executed. No polymorphism is used here.
51. Polymorphism is possible in C language.
a) True
b) False
Answer: a
Explanation: It is possible to implement polymorphism in C language, even though it
doesn’t support class. We can use structures and then declare pointers which in turn
points to some function. In this way we simulate the functions like member functions
but not exactly member function. Now we can overload these functions, hence
implementing polymorphism in C language.
52. Which problem may arise if we use abstract class functions for polymorphism?
a) All classes are converted as abstract class
b) Derived class must be of abstract type
c) All the derived classes must implement the undefined functions
d) Derived classes can’t redefine the function
Answer: c
Explanation: The undefined functions must be defined is a problem, because one may
need to implement few undefined functions from abstract class, but he will have to
define each of the functions declared in abstract class. Being useless task, it is a
problem sometimes.
Answer: d
Explanation: It never increases function definition overhead, one way or another if
you don’t use polymorphism, you will use the definition in some other way, so it
actually helps to write efficient codes.
54. If 2 classes derive one base class and redefine a function of base class, also
overload some operators inside class body. Among these two things of function and
operator overloading, where is polymorphism used?
a) Function overloading only
b) Operator overloading only
c) Both of these are using polymorphism
d) Either function overloading or operator overloading because polymorphism can be
applied only once in a program
Answer: d
Explanation: Both of them are using polymorphism. It is not necessary that
polymorphism can be used only once in a program, it can be used anywhere, any
number of times in a single program.
A. A class
B. An object
C. A method
D. A data field
Answer. B
A. A class
B. An object
C. A method
D. A data field
Answer. A
A. def
B. return
C. class
D. All of the above.
Answer. C
A. A constructor
B. A class
C. A value-returning method
D. A None method
Answer. A
class Sales:
def __init__(self, id):
self.id = id
id = 100
val = Sales(123)
print (val.id)
Q. Which of the following can be used to invoke the __init__ method in B from
A, where A is a subclass of B?
A. super().__init__()
B. super().__init__(self)
C. B.__init__()
D. B.__init__(self)
Answer. A, D
class test:
def __init__(self,a=""Hello World""):
self.a=a
def display(self):
print(self.a)
obj=test()
obj.display()
(A) The program has an error because constructor can’t have default arguments
(B) Nothing is displayed
(C) “Hello World” is displayed
(D) The program has an error display function doesn’t have parameters
Answer: C
Explanation: The program has no error. “Hello World” is displayed. Execute in
python shell to verify.
std=Student("Simon",1)
std.id=2
print(std.id)
A. 1
1
B. 1
2
C. 2
1
D. 2
2
Ans : B
Explanation: When object with id =1 is created for Student, constructor is invoked and
it prints 1. Later, id has been changed to 2, so next print statement prints 2.
A. 100
100
B. 100
102
C. 102
102
D. Error
Ans : B
A. attribute
B. object
C. argument
D. function
Ans : D
Std_firstName = "Wick"
name = Std_Name(Std_firstName, 'F', "Bob")
Std_firstName = "Ann"
name.lastName = "Nick"
print(name.Std_firstName, name.Std_lastName)
B. Ann Nick
C. Wick Bob
D. Wick Nick
Q. Which of the following option is not a core data type in the python language?
A. Dictionary
B. Lists
C. Class
D. All of the above
Answer: C
A. __a
B. __b
C. __c__
D. __d__
Answer: B
Explanation: Variables such as self.__b are private members of the class.
A. Error
B. Nothing is printed
C. __str__ called
D. __repr__ called
Answer: C
Explanation: __str__ is used for producing a string representation of an object’s value
that Python can evaluate. Execute in python shell to verify.
def display(self):
print(self.a)
obj=test()
obj.display()
A. The program has an error because constructor can’t have default arguments
B. Nothing is displayed
C. “Hello World” is displayed
D. The program has an error display function doesn’t have parameters
Answer: C
Explanation: The program has no error. “Hello World” is displayed. Execute in
python shell to verify.