Professional Documents
Culture Documents
PYTHON Answer: a
Explanation: The statement is true. This
PROGRAMMING
Answer: a
Explanation: Algorithms help us to
UNIT I ALGORITHMIC understand scalability. Performance often
draws the line between what is feasible and
PROBLEM SOLVING what is impossible.
CSE-R17.BLOGSPOT.COM
6. When an algorithm is written in the form of d) Array
a programming language, it becomes a
_________ Answer: b
a) Flowchart Explanation: In a queue, the items are
b) Program inserted from the rear end and deleted from
c) Pseudo code the front end.
d) Syntax
9. Another name for 1-D arrays.
Answer: b a) Linear arrays
Explanation: An algorithm becomes a 10. A data structure that follows the FIFO
program when it is written in the form of a principle.
programming language. Thus, any program is a) Queue
an algorithm. b) LL
c) Stack
7. Any algorithm is a program. d) Union
a) True
b) False Answer: a
Explanation: The answer is Queue. A Queue
Answer: b follows the FIFO principle. FIFO stands for
Explanation: The statement is false. An First In First Out.
algorithm is represented in the form of a
programming language is called a program.
TOPIC 1.2 FLOW CHARTS
Any program is an algorithm but the reverse
is not true.
1. The symbol denotes _______
8. A system wherein items are added from
one and removed from the other end. a)
a) I/O
Stack
b) Flow
b) Queue
c) Terminal
c) Linked List d) Decision
b) False
Answer: c
Explanation: The symbol denotes a terminal. Answer: b
It is used for indication of start and stop Explanation: The statement is false. The
nodes of a program. correct statement would be: In computer
science, flowchart refers to a pictorial
2. In computer science, algorithm refers to a representation of an algorithm.
pictorial representation of a flowchart. a)
True
CSE-R17.BLOGSPOT.COM
flowchart is nothing but a pictorial c) Initialization
representation of an algorithm. d) I/O
CSE-R17.BLOGSPOT.COM •
Repetition
1. A ______________ is diagram that Other options are irrelevant.
depicts the flow of a program.
a) Algorithm 2. Terminals are represented by diagonals in a
b) Hash Table flowchart.
c) Graph a) True
d) Flowchart b) False
Answer: d Answer: b
Explanation: A flowchart is a diagram that Explanation: The statement is false.
helps us determine the flow of the program. Terminals are represented by rounded
rectangles. They indicate the starting or b) case
ending point in a flowchart. c) repetition
d) process
3. The operation represented by
parallelograms. Answer: c
a) Input/Output Explanation: The action performed by a
b) Assignment repetition structure must eventually cause the
c) Comparison loop to terminate. Otherwise, an infinite loop
d) Conditions is created.
a)
sequence
shapes. They also show the flow of the
CSE-R17.BLOGSPOT.COM
b) case b) Input/Output
c) repetition c) Process
d) process d) Module
Answer: c Answer: a
Explanation: This is a repetition structure. Explanation: The answer is decision.
The action performed by a repetition structure Conditions are given in this box and then the
must eventually cause the loop to terminate. result is checked accordingly if the condition
Otherwise, an infinite loop is created. is true or false.
CSE-R17.BLOGSPOT.COM
4. ______________ is used to show hierarchy c) Tokens
in a pseudo code. d) Functions
a) Indentation
b) Curly Braces Answer: b
c) Round Brackets Explanation: Variables begin with a
d) Semicolon lowercase. They contain no spaces. They also
involve the consistent use of names.
Answer: a
Explanation: Each design structure uses a 8. Another notation for exponentiation.
particular indentation pattern. a) *
Indentation should be considered in the b) **
following cases: c) ***
Sequence d) *^
Selection
Loop. Answer: b
Explanation: Double asterisk sign is also
5. The statement that tells the computer to get used for exponentiation. The general notation
a value from an input device and store it in a is ^ sign.
memory location.
a) read 9. A symbol used for grouping.
b) write a) ()
c) READ b) {}
d) WRITE c) [].
d) ” ”
Answer: c
Explanation: The READ statement is used to Answer: a
take the input. READ being a keyword should Explanation: Parenthesis is used for grouping
be in capital letters. while working with fields. There are other
symbols like *, +, -, **, etc.
6. _____________ are identified by their
addresses, we give them names (field names / 10. A statement used to close the IF block.
variable names) using words. a) ELSE
a) Memory variables b) ELSEIF
b) Memory Locations c) END
c) Memory Addresses d) ENDIF
d) Data variables
Answer: d
Answer: b Explanation: The answer is ENDIF. It is used
Explanation: Memory locations are
Downloaded From: https://cse-r17.blogspot.com 6
First Semester MCQ for Regulations 2017
b) 1st_string
c) foo
d) _
to close the IF block. ENDIF statement
should be in line with the IF statement. Answer: b
Explanation: Variable names should not start
with a number.
CSE-R17.BLOGSPOT.COM
STATEMENTS Answer: d
Explanation: All the statements will execute
TOPIC 2.1 VARIABLES successfully but at the cost of reduced
readability.
1. Is Python case sensitive when dealing with 4. Which of the following is an invalid
identifiers? variable?
a) yes
a) my_string_1
b) no
c) machine dependent with an underscore discouraged?
d) none of the mentioned a) they are used to indicate a private variables
of a class
Answer: a b) they confuse the interpreter
Explanation: Case is always significant. c) they are used to indicate global variables
d) they slow down execution
2. What is the maximum possible length of an
identifier? Answer: a
a) 31 characters Explanation: As Python has no concept of
b) 63 characters private variables, leading underscores are
c) 79 characters used to indicate variables that must not be
d) none of the mentioned accessed from outside the class.
CSE-R17.BLOGSPOT.COM a)
abc = 1,000,000
b) a b c = 1000 2000 3000
c) a,b,c = 1000, 2000, 3000 TOPIC 2.2 OPERATORS
d) a_b_c = 1,000,000
CSE-R17.BLOGSPOT.COM
7. What is the output of this expression, and 3*1 = 3. Final answer is 3.
3*1**3?
a) 27 8. Which one of the following has the same
b) 9 precedence level?
c) 3 a) Addition and Subtraction
d) 1 b) Multiplication, Division and Addition
c) Multiplication, Division, Addition and
Answer: c Subtraction
Explanation: First this expression will solve d) Addition and Multiplication
1**3 because exponential has higher
precedence than multiplication, so 1**3 = 1 Answer: a
Explanation: “Addition and Subtraction” are c) Tuples
at the same precedence level. Similarly, d) Class
“Multiplication and Division” are at the same
precedence level. However, Multiplication Answer: d
and Division operators are at a higher Explanation: Class is a user defined data
precedence level than Addition and type.
Subtraction operators.
2. Given a function that does not return any
9. The expression Int(x) implies that the value, What value is thrown by default when
variable x is converted to integer. executed in shell.
a) True a) int
b) False b) bool
c) void
Answer: a d) None
Explanation: None.
is, Parenthesis, Exponentiation, Division, Answer: d
Multiplication, Addition, Subtraction. Note Explanation: Python shell throws a
that the precedence order of Division and NoneType object back.
Multiplication is the same. Likewise, the
order of Addition and Subtraction is also the 3. What will be the output of the following
same. Python code?
1. >>>str="hello"
TOPIC 2.3 DATA TYPES
2. >>>str[:2]
1. Which of these in not a core data type? 3. >>>
a) Lists
b) Dictionary
Answer: a a) SyntaxError
Explanation: We are printing only the 1st b) NameError
two bytes of string and hence the answer is
CSE-R17.BLOGSPOT.COM c)
ValueError
4. Which of the following will run without d) TypeError
errors?
a) round(45.8) Answer: b
b) round(6352.898,2,5) Explanation: Mango is not defined hence
c) round() name error.
d) round(7463.123,2,1)
8. What will be the output of the following
Answer: a Python code snippet?
Explanation: Execute help(round) in the
shell to get details of the parameters that are 1. def example(a):
passed into the round function. 2. a = a + '2'
5. What is the return type of function id? 3. a = a*2
a) int
b) float 4. return a
c) bool
5. >>>example("hello")
d) dict
a) indentation Error
Answer: a
b) cannot perform mathematical operation on
Explanation: Execute help(id) to find out
strings
details in python shell.id returns a integer
c) hello2
value that is unique.
d) hello2hello2
6. In python we do not specify types, it is
Answer: a
directly interpreted by the compiler, so
Explanation: Python codes have to be
consider the following operation to be
indented properly.
performed.
9. What data type is the object below?
1. >>>x = 13 ? 2
L = [1, 23, 'hello', 1]
objective is to make sure x has a integer
value, select all that apply (python 3.xx) a) list
a) x = 13 // 2 b) dictionary
b) x = int(13 / 2) c) array
c) x = 13 % 2 d) tuple
d) All of the mentioned
Answer: a
Answer: d Explanation: List data type can store any
Explanation: // is integer operation in python
CSE-R17.BLOGSPOT.COM
Answer: d
3. >>>average = (grade1 + grade2) / 2
Explanation: Dictionary stores values in
terms of keys and values.
a) 85.0
11. Which of the following results in a b) 85.1
SyntaxError? c) 95.0
a) ‘”Once upon a time…”, she said.’ d) 95.1
b) “He said, ‘Yes!'” Answer: a
c) ‘3\’ Explanation: Cause a decimal value of 0 to
d) ”’That’s okay”’ appear as output.
Answer: c
14. Select all options that print.
Explanation: Carefully look at the colons.
hello-how-are-you
12. The following is displayed by a print
function call. Select all of the function calls a) print(‘hello’, ‘how’, ‘are’, ‘you’) b)
that result in this output. print(‘hello’, ‘how’, ‘are’, ‘you’ + ‘-‘ * 4) c)
print(‘hello-‘ + ‘how-are-you’)
1. tom d) print(‘hello’ + ‘-‘ + ‘how’ + ‘-‘ + ‘are’ +
‘you’)
2. dick
3. harry Answer: c
Explanation: Execute in the shell.
a)
15. What is the return value of trunc()?
print('''tom a) int
b) bool
\ndick
c) float
\nharry''') d) None
b) print(”’tomdickharry”’) Answer: a
c) print(‘tom\ndick\nharry’) Explanation: Execute help(math.trunc) to get
d) details.
print('tom
TOPIC 2.4 NUMERIC TYPES
dick (INT, FLOAT)
harry')
2. >>>grade2 = 90
CSE-R17.BLOGSPOT.COM
be represented accurately in binary. The c) x = 19023
round off errors from 0.1 and 0.2 accumulate d) x = 03964
and hence there is a difference of 5.5511e-17
between (0.1 + 0.2) and 0.3. Answer: d
Explanation: Numbers starting with a 0 are
2. Which of the following is not a complex octal numbers but 9 isn’t allowed in octal
number? numbers.
a) k = 2 + 3j
b) k = complex(2, 3) 7. What is the result of cmp(3, 1)?
c) k = 2 + 3l a) 1
d) k = 2 + 3J b) 0
c) True
Answer: c d) False
Explanation: l (or L) stands for long.
Answer: a
3. What is the type of inf? Explanation: cmp(x, y) returns 1 if x > y, 0 if
a) Boolean x == y and -1 if x < y.
b) Integer
c) Float 8. Which of the following is incorrect?
d) Complex a) float(‘inf’)
b) float(‘nan’)
Answer: c c) float(’56’+’78’)
Explanation: Infinity is a special case of d) float(’12+34′)
floating point numbers. It can be obtained by
float(‘inf’). Answer: d
Explanation: ‘+’ cannot be converted to a
4. What does ~4 evaluate to? float.
a) -5
b) -4 9. What is the result of round(0.5) –
c) -3 round(-0.5)?
d) +3 a) 1.0
b) 2.0
Answer: a c) 0.0
Explanation: ~x is equivalent to -(x+1). d) Value depends on Python version
CSE-R17.BLOGSPOT.COM
Python 2.7.17 (default, Nov 7 2019, 10:0
7:09) 4
1.0
In the above output, you can see that the
>>> round(-0.5) round() functions on 0.5 and -0.5 are moving
-1.0 c) 0.75
d) 7
>>>
Answer: d
In the above output, you can see that the Explanation: ^ is the Binary XOR operator.
round() functions on 0.5 and -0.5 are moving
away from 0 and hence “round(0.5) – TOPIC 2.5 PRECEDENCE OF
(round(-0.5)) = 1 – (-1) = 2” OPERATORS
$ python3
1. The value of the expressions
Python 3.6.8 (default, Oct 7 2019, 12:5 4/(3*(2-1)) and 4/3*(2-1) is the same.
9:55)
a) True
>>> round(0.5) b) False
0 Answer: a
Explanation: Although the presence of
>>> round(-0.5)
parenthesis does affect the order of
0 precedence, in the case shown above, it is not
making a difference. The result of both of
>>> round(2.5) these expressions is 1.333333333. Hence the
2 statement is true.
CSE-R17.BLOGSPOT.COM
Answer: b c) 22
Explanation: The above expression is d) 23
evaluated as: 16%15//16, which is equal to
1//16, which results in 0. Answer: b
Explanation: The expression shown above is
4. Which of the following operators has its an example of explicit conversion. It is
associativity from right to left? evaluated as int(43.55+1) = int(44.55) = 44.
a) + Hence the result of this expression is 44.
b) //
c) % 6. What is the value of the following
d) ** expression?
Answer: d
CSE-R17.BLOGSPOT.COM
10. What is the value of the following
expression? print(4.00/(2.0+2.0))
float(22//3+3/3) a) Error
b) 1.0
a) 8 c) 1.00
b) 8.0 d) 1
c) 8.3
d) 8.33 Answer: b
Explanation: The result of the expression
Answer: b shown above is 1.0 because print rounds off
Explanation: The expression shown above is digits.
evaluated as: float( 7+1) = float(8) = 8.0.
Hence the result of this expression is 8.0. 2. What will be the value of X in the
following Python expression?
1. What will be the output of the
following Python expression? X = 2+9*((3*12)-8)/10
a) 30.0 a) (1,3)
b) 30.8 b) (0,3)
c) 28.4 c) (1,0)
d) 27.2 d) (3,1)
a) 4.7 – 1.5
b) 7.9 * 6.3 Answer: a
c) 1.7 % 2 Explanation: The expressions are evaluated
d) 3.4 + 4.6 as: 4%3 and 6//2 respectively. This results in
the answer (1,3). This is because the
Answer: c associativity of both of the expressions shown
Explanation: Coercion is the implicit above is left to right.
(automatic) conversion of operands to a
common type. Coercion is automatically 5. Which among the following list of
performed on mixed-type expressions. The operators has the highest precedence?
expression 1.7 % 2 is evaluated as 1.7 % 2.0
(that is, automatic conversion of int to float). +, -, **, %, /, <<, >>, |
CSE-R17.BLOGSPOT.COM a)
5.0
b) 5 Explanation: The above expression is an
c) 4.0 example of explicit conversion. It is evaluated
d) 4 as: float(4+int(2.39)%2) = float(4+2%2) =
float(4+0) = 4.0. Hence the result of this
Answer: c expression is 4.0.
as: (2**2)**3.
7. Which of the following expressions is an a) True
example of type conversion? b) False
a) 4.0 + float(3)
b) 5.3 + 6.3 Answer: b
c) 5.0 + 3 Explanation: The value of the expression
d) 3 + 7 (2**2)**3 = 4**3 = 64. When the expression
2**2**3 is evaluated in python, we get the
Answer: a result as 256, because this expression is
Explanation: Type conversion is nothing but evaluated as 2**(2**3). This is because the
explicit conversion of operands to a specific associativity of exponentiation operator (**)
type. Options 5.3 + 6.3 and 5.0 + 3 are is from right to left and not from left to right.
examples of implicit conversion whereas
option 4.0 + float(3) is an example of explicit
conversion or type conversion.
TOPIC 2.6 BITWISE OPERATOR
8. Which of the following expressions results 1. What will be the output of the following
in an error? Python code snippet if x=1?
a) float(‘10’)
x<<2
b) int(‘10’)
c) float(’10.8’) a) 8
d) int(’10.8’) b) 1
c) 2
Answer: d
d) 4
Explanation: All of the above examples
show explicit conversion. However the Answer: d
expression int(’10.8’) results in an error. Explanation: The binary form of 1 is 0001.
Answer: b The expression x<<2 implies we are
Explanation: The order of precedence is: **, performing bitwise left shift on x. This shift
//, +. The expression 4+2**5//10 is evaluated yields the value: 0100, which is the binary
as 4+32//10, which is equal to 4+3 = 7. Hence form of the number 4.
the result of the expression shown above is 7.
a) 8 Answer: c
b) 4 Explanation: The symbol ‘&’ represents
c) 2 bitwise AND. This gives 1 if both the bits are
d) 1 equal to 1, else it gives 0. The binary form of
15 is 1111 and that of 12 is 1100. Hence on
Answer: a performing the bitwise AND operation, we
Explanation: When the value of x is equal to get 1100, which is equal to 12.
8 (1000), then x>>2 (bitwise right shift)
yields the value 0010, which is equal to 2. 7. Which of the following expressions results
Hence the value of x is 8. in an error?
a) int(1011)
4. What will be the output of the following b) int(‘1011’,23)
Python expression? c) int(1011,2)
d) int(‘1011’)
int(1011)?
Answer: c
a) 1011 Explanation: The expression int(1011,2)
b) 11 results in an error. Had we written this
c) 13 expression as int(‘1011’,2), then there would
d) 1101 not be an error.
Answer: a
8. Which of the following represents the
Explanation: The result of the expression bitwise XOR operator?
shown will be 1011. This is because we have
a) &
not specified the base in this expression.
b) ^
Hence it automatically takes the base as 10.
c) |
gives the result 15. The expression int(‘1111’,
d) !
10) will give the result 1111.
CSE-R17.BLOGSPOT.COM
a) ‘0bx1000’ Explanation: The one’s complement of a
b) 8 value is obtained by simply changing all the
c) 1000 1’s to 0’s and all the 0’s to 1’s. Hence the
d) ‘0b1000’ one’s complement of 110010101 is
001101010.
Answer: d
Explanation: The prefix 0x specifies that the 3. Bitwise _________ gives 1 if either of the
value is hexadecimal in nature. When we bits is 1 and 0 when both of the bits are 1. a)
convert this hexadecimal value to binary OR
form, we get the result as: ‘0b1000’. b) AND
c) XOR
10. What will be the output of the d) NOT
following Python expression?
Answer: c
0x35 | 0x75 Explanation: Bitwise XOR gives 1 if either
of the bits is 1 and 0 when both of the bits are
a) 115
1.
b) 116
c) 117 4. What will be the output of the following
d) 118 Python expression?
Answer: c 4^12
Explanation: The binary value of 0x35 is
110101 and that of 0x75 is 1110101. On OR a) 2
ing these two values we get the output as: b) 4
1110101, which is equal to 117. Hence the c) 8
result of the above expression is 117. d) 12
CSE-R17.BLOGSPOT.COM
always gives 0.
8. What will be the output of the following
6. What will be the value of the following Python code if a=10 and b =20?
Python expression?
a=10
bin(10-2)+bin(12^4) b=20
a=a^b
b=a^b
a) 0b10000
b) 0b10001000 Explanation: The code shown above is used
c) 0b1000b1000 to swap the contents of two memory locations
d) 0b10000b1000 using bitwise X0R operator. Hence the output
of the code shown above is: 20 10.
Answer: d
Explanation: The output of bin(10-2) = 9. What is the two’s complement of -44?
0b1000 and that of bin(12^4) is ob1000. a) 1011011
Hence the output of the above expression is: b) 11010100
0b10000b1000. c) 11101011
d) 10110011
7. Which of the following expressions can be
used to multiply a given number ‘a’ by 4? a) Answer: b
a<<2 Explanation: The binary form of -44 is
b) a<<4 00101100. The one’s complement of this
c) a>>2 value is 11010011. On adding one to this we
d) a>>4 get: 11010100 (two’s complement).
True
CSE-R17.BLOGSPOT.COM
True
not(3>4)
b) function bool. This does not amount to zero
and hence the output is true. In the second
False function, an empty list is passed to the
function bool. Hence the output is false.
True
c)
False
2. What will be the output of the following
Python code snippet?
False
['hello', 'morning'][bool('')]
d)
a) error
True b) no output
c) hello
False d) morning
not(1&1)
Answer: d
Explanation: The Boolean function returns a)
true if the argument passed to the bool
function does not amount to zero. In the first True
example, the string ‘False’ is passed to the
True False
b) Answer: b
Explanation: The function not returns true if
True the argument amounts to false, and false if the
argument amounts to true. Hence the first
False function returns false, and the second
c) function returns false.
False
CSE-R17.BLOGSPOT.COM
amount to zero. Else ‘t’ is printed. The
argument given to the Boolean function in the Answer: d
above case is ‘spam’, which does not amount Explanation: The code shown above returns
to zero. Hence the output is t. a new list containing only those elements of
the list l which do not amount to zero. Hence
5. What will be the output of the following the output is: [1, 2, ‘hello’].
Python code?
6. What will be the output of the following
l=[1, 0, 2, 0, 'hello', '', []] Python code if the system date is 21st June,
list(filter(bool, l)) 2017 (Wednesday)?
a) Error [] or {}
b) [1, 0, 2, 0, ‘hello’, ”, []] {} or []
c) [1, 0, 2, ‘hello’, ”, []]
d) [1, 2, ‘hello’] a)
[]
pass
{} x=Truth()
bool(x)
b)
a) pass
[] b) true
c) false
[]
d) error
c)
Answer: b
Answer: c
Explanation: If the truth method is not
Explanation: The code shown above shows
defined, the object is considered true. Hence
two functions. In both the cases the right
the output of the code shown above is true.
operand is returned. This is because each
function is evaluated from left to right. Since 8. What will be the output of the following
the left operand is false, it is assumed that the Python code?
right operand must be true and hence the
right operand is returned in each of the above if (9 < 0) and (0 < -9):
case. print("hello")
elif (9 > 0) or False:
print("good")
else:
print("bad")
7. What will be the output of the following
Python code? a) error
class Truth:
b) hello
c) good
d) bad
CSE-R17.BLOGSPOT.COM
9. Which of the following Boolean d) not(-6>10 or-6==10)
expressions is not logically equivalent to the
other three? Answer: d
a) not(-6<0 or-6>10) Explanation: The expression not(-6<0 or
b) -6>=0 and -6<=10 -6>10) returns the output False.
c) not(-6<10 or-6==10) The expression -6>=0 and -6<=10 returns the
output False. print(x)
The expression not(-6<10 or -6==10) returns
a) [‘ab’, ‘cd’]
the output False.
b) [‘AB’, ‘CD’]
The expression not(-6>10 or -6==10) returns
c) [None, None]
the output True.
d) none of the mentioned
10. What will be the output of the following
Answer: a
Python code snippet?
Explanation: The function upper() does not
not(10<20) and not(10>30) modify a string in place, it returns a new
string which isn’t being stored anywhere.
a) True
b) False 2. What will be the output of the following
c) Error Python code?
d) No output
x = ['ab', 'cd']
for i in x:
Answer: b
x.append(i.upper())
Explanation: The expression not(10<20) print(x)
returns false. The expression not(10>30)
returns true. The and operation between false a) [‘AB’, ‘CD’]
and true returns false. Hence the output is b) [‘ab’, ‘cd’, ‘AB’, ‘CD’]
false. c) [‘ab’, ‘cd’]
d) none of the mentioned
Sanfoundry Global Education & Learning
Series – Python. Answer: d
Explanation: The loop does not terminate as
1. What will be the output of the following
new elements are being added to the list in
Python code?
each iteration.
x = ['ab', 'cd']
for i in x: 3. What will be the output of the following
i.upper() Python code?
i + = 1 i = 5
while True:
a) 1 2 if i%0O9 == 0:
b) 1 2 3
CSE-R17.BLOGSPOT.COM
c) error d) none of the mentioned
i += 1
Answer: c
Explanation: SyntaxError, there shouldn’t be a) 5 6 7 8
a space between + and = in +=. b) 5 6 7 8 9
c) 5 6 7 8 9 10 11 12 13 14 15 ….
4. What will be the output of the following d) error
Python code?
Answer: d
i = 1 Explanation: 9 isn’t allowed in an octal
while True: number.
if i%0O7 == 0:
break
7. What will be the output of the following
print(i)
i += 1 Python code?
a) 1 2 3 4 5 6 i = 1
while True:
b) 1 2 3 4 5 6 7 if i%2 == 0:
c) error break
d) none of the mentioned print(i)
i += 2
Answer: a
Explanation: Control exits the loop when i a) 1
becomes 7. b) 1 2
c) 1 2 3 4 5 6 …
5. What will be the output of the following d) 1 3 5 7 9 11 …
Python code?
Answer: d
i = 5 Explanation: The loop does not terminate
while True: since i is never an even number.
if i%0O11 == 0:
break
8. What will be the output of the following
print(i)
i += 1 Python code?
a) 5 6 7 8 9 10 i = 2
while True:
b) 5 6 7 8 if i%3 == 0:
c) 5 6 break
d) error print(i)
break i += 2
print(i)
CSE-R17.BLOGSPOT.COM
loop. c) error
d) none of the mentioned
9. What will be the output of the following
Python code? Answer: b
Explanation: The else part is not executed if
i = 1 control breaks out of the loop.
while False:
if i%2 == 0:
break
2. What will be the output of the following
print(i) Python code?
i += 2
i = 0
a) 1 while i < 3:
print(i)
b) 1 3 5 7 …
i += 1
c) 1 2 3 4 … else:
d) none of the mentioned print(0)
Answer: d a) 0 1 2 3 0
Explanation: Control does not enter the loop b) 0 1 2 0
because of False. c) 0 1 2
d) error
10. What will be the output of the following
Python code? Answer: b
Explanation: The else part is executed when
True = False the condition in the while statement is false.
while True:
print(True)
break
3. What will be the output of the following
Python code?
a) True
x = "abcdef"
b) False
while i in x:
c) None print(i, end=" ")
d) none of the mentioned
a) a b c d e f
Answer: d b) abcdef
Explanation: SyntaxError, True is a keyword c) i i i i i i …
and it’s value cannot be changed. d) error
1. What will be the output of the following Answer: d
Python code? Explanation: NameError, i is not defined.
b) 0 1 2
a) no output
CSE-R17.BLOGSPOT.COM
b) i i i i i i … Explanation: Here i i i i i … printed
c) a b c d e f continuously because as the value of i or x
d) abcdef isn’t changing, the condition will always
while i in x:
Answer: a x = x[:-1]
Explanation: “i” is not in “abcdef”. print(i, end = " ")
x = "abcdef"
Answer: c
i = "a"
Explanation: As the value of i or x isn’t while i in x[:-1]:
changing, the condition will always evaluate print(i, end = " ")
to True.
a) a a a a a
6. What will be the output of the following b) a a a a a a
Python code? c) a a a a a a …
d) a
x = "abcdef"
i = "a" Answer: c
while i in x:
Explanation: String x is not being altered and
print('i', end = " ")
i is in x[:-1].
a) no output
b) i i i i i i … 9. What will be the output of the following
c) a a a a a a … Python code?
d) a b c d e f
x = "abcdef"
i = "a"
Answer: b while i in x:
x = x[1:] b) a
print(i, end = " ")
a) a a a a a a
CSE-R17.BLOGSPOT.COM
x = "abcdef"
i = "a" 2. What will be the output of the following
while i in x[1:]:
Python code?
print(i, end = " ")
x = 'abcd'
a) a a a a a a for i in x:
b) a print(i.upper())
c) no output
d) error a) a b c d
b) A B C D
Answer: c x = 'abcd'
Explanation: i is not in x[1:]. for i in range(x):
print(i)
1. What will be the output of the following
a) a b c d
Python code?
b) 0 1 2 3
x = 'abcd' c) error
for i in x: d) none of the mentioned
print(i)
x.upper() Answer: c
Explanation: range(str) is not allowed.
a) a B C D
b) a b c d 4. What will be the output of the following
c) A B C D Python code?
d) error
x = 'abcd'
Answer: b for i in range(len(x)):
Explanation: Changes do not happen in print(i)
place, rather a new instance of the string is
a) a b c d
returned.
b) 0 1 2 3
c) error x = 'abcd'
for i in range(len(x)):
d) 1 2 3 4
print(i.upper())
Answer: b a) a b c d
Explanation: i takes values 0, 1, 2 and 3. b) 0 1 2 3
c) error
5. What will be the output of the following
d) 1 2 3 4
Python code?
CSE-R17.BLOGSPOT.COM
i.upper()
print (x)
d) none of the mentioned
Answer: a
a) a b c d Explanation: Changes do not happen in
b) 0 1 2 3 place, rather a new instance of the string is
c) error returned.
d) none of the mentioned
8. What will be the output of the following
Answer: c
Python code snippet?
Explanation: Objects of type int have no
attribute upper(). x = 'abcd'
for i in range(len(x)):
7. What will be the output of the following i[x].upper()
Python code snippet? print (x)
x = 'abcd' a) abcd
for i in range(len(x)): b) ABCD
x[i].upper() c) error
print (x) d) none of the mentioned
for i in range(len(x)):
a) abcd x = 'a'
b) ABCD print(x)
c) error
c) a a a a
a) a d) none of the mentioned
b) abcd abcd abcd
c) a a a a Answer: d
d) none of the mentioned Explanation: abcd a a a is the output as x is
modified only after ‘abcd’ has been printed
Answer: c once.
Explanation: range() is computed only at the
time of entering the loop. 1. What will be the output of the following
Python code?
10. What will be the output of the following
Python code snippet? x = 123
for i in x:
x = 'abcd' print(i)
for i in range(len(x)):
print(x) a) 1 2 3
x = 'a' b) 123
c) error
a) a d) none of the mentioned
b) abcd abcd abcd abcd
CSE-R17.BLOGSPOT.COM
print(i) for x, y in d:
print(x, y)
a) 0 1 2
b) a b c a) 0 1 2
c) 0 a 1 b 2 c b) a b c
d) none of the mentioned c) 0 a 1 b 2 c
d) none of the mentioned
Answer: a
Explanation: Loops over the keys of the Answer: d
dictionary. Explanation: Error, objects of type int aren’t
iterable.
3. What will be the output of the following
Python code? 4. What will be the output of the following
Python code?
d = {0: 'a', 1: 'b', 2: 'c'}
c) 0 a 1 b 2 c
d = {0: 'a', 1: 'b', 2: 'c'} d) none of the mentioned
for x, y in d.items():
print(x, y)
Answer: b
a) 0 1 2 Explanation: Loops over the values.
b) a b c
7. What will be the output of the following
c) 0 a 1 b 2 c
Python code?
d) none of the mentioned
d = {0: 'a', 1: 'b', 2: 'c'}
Answer: c for x in d.values():
Explanation: Loops over key, value pairs. print(d[x])
c) 0 a 1 b 2 c
d) none of the mentioned a) 0 1 2
b) a b c
Answer: b c) 0 a 1 b 2 c
Explanation: Loops over the keys and prints d) none of the mentioned
the values.
Answer: d
6. What will be the output of the following Explanation: Causes a KeyError.
Python code?
8. What will be the output of the following
d = {0: 'a', 1: 'b', 2: 'c'} Python code?
for x in d.values():
print(x) d = {0, 1, 2}
for x in d.values():
a) 0 1 2
b) a b c
CSE-R17.BLOGSPOT.COM
attribute values. for x in d:
print(x)
9. What will be the output of the following
Python code? a) 0 1 2
b) {0, 1, 2} {0, 1, 2} {0, 1, 2}
d = {0, 1, 2} c) error
d) none of the mentioned b) 0 1
c) error
Answer: a d) none of the mentioned
Explanation: Loops over the elements of the
set and prints them. Answer: c
Explanation: Object of type float cannot be
10. What will be the output of the following interpreted as an integer.
Python code?
2. What will be the output of the following
d = {0, 1, 2} Python code?
for x in d:
print(d.add(x)) for i in range(int(2.0)):
print(i)
a) 0 1 2
b) 0 1 2 0 1 2 0 1 2 … a) 0.0 1.0
c) None None None b) 0 1
d) None of the mentioned c) error
d) none of the mentioned
Answer: c
Explanation: Variable x takes the values 0, 1 Answer: b
and 2. set.add() returns None which is Explanation: range(int(2.0)) is the same as
printed. range(2).
11. What will be the output of the following 3. What will be the output of the following
Python code? Python code?
for i in range(0): for i in range(float('inf')):
print(i) print (i)
Python code?
a) 0.0 0.1 0.2 0.3 …
for i in range(2.0): b) 0 1 2 3 …
print(i) c) 0.0 1.0 2.0 3.0 …
d) none of the mentioned
a) 0.0 1.0
for i in
CSE-R17.BLOGSPOT.COM
a) 0.0 0.1 0.2 0.3 …
b) 0 1 2 3 … 8. What will be the output of the following
c) 0.0 1.0 2.0 3.0 … Python code snippet?
d) none of the mentioned
for i in '':
print (i)
Answer: d
Explanation: OverflowError, cannot convert a) None
float infinity to integer. b) (nothing is printed)
c) error
5. What will be the output of the following
d) none of the mentioned
Python code snippet?
Answer: b
for i in [1, 2, 3, 4][::-1]:
print (i) Explanation: The string does not have any
character to loop over.
a) 1 2 3 4
b) 4 3 2 1 9. What will be the output of the following
c) error Python code snippet?
d) none of the mentioned
x = 2
for i in range(x):
Answer: b
x += 1
Explanation: [::-1] reverses the list. print (x)
a) a b c d Answer: c
b) d c b a Explanation: Variable x is incremented and
c) error printed twice.
d) none of the mentioned
10. What will be the output of the following
Answer: b Python code snippet?
Explanation: ‘ ‘.join(reversed(list(‘abcd’)))
x = 2
reverses a string. for i in range(x):
x -= 2
7. What will be the output of the following print (x)
Python code snippet?
Explanation: [::-1] reverses the string.
CSE-R17.BLOGSPOT.COM
Python code?
3. What will be the output of the following
for i in range(10): Python code?
if i == 5:
Answer: a
break
else: Explanation: The first statement creates a
print(i) generator object.
else:
print("Here") 4. What will be the output of the following
Python code?
a) 0 1 2 3 4 Here
b) 0 1 2 3 4 5 Here x = (i for i in range(3))
c) 0 1 2 3 4 for i in x:
d) 1 2 3 4 5 print(i)
for i in x:
print(i)
Answer: c
Explanation: The else part is executed if a) 0 1 2
control doesn’t break out of the loop. b) error
c) 0 1 2 0 1 2
2. What will be the output of the following
d) none of the mentioned
Python code?
Answer: a
for i in range(5):
if i == 5: Explanation: We can loop over a generator
break object only once.
else:
print(i) 5. What will be the output of the following
else: Python code?
print("Here")
string = "my name is x"
a) 0 1 2 3 4 Here for i in string:
b) 0 1 2 3 4 5 Here print (i, end=", ")
c) 0 1 2 3 4
d) 1 2 3 4 5 a) m, y, , n, a, m, e, , i, s, , x,
b) m, y, , n, a, m, e, , i, s, , x
Answer: a c) my, name, is, x,
Explanation: The else part is executed if d) error
control doesn’t break out of the loop.
Answer: a 6. What will be the output of the following
Explanation: Variable i takes the value of Python code?
one character at a time.
CSE-R17.BLOGSPOT.COM
Answer: c d) error
Explanation: Variable i takes the value of
one word at a time. Answer: a
Explanation: The value of a[0] changes in
7. What will be the output of the following each iteration. Since the first value that it
Python code snippet? takes is itself, there is no visible error in the
current example.
a = [0, 1, 2, 3]
for a[-1] in a: 9. What will be the output of the following
print(a[-1])
Python code snippet?
a) 0 1 2 3 d) none of the mentioned
b) 0 1 2 2
Answer: c
c) 3 3 3 3
Explanation: SyntaxError, not in isn’t
d) error
allowed in for loops.
Answer: b
10. What will be the output of the following
Explanation: The value of a[-1] changes in
Python code snippet?
each iteration.
string = "my name is x"
8. What will be the output of the following for i in ' '.join(string.split()):
Python code snippet? print (i, end=", ")
a = [0, 1, 2, 3] a) m, y, , n, a, m, e, , i, s, , x,
for a[0] in a: b) m, y, , n, a, m, e, , i, s, , x
print(a[0])
c) my, name, is, x,
a) 0 1 2 3 d) error
b) 0 1 2 2
Answer: a
c) 3 3 3 3
Explanation: Variable i takes the value of b) bc
one character at a time. c) bca
d) abc
TOPIC 3.2 PYTHON STRINGS Answer: d
Explanation: + operator is concatenation
1. What will be the output of the following operator.
Python statement?
2. What will be the output of the following
1. >>>"a"+"bc" Python statement?
a) a
CSE-R17.BLOGSPOT.COM
string.
4. >>> str1[-1:]
3. The output of executing string.ascii_letters
can also be achieved by: a) olleh
a) string.ascii_lowercase_string.digits b) hello
b) c) h
string.ascii_lowercase+string.ascii_uppercase d) o
c) string.letters Answer: d
d) string.lowercase_string.uppercase Explanation: -1 corresponds to the last index.
Answer: b 5. What arithmetic operators cannot be used
Explanation: Execute in shell and check. with strings?
4. What will be the output of the following a) +
Python code? b) *
c) –
1. >>> str1 = 'hello' d) All of the mentioned
a) a new line and hello
2. >>> str2 = ',' b) \nhello
c) the letter r and then hello
3. >>> str3 = 'world' d) error
Explanation: String literal separated by
Answer: b whitespace are allowed. They are
Explanation: When prefixed with the letter concatenated.
‘r’ or ‘R’ a string literal becomes a raw string
and the escape sequences such as \n are not 8. What will be the output of the following
converted. Python statement?
a) Error 97
b) Output equivalent to print ‘new\nline’
c) newline 98
d) new line c) x\97
d) \x97\x98
Answer: c
1. >>>str1="helloworld"
CSE-R17.BLOGSPOT.COM b)
None 22
2. >>>str1[::-1]
10. What will be the output of the following
a) dlrowolleh Python code?
b) hello
c) world print(0xA + 0xB + 0xC)
d) helloworld
a) 0xA0xB0xC
Answer: a b) Error
Explanation: Execute in shell to verify. c) 0x22
d) 33
2. def __init__(self, id): 3.
Answer: d self.id = str(id)
Explanation: 0xA and 0xB and 0xC are
hexadecimal integer literals representing the 4. id="224"
decimal values 10, 11 and 12 respectively.
5.
There sum is 33.
6. >>>temp = tester(12)
1. What will be the output of the following
Python code? 7. >>>print(temp.id)
4. Answer: c
Explanation: Id in this case will be the
5. class child(father):
attribute of the class.
6. def __init__(self, param):
c) 22 None 3. What will be the output of the following
d) Error is generated Python code?
CSE-R17.BLOGSPOT.COM
1. >>>example = "snow world"
2. >>>example[3] = 's'
3. >>>print example 8. What will be the output of the following
Python code?
a) snow
b) snow world 1. >>>example = "helle"
c) Error
d) snos world 2. >>>example.rfind("e")
Answer: c a) -1
Explanation: Strings cannot be modified. b) 4
c) 3
5. What will be the output of the following d) 1
Python code?
Answer: b
1. >>>max("what are you") Explanation: Returns highest index.
Answer: d 2. >>>example[::-1].startswith("d")
Explanation: Max returns the character with
a) dlrowolleh
the highest ascii value.
b) True
6. Given a string example=”hello” what is the c) -1
output of example.count(‘l’)? d) None
a) 2
Answer: b
b) 1
Explanation: Starts with checks if the given
c) None
string starts with the parameter that is
d) 0
passed.
Answer: a
10. To concatenate two strings to a third
Explanation: l occurs twice in hello.
what statements are applicable?
7. What will be the output of the following a) s3 = s1 . s2
Python code? b) s3 = s1.add(s2)
Answer: c c) s3 = s1.__add__(s2)
Explanation: Returns lowest index. d) s3 = s1 * s2
1. >>>chr(ord('A'))
Answer: d
Answer: c
Explanation: Execute help(string.strip) to
Explanation: __add__ is another method that find details.
can be used for concatenation.
5. The format function, when applied on a
1. What will be the output of the
string returns ___________
following Python statement?
a) Error
b) int c) bool
CSE-R17.BLOGSPOT.COM
a) A
b) B Answer: d
c) a Explanation: Format function returns a
d) Error string.
3. >>>print(format(924.656, "3.2f"))
2. >>>print(format(111, "4d"), end =
d) s.getItem(3)
a) Welcome# 111#924.66
b) Welcome#111#924.66 Answer: c
c) Welcome#111#.66 Explanation: __getitem(..) can be used to get
d) Welcome # 111#924.66 character at index specified as parameter.
b) s.getitem(3)
c) s.__getitem__(3) 3. To return the length of string s what
CSE-R17.BLOGSPOT.COM
Answer: d a) s.__len__()
Explanation: Execute in the shell to verify. b) len(s)
c) size(s)
9. What will be displayed by print(ord(‘b’) – d) s.size()
ord(‘a’))?
a) 0 Answer: a
b) 1 Explanation: Execute in shell to verify.
c) -1
d) 2 4. If a class defines the __str__(self) method,
for an object obj for the class, you can use
Answer: b which command to invoke the __str__
Explanation: ASCII value of b is one more method.
than a. Hence the output of this code is 98-97, a) obj.__str__()
which is equal to 1. b) str(obj)
c) print obj
10. Say s=”hello” what will be the return d) all of the mentioned
value of type(s)?
a) int Answer: d
b) bool Explanation: Execute in shell to verify.
c) str
d) String 5. To check whether string s1 contains
another string s2, use ________
Answer: c a) s1.__contains__(s2)
Explanation: str is used to represent strings b) s2 in s1
in python. c) s1.contains(s2)
d) si.in(s2)
1. What is “Hello”.replace(“l”, “e”)?
a) Heeeo Answer: a
b) Heelo Explanation: s2 in s1 works in the same way
c) Heleo as calling the special function __contains__ .
d) None
6. Suppose i is 5 and j is 4, i + j is same as
Answer: a ________
Explanation: Execute in shell to verify. a) i.__add(j)
b) i.__add__(j)
2. To retrieve the character at index 3 from c) i.__Add(j)
string s=”Hello” what command do we d) i.__ADD(j)
execute (multiple answers allowed)? a) s[]
command do we execute?
Downloaded From: https://cse-r17.blogspot.com 37
First Semester MCQ for Regulations 2017
7. firstName = "John"
8. name = Name(firstName, 'F', "Smith"
Answer: b )
Explanation: Execute in shell to verify.
9. firstName = "Peter"
7. What will be the output of the following
10. name.lastName = "Pan"
Python code?
11. print(name.firstName, name.lastName
1. class Count:
2. def __init__(self, count = 0):
CSE-R17.BLOGSPOT.COM
3. self.__count = count
2. def __init__(self, firstName, m i,
4. lastName):
print("abc. DEF".capitalize())
CSE-R17.BLOGSPOT.COM
a) abc. def least one parameter.
b) ABC. DEF
c) Abc. def 4. What will be the output of the following
d) Abc. Def Python code?
Answer: c print("abcdef".center(0))
Explanation: The first letter of the string is
a) cd
converted to uppercase and the others are
converted to lowercase. b) abcdef
c) error
3. What will be the output of the following d) none of the mentioned
Python code?
Answer: b
print("abcdef".center()) Explanation: The entire string is printed
when the argument passed to center() is less
a) cd than the length of the string.
b) abcdef Answer: b
c) error Explanation: Padding is done towards the
d) none of the mentioned left-hand-side first when the final string is of
odd length. Extra spaces are present since we
Answer: c haven’t overridden the value of sep.
Explanation: The function center() takes at
Python code?
6. What will be the output of the following
Python code? print('*', "abcde".center(6), '*', sep=''
)
print('*', "abcdef".center(7), '*', sep=
'') a) * abcde *
b) * abcde *
a) * abcdef * c) *abcde *
b) * abcdef * d) * abcde*
c) *abcdef *
d) * abcdef* Answer: c
Explanation: Padding is done towards the
Answer: d right-hand-side first when the final string is of
Explanation: Padding is done towards the even length.
left-hand-side first when the final string is of
odd length. 8. What will be the output of the following
Python code?
7. What will be the output of the following
CSE-R17.BLOGSPOT.COM
must be a character, not an int.
10. What will be the output of the
9. What will be the output of the following following Python code?
Python code?
print("abcdef".center(10, '12'))
print("abcdef".center(7, '1'))
a) 12abcdef12
a) 1abcdef b) abcdef1212
b) abcdef1 c) 1212abcdef
c) abcdef d) error
d) error
Answer: d
Answer: a Explanation: The fill character must be
Explanation: The character ‘1’ is used for exactly one character long.
padding instead of a space.
1. What will be the output of the
following Python code? c) 1
d) none of the mentioned
print("xyyzxyzxzxyy".count('yy'))
Answer: c
a) 2 Explanation: Counts the number of times the
b) 0 substring ‘yy’ is present in the given string,
c) error starting from position 2.
d) none of the mentioned
a) 2 4. What will be the output of the following
b) 0 Python code?
c) 1
d) none of the mentioned print("xyyzxyzxzxyy".count('xyy', 0, 100)
)
Answer: a
Explanation: Counts the number of times the a) 2
substring ‘yy’ is present in the given string, b) 0
starting from position 1. c) 1
d) error
3. What will be the output of the following
Python code? Answer: a
Explanation: An error will not occur if the
print("xyyzxyzxzxyy".count('yy', 2)) end value is greater than the length of the
string itself.
a) 2
b) 0
CSE-R17.BLOGSPOT.COM
Answer: b
Explanation: Counts the number of times the print("xyyzxyzxzxyy".count('xyy', -10, -1
))
substring ‘xyy’ is present in the given string,
starting from position 2 and ending at a) 2
position 11. b) 0
c) 1
6. What will be the output of the following
d) error
Python code?
Explanation: The function returns True if the
Answer: b given string ends with the specified substring.
Explanation: Counts the number of times the
substring ‘xyy’ is present in the given string, 10. What will be the output of the following
starting from position 2 and ending at Python code?
position 11.
print("xyyzxyzxzxyy".endswith("xyy", 0, 2
7. What will be the output of the following ))
Python code?
a) 0
print('abc'.encode()) b) 1
c) True
a) abc d) False
b) ‘abc’
c) b’abc’ Answer: d
d) h’abc’ Explanation: The function returns False if
the given string does not end with the
Answer: c specified substring.
Explanation: A bytes object is returned by
encode. 1. What will be the output of the following
Python code?
8. What is the default value of encoding in
encode()? print("ab\tcd\tef".expandtabs())
a) ascii
a) ab cd ef
Python code?
b) abcdef
print("xyyzxyzxzxyy".endswith("xyy")) c) ab\tcd\tef
d) ab cd ef
a) 1
b) True Answer: a
c) 3 Explanation: Each \t is converted to 8 blank
d) 2
Answer: b
CSE-R17.BLOGSPOT.COM
c) ab\tcd\tef returned.
d) ab cd ef
6. What will be the output of the following
Answer: d Python code?
Explanation: Each \t is converted to 4 blank
spaces. print("ccdcddcd".find("c"))
a) ab+cd+ef Answer: b
b) ab++++++++cd++++++++ef Explanation: The first position in the given
c) ab cd ef string at which the substring can be found is
d) none of the mentioned returned.
CSE-R17.BLOGSPOT.COM b)
Answer: a
CSE-R17.BLOGSPOT.COM
Explanation: The arguments passed to the
function format can be integers also. 8. What will be the output of the following
Python code snippet?
6. What will be the output of the following
Python code snippet? print('{:,}'.format('1112223334'))
CSE-R17.BLOGSPOT.COM
Python code?
Answer: b
print('ab12'.isalnum()) Explanation: The character , is not a letter or
a digit.
a) True
b) False 5. What will be the output of the following
c) None Python code?
d) Error
print('ab'.isalpha())
Answer: a
Explanation: The string has only letters and a) True
digits. b) False
c) None
4. What will be the output of the following d) Error
Python code?
Answer: a
print('ab,12'.isalnum()) Explanation: The string has only letters.
a) True Answer: b
b) False Explanation: Space is not a letter.
c) None
d) Error 7. What will be the output of the following
Python code snippet?
b) False
print('0xa'.isdigit()) c) None
d) Error
a) True
b) False Answer: b
c) None Explanation: If there are no characters then
d) Error False is returned.
Answer: b 9.What will be the output of the following
Explanation: Hexadecimal digits aren’t Python code snippet?
considered as digits (a-f).
print('my_string'.isidentifier())
8. What will be the output of the following
Python code snippet? a) True
b) False
print(''.isdigit()) c) None
d) Error
a) True
c) None
Answer: a
d) Error
Explanation: It is a valid identifier.
Answer: a
10. What will be the output of the
Explanation: There are no uppercase letters.
following Python code snippet?
4. What will be the output of the following
print('__foo__'.isidentifier())
Python code snippet?
CSE-R17.BLOGSPOT.COM
b) False
c) None Answer: a
d) Error Explanation: Even keywords are considered
as valid identifiers.
Answer: a
Explanation: It is a valid identifier. 2. What will be the output of the following
Python code snippet?
1. What will be the output of the
following Python code snippet? print('abc'.islower())
print('for'.isidentifier()) a) True
b) False
a) True c) None
b) False d) Error
c) None
d) Error Answer: a
Explanation: There are no uppercase letters. b) False
c) None
3. What will be the output of the following d) Error
Python code snippet?
Answer: b
print('fewjfw 1,'.islower()) Explanation: The character . is not a numeric
character.
a) True
b) False 6. What will be the output of the following
print('11'.isnumeric()) Python code snippet?
a) True print('125erdE a'.isprintable())
b) False
c) None a) True
d) Error b) False
c) None
Answer: a d) Error
Explanation: All the character are numeric.
Answer: a
5. What will be the output of the following Explanation: All those characters are
Python code snippet? printable.
print('1.1'.isnumeric()) 7. What will be the output of the following
a) True Python code snippet?
a) True print('#World'.istitle())
b) False a) True
c) None b) False
d) Error c) None
Answer: b d) error
CSE-R17.BLOGSPOT.COM
8. What will be the output of the following
Python code snippet? Answer: a
Explanation: Tab Spaces are considered as
print('\t'.isspace()) spaces.
a) True 9. What will be the output of the following
b) False Python code snippet?
c) None
d) Error print('HelloWorld'.istitle())
c) rn
a) True d) r
b) False
c) None Answer: b
d) Error Explanation: Uppercase letters are converted
to lowercase. The other characters are left
Answer: b unchanged.
Explanation: The letter W is uppercased.
3. What will be the output of the following
10. What will be the output of the following Python code?
Python code snippet?
print('''
print('Hello World'.istitle()) \tfoo'''.lstrip())
a) True a) \tfoo
b) False b) foo
c) None c) foo
d) Error d) none of the mentioned
Answer: a Answer: b
Explanation: It is in title form. Explanation: All leading whitespace is
Answer: a removed.
Explanation: It is in the form of a title.
4. What will be the output of the following
2. What will be the output of the following Python code?
Python code?
print('xyyzxxyxyy'.lstrip('xyy'))
print('GYMK'.lower())
a) error
a) n b) zxxyxyy
b) gymk
(‘abef’)
print('xyxxyyzxxy'.lstrip('xyy')) c) (‘ab’, ‘cd’, ‘ef’)
d) 2
a) zxxy
b) xyxxyyzxxy Answer: c
c) xyxzxxy Explanation: The string is split into three
d) none of the mentioned parts by partition.
Answer: a 9. What will be the output of the following
Explanation: All combinations of the Python code?
characters passed as an argument are removed
from the left hand side. print('abcdefcdgh'.partition('cd'))
6. What will be the output of the following a) (‘ab’, ‘cd’, ‘ef’, ‘cd’, ‘gh’)
Python code? b) (‘ab’, ‘cd’, ‘efcdgh’)
c) (‘abcdef’, ‘cd’, ‘gh’)
print('cba'.maketrans('abc', '123')) d) error
a) {97: 49, 98: 50, 99: 51} Answer: b
b) {65: 49, 66: 50, 67: 51} Explanation: The string is partitioned at the
c) 321 point where the separator first appears.
d) 123
10. What will be the output of the following
Answer: a Python code?
Explanation: A translation table is returned
by maketrans. print('abcd'.partition('cd'))
CSE-R17.BLOGSPOT.COM
2. What will be the output of the following a) ab1ef2
Python code snippet? b) abcefd
c) ab1efd
print('abef'.partition('cd')) d) ab12ed2
a) (‘abef’) Answer: b
b) (‘abef’, ‘cd’, ”) Explanation: The first substring is not
c) (‘abef’, ”, ”) present in the given string and hence nothing
d) error is replaced.
Answer: c 6. What will be the output of the following
Explanation: The separator is not present in Python code snippet?
the string hence the second and the third
elements of the tuple are null strings. print('xyyxyyxyxyxxy'.replace('xy', '12',
0))
3. What will be the output of the following
Python code snippet? a) xyyxyyxyxyxxy
b) 12y12y1212x12
print('abcdef12'.replace('cd', '12')) c) 12yxyyxyxyxxy
d) xyyxyyxyxyx12
a) ab12ef12
b) abcdef12 Answer: a
c) ab12efcd Explanation: The first 0 occurrences of the
d) none of the mentioned given substring are replaced.
Answer: a 7. What will be the output of the following
Explanation: All occurrences of the first Python code snippet?
substring are replaced by the second
substring. print('xyyxyyxyxyxxy'.replace('xy', '12',
100))
4. What will be the output of the following
Python code snippet? a) xyyxyyxyxyxxy
b) 12y12y1212x12
print('abef'.replace('cd', '12')) c) none of the mentioned
d) error
a) abef
b) 12 Answer: b
c) error Explanation: The first 100 occurrences of the
d) none of the mentioned given substring are replaced.
Downloaded From: https://cse-r17.blogspot.com 49
First Semester MCQ for Regulations 2017
d) (‘ab’, ‘ef’, ‘gh’, ”)
a) [‘ab’, ‘ef’, ‘ghcd’]
b) [‘ab’, ‘efcdghcd’]
8. What will be the output of the following
c) [‘abcdef’, ‘ghcd’]
Python code snippet?
d) none of the mentioned
print('abcdefcdghcd'.split('cd'))
Answer: a
a) [‘ab’, ‘ef’, ‘gh’] Explanation: The string is split into a
b) [‘ab’, ‘ef’, ‘gh’, ”] maximum of maxsplit+1 substrings.
c) (‘ab’, ‘ef’, ‘gh’)
CSE-R17.BLOGSPOT.COM
Answer: b possible.
Explanation: The given string is split and a
list of substrings is returned. 1. What will be the output of the
following Python code snippet?
9. What will be the output of the following
Python code snippet? print('abcdefcdghcd'.split('cd', 2))
2. What will be the output of the following
print('abcdefcdghcd'.split('cd', 0)) Python code snippet?
a) [‘abcdefcdghcd’] print('ab\ncd\nef'.splitlines())
b) ‘abcdefcdghcd’
c) error a) [‘ab’, ‘cd’, ‘ef’]
d) none of the mentioned b) [‘ab\n’, ‘cd\n’, ‘ef\n’]
c) [‘ab\n’, ‘cd\n’, ‘ef’]
Answer: a d) [‘ab’, ‘cd’, ‘ef\n’]
Explanation: The given string is split at 0
occurances of the specified substring. Answer: a
Explanation: It is similar to calling
10. What will be the output of the split(‘\n’).
following Python code snippet?
3. What will be the output of the following
print('abcdefcdghcd'.split('cd', -1)) Python code snippet?
CSE-R17.BLOGSPOT.COM
c) Ab Cd-Ef 9: 100}))
d) None of the mentioned
a) bcde
Answer: c b) abcd
Explanation: The first letter of every word is c) error
capitalized. Special symbols terminate a d) none of the mentioned
word.
Answer: d
6. What will be the output of the following Explanation: The output is bcdd since no
Python code snippet? translation is provided for d.
CSE-R17.BLOGSPOT.COM
which is present in the random module. The decimal digits. In the above case, since the
functions sqrt and factorial are a part of the number of decimal places has not been
math module. The print function is a built-in specified, the decimal number is rounded off
function which prints a value directly to the to a whole number. Hence the output will be
system output. 5.
2. What will be the output of the following 3. The function pow(x,y,z) is evaluated
Python expression? as: a) (x**y)**z
b) (x**y) / z
round(4.576) c) (x**y) % z
d) (x**y)*z
a) 4.5
b) 5 Answer: c
c) 4 Explanation: The built-in function pow() can
d) 4.6 accept two or three arguments. When it takes
in two arguments, they are evaluated as x**y.
Answer: b
When it takes in three arguments, they are
Explanation: This is a built-in function
which rounds a number to give precision in evaluated as (x**y)%z.
number should be rounded off to two decimal
4. What will be the output of the following places. Hence the output will be 4.57.
Python function?
any one of the elements of the iterable is zero 6. What will be the output of the following
and true if all the elements of the iterable are Python function?
non zero. Hence the output of this function
will be false. any([2>8, 4>2, 1>2])
a) 4.5 Answer: b
b) 4.6 Explanation: The built-in function any()
c) 4.57 returns true if any or more of the elements of
d) 4.56 the iterable is true (non zero), If all the
elements are zero, it returns false.
Answer: c
Explanation: The function round is used to 7. What will be the output of the following
round off the given decimal number to the Python function?
specified decimal places. In this case, the
a) 2
import math b) False
abs(math.sqrt(25))
c) -3
a) Error d) -4
b) -5
Answer: b
c) 5
Explanation: The function max() is being
d) 5.0
Answer: d
CSE-R17.BLOGSPOT.COM
Explanation: The abs() function prints the
absolute value of the argument passed. For a) Error, 6
example: abs(-5)=5. Hence, in this case we b) 12, Error
get abs(5.0)=5.0. c) 12, 6
d) Error, Error
8. What will be the output of the following
Python function? Answer: a
Explanation: The first function will result in
sum(2,4,6) an error because the function sum() is used to
sum([1,2,3]) find the sum of iterable numbers. Hence the
outcomes will be Error and 6 respectively.
a)
9. What will be the output of the following
Python function? a
Error
all(3,0,4.2)
a) True b)
b) False
‘a’
c) Error
d) 0 a
Answer: c c)
Explanation: The function all() returns
‘True’ if any one or more of the elements of Error
the iterable are non zero. In the above case,
a
the values are not iterable, hence an error is
thrown. d)
10. What will be the output of the Error
following Python function?
used to find the maximum value from among Error
-3, -4 and false. Since false amounts to the Answer: c
value zero, hence we are left with min(0, 2, 7) Explanation: The built-in function chr()
Hence the output is 0 (false). returns the alphabet corresponding to the
value given as an argument. This function
1. What will be the output of the following
accepts only integer type values. In the first
Python functions?
function, we have passed a string. Hence the
chr(‘97’) first function throws an error.
chr(97)
a)
(2.00, 0.50)
2. What will be the output of
the following Python function?(2.00, 0.00) b)
a) Error
CSE-R17.BLOGSPOT.COM
b) 1 c) 2j
d) 1+2j
c)
Answer: d
(2.0, 0.5)
Explanation: The built-in function complex()
returns the argument in a complex form. (2.0, 0.0)
Hence the output of the function shown above
will be 1+2j. d)
Answer: a
Explanation: The complex function returns
0j if both of the arguments are omitted, that 6. The function complex(‘2-3j’) is valid but
is, if the function is in the form of complex() the function complex(‘2 – 3j’) is invalid. a)
or complex(0), then the output will be 0j. True
b) False
4. The function divmod(a,b), where both ‘a’
and ‘b’ are integers is evaluated as: a) (a%b, Answer: a
a//b) Explanation: When converting from a string,
b) (a//b, a%b) the string must not contain any blank spaces
c) (a//b, a*b) around the + or – operator. Hence the
d) (a/b, a%b) function complex(‘2 – 3j’) will result in an
error.
Answer: b
Explanation: The function divmod(a,b) is 7. What will be the output of the following
evaluated as a//b, a%b, if both ‘a’ and ‘b’ are Python function?
integers.
list(enumerate([2, 3]))
5. What will be the output of the following
a) Error
Python function?
b) [(1, 2), (2, 3)]
divmod(10.5,5) c) [(0, 2), (1, 3)]
divmod(2.4,1.2) d) [(2, 3)]
(2, 0)
CSE-R17.BLOGSPOT.COM
Python functions? given number.
x=3
eval('x^2')
10. Which of the following functions does
a) Error
not necessarily accept only iterables as
b) 1
arguments?
c) 9
a) enumerate()
d) 6
b) all()
Answer: b c) chr()
Explanation: The function eval is use to d) max()
evaluate the expression that it takes as an
Answer: c
argument. In the above case, the eval()
Explanation: The functions enumerate(), all()
function is used to perform XOR operation
and max() accept iterables as arguments
between 3 and 2. Hence the output is 1.
whereas the function chr() throws an error on
9. What will be the output of the following receiving an iterable as an argument. Also
Python functions? note that the function chr() accepts only
integer values.
float('1e-003')
float('2e+003') 1. Which of the following functions
accepts only integers as arguments?
a) a) ord()
b) min()
3.00
c) chr()
300 d) any()
b) Answer: c
Explanation: The function chr() accepts only
0.001 integers as arguments. The function ord()
2000.0 accepts only strings. The functions min() and
max() can accept floating point as well as
c) integer arguments.
CSE-R17.BLOGSPOT.COM
list, we use: list(reversed(l)). The output will
65
be: [4,3,2].
Error
a) c)
Error
A
Error
65
b) d)
Error
Error
Explanation: The output of the first function
Junk value
Answer: a
6. Which of the following functions will not 9. What will be the output of the following
result in an error when no arguments are Python function?
passed to it?
CSE-R17.BLOGSPOT.COM
b) divmod()
c) all() 8. Which of the following functions does
d) float() not throw an error?
a) ord()
Answer: d b) ord(‘ ‘)
Explanation: The built-in functions min(), c) ord(”)
max(), divmod(), ord(), any(), all() etc throw d) ord(“”)
an error when no arguments are passed to
them. However there are some built-in Answer: b
functions like float(), complex() etc which do Explanation: The function ord() accepts a
not throw an error when no arguments are character. Hence ord(), ord(”) and ord(“”)
passed to them. The output of float() is 0.0. len(["hello",2, 4, 6])
a) f Answer: a
b) 0xF Explanation: The function len() returns the
c) 0Xf length of the number of elements in the
d) 0xf iterable. Therefore the output of the function
shown above is 4.
Answer: d
Explanation: The function hex() is used to 10. What will be the output of the following
convert the given argument into its Python function?
hexadecimal representation, in lower case.
Hence the output of the function hex(15) is oct(7)
oct(‘7’)
0xf.
a) c)
Error 0o7
07 Error
b) d)
0o7 07
Error 0o7
CSE-R17.BLOGSPOT.COM
TOPIC 3.3.2 USER DEFINED
Answer: c
FUNCTIONS
Explanation: None.
1. Which of the following is the use 3. What will be the output of the following
of function in python? Python code?
a) Functions are reusable pieces of programs
b) Functions don’t provide better modularity 1. def sayHello():
for your application
c) you can’t also create your own functions 2. print('Hello World!')
d) All of the mentioned 3. sayHello()
Answer: a 4. sayHello()
Explanation: Functions are reusable pieces
of programs. They allow you to give a name a)
to a block of statements, allowing you to run c)
that block using the specified name anywhere
Hello
in your program and any number of times.
Hello
2. Which keyword is used for function?
a) Fun d) None of the mentioned
b) Define
c) Def Answer: a
d) Function Explanation: Functions are defined using the
def keyword. After this keyword comes an
identifier name for the function, followed by
a pair of parentheses which may enclose 6. sayHello() # call the function agai
n
some names of variables, and by the final
colon that ends the line. Next follows the 4. What will be the output of the following
block of statements that are part of this Python code?
function.
1. def printMax(a, b):
1. def sayHello():
2. if a > b:
2. print('Hello World!') # block b
elonging to the function 3. print(a, 'is maximum') 4. elif
3. # End of function # a == b:
4.
5. sayHello() # call the function
CSE-R17.BLOGSPOT.COM
b) 4
4. x = 2
c) 4 is maximum
d) None of the mentioned 5. print('Changed local x to', x) 6.
Answer: c func(x)
Explanation: Here, we define a function
7. print('x is now', x)
called printMax that uses two parameters
called a and b. We find out the greater a)
number using a simple if..else statement and
then print the bigger number. x is 50
1. x = 50 b)
2. def func(x): x is 50
3. print('x is', x) Changed local x to 2
x is now 2 1. x = 50
Answer: a 2. def func():
Explanation: The first time that we print the
value of the name x with the first line in the 3. global x
function’s body, Python uses the value of the
parameter declared in the main block, above 4. print('x is', x)
the function definition. 5. x = 2
Next, we assign the value 2 to x. The name x
is local to our function. So, when we change 6. print('Changed global x to', x) 7.
the value of x in the function, the x defined in func()
the main block remains unaffected. With the
last print function call, we display the value 8. print('Value of x is', x)
of x as defined in the main block, thereby
confirming that it is actually unaffected by a)
the local assignment within the previously
x is 50
called function.
Changed global x to 2
6. What will be the output of the following
Python code? Value of x is 50
Hello
b)
HelloHelloHelloHelloHello
x is 50
Answer: a
Changed global x to 2 Value of x is 2 Explanation: For some functions, you may
want to make some parameters optional and
c)
CSE-R17.BLOGSPOT.COM
x is 50
7. What will be the output of the following
Changed global x to 50
Python code?
Value of x is 50
1. def say(message, times = 1):
d) None of the mentioned
2. print(message * times)
Answer: b
3. say('Hello')
Explanation: The global statement is used to
declare that x is a global variable – hence, 4. say('World', 5)
when we assign a value to x inside the
function, that change is reflected when we use a)
the value of x in the main block.
Hello
In the first usage of say, we supply only the
WorldWorldWorldWorldWorld
string and it prints the string once. In the
b) second usage of say, we supply both the
string and an argument 5 stating that we want
Hello to say the string message 5 times.
World 5
is 25 and c is 24 2. if x > y:
CSE-R17.BLOGSPOT.COM
a is 50 and b is 100 and c is 5
a is 100 and b is 5 and c is 50
c)
d) None of the mentioned
a is 3 and b is 7 and c is 10
Answer: c
a is 25 and b is 5 and c is 24 Explanation: If you have some functions
with many parameters and you want to
specify only some of them, then you can give 7. return y
values for such parameters by naming them – 8.
this is called keyword arguments – we use the
name (keyword) instead of the position 9. print(maximum(2, 3))
(which we have been using all along) to
specify the arguments to the function. The a) 2
function named func has one parameter b) 3
without a default argument value, followed c) The numbers are equal
by two parameters with default argument d) None of the mentioned
values.
Answer: b
In the first usage, func(3, 7), the parameter a Explanation: The maximum function returns
gets the value 3, the parameter b gets the the maximum of the parameters, in this case
value 7 and c gets the default value of 10. the numbers supplied to the function. It uses a
simple if..else statement to find the greater
In the second usage func(25, c=24), the value and then returns that value.
variable a gets the value of 25 due to the
position of the argument. Then, the parameter 10. Which of the following is a feature
c gets the value of 24 due to naming i.e. of DocString?
keyword arguments. The variable b gets the a) Provide a convenient way of associating
default value of 5. documentation with Python modules,
functions, classes, and methods
In the third usage func(c=50, a=100), we use b) All functions should have a docstring c)
keyword arguments for all specified values. Docstrings can be accessed by the __doc__
Notice that we are specifying the value for attribute on objects
d) All of the mentioned
3. return x
4. elif x == y: Answer: d
Explanation: Python has a nifty feature
5. return 'The numbers are equ al' called documentation strings, usually referred
to by its shorter name docstrings. DocStrings
6. else:
CSE-R17.BLOGSPOT.COM
c) Improving clarity of the code object’s id.
d) All of the mentioned
6. Which of the following refers to
Answer: d mathematical function?
Explanation: None. a) sqrt
b) rhombus
2. What are the two main types of functions? c) add
a) Custom function d) rhombus
b) Built-in function & User defined function
c) User function Answer: a
d) System function Explanation: Functions that are always
available for usage, functions that are
Answer: b contained within external modules, which
Explanation: Built-in functions and user must be imported and functions defined by a
defined ones. The built-in functions are part programmer with the def keyword. Eg: math
of the Python language. Examples are: dir(), import sqrt
len() or abs(). The user defined functions are A sqrt() function is imported from the math
functions created with the def keyword. module.
3. Where is function defined? 7. What will be the output of the following
a) Module Python code?
b) Class
c) Another function 1. def cube(x):
d) All of the mentioned
2. return x * x * x
Answer: d
Explanation: Functions can be defined inside 3. x = cube(3)
a module, a class or another function. 4. print x
4. What is called when a function is defined a) 9
inside a class? b) 3
a) Module c) 27
b) Class d) 30
c) Another function
d) Method Answer: c
Explanation: A function is created to do a
Answer: d specific task. Often there is a result from such
Explanation: None. a task. The return keyword is used to return
unique id. The id() function returns the
1. def C2F(c):
values from a function. A function
5. return r
may or may not return a value. If a
function does not have a return 6. print power(3)
keyword, it will send a none value.
7. print power(3, 3) a)
8. What will be the output of the
212
CSE-R17.BLOGSPOT.COM
2. return c * 9/5 + 32
9
3. print C2F(100)
27
4. print C2F(0)
c)
a)
567
212
98
32
d) None of the mentioned
b)
Answer: b
314 Explanation: The arguments in Python
functions may have implicit values. An
24
implicit value is used, if no value is provided.
c) Here we created a power function. The
function has one argument with an implicit
567 value. We can call the function with one or
two arguments.
98
10. What will be the output of the following
d) None of the mentioned
Python code?
Answer: a
1. def sum(*args):
Explanation: The code shown above is used
to convert a temperature in degree celsius to 2. '''Function returns the sum 3.
fahrenheit.
of all values'''
9. What will be the output of the following 4. r = 0
Python code?
5. for i in args:
1. def power(x, y=2):
6. r += i
2. r = 1
7. return r
3. for i in range(y):
8. print sum.__doc__
4. r = r * x
32
b)
CSE-R17.BLOGSPOT.COM
b) a) 48
b) 14
6 c) 64
100 d) None of the mentioned
c) Answer: a
Explanation: The lambda keyword creates an
123 anonymous function. The x is a parameter,
that is passed to the lambda function. The
12345
parameter is followed by a colon character.
d) None of the mentioned The code next to the colon is the expression
that is executed, when the lambda function is
Answer: a called. The lambda function is assigned to the
Explanation: We use the * operator to z variable.
indicate, that the function will accept The lambda function is executed. The number
arbitrary number of arguments. The sum() 8 is passed to the anonymous function and it
function will return the sum of all arguments. returns 48 as the result. Note that z is not a
The first string in the function body is called name for this function. It is only a variable to
the function documentation string. It is used which the anonymous function was assigned.
to document the function. The string must be
in triple quotes. 3. What will be the output of the following
Python code?
1. Python supports the creation of
anonymous functions at runtime, using a 1. lamb = lambda x: x ** 3
construct called __________
2. print(lamb(5))
a) lambda
b) pi a) 15
c) anonymous b) 555
d) none of the mentioned c) 125
d) None of the mentioned
Answer: a
Explanation: Python supports the creation of Answer: c
anonymous functions (i.e. functions that are Explanation: None.
not bound to a name) at runtime, using a
construct called lambda. Lambda functions 4. Does Lambda contains return statements?
are restricted to a single expression. They can a) True
be used wherever normal functions can be b) False
used.
Downloaded From: https://cse-r17.blogspot.com 64
First Semester MCQ for Regulations 2017
5. Lambda is a statement.
5.
Answer: b
Explanation: lambda definition 6. who = writer() 7.
does not include a return statement.
who('Arthur')
it always contains an expression
which is returned. Also note that a) Arthur Sir
we can put a lambda definition b) Sir Arthur
anywhere a function is expected. c) Arthur
We don’t have to assign it to a
variable at all.
CSE-R17.BLOGSPOT.COM
a) True
b) False 8. What will be the output of the following
Python code?
Answer: b
Explanation: lambda is an anonymous 1. def writer():
function in Python. Hence this statement is
2. title = 'Sir'
false.
3. name = (lambda x:title + ' ' + x)
6. Lambda contains block of
statements. a) True 4. return name
b) False d) None of the mentioned
Answer: b Answer: b
Explanation: None. Explanation: None.
7. What will be the output of the following 9. What will be the output of the following
Python code? Python code?
a) 432 3. lambda x: x ** 4]
b) 24000
4.
c) 430
d) No output 5. for f in L:
Answer: a 6. print(f(3))
Explanation: None.
a)
27 12
81 c)
343 9
b) 27
6 81
CSE-R17.BLOGSPOT.COM
y) outside all function definitions is referred to
as a global variable and can be used by
2. min(101*99, 102*98)
multiple functions of the program.
a) 9997
2. What is a variable defined inside a function
b) 9999
c) 9996 referred to as?
d) None of the mentioned a) A global variable
b) A volatile variable
Answer: c c) A local variable
Explanation: None. d) An automatic variable
4. What will be the output of the following 5. What will be the output of the following
Python code? Python code?
Answer: b
Answer: b
10 56 b) 45 56 Explanation: The parameter two is a variable
parameter and consists of (2,3,4). Hence the
a) Integer data type is tuple.
b) Tuple
c) Dictionary
CSE-R17.BLOGSPOT.COM c)
10
6. What will be the output of the following
20
Python code?
d) Syntax Error
def change(i = 1, j = 2):
i = i + j
Answer: a j = j + 1
Explanation: The statement “global b” print(i, j)
allows the global value of b to be accessed change(j = 1, i = 2)
and changed. Whereas the variable a is local
and hence the change isn’t reflected outside a) An exception is thrown because of
the function. conflicting values
b) 1 2
c) 3 3 function doesn’t have a return statement.
d) 3 2
9. What will be the output of the following
Answer: d Python code?
Explanation: The values given during
function call is taken into consideration, that def display(b, n):
while n > 0:
is, i=2 and j=1.
print(b,end="")
n=n-1
7. What will be the output of the following display('z',3)
Python code?
a) zzz
def change(one, *two): b) zz
print(type(two))
change(1,2,3,4)
c) An exception is executed
8. If a function doesn’t have a return d) Infinite loop
statement, which of the following does the
Answer: a
function return?
Explanation: The loop runs three times and
a) int
‘z’ is printed each time.
b) null
c) None 10. What will be the output of the following
d) An exception is thrown without the return Python code?
statement
def find(a, **b):
Answer: c print(type(b))
Explanation: A function can exist without a find('letters',A='1',B='2')
return statement and returns None if the
CSE-R17.BLOGSPOT.COM
TOPIC 3.3 ARGUMENTS AND sys.argv?
PARAMETERS a) set
b) list
c) tuple
1. What is the type of each element in
d) string
passed to a function in a single function call?
Answer: d a) zero
Explanation: It is a list of strings. b) one
c) zero or more
2. What is the length of sys.argv? d) one or more
a) number of arguments
b) number of arguments + 1 Answer: c
c) number of arguments – 1 Explanation: Zero keyword arguments may
d) none of the mentioned be passed if all the arguments have default
values.
Answer: b
Explanation: The first argument is the name 6. What will be the output of the following
of the program itself. Therefore the length of Python code?
sys.argv is one more than the number
arguments. def foo(fname, val):
print(fname(val))
3. What will be the output of the following foo(max, [1, 2, 3])
foo(min, [1, 2, 3])
Python code?
def foo(k):
a) 3 1
k[0] = 1 b) 1 3
q = [0] c) error
foo(q) d) none of the mentioned
print(q)
Answer: a
a) [0] Explanation: It is possible to pass function
b) [1] names as arguments to other functions.
c) [1, 0]
d) [0, 1] 7. What will be the output of the following
d) two underscores followed by a valid Python code?
identifier
def foo():
Answer: c return total + 1
Explanation: Refer documentation.
Answer: b
CSE-R17.BLOGSPOT.COM
Explanation: It is possible to read the value
of a global variable directly. Answer: b
Explanation: When a list is a default value,
8. What will be the output of the following the same list will be reused.
Python code?
1. What will be the output of the following
def foo(): Python code?
total += 1
return total def foo(k):
total = 0 k = [1]
print(foo()) q = [0]
foo(q)
a) 0 print(q)
b) 1
c) error a) [0]
d) none of the mentioned b) [1]
c) [1, 0]
Answer: c d) [0, 1]
Explanation: It is not possible to change the
value of a global variable without explicitly Answer: a
specifying it. Explanation: A new list object is created in
the function and the reference is lost. This can
9. What will be the output of the following be checked by comparing the id of k before
Python code? and after k = [1].
CSE-R17.BLOGSPOT.COM
4. What is the type of sys.argv? def foo(x):
x[0] = ['def']
a) set
x[1] = ['abc']
b) list return id(x)
c) tuple q = ['abc', 'def']
d) string print(id(q) == foo(q))
Answer: b a) True
Explanation: It is a list of elements. b) False
c) None
5. What is the value stored in sys.argv[0]? d) Error
a) null
b) you cannot access it Answer: a
c) the program’s name Explanation: The same object is modified in
d) the first argument the function.
CSE-R17.BLOGSPOT.COM
Python code? d) 99
CSE-R17.BLOGSPOT.COM
16 d) 4 15
hello Answer: c
Answer: a Explanation: At the time of leader
Explanation: The code shown above will processing, the value of ‘x’ is 12. It is not
result in an error because ‘x’ is a global modified later. The value passed to the
variable. Had it been a local variable, the function f1 is 4. Hence the output of the code
output would be: 16 shown above is 4 12.
hello
7. What will be the output of the following
Python code?
world
a) Error
b) 12 4 hello
c) 4 12
hello
Explanation: Since the variable ‘a’ has been
c) explicitly specified as a global variable, the
value of a passed to the function is ‘world’.
hello
Hence the output of this code is:
world world
hello
world hello
d)
Answer: d
return b
print(f1(2,[3,4])) error b)
CSE-R17.BLOGSPOT.COM
Explanation: In the code shown above, the c) 10 20 30 40
integer 2 is appended to the list [3,4]. Hence d) 5 10 15 40
the output of the code is [3,4,2]. Both the
variables a and b are local variables. Answer: c
Explanation: The above code shows a
9. What will be the output of the following combination of local and global variables.
Python code? The output of this code is: 10 20 30 40
def f(p, q, r): 10. What will be the output of the following
global s Python code?
p = 10
q = 20 def f(x):
r = 30 print("outer")
s = 40 def f1(a):
print(p,q,r,s) print("inner")
p,q,r,s = 1,2,3,4 print(a,x)
f(5,10,15) f(3)
f1(1)
a) 1 2 3 4
b) 5 10 15 4 a)
x = 4
outer def f2(a,b):
global x
outer
return a+b+x
f1()
inner
total = f2(1,2)
print(total)
d) error
a) Error
Answer: a
b) 7
Explanation: The error will be caused due to
c) 8
the statement f1(1) because the function is
d) 15
nested. If f1(1) had been called inside the
function, the output would have been Answer: b
different and there would be no error. Explanation: In the code shown above, the
variable ‘x’ has been declared as a global
11. What will be the output of the following
variable under both the functions f1 and f2.
Python code? The value returned is a+b+x = 1+2+4 = 7.
x = 5
def f1(): 12. What will be the output of the following
global x Python code?
CSE-R17.BLOGSPOT.COM
b) 90
c) 80 y, z = 1, 2
def f():
d) Error
global x
x = y+z
Answer: a
Explanation: The output of the code shown a) x
above is 100. This is because the variable ‘x’ b) y and z
has been declared as global within the c) x, y and z
functions f1 and f2. d) Neither x, nor y, nor z
13. Read the following Python code Answer: c
carefully and point out the global variables? Explanation: In the code shown above, x, y
and z are global variables inside the function within the function. Hence the output is 2.
f. y and z are global because they are not
assigned in the function. x is a global variable 3. On assigning a value to a variable inside a
because it is explicitly specified so in the function, it automatically becomes a global
code. Hence, x, y and z are global variables. variable.
a) True
1. Which of the following data structures b) False
is returned by the functions globals() and
locals()? Answer: b
a) list Explanation: On assigning a value to a
b) set variable inside a function, t automatically
c) dictionary becomes a local variable. Hence the above
d) tuple statement is false.
def cg():
global x 4. What will be the output of the following
x=x+1 Python code?
cg()
x e="butter"
def f(a): print(a)+e
a) 2 f("bitter")
b) 1
c) 0 a) error
d) Error b)
Answer: a butter
Explanation: Since ‘x’ has been declared a
error
global variable, it can be modified very easily
bitter a) Error
b) 4
error c) Junk value
d) 1
d) bitterbutter
Answer: c
CSE-R17.BLOGSPOT.COM Ex
planation: The output of the code shown
above will be ‘bitter’, followed by an error. Python code?
The error is because the operand ‘+’ is Answer: d
unsupported on the types used above. Explanation: In the code shown above, when
we call the function f, a new namespace is
5. What happens if a local variable exists with created. The assignment x=4 is performed in
the same name as the global variable you the local namespace and does not affect the
want to access? global namespace. Hence the output is 1.
a) Error
b) The local variable is shadowed 8. ______________ returns a dictionary of
c) Undefined behavior the module namespace.
d) The global variable is shadowed ________________ returns a dictionary of
the current namespace.
Answer: d a)
Explanation: If a local variable exists with
the same name as the local variable that you locals()
want to access, then the global variable is
globals()
shadowed. That is, preference is given to the
local variable. b)
6. What will be the output of the following locals()
Python code?
locals()
a=10
globals()['a']=25 c)
print(a)
globals()
a) 10
locals()
b) 25
c) Junk value d)
d) Error
globals()
Answer: b
Explanation: In the code shown above, the globals()
value of ‘a’ can be changed by using globals() Answer: c
function. The dictionary returned is accessed Explanation: The function globals() returns a
using key of the variable ‘a’ and modified to dictionary of the module namespace, whereas
25. the function locals() returns a dictionary of
the current namespace.
7. What will be the output of the following
CSE-R17.BLOGSPOT.COM
b) A function execution instance that calls c) num*(num-1)
another execution instance of the same d) fact(num)*fact(num-1)
function
c) A class method that calls another class Answer: a
method Explanation: Suppose n=5 then, 5*4*3*2*1
d) An in-built method that is automatically is returned which is the factorial of 5.
called
5. What will be the output of the following
Answer: b Python code?
Explanation: The appropriate definition for a
def test(i,j):
recursive function is a function execution if(i==0):
instance that calls another execution instance return j
of the same function either directly or else:
indirectly. return test(i-1,i+j)
print(test(4,7))
2. Only problems that are recursively defined
a) 13
can be solved using recursion.
b) 7
a) True
c) Infinite loop
b) False
d) 17
Answer: b
Answer: d
Explanation: There are many other problems
Explanation: The test(i-1,i+j) part of the
can also be solved using recursion.
function keeps calling the function until the
3. Which of these is false about recursion? base condition of the function is satisfied.
a) Recursive function can be replaced by a
6. What will be the output of the following
non-recursive function
Python code?
b) Recursive functions usually take more
memory space than non-recursive function l=[]
c) Recursive functions run faster than non def convert(b):
recursive function if(b==0):
d) Recursion makes programs easier to return l
dig=b%2
understand l.append(dig)
convert(b//2)
Answer: c convert(6)
Explanation: The speed of a program using l.reverse()
recursion is slower than the speed of its non for i in l:
recursive equivalent. print(i,end="")
b) (num-1)*(num-2)
Downloaded From: https://cse-r17.blogspot.com 76
First Semester MCQ for Regulations 2017
binary equivalent of the number.
a) Every recursive function must have a base
case
a) 011
b) Infinite recursion can occur if the base case
b) 110
c) 3 isn’t properly mentioned
d) Infinite loop c) A recursive function makes the code easier
to understand
Answer: b d) Every recursive function must have a
Explanation: The above code gives the return value
CSE-R17.BLOGSPOT.COM
7. What is tail recursion? Explanation: A recursive function is tail
a) A recursive function that has two base recursive when recursive call is executed by
cases the function in the last.
b) A function where the recursive functions
leads to an infinite loop 9. Which of the following statements is false
c) A recursive function where the function about recursion?
doesn’t return anything and just prints the Answer: d
values Explanation: A recursive function needn’t
d) A function where the recursive call is the have a return value.
last thing executed by the function
10. What will be the output of the following
Answer: d Python code?
Explanation: A recursive function is tail
def fun(n):
recursive when recursive call is executed by
if (n > 100):
the function in the last. return n - 5
return fun(fun(n+11));
8. Observe the following Python code?
print(fun(45))
def a(n):
if n == 0: a) 50
return 0 b) 100
else:
c) 74
return n*a(n - 1)
def b(n, tot): d) Infinite loop
if n == 0:
return tot Answer: b
else: Explanation: The fun(fun(n+11)) part of the
return b(n-2, tot-2) code keeps executing until the value of n
becomes greater than 100, after which n-5 is
a) Both a() and b() aren’t tail recursive returned and printed.
b) Both a() and b() are tail recursive c)
b() is tail recursive but a() isn’t 11. Recursion and iteration are the same
d) a() is tail recursive but b() isn’t programming approach.
a) True
Answer: c
b) False
12. What happens if the base condition
Answer: b isn’t defined in recursive programs?
Explanation: In recursion, the function calls a) Program gets into an infinite loop
itself till the base condition is reached b) Program runs once
whereas iteration means repetition of process
for example in for-loops.
CSE-R17.BLOGSPOT.COM
recursion?
a) Making the code look clean
b) A complex task can be broken into sub Explanation: Recursive functions may be
problems hard to debug as the logic behind recursion
c) Recursive calls take up less memory d) may be hard to follow.
Sequence generation is easier than a nested
iteration 15. What will be the output of the
following Python code?
Answer: c
Explanation: Recursive calls take up a lot of def a(n):
if n == 0:
memory and time as memory is taken up each return 0
time the function is called. elif n == 1:
return 1
14. Which of these is not true else:
about recursion? return a(n-1)+a(n-2)
a) It’s easier to code some real-world for i in range(0,4):
print(a(i),end=" ")
problems using recursion than non-recursive
equivalent
b) Recursive functions are easy to debug c)
UNIT IV LISTS, TUPLES,
Recursive calls take up a lot of memory d) DICTIONARIES
Programs using recursion take longer time
than their non-recursive equivalent
TOPIC 4.1 LISTS ( LIST
Answer: b OPERATIONS, LIST SLICES,
LIST METHODS, LIST LOOP,
2. What is the output when we execute
MUTABILITY, ALIASING,
list(“hello”)?
CLONING LISTS, LIST a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
PARAMETERS) b) [‘hello’]
c) [‘llo’]
1. Which of the following commands d) [‘olleh’]
will create a list?
a) list1 = list() Answer: a
b) list1 = [] Explanation: Execute in the shell to verify.
c) list1 = list([1, 2, 3])
3. Suppose listExample is [‘h’,’e’,’l’,’l’,’o’],
d) all of the mentioned
what is len(listExample)?
Answer: d a) 5
Explanation: Execute in the shell to verify
CSE-R17.BLOGSPOT.COM
a) 2445 Explanation: Min returns the minimum
b) 133 element in the list.
c) 12454
d) 123 6. Suppose list1 is [1, 5, 9], what is
sum(list1)?
Answer: c a) 1
Explanation: Max returns the maximum b) 9
element in the list. c) 15
d) Error
5. Suppose list1 is [3, 5, 25, 1, 3], what is
min(list1)? Answer: c
a) 3 Explanation: Sum returns the sum of all
b) 5 elements in the list.
c) 25
d) 1 7. To shuffle the list(say list1) what function
do we use?
Answer: d a) list1.shuffle()
b) shuffle(list1) b) Error
c) random.shuffle(list1) c) 25
d) random.shuffleList(list1) d) [25, 14, 222, 33, 2]
Answer: c Answer: a
Explanation: Execute in the shell to verify. Explanation: Execute in the shell to verify.
Explanation: Slicing is allowed in lists just
as in the case of strings. 1. What will be the output of the following
Python code?
9. Suppose list1 is [2, 33, 222, 14, 25], What
is list1[-1]? 1. >>>names = ['Amir', 'Bear', 'Charlt
on', 'Daman']
a) Error
b) None 2. >>>print(names[-1][-1])
c) 25
d) 2 a) A
b) Daman
Answer: c c) Error
Explanation: -1 corresponds to the last index d) n
in the list.
Answer: d
10. Suppose list1 is [2, 33, 222, 14, 25], Explanation: Execute in the shell to verify.
What is list1[:-1]?
a) [2, 33, 222, 14]
3. names3 = names1[:]
CSE-R17.BLOGSPOT.COM
4.
9. for ls in (names1, names2, names3):
5. names2[0] = 'Alice'
10. if ls[0] == 'Alice':
6. names3[1] = 'Bob'
11. sum += 1
7.
12. if ls[1] == 'Bob':
8. sum = 0
13. sum += 10
14. 1. >>>list1 = [11, 2, 23]
15. print sum 2. >>>list2 = [11, 2, 2]
a) 11 3. >>>list1 < list2 is
b) 12
c) 21 a) True
d) 22 b) False
c) Error
Answer: b d) None
Explanation: When assigning names1 to
names2, we create a second reference to the Answer: b
same list. Changes to names2 affect names1. Explanation: Elements are compared one by
When assigning the slice of all elements in one.
names1 to names3, we are creating a full
copy of names1 which can be modified 6. To add a new element to a list we use
independently. which command?
a) list1.add(5)
3. Suppose list1 is [1, 3, 2], What is list1 * 2? b) list1.append(5)
a) [2, 6, 4] c) list1.addLast(5)
b) [1, 3, 2, 1, 3] d) list1.addEnd(5)
b) [0, 1, 2, 3, 4]
c) [0.0, 0.5, 1.0, 1.5] Answer: b
d) [0.0, 0.5, 1.0, 1.5, 2.0] Explanation: We use the function append to
add an element to the list.
Answer: c
Explanation: Execute in the shell to verify. 7. To insert 5 to the third position in list1, we
use which command?
5. What will be the output of the following a) list1.insert(3, 5)
Python code? b) list1.insert(2, 5)