You are on page 1of 93

First Semester MCQ for Regulations 2017 2.

In computer science, algorithm refers to a

special method usable by a computer for the


GE8151 PROBLEM solution to a problem.
a) True
SOLVING AND b) False

PYTHON Answer: a
Explanation: The statement is true. This
PROGRAMMING

CSE-R17.BLOGSPOT.COM word algorithm refers to a special method


usable by a computer for the solution to a
Khowarizmi.
SEMESTER - I problem. The statement of the problem
specifies in general terms the desired
input/output relationship.
MCQ - Regulations 3. This characteristic often draws the line

2017 between what is feasible and what is


impossible.
a) Performance
b) System Evaluation
Department of CSE c) Modularity
d) Reliability

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.

4. The time that depends on the input: an


TOPIC 1.1 ALGORITHMS
already sorted sequence that is easier to sort.
a) Process
1. The word ____________comes from the b) Evaluation
name of a Persian mathematician Abu Ja’far c) Running
Mohammed ibn-i Musa al Khowarizmi. a) d) Input
Flowchart
b) Flow Answer: c
c) Algorithm Explanation: The running time depends on
d) Syntax the input: an already sorted sequence is easier
to sort. The running time is given by the size
Answer: c of the input, since short sequences are easier
Explanation: The word algorithm comes to sort than the longer ones. Generally, we
from the name of a Persian mathematician seek upper bounds on the running time,
Abu Ja’far Mohammed ibn-i Musa al because it is reliable.
a) as pseudo codes
5. Which of the following is incorrect? b) as syntax
Algorithms can be represented:

Downloaded From: https://cse-r17.blogspot.com 1


First Semester MCQ for Regulations 2017
-As pseudo codes.
b) Lists
c) Horizontal array
c) as programs
d) Vertical array
d) as flowcharts
Answer: a
Answer: b
Explanation: Linear arrays are the 1-
Explanation: Representation of algorithms:
Dimensional arrays wherein only one row is
-As programs
present and the items are inserted.
-As flowcharts

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

Downloaded From: https://cse-r17.blogspot.com 2


First Semester MCQ for Regulations 2017
Explanation: It is called as flowcharting. A
Answer: b
Explanation: A diamond shape box denotes
3. The process of drawing a flowchart for an
either a truth value or a false value. It jumps
algorithm is called __________
onto two different statements following it via
a) Performance
flow lines.
b) Evaluation
c) Algorithmic Representation 7. There should be certain set standards on
d) Flowcharting the amount of details that should be provided
in a flowchart.
Answer: d

CSE-R17.BLOGSPOT.COM
flowchart is nothing but a pictorial c) Initialization
representation of an algorithm. d) I/O

4. Actual instructions in flowcharting are Answer: a


represented in __________ Explanation: A diamond shape box denotes
a) Circles the decision making statements. It jumps to a
b) Boxes truth value or a false value.
c) Arrows
d) Lines 6. A box that can represent two
different conditions.
Answer: b a) Rectangle
Explanation: The actual instructions are b) Diamond
written in boxes. Boxes are connected by c) Circle
using arrows to indicate the exact flow of a d) Parallelogram
flowchart and the order in which they are to a) True
be executed. b) False

5. The following box denotes? Answer: b


Explanation: The statement is false. There
should be no set standards on the amount of
details that should be provided in a flowchart.
a) Decision
b) Initiation 8. A detailed flowchart is called ______
a) Stack c) Systematic testing
b) Macro d) Improper documentation
c) Micro
d) Union Answer: d
Explanation: Flowcharts provide a proper
Answer: c documentation. It also provides systematic
Explanation: A detailed flowchart or a debugging.
flowchart with more details is called as micro
flowchart. It represents all the components of 10. A flowchart that outlines the
the algorithm that is followed. main segments of a program.
a) Queue
9. Which of the following is not an advantage b) Macro
of a flowchart? c) Micro
a) Better communication d) Union
b) Efficient coding

Downloaded From: https://cse-r17.blogspot.com 3


First Semester MCQ for Regulations 2017
a) Process
b) Sequence
c) Repetition
Answer: b
d) Case
Explanation: The answer is Macro
Flowchart. A macro flowchart outlines the Answer: a
important components of a program. It Explanation: There are basically four
therefore shows fewer details. flowcharting structures:
• Decision
TOPIC 1.3 DESIGNING
FLOWCHARTS

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.

Answer: a 6. The following symbol denotes:


Explanation: The input/output operations are
represented by parallelograms. They
generally are used to display messages during a) Module
input and output part of a program. b) Terminal
c) Process
4. Which of the following is not a flowchart d) i/o operation
structure?
• Case Answer: a
• Sequence. Explanation: This symbol is that of a module.
The terminal is denoted by a rounded
5. The action performed by a ___________ rectangle. I/O operation by a parallelogram
structure must eventually cause the loop to and process by a rectangle.
terminate.
a) sequence

Downloaded From: https://cse-r17.blogspot.com 4


First Semester MCQ for Regulations 2017
program.

10. The following box denotes?


7. What type of structure is this?

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.

8. What type of a structure is this?


TOPIC 1.4 PSEUDO CODE

1. Keep the statement language


______________ while writing a pseudo
code.
a) Dependent
b) Independent
c) Case sensitive
a) sequence b) case
d) Capitalized
c) repetition
d) process Answer: b
Explanation: The statement’s language
Answer: b
should be independent. Other rules are to
Explanation: This is a case structure. Certain
write only one statement per line and end
cases are given along with a default case in
multiline structures.
the case structure.
2. Capitalize initial keyword – This is a rule
9. A _______ is a connector showing the
while writing a pseudo code.
relationship between the representative
a) True
shapes.
b) False
a) line
b) arrow Answer: a
c) Process Explanation: The statement is true. It is an
d) box important rule to capitalize the initial
keyword while writing a pseudo code.
Answer: b
Explanation: Arrows are the connectors that 3. Which of the following is not a keyword?
show the relationship between different a) Read
a) Decision

Downloaded From: https://cse-r17.blogspot.com 5


First Semester MCQ for Regulations 2017
c) start
d) endif
b) Write Answer: c
Explanation: Start is not a Keyword. Other to a location addresses such as 19087.
words like read, write, if, else, etc are
keywords and convey a special meaning. 7. ____________ begins with lower case
identified by their addresses, we give them letters.
names (field names/variable names) using a) Keywords
words descriptive to us such as ctr as opposed b) Variables

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.

5. Why are local variable names beginning


UNIT II DATA,
EXPRESSIONS,

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.

Answer: d 6. Which of the following is not a keyword?


Explanation: Identifiers can be of any length. a) eval
b) assert
3. Which of the following is invalid? c) nonlocal
a) _a = 1 d) pass
b) __a = 1
c) __str__ = 1 Answer: a
d) none of the mentioned Explanation: eval can be used as a variable.
capitalized while the others are in lower case.
7. All keywords in Python are in _________
a) lower case 8. Which of the following is true for variable
b) UPPER CASE names in Python?
c) Capitalized a) unlimited length
d) None of the mentioned b) all private members must have leading and
trailing underscores
Answer: d c) underscore and ampersand are the only two
Explanation: True, False and None are

Downloaded From: https://cse-r17.blogspot.com 7


First Semester MCQ for Regulations 2017
statement?
Answer: b
Explanation: When both of the operands are
special characters allowed
integer then python chops out the fraction
d) none of the mentioned
part and gives you the round off value, to get
Answer: a the accurate answer use floor division. This is
Explanation: Variable names can be of any floor division. For ex, 5/2 = 2.5 but both of
length. the operands are integer so answer of this
expression in python is 2. To get the 2.5
9. Which of the following is an invalid answer, use floor division.

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

Answer: b 1. Which is the correct operator


y
Explanation: Spaces are not allowed in for power(x )?
variable names. a) X^y
b) X**y
10. Which of the following cannot be a c) X^^y
variable? d) None of the mentioned
a) __init__
b) in Answer: b
c) it Explanation: In python, power operator is
d) on x**y i.e. 2**3=8.

Answer: b 2. Which one of these is floor division?


Explanation: in is a keyword. a) /
b) // 3 is?
c) % a) 7
d) None of the mentioned b) 1
3. What is the order of precedence in python? c) 0
i) Parentheses d) 5
ii) Exponential
iii) Multiplication Answer: b
iv) Division Explanation: Modulus operator gives the
v) Addition remainder. So, 22%3 gives the remainder, that
vi) Subtraction is, 1.
a) i,ii,iii,iv,v,vi
5. Mathematical operations can be performed
b) ii,i,iii,iv,v,vi
c) ii,i,iv,iii,v,vi on a string.
d) i,ii,iii,iv,vi,v a) True
b) False
Answer: a
Explanation: For order of precedence, just Answer: b
remember this PEMDAS (similar to Explanation: You can’t perform
BODMAS). mathematical operation on string even if the
string is in the form: ‘1234…’.
4. What is the answer to this expression, 22 %

Downloaded From: https://cse-r17.blogspot.com 8


First Semester MCQ for Regulations 2017
Explanation: None.
10. Which one of the following has the
highest precedence in the expression?
6. Operators with the same precedence are
a) Exponential
evaluated in which manner?
b) Addition
a) Left to Right
c) Multiplication
b) Right to Left
d) Parentheses
c) Can’t say
d) None of the mentioned Answer: d
Explanation: Just remember: PEMDAS, that
Answer: a

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

Downloaded From: https://cse-r17.blogspot.com 9


First Semester MCQ for Regulations 2017
“he”.
3.0 and int(..) is a type cast operator.
a) he 7. What error occurs when you execute the
b) lo following Python code snippet?
c) olleh
d) hello apple = mango

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

Downloaded From: https://cse-r17.blogspot.com 10


First Semester MCQ for Regulations 2017
c) class
d) dictionary
Answer: c
values within it.
Explanation: The \n adds a new line.
10. In order to store values in terms of key
and value we use what core data type. a)
list 13. What is the average value of
b) tuple the following Python code snippet?
1. >>>grade1 = 80

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

Downloaded From: https://cse-r17.blogspot.com 11


First Semester MCQ for Regulations 2017
Explanation: Neither of 0.1, 0.2 and 0.3 can
c) +11
d) -5
1. What is the output of print 0.1 + 0.2 ==
0.3? Answer: a
a) True Explanation: ~x is equivalent to -(x+1).
b) False
c) Machine dependent 6. Which of the following is incorrect?
d) Error a) x = 0b101
b) x = 0x4f5
Answer: b

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

5. What does ~~~~~~5 evaluate to? Answer: d


a) +5 Explanation: The behavior of the round()
b) -11 function is different in Python 2 and Python
3. In Python 2, it rounds off numbers away from 0 when the number to be rounded off is

Downloaded From: https://cse-r17.blogspot.com 12


First Semester MCQ for Regulations 2017
towards 0 and hence “round(0.5) –
(round(-0.5)) = 0 – 0 = 0“. Also note that the
round(2.5) is 2 (which is an even number)
exactly halfway through. round(0.5) is 1 and
whereas round(3.5) is 4 (which is an even
round(-0.5) is -1 whereas in Python 3, it
number).
rounds off numbers towards nearest even
number when the number to be rounded off is 10. What does 3 ^ 4 evaluate to?
exactly halfway through. See the below a) 81
output. b) 12
$ python

CSE-R17.BLOGSPOT.COM
Python 2.7.17 (default, Nov 7 2019, 10:0
7:09) 4

>>> round(0.5) >>>

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.

>>> round(3.5) 2. What will be the value of the following


Python expression?
4 + 3 % 5 Answer: b
Explanation: The order of precedence is: %,
a) 4 +. Hence the expression above, on
b) 7 simplification results in 4 + 3 = 7. Hence the
c) 2 result is 7.
d) 0

Downloaded From: https://cse-r17.blogspot.com 13


First Semester MCQ for Regulations 2017
d) 1
a) (6.0, 16.0)
b) (6.00, 16.00)
3. Evaluate the expression given below if A =
c) (6, 16)
16 and B = 15.
d) (6.00, 16.0)
A % B // A
Answer: a
a) 0.0 Explanation: The result of the expression
b) 0 shown above is (6.0, 16.0). This is because
c) 1.0 the result is automatically rounded off to one

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 2+4.00, 2**4.0


Explanation: All of the operators shown decimal place.
above have associativity from left to right,
7. Which of the following is the truncation
except exponentiation operator (**) which
division operator?
has its associativity from right to left.
a) /
5. What will be the value of x in the b) %
following Python expression? c) //
d) |
x = int(43.55+2/2)
Answer: c
a) 43 Explanation: // is the operator for truncation
b) 44 division. It is called so because it returns only
the integer part of the quotient, truncating the Explanation: Expression 1 is evaluated as:
decimal part. For example: 20//3 = 6. 2**9, which is equal to 512. Expression 2 is
evaluated as 8**2, which is equal to 64. The
8. What are the values of the following last expression is evaluated as 2**(3**2).
Python expressions? This is because the associativity of **
operator is from right to left. Hence the
2**(3**2)
result of the third expression is 512.
(2**3)**2
2**3**2
9. What is the value of the following
a) 64, 512, 64 expression?
b) 64, 64, 64
8/4/2, 8/(4/2)
c) 512, 512, 512
d) 512, 64, 512

Answer: d

Downloaded From: https://cse-r17.blogspot.com 14


First Semester MCQ for Regulations 2017
4.0).
Answer: d
Explanation: The expression shown above is
a) (1.0, 4.0)
evaluated as: 2+9*(36-8)/10, which simplifies
b) (1.0, 1.0)
to give 2+9*(2.8), which is equal to 2+25.2 =
c) (4.0. 1.0)
27.2. Hence the result of this expression is
d) (4.0, 4.0)
27.2.
Answer: a
3. Which of the following expressions
Explanation: The above expressions are
evaluated as: 2/2, 8/2, which is equal to (1.0, involves coercion when evaluated in Python?

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). +, -, **, %, /, <<, >>, |

4. What will be the output of the following a) <<, >>


Python expression? b) **
c) |
24//6%3, 24//4//2 d) %

Downloaded From: https://cse-r17.blogspot.com 15


First Semester MCQ for Regulations 2017
9. What will be the value of the following
Python expression?
Answer: b 4+2**5//10
Explanation: The highest precedence is that
of the exponentiation operator, that is of **. a) 3
b) 7
6. What will be the value of the following c) 77
Python expression? d) 0
float(4+int(2.39)%2)

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.

10. The expression 2**2**3 is evaluates

Downloaded From: https://cse-r17.blogspot.com 16


First Semester MCQ for Regulations 2017
d) ‘0b11011’
5. To find the decimal value of 1111, that is
15, we can use the function:
2. What will be the output of the following
a) int(1111,10)
Python expression?
b) int(‘1111’,10)
bin(29) c) int(1111,2)
d) int(‘1111’,2)
a) ‘0b10111’
b) ‘0b11101’ Answer: d
c) ‘0b11111’ Explanation: The expression int(‘1111’,2)
CSE-R17.BLOGSPOT.COM
Answer: b
Explanation: The binary form of the number 6. What will be the output of the following
29 is 11101. Hence the output of this Python expression if x=15 and y=12?
expression is ‘0b11101’.
x & y
3. What will be the value of x in the
a) b1101
following Python expression, if the result of
b) 0b1101
that expression is 2?
c) 12
x>>2 d) 1101

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.

Downloaded From: https://cse-r17.blogspot.com 17


First Semester MCQ for Regulations 2017
bitwise OR and ! represents bitwise NOT.

9. What is the value of the following Python


Answer: b
expression?
Explanation: The ^ operator represent
bitwise XOR operation. &: bitwise AND, | :
a) 001101010
bin(0x8) b) 110010101
original value. For example, the two’s c) 001101011
complement of 10000000 is also equal to d) 110010100
10000000. Hence the statement is false.

2. The one’s complement of 110010101 is:

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

1. It is not possible for the two’s Answer: c


complement value to be equal to the original Explanation: ^ is the XOR operator. The
value in any case. binary form of 4 is 0100 and that of 12 is
a) True 1100. Therefore, 0100^1100 is 1000, which is
b) False equal to 8.

Answer: b 5. Any odd number on being AND-ed with


Explanation: In most cases the value of two’s ________ always gives 1. Hint: Any even
complement is different from the original number on being AND-ed with this value
value. However, there are cases in which the always gives 0.
two’s complement value may be equal to the
Answer: a
Downloaded From: https://cse-r17.blogspot.com 18
First Semester MCQ for Regulations 2017
even number on being
AND-ed with this value
a=a^b
a) 10 print(a,b)
b) 2
c) 1 a) 10 20
d) 0 b) 10 10
c) 20 10
Answer: c d) 20 20
Explanation: Any odd
number on being AND-ed Answer: c
with 1 always gives 1. Any

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).

Answer: a 10. What will be the output of the following


Explanation: Let us consider an example Python expression?
wherein a=2. The binary form of 2 is 0010. ~100?
When we left shift this value by 2, we get
1000, the value of which is 8. Hence if we a) 101
want to multiply a given number ‘a’ by 4, we b) -101
can use the expression: a<<2. c) 100
d) -100
Sanfoundry Global Education & Learning
Answer: b Series – Python.
Explanation: Suppose we have an expression
~A. This is evaluated as: -A – 1. Therefore,
TOPIC 2.7 BOOLEAN
the expression ~100 is evaluated as -100 – 1,
which is equal to -101.

Downloaded From: https://cse-r17.blogspot.com 19


First Semester MCQ for Regulations 2017
Answer: c
Explanation: The line of code shown above
can be simplified to state that ‘hello’ should
1. What will be the output of the following
be printed if the argument passed to the
Python code snippet?
Boolean function amounts to zero, else
bool(‘False’) ‘morning’ will be printed.
bool()
3. What will be the output of the following
a) Python code snippet?

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

True 4. What will be the output of the following


Python code?
d)
['f', 't'][bool('spam')]
False

Downloaded From: https://cse-r17.blogspot.com 20


First Semester MCQ for Regulations 2017
code can be translated to
state that ‘f’ is printed if
the argument passed to the
a) t
Boolean function
b) f
{} [] d)
c) No output
d) Error
{} {}
Answer: a
Explanation: The line of

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

Downloaded From: https://cse-r17.blogspot.com 21


First Semester MCQ for Regulations 2017

c) good
d) bad

Answer: c UNIT III CONTROL


Explanation: The code shown above prints FLOW, FUNCTIONS
the appropriate option depending on the
conditions given. The condition which
matches is (9>0), and hence the output is: TOPIC 3.1 FOR AND WHILE
good. LOOPS

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?

Downloaded From: https://cse-r17.blogspot.com 22


First Semester MCQ for Regulations 2017
Answer: b
Explanation: 0O11 is an octal number.
i = 1
while True:
6. What will be the output of the following
if i%3 == 0: break print(i) 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)

Downloaded From: https://cse-r17.blogspot.com 23


First Semester MCQ for Regulations 2017
Explanation: The numbers 2
and 4 are printed. The next
value of i is 6 which is divisible
a) 2 4 6 8 10 …
by 3 and hence control exits the
b) 2 4
i = 0
c) 2 3 while i < 5: print(i) i +=
d) error 1
if i == 3: break else:
Answer: b
print(0) a) 0 1 2 0

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

Downloaded From: https://cse-r17.blogspot.com 24


First Semester MCQ for Regulations 2017
evaluate to True. But also here we use a
citation marks on “i”, so, here i treated as a
string, not like a variable.
4. What will be the output of the following
Python code? 7. What will be the output of the following
x = "abcdef" Python code?
i = "i"
while i in x: x = "abcdef"
print(i, end=" ") i = "a"

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 = " ")

5. What will be the output of the following a) i i i i i i


Python code? b) a a a a a a
c) a a a a a
x = "abcdef" d) none of the mentioned
i = "a"
while i in x:
Answer: b
print(i, end = " ")
Explanation: The string x is being shortened
a) no output by one character in each iteration.
b) i i i i i i …
c) a a a a a a … 8. What will be the output of the following
d) a b c d e f Python code?

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

Downloaded From: https://cse-r17.blogspot.com 25


First Semester MCQ for Regulations 2017
Python code?
c) a B C D
d) error
c) no output
d) error Answer: b
Explanation: The instance of the string
Answer: b
returned by upper() is being printed.
Explanation: The string x is being shortened
by one character in each iteration. 3. What will be the output of the following
Python code?
10. What will be the output of the following

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?

Downloaded From: https://cse-r17.blogspot.com 26


First Semester MCQ for Regulations 2017
for i in range(len(x)):
Answer: c
Explanation: Objects of type int aren’t
Answer: c subscriptable. However, if the statement was
Explanation: Objects of type int have no x[i], an error would not have been thrown.
attribute upper().
9. What will be the output of the following
6. What will be the output of the following Python code snippet?
Python code snippet?
x = 'abcd'
x = 'abcd'

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

Downloaded From: https://cse-r17.blogspot.com 27


First Semester MCQ for Regulations 2017
for i in d:
5. What will be the output of the following
Python code?
Answer: c
Explanation: Objects of type int are not d = {0: 'a', 1: 'b', 2: 'c'}
iterable. for x in d.keys():
print(d[x])
2. What will be the output of the following
Python code? a) 0 1 2
b) a b c
d = {0: 'a', 1: 'b', 2: 'c'}

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

Downloaded From: https://cse-r17.blogspot.com 28


First Semester MCQ for Regulations 2017
a) 0
b) no output
print(x)
c) error
d) none of the mentioned
a) 0 1 2
b) None None None Answer: b
c) error Explanation: range(0) is empty. 1. What
d) none of the mentioned
will be the output of the following
Answer: c
Explanation: Objects of type set have no

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

Downloaded From: https://cse-r17.blogspot.com 29


First Semester MCQ for Regulations 2017
range(int(float('inf'))): print
(i)
for i in 'abcd'[::-1]: print (i)
Answer: d
Explanation: Error, objects of type a) a b c d
float cannot be interpreted as an b) d c b a
integer. c) error
d) none of the mentioned Answer: b
4. What will be the output of the
following Python code?

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)

6. What will be the output of the following a) 0 1 2 3 4 …


Python code snippet? b) 0 1
c) 3 4
for i in ''.join(reversed(list('abcd'))):
print (i)
d) 0 1 2 3

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.

Downloaded From: https://cse-r17.blogspot.com 30


First Semester MCQ for Regulations 2017
the following

x = (i for i in range(3)) for i in


a) 0 1 2 3 4 … x:
b) 0 -2 print(i)
c) 0
d) error a) 0 1 2
b) error
Answer: b c) 0 1 2 0 1 2
Explanation: The loop is entered d) none of the mentioned

twice. 1. What will be the output of

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.

Downloaded From: https://cse-r17.blogspot.com 31


First Semester MCQ for Regulations 2017
d) error
a = [0, 1, 2, 3] i = -2
for i not in a: print(i) i
string = "my name is x" for i += 1
in string.split(): print (i,
end=", ")
a) -2 -1
a) m, y, , n, a, m, e, , i, s, , x, b) b) 0
m, y, , n, a, m, e, , i, s, , x c) my, c) error
name, is, x,

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

Downloaded From: https://cse-r17.blogspot.com 32


First Semester MCQ for Regulations 2017
Explanation: Slice operation is performed on
Answer: c
Explanation: + is used to concatenate and *
1. >>>"abcd"[2:]
is used to multiply strings.
a) a
6. What will be the output of the following
b) ab
c) cd Python code?
d) dc
1. >>>print (r"\nhello")
Answer: c

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?

7. What will be the output of the following 1. >>> print('x\97\x98')


Python statement?
a) Error
1. >>>print('new' 'line') b)

a) Error 97
b) Output equivalent to print ‘new\nline’
c) newline 98
d) new line c) x\97
d) \x97\x98
Answer: c

Downloaded From: https://cse-r17.blogspot.com 33


First Semester MCQ for Regulations 2017
7. self.o2 = param
8.
Answer: c
Explanation: \x is an escape sequence that 9. >>>obj = child(22)
means the following 2 digits are a
10. >>>print "%d %d" % (obj.o1,
hexadecimal number encoding a character.
obj.o2) a) None None
9. What will be the output of the following
Python code?

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)

1. class father: a) 224


b) Error
2. def __init__(self, param): 3. c) 12
self.o1 = param d) None

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?

Answer: d 1. >>>example = "snow world"


Explanation: self.o1 was never created. 2. >>>print("%s" % example[4:7])
2. What will be the output of the following a) wo
Python code? b) world
1. class tester:

Downloaded From: https://cse-r17.blogspot.com 34


First Semester MCQ for Regulations 2017
following Python code?
1. >>>example = "helle" 2.
c) sn >>>example.find("e")
d) rl
a) Error
Answer: a b) -1
Explanation: Execute in the shell and c) 1
verify. d) 0

4. What will be the output of the

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.

a) error 9. What will be the output of the following


b) u Python code?
c) t
d) y 1. >>>example="helloworld"

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

Downloaded From: https://cse-r17.blogspot.com 35


First Semester MCQ for Regulations 2017

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.

Answer: a 6. What will be the output of the “hello”


Explanation: Execute in shell to verify. +1+2+3?
a) hello123
2. What will be the output of the following b) hello
Python statement? c) Error
d) hello6
1. >>>print(chr(ord('b')+1))
Answer: c
a) a Explanation: Cannot concatenate str and int
b) b objects.
c) c
d) A 7. What will be the output of the following
Python code?
Answer: c
Explanation: Execute in the shell to verify. 1. >>>print("D", end = ' ')
3. Which of the following statement prints 2. >>>print("C", end = ' ')
hello\example\test.txt?
a) print(“hello\example\test.txt”) 3. >>>print("B", end = ' ')
b) print(“hello\example\test.txt”) 4. >>>print("A", end = ' ')
c) print(“hello\”example\”test.txt”)
d) print(“hello”\example”\test.txt”) a) DCBA
b) A, B, C, D
Answer: b c) D C B A
Explanation: \is used to indicate that the next d) D, C, B, A will be displayed on four lines
\ is not an escape sequence.
Answer: c
4. Suppose s is “\t\tWorld\n”, what is Explanation: Execute in the shell.
s.strip()?
a) \t\tWorld\n 8. What will be the output of the following
b) \t\tWorld\n Python statement?(python 3.xx)
c) \t\tWORLD\n
d) World 1. >>>print(format("Welcome",
"10s"), end = '#')
d) str

Downloaded From: https://cse-r17.blogspot.com 36


First Semester MCQ for Regulations 2017
'#')

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):

5. c1 = Count(2) 3. self.firstName = firstName 4.


self.mi = mi
6. c2 = Count(2)
5. self.lastName = lastName 6.
7. print(id(c1) == id(c2), end = " ")
)
8.
a) Peter Pan
9. s1 = "Good" b) John Pan
10. s2 = "Good" c) Peter Smith
d) John Smith
11. print(id(s1) == id(s2))
Answer: b
a) True False Explanation: Execute in the shell to verify.
b) True True
c) False True 9. What function do you use to read a string?
d) False False a) input(“Enter a string”)
b) eval(input(“Enter a string”))
Answer: c c) enter(“Enter a string”)
Explanation: Execute in the shell objects d) eval(enter(“Enter a string”))
cannot have same id, however in the case of
strings its different. Answer: a
Explanation: Execute in shell to verify.
8. What will be the output of the following
Python code? 10. Suppose x is 345.3546, what is
format(x, “10.3f”) (_ indicates space).
1. class Name: a) __345.355
b) ___345.355
c) ____345.355
d) _____345.354 print("abc DEF".capitalize())

Answer: b a) abc def


Explanation: Execute in the shell to verify. b) ABC DEF
c) Abc def
1. What will be the output of the following d) Abc Def
Python code?

Downloaded From: https://cse-r17.blogspot.com 38


First Semester MCQ for Regulations 2017
5. What will be the output of the following
Python code?
Answer: c print('*', "abcdef".center(7), '*')
Explanation: The first letter of the string is
converted to uppercase and the others are a) * abcdef *
converted to lowercase. b) * abcdef *
c) *abcdef *
2. What will be the output of the following d) * abcdef*
Python code?

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

Downloaded From: https://cse-r17.blogspot.com 39


First Semester MCQ for Regulations 2017
Explanation: TypeError, the fill character
Answer: a
print("abcdef".center(7, 1))
Explanation: Counts the number of times the
substring ‘yy’ is present in the given string.
a) 1abcdef
b) abcdef1 2. What will be the output of the following
c) abcdef Python code?
d) error
print("xyyzxyzxzxyy".count('yy', 1))
Answer: d

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

Downloaded From: https://cse-r17.blogspot.com 40


First Semester MCQ for Regulations 2017
d) error
b) qwerty
c) utf-8
5. What will be the output of the following
d) utf-16
Python code?
Answer: c
print("xyyzxyzxzxyy".count('xyy', 2, 11))
Explanation: The default value of encoding
a) 2 is utf-8.
b) 0
c) 1 9. What will be the output of the following

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

Downloaded From: https://cse-r17.blogspot.com 41


First Semester MCQ for Regulations 2017
b) abcdef
print("abcdef".find("cd"))

spaces by default. a) True


b) 2
2. What will be the output of the following c) 3
Python code? d) None of the mentioned
print("ab\tcd\tef".expandtabs(4))
Answer: b
a) ab  cd  ef Explanation: The first position in the given

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"))

3. What will be the output of the following a) 4


Python code? b) 0
c) Error
print("ab\tcd\tef".expandtabs('+')) d) True

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.

Answer: d 7. What will be the output of the following


Explanation: TypeError, an integer should be Python code?
passed as an argument.
print("Hello {0} and {1}".format('foo',
4. What will be the output of the following 'bin'))
Python code?
a) Hello foo and bin
print("abcdef".find("cd") == "cd" in "abc b) Hello {0} and {1} foo bin
def") c) Error
d) Hello 0 and 1
a) True
b) False Answer: a
c) Error Explanation: The numbers 0 and 1 represent
d) None of the mentioned the position at which the strings are present.

Answer: b 8. What will be the output of the following


Explanation: The function find() returns the Python code?
position of the sunstring in the given string
whereas the in keyword returns a value of print("Hello {1} and {0}".format('bin',
'foo'))
Boolean type.
a) Hello foo and bin
5. What will be the output of the following
Python code? b) Hello bin and foo
string at which the substring can be found is

Downloaded From: https://cse-r17.blogspot.com 42


First Semester MCQ for Regulations 2017
the position at which the strings are present.

9. What will be the output of the following


c) Error
Python code?
d) None of the mentioned their names.
Answer: a 2. What will be the output of the following
Explanation: The numbers 0 and 1 represent Python code?
print("Hello {0!r} and {0!s}".format('fo a) Hello foo and foo
o', 'bin'))

CSE-R17.BLOGSPOT.COM b)

Hello ‘foo’ and foo


print("Hello {} and {}".format('foo', 'bi c) Hello foo and ‘bin’
n')) d) Error
a) Hello foo and bin Answer: b
b) Hello {} and {} Explanation: !r causes the characters ‘ or ” to
c) Error be printed as well.
d) Hello and
3. What will be the output of the following
Answer: a Python code?
Explanation: It is the same as Hello {0} and
{1}. print("Hello {0} and {1}".format(('foo',
'bin')))
10. What will be the output of the
following Python code? a) Hello foo and bin
b) Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)
print("Hello {name1} and {name2}".format( c) Error
'foo', 'bin')) d) None of the mentioned
a) Hello foo and bin Answer: c
b) Hello {name1} and {name2} Explanation: IndexError, the tuple index is
c) Error out of range.
d) Hello and
4. What will be the output of the following
Answer: c
Python code?
Explanation: The arguments passed to the
function format aren’t keyword print("Hello {0[0]} and {0[1]}".format((
arguments. 'foo', 'bin')))

1. What will be the output of the a) Hello foo and bin


following Python code? b) Hello (‘foo’, ‘bin’) and (‘foo’, ‘bin’)
c) Error
print("Hello {name1} and {name2}".format( d) None of the mentioned
name1='foo', name2='bin'))
Answer: a
a) Hello foo and bin
Explanation: The elements of the tuple are
b) Hello {name1} and {name2} accessed by their indices.
c) Error
d) Hello and 5. What will be the output of the following
Python code snippet?
Answer: a
Explanation: The arguments are accessed by
Downloaded From: https://cse-r17.blogspot.com 43
First Semester MCQ for Regulations 2017
Answer: d
Explanation: An integer is expected.
print('The sum of {0} and {1} is {2}'.for
mat(2, 10, 12))
9. What will be the output of the following
Python code snippet?
a) The sum of 2 and 10 is 12
print('{:$}'.format(1112223334))
b) Error
c) The sum of 0 and 1 is 2 a) 1,112,223,334
d) None of the mentioned

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'))

print('The sum of {0:b} and {1:x} is {2: a) 1,112,223,334


o}'.format(2, 10, 12)) b) 111,222,333,4
c) 1112223334
a) The sum of 2 and 10 is 12 d) Error
b) The sum of 10 and a is 14 b) 111,222,333,4
c) The sum of 10 and a is c c) 1112223334
d) Error d) Error
Answer: b Answer: d
Explanation: 2 is converted to binary, 10 to Explanation: $ is an invalid format code.
hexadecimal and 12 to octal.
10. What will be the output of the following
7. What will be the output of the following Python code snippet?
Python code snippet?
print('{:#}'.format(1112223334))
print('{:,}'.format(1112223334))
a) 1,112,223,334
a) 1,112,223,334 b) 111,222,333,4
b) 111,222,333,4 c) 1112223334
c) 1112223334 d) Error
d) Error
Answer: c
Answer: a Explanation: The number is printed as it is.
Explanation: A comma is added after every
third digit from the right. 1. What will be the output of the following
Python code?
Explanation: .2 specifies the precision.
print('{0:.2}'.format(1/3))
2. What will be the output of the following
a) 0.333333 Python code?
b) 0.33
c) 0.333333:.2 print('{0:.2%}'.format(1/3))
d) Error
a) 0.33
Answer: b b) 0.33%

Downloaded From: https://cse-r17.blogspot.com 44


First Semester MCQ for Regulations 2017
6. What will be the output of the following
Python code?
c) 33.33% print('a B'.isalpha())
d) 33%
a) True
Answer: c b) False
Explanation: The symbol % is used to c) None
represent the result of an expression as a d) Error
percentage.

3. What will be the output of the following

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

Downloaded From: https://cse-r17.blogspot.com 45


First Semester MCQ for Regulations 2017
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?

Downloaded From: https://cse-r17.blogspot.com 46


First Semester MCQ for Regulations 2017
Explanation: None.
1. What will be the output of the following
print(''''''.isspace())
Python code?

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

Downloaded From: https://cse-r17.blogspot.com 47


First Semester MCQ for Regulations 2017
so it’s behaviour does not depend on the
object from which it is being called.
c) z 8. What will be the output of the following
d) zxxy Python code?
Answer: b print('abcdef'.partition('cd'))
Explanation: The leading characters
containing xyy are removed. a) (‘ab’, ‘ef’)

5. What will be the output of the following


Python code?
CSE-R17.BLOGSPOT.COM b)

(‘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'))

7. What will be the output of the following a) (‘ab’, ‘cd’, ”)


Python code? b) (‘ab’, ‘cd’)
c) error
print('a'.maketrans('ABC', '123')) d) none of the mentioned
a) {97: 49, 98: 50, 99: 51} Answer: a
b) {65: 49, 66: 50, 67: 51} Explanation: The last item is a null string.
c) {97: 49}
d) 1 1. What will be the output of the following
Python code snippet?
Answer: b
Explanation: maketrans() is a static method print('cd'.partition('cd'))

Downloaded From: https://cse-r17.blogspot.com 48


First Semester MCQ for Regulations 2017
last item of the tuple returned are null strings.
Answer: a
Explanation: The first substring is not
a) (‘cd’)
present in the given string and hence nothing
b) (”)
is replaced.
c) (‘cd’, ”, ”)
d) (”, ‘cd’, ”) 5. What will be the output of the following
Answer: d Python code snippet?
Explanation: The entire string has been print('abcefd'.replace('cd', '12'))
passed as the separator hence the first and the

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?

a) [‘ab’, ‘ef’, ‘gh’] print('Ab!2'.swapcase())


b) [‘ab’, ‘ef’, ‘gh’, ”]
c) (‘ab’, ‘ef’, ‘gh’) a)
d) (‘ab’, ‘ef’, ‘gh’, ”) b) ab12
c) aB!2
Answer: b d)
Explanation: Calling the function with a
negative value for maxsplit is the same as Answer: c
calling it without any maxsplit specified. The Explanation: Lowercase letters are converted
string will be split into as many substring s as to uppercase and vice-versa.
b) Ab cd eF
4. What will be the output of the following c) Ab Cd Ef
Python code snippet? d) None of the mentioned
print('ab cd ef'.title()) Answer: c
Explanation: The first letter of every word is
a) Ab cd ef

Downloaded From: https://cse-r17.blogspot.com 50


First Semester MCQ for Regulations 2017
b) Ab Cd-ef
print('abcd'.translate({'a': '1', 'b':
'2', 'c': '3', 'd': '4'}))
capitalized.
a) abcd
5. What will be the output of the following b) 1234
Python code snippet? c) error
d) none of the mentioned
print('ab cd-ef'.title())
Answer: a
a) Ab cd-ef

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.

print('abcd'.translate('a'.maketrans('ab 8. What will be the output of the following


c', 'bcd'))) Python code snippet?
Explanation: The function translate expects a
a) bcde dictionary of integers. Use maketrans()
b) abcd instead of doing the above.
c) error
d) bcdd 9. What will be the output of the following
Python code snippet?
Answer: d
Explanation: The output is bcdd since no print('ab'.zfill(5))
translation is provided for d.
a) 000ab
7. What will be the output of the following b) 00ab0
Python code snippet? c) 0ab00
d) ab000
print('abcd'.translate({97: 98, 98: 99, 9
c) +0099
Answer: a d) +++99
Explanation: The string is padded with zeros
on the left hand side. It is useful for Answer: c
formatting numbers. Explanation: zeros are filled in between the
first sign and the rest of the string.
10. What will be the output of the following
Python code snippet?
TOPIC 3.3 FUNCTIONS
print('+99'.zfill(5))

a) 00+99 TOPIC 3.3.1 BUILT-IN


b) 00099 FUNCTIONS

Downloaded From: https://cse-r17.blogspot.com 51


First Semester MCQ for Regulations 2017
Explanation: The function seed is a function
all([2,4,0,6])

1. Which of the following functions is a built a) Error


in function in python? b) True
a) seed() c) False
b) sqrt() d) 0
c) factorial()
d) print() Answer: c
Explanation: The function all returns false if
Answer: 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])

5. What will be the output of the following a) Error


Python expression? b) True
c) False
round(4.5676,2)? d) 4>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

Downloaded From: https://cse-r17.blogspot.com 52


First Semester MCQ for Regulations 2017
min(max(False,-3,-4), 2,7)

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)

Downloaded From: https://cse-r17.blogspot.com 53


First Semester MCQ for Regulations 2017

a)

(2.00, 0.50)
2. What will be the output of
the following Python function?(2.00, 0.00) b)

complex(1+2j) (2, 0.5)

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)

3. What is the output of the function (2, 0.5)


complex()? (2)
a) 0j
b) 0+0j Answer: c
c) 0 Explanation: See python documentation for
d) Error the function divmod.

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)

Downloaded From: https://cse-r17.blogspot.com 54


First Semester MCQ for Regulations 2017
be: [(0, 2), (1,3)].

8. What will be the output of the following


Answer: c Error
Explanation: The built-in function
enumerate() accepts an iterable as an 2003
argument. The function shown in the above
Answer: b
case returns containing pairs of the numbers
Explanation: The output of the first function
given, starting from 0. Hence the output will
will be 0.001 and that of the second function floating point number up to 3 decimal places
will be 2000.0. The first function created a

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.

0.001 2. Suppose there is a list such that: l=[2,3,4].


If we want to print this list in reverse order,
200
which of the following methods should be
d) used?
and the second function adds 3 zeros after the a) reverse(l)

Downloaded From: https://cse-r17.blogspot.com 55


First Semester MCQ for Regulations 2017
function accepts only an
iterable as an argument. To
print the output in the form
b) list(reverse[(l)])
of a
c) reversed(l)
c)
d) list(reversed(l))
A
Answer: d
Explanation: The built-in Error d)
function reversed() can be
used to reverse the Error
elements of a list. This

CSE-R17.BLOGSPOT.COM
list, we use: list(reversed(l)). The output will
65
be: [4,3,2].
Error

3. What will be the output of the following Answer: b


Python function? Explanation: The built-in function ord() is
used to return the ASCII value of the
float(' -12345\n')
alphabet passed to it as an argument. Hence
(Note that the number of blank spaces before the first function results in an error and the
the number is 5) output of the second function is 65.
a)   -12345.0 (5 blank spaces before the
number)
b) -12345.0 5. What will be the output of the following
c) Error Python function?
d) -12345.000000000…. (infinite decimal
places) float(‘-infinity’)
float(‘inf’)
Answer: b
Explanation: The function float() will a)
remove all the blank spaces and convert the
–inf
integer to a floating point number. Hence the
output will be: -12345.0. inf

4. What will be the output of the following b)


Python function?
–infinity
ord(65)
ord(‘A’) inf

a) c)

Error
A
Error
65

b) d)

Error
Error
Explanation: The output of the first function
Junk value

Answer: a

Downloaded From: https://cse-r17.blogspot.com 56


First Semester MCQ for Regulations 2017
a) min()
throw errors. However the function ord(‘ ‘)
will be –inf and that of the second function does not throw an error because in this case,
will be inf. we are actually passing a blank space as an
argument. The output of ord(‘ ‘) is 32 (ASCII
value corresponding to blank space).

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])

7. What will be the output of the following a) 4


Python function? b) 3
c) Error
hex(15) d) 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

Downloaded From: https://cse-r17.blogspot.com 57


First Semester MCQ for Regulations 2017
the second function results in an
error while the output of the
first function is 0o7.
Answer: c
Explanation: The function oct()Hello World! Hello World! b)
is used to convert its argument
into octal form. This function 'Hello World!' 'Hello World!'
does not accept strings. Hence

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

Downloaded From: https://cse-r17.blogspot.com 58


First Semester MCQ for Regulations 2017
a) 3
c)
5. print(a, 'is equal to', b) 6.
x is 50
else:
Changed local x to 2 x is now 100
7. print(b, 'is maximum') 8.
d) None of the mentioned
printMax(3, 4)

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

5. What will be the output of the following Changed local x to 2


Python code? x is now 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

Downloaded From: https://cse-r17.blogspot.com 59


First Semester MCQ for Regulations 2017
d)

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

c) 8. What will be the output of the following


Hello Python code?

World,World,World,World,World 1. def func(a, b=5, c=10):


use default values in case the user does not
want to provide values for them. This is done 2. print('a is', a, 'and b is', b,
'and c is', c)
with the help of default argument values. You
can specify default argument values for 3.
parameters by appending to the parameter
name in the function definition the 4. func(3, 7)
assignment operator (=) followed by the
5. func(25, c = 24)
default value.
The function named say is used to print a 6. func(c = 50, a = 100)
string as many times as specified. If we don’t
supply a value, then by default, the string is a)
printed just once. We achieve this by
specifying a default argument value of 1 to a is 7 and b is 3 and c is 10
the parameter times.

Downloaded From: https://cse-r17.blogspot.com 60


First Semester MCQ for Regulations 2017
parameter c before that for a even though a is
defined before c in the function definition.
a is 25 and b is 5 and c is 24 a is 5 and
9. What will be the output of the following
b is 100 and c is 50 b) Python code?

a is 3 and b is 7 and c is 10 a is 5 and b


1. def maximum(x, y):

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:

Downloaded From: https://cse-r17.blogspot.com 61


First Semester MCQ for Regulations 2017
simpler pieces
5. Which of the following is the use of id()
function in python?
are an important tool that you should make
a) Id returns the identity of the object b)
use of since it helps to document the program
better and makes it easier to understand. Every object doesn’t have a unique id c)
All of the mentioned
1. Which are the advantages of functions in d) None of the mentioned
python?
Answer: a
a) Reducing duplication of code
Explanation: Each object in Python has a
b) Decomposing complex problems into

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

Downloaded From: https://cse-r17.blogspot.com 62


First Semester MCQ for Regulations 2017
following Python code?

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)

Downloaded From: https://cse-r17.blogspot.com 63


First Semester MCQ for Regulations 2017
2. What will be the output of the following
Python code?
9. print sum(1, 2, 3) 1. y = 6
10. print sum(1, 2, 3, 4, 5) a) 2. z = lambda x: x * y
6 3. print z(8)
15

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?

1. def f(x, y, z): return x + y + z 1. L = [lambda x: x ** 2,


2. f(2, 30, 400) 2. lambda x: x ** 3,

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

Downloaded From: https://cse-r17.blogspot.com 65


First Semester MCQ for Regulations 2017
else
i=0
def change(i):
d) None of the mentioned i=i+1
return i
Answer: c change(1)
print(i)
Explanation: None.

10. What will be the output of the a) 1


following Python code? b) Nothing is displayed c) 0

1. min = (lambda x, y: x if x < y

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

1. What is a variable defined outside Answer: c


a function referred to as? Explanation: The variable inside a function
a) A static variable is called as local variable and the variable
b) A global variable definition is confined only to that function.
c) A local variable
d) An automatic variable 3. What will be the output of the following
Python code?
Answer: b d) An exception is thrown
Explanation: The value of a variable defined
Answer: c
Explanation: Any change made in to an Explanation: Since a list is mutable, any
immutable data type in a function isn’t change made in the list in the function is
reflected outside the function. reflected outside the function.

4. What will be the output of the following 5. What will be the output of the following
Python code? Python code?

def a(b): a=10


b = b + [5] b=20
def change():
c = [1, 2, 3, 4] global b
a(c) a=45
print(len(c)) b=56
change()
a) 4 print(a)
b) 5 print(b)
c) 1
d) An exception is thrown a)

Answer: b

Downloaded From: https://cse-r17.blogspot.com 66


First Semester MCQ for Regulations 2017
d) An exception is thrown

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

Downloaded From: https://cse-r17.blogspot.com 67


First Semester MCQ for Regulations 2017
Answer: b
Explanation: Lists are passed by reference.
a) String 4. How are keyword arguments specified in
b) Tuple the function heading?
c) Dictionary a) one-star followed by a valid identifier
d) An exception is thrown b) one underscore followed by a valid
Answer: c identifier
Explanation: b combines the remaining c) two stars followed by a valid identifier
parameters into a dictionary.

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.

5. How many keyword arguments can be

Downloaded From: https://cse-r17.blogspot.com 68


First Semester MCQ for Regulations 2017
def foo(i, x=[]):
x.append(i)
return x
total = 0 for i in range(3):
print(foo()) print(foo(i))

a) 0 a) [0] [1] [2]


b) 1 b) [0] [0, 1] [0, 1, 2]
c) error c) [1] [2] [3]
d) none of the mentioned

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].

def foo(x): 2. How are variable length arguments


x = ['def', 'abc'] specified in the function heading?
return id(x)
q = ['abc', 'def']
a) one star followed by a valid identifier
print(id(q) == foo(q)) b) one underscore followed by a valid
identifier
a) True c) two stars followed by a valid identifier
b) False d) two underscores followed by a valid
c) None identifier
d) Error
Answer: a
Answer: b Explanation: Refer documentation.
Explanation: A new object is created in the
function. 3. Which module in the python standard
library parses options received from the
10. What will be the output of the following command line?
Python code?
d) [1] [1, 2] [1, 2, 3]

Downloaded From: https://cse-r17.blogspot.com 69


First Semester MCQ for Regulations 2017
from the command line.
within square brackets ([])
d) identifier
a) getopt
b) os Answer: d
c) getarg Explanation: Refer documentation.
d) main
8. What will be the output of the following
Answer: a Python code?
Explanation: getopt parses options received

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.

Answer: c 9. Where are the arguments received from the


Explanation: Refer documentation. command line stored?
a) sys.argv
6. How are default arguments specified in the b) os.argv
function heading? c) argv
a) identifier followed by an equal to sign and d) none of the mentioned
the default value
b) identifier followed by the default value Answer: a
within backticks (“) Explanation: Refer documentation.
c) identifier followed by the default value
within square brackets ([]) 10. What will be the output of the following
d) identifier Python code?

def foo(i, x=[]):


Answer: a
x.append(x.append(i))
Explanation: Refer documentation. return x
for i in range(3):
7. How are required arguments specified in y = foo(i)
the function heading? print(y)
a) identifier followed by an equal to sign and
the default value a) [[[0]], [[[0]], [1]], [[[0]], [[[0]], [1]], [2]]]
b) identifier followed by the default value b) [[0], [[0], 1], [[0], [[0], 1], 2]]
within backticks (“) c) [0, None, 1, None, 2, None]
c) identifier followed by the default value d) [[[0]], [[[0]], [1]], [[[0]], [[[0]], [1]], [2]]]
Downloaded From: https://cse-r17.blogspot.com 70
First Semester MCQ for Regulations 2017
following
def san(x): print(x+1) x=-2
x=4
Answer: c san(12)
Explanation: append() returns
None. a) 13
b) 10
c) 2
TOPIC 3.4 LOCAL AND
GLOBAL SCOPE

1. What will be the output of the

CSE-R17.BLOGSPOT.COM
Python code? d) 99

def f1(): Answer: b


x=15 Explanation: The variable x is a local
print(x)
x=12
variable. It is first printed and then modified.
f1() Hence the output of this code is 100.

a) Error 3. What will be the output of the following


b) 12 Python code?
c) 15 d) 5
d) 1512
Answer: a
Answer: c Explanation: The value passed to the
Explanation: In the code shown above, x=15 function san() is 12. This value is
is a local variable whereas x=12 is a global incremented by one and printed. Hence the
variable. Preference is given to local variable output of the code shown above is 13.
over global variable. Hence the output of the
code shown above is 15. 4. What will be the output of the following
Python code?
2. What will be the output of the following
def f1():
Python code?
global x
x+=1
def f1():
print(x)
x=100
x=12
print(x)
print("x")
x=+1
f1()
a) Error
a) Error b) 13
b) 100 c)
c) 101
variable ‘x’ been a local variable, the output
13 would have been:
x 13
x
d) x
5. What will be the output of the following
Answer: d Python code?
Explanation: In the code shown above, the
variable ‘x’ is declared as global within the def f1(x):
global x
function. Hence the output is ‘x’. Had the

Downloaded From: https://cse-r17.blogspot.com 71


First Semester MCQ for Regulations 2017
d)
a = "world" f()
print(a)
x+=1
print(x) f1(15) a)
print("hello")
hello hello
a) error
b) hello
c) 16

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?

6. What will be the output of the following def f():


global a
Python code? print(a)
a = "hello"
x=12 print(a)
def f1(a,b=x): world
print(a,b)
x=15
b)
f1(4)

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)

world 8. What will be the output of the following


hello
Python code?

world def f1(a,b=[]):


b.append(a)
Answer: b

Downloaded From: https://cse-r17.blogspot.com 72


First Semester MCQ for Regulations 2017
d) [3,4,2]

Answer: d
return b
print(f1(2,[3,4])) error b)

a) [3,2,4] inner error c)


b) [2,3,4]
c) Error

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?

Downloaded From: https://cse-r17.blogspot.com 73


First Semester MCQ for Regulations 2017
Answer: c
Explanation: Both the functions, that is,
x=100
globals() and locals() return value of the data
def f1(): structure dictionary.
global x x=90
def f2(): 2. What will be the output of the following
global x x=80 Python code?
print(x)
x=1
a) 100

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

Downloaded From: https://cse-r17.blogspot.com 74


First Semester MCQ for Regulations 2017
def f(): x=4
x=1
f()
c) x

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

Downloaded From: https://cse-r17.blogspot.com 75


First Semester MCQ for Regulations 2017

TOPIC 3.5 RECURSION


Sanfoundry Global Education & Learning
Series – Python. 1. Which is the most appropriate definition
for recursion?
a) A function that calls itself return 1
else:
4. Fill in the line of the following Python
code for calculating the factorial of a number. return _____________________ a)

def fact(num): num*fact(num-1)


if num == 0:

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.

Downloaded From: https://cse-r17.blogspot.com 77


First Semester MCQ for Regulations 2017
a) 0 1 2 3
b) An exception is thrown
c) 0 1 1 2 3
c) Program runs n number of times where n is
d) 0 1 1 2
the argument given to the function d) An
exception is thrown Answer: d
Explanation: The above piece of code prints
Answer: a
the Fibonacci series.
Explanation: The program will run until the
system gets out of memory.

13. Which of these is not true about

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

Downloaded From: https://cse-r17.blogspot.com 78


First Semester MCQ for Regulations 2017
what is max(list1)?
8. Suppose list1 is [4, 2, 2, 4, 5, 2, 1, 0],
Which of the following is correct syntax for
b) 4
slicing operation?
c) None
a) print(list1[0])
d) Error
b) print(list1[:2])
Answer: a c) print(list1[:-2])
Explanation: Execute in the shell and verify. d) all of the mentioned

4. Suppose list1 is [2445,133,12454,123], Answer: d

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]

Downloaded From: https://cse-r17.blogspot.com 79


First Semester MCQ for Regulations 2017
c) [1, 3, 2, 1, 3, 2]
d) [1, 3, 2, 3, 2, 1]
2. What will be the output of the following Answer: c
Python code? Explanation: Execute in the shell and verify.
1. names1 = ['Amir', 'Bear', 4. Suppose list1 = [0.5 * x for x in
'Charlto n', 'Daman']
range(0, 4)], list1 is:
2. names2 = names1 a) [0, 1, 2, 3]

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)

Downloaded From: https://cse-r17.blogspot.com 80

You might also like