0% found this document useful (0 votes)
93 views

Pythontest 2

The document contains 20 questions about Python programming concepts like functions, classes, exceptions, OOP concepts etc. For each question there are 4 multiple choice options and the correct option is explained. The questions assess knowledge of Python basics like data types, functions, OOPs concepts etc.

Uploaded by

Monisha arumugam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Pythontest 2

The document contains 20 questions about Python programming concepts like functions, classes, exceptions, OOP concepts etc. For each question there are 4 multiple choice options and the correct option is explained. The questions assess knowledge of Python basics like data types, functions, OOPs concepts etc.

Uploaded by

Monisha arumugam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Question: 1Time Taken: 

55 sec  

What will be the output of the following Python code?

i=0
def change(i):
   i=i+1
   return i
change(1)
print(i)

  (1)  
1

  (2)  
Nothing is displayed

 
  (3)  
0

  (4)  
An exception is thrown

Option 3:
Explanation: Any change made in to an immutable data type in a function isn’t
reflected outside the function.

Report this question / solution


Question: 2Time Taken: 63 sec  

Which of the following is the use of id() function in python?

  (1)  
Id returns the identity of the object
  (2)  
Every object doesn’t have a unique id

  (3)  
All of the mentioned

  (4)  
None of the mentioned

Option 1: Explanation: Each object in Python has a unique id. The id() function
returns the object’s id.
Report this question / solution
Question: 3Time Taken: 102 sec  

Suppose there is a list such that: l=[2,3,4]. If we want to print this list in reverse order,
which of the following methods should be used?

  (1)  
reverse(l)

  (2)  
list(reverse[(l)])

  (3)  
reversed(l)

  (4)  
list(reversed(l))

Option 4:
Explanation: The built-in function reversed() can be used to reverse the elements of
a list. This function accepts only an iterable as an argument. To print the output in
the form of a list, we use: list(reversed(l)). The output will be: [4,3,2].
Report this question / solution
Question: 4Time Taken: 69 sec  

What will be the output of the following Python function?

hex(15)

  (1)  
f

  (2)  
0xF

  (3)  
0Xf

  (4)  
0xf

Option 4:
Explanation: The function hex() is used to convert the given argument into its
hexadecimal representation, in lower case. Hence the output of the function hex(15)
is 0xf.

Report this question / solution


Question: 5Time Taken: 58 sec  

What will be the output of the following Python code?

def change(i = 1, j = 2):


  i=i+j
  j=j+1
    print(i, j)
change(j = 1, i = 2)

  (1)  
An exception is thrown because of conflicting values
  (2)  
12

  (3)  
33

 
  (4)  
32

Option 4:
Explanation: The values given during function call is taken into consideration, that is,
i=2 and j=1.

Report this question / solution


Question: 6Time Taken: 29 sec  

Which of the following is a feature of DocString?

  (1)  
Provide a convenient way of associating documentation with Python modules,
functions, classes, and methods

  (2)  
All functions should have a docstring

  (3)  
Docstrings can be accessed by the __doc__ attribute on objects

 
  (4)  
All of the mentioned
Option 4:
Explanation: Python has a nifty feature called documentation strings, usually referred
to by its shorter name docstrings.

DocStrings are an important tool that you should make use of since it helps to
document the program better and makes it easier to understand.

Report this question / solution


Question: 7Time Taken: 69 sec  

What will be the output of the following Python code?

min = (lambda x, y: x if x < y else y)


min(101*99, 102*98)

  (1)  
9997

  (2)  
9999

  (3)  
9996

  (4)  
None of the mentioned

Option 3:
Executable Output

Report this question / solution


Question: 8Time Taken: 22 sec  

What is called when a function is defined inside a class?


  (1)  
Module

  (2)  
Class

  (3)  
Another function

 
  (4)  
Method

Option 4: Theory Concept


Report this question / solution
Question: 9Time Taken: 80 sec  

What will be the output of the following Python code?

def maximum(x, y):


    if x > y:
        return x
    elif x == y:
        return 'The numbers are equal'
    else:
        return y
 
print(maximum(2, 3))

  (1)  
2

 
  (2)  
3

  (3)  
The numbers are equal
  (4)  
None of the mentioned

Option 2:
Explanation: The maximum function returns the maximum of the parameters, in this
case the numbers supplied to the function.

It uses a simple if..else statement to find the greater value and then returns that
value.

Report this question / solution


Question: 10Time Taken: 42 sec  

What will be the output of the following Python code?

y=6
z = lambda x: x * y
print z(8)

 
  (1)  
48

  (2)  
14

  (3)  
64

  (4)  
None of the mentioned

Option 1:
Explanation: The lambda keyword creates an anonymous function. The x is a
parameter, that is passed to the lambda function. The parameter is followed by a
colon character. The code next to the colon is the expression that is executed, when
the lambda function is called. The lambda function is assigned to the z variable.
The lambda function is executed. The number 8 is passed to the anonymous
function and it returns 48 as the result. Note that z is not a name for this function. It is
only a variable to which the anonymous function was assigned.

Report this question / solution

Section- Python OOPS

Question: 11Time Taken: 78 sec  

What will be the output of the following Python code?

class test:
    def __init__(self):
        self.variable = 'Old'
        self.Change(self.variable)
    def Change(self, var):
        var = 'New'
obj=test()
print(obj.variable)

  (1)  
Error because function change can’t be called in the __init__ function

  (2)  
‘New’ is printed

 
  (3)  
‘Old’ is printed

  (4)  
Nothing is printed

Option 3:
Explanation: This is because strings are immutable. Hence any change made isn’t
reflected in the original string.
Report this question / solution
Question: 12Time Taken: 175 sec  

What will be the output of the following Python code?

class A:
    def __init__(self, x= 1):
        self.x = x
class der(A):
    def __init__(self,y = 2):
        super().__init__()
        self.y = y
def main():
    obj = der()
    print(obj.x, obj.y)
main()

  (1)  
Error, the syntax of the invoking method is wrong

  (2)  
The program runs fine but nothing is printed

  (3)  
10

  (4)  
12

Option 4:
Explanation: In the above piece of code, the invoking method has been properly
implemented and hence x=1 and y=2.

Report this question / solution


Question: 13Time Taken: 51 sec  

What will be the output of the following Python code?


g = (i for i in range(5))
type(g)

  (1)  
class <’loop’>

  (2)  
class <‘iteration’>

 
  (3)  
class <’range’>

 
  (4)  
class <’generator’>

Option 4:
Explanation: Another way of creating a generator is to use parenthesis. Hence the
output of the code shown above is: class<’generator’>.

Report this question / solution


Question: 14Time Taken: 162 sec  

What will be the output of the following Python code?

class Demo:
    def check(self):
        return " Demo's check "  
    def display(self):
        print(self.check())
class Demo_Derived(Demo):
    def check(self):
        return " Derived's check "
Demo().display()
Demo_Derived().display()

 
  (1)  
Demo’s check Derived’s check

  (2)  
Demo’s check Demo’s check

  (3)  
Derived’s check Demo’s check

  (4)  
Syntax error

Option 1:
Explanation: Demo().display() invokes the display() method in class Demo and
Demo_Derived().display() invokes the display() method in class Demo_Derived.

Report this question / solution


Question: 15Time Taken: 69 sec  

Which function overloads the // operator?

  (1)  
__div__()

  (2)  
__ceildiv__()

 
  (3)  
__floordiv__()

  (4)  
__truediv__()

Option 3: Explanation: __floordiv__() is for //.


Report this question / solution
Question: 16Time Taken: 78 sec  

The purpose of name mangling is to avoid unintentional access of private class


members.

 
  (1)  
True

  (2)  
False

  (3)  
Cant say

  (4)  
None

Option 1: Explanation: Name mangling prevents unintentional access of private


members of a class, while still allowing access when needed. Unless the variable is
accessed with its mangled name, it will not be found.
Report this question / solution
Question: 17Time Taken: 106 sec  

What will be the output of the following Python code?

class A:
    def __init__(self):
        self._x = 5       
class B(A):
    def display(self):
        print(self._x)
def main():
    obj = B()
    obj.display()
main()

  (1)  
Error, invalid syntax for object declaration
  (2)  
Nothing is printed

  (3)  
5

  (4)  
Error, private class member can’t be accessed in a subclass

Option 3:
Explanation: The class member x is protected, not private and hence can be
accessed by subclasses.

Report this question / solution


Question: 18Time Taken: 78 sec  

What is hasattr(obj,name) used for?

  (1)  
To access the attribute of the object

  (2)  
To delete an attribute

 
  (3)  
To check if an attribute exists or not

  (4)  
To set an attribute

Option 3: Explanation: hasattr(obj,name) checks if an attribute exists or not and


returns True or False.
Report this question / solution
Question: 19Time Taken: 40 sec  
Which of the following statements is true?

  (1)  
The standard exceptions are automatically imported into Python programs

  (2)  
All raised standard exceptions must be handled in Python

  (3)  
When there is a deviation from the rules of a programming language, a semantic
error is thrown

  (4)  
If any exception is thrown in try block, else block is executed

Option 1: Explanation: When any exception is thrown in try block, except block is
executed. If exception in not thrown in try block, else block is executed. When there
is a deviation from the rules of a programming language, a syntax error is thrown.
The only true statement above is: The standard exceptions are automatically
imported into Python programs.
Report this question / solution
Question: 20Time Taken: 82 sec  

What happens when ‘1’ == 1 is executed?

  (1)  
we get a True

  (2)  
we get a False

  (3)  
an TypeError occurs

  (4)  
a ValueError occurs
Option 2: Explanation: It simply evaluates to False and does not raise any exception.
Report this question / solution

Section- Python Recursion

Question: 21Time Taken: 61 sec  

 What is tail recursion?

  (1)  
A recursive function that has two base cases

  (2)  
A function where the recursive functions leads to an infinite loop

  (3)  
A recursive function where the function doesn’t return anything and just prints the
values

 
  (4)  
A function where the recursive call is the last thing executed by the function

Option 4: Explanation: A recursive function is tail recursive when recursive call is


executed by the function in the last.
Report this question / solution
Question: 22Time Taken: 45 sec  

Recursion and iteration are the same programming approach.

  (1)  
True

 
  (2)  
False

  (3)  
cant say
  (4)  
None

Option 2: Explanation: In recursion, the function calls itself till the base condition is
reached whereas iteration means repetition of process for example in for-loops.
Report this question / solution
Question: 23Time Taken: 40 sec  

Fill in the line of the following Python code for calculating the factorial of a number.

def fact(num):
    if num == 0: 
        return 1
    else:
        return _____________________

 
  (1)  
num*fact(num-1)

  (2)  
(num-1)*(num-2)

  (3)  
num*(num-1)

  (4)  
fact(num)*fact(num-1)

Option 1:
Explanation: Suppose n=5 then, 5*4*3*2*1 is returned which is the factorial of 5.

Report this question / solution


Question: 24Time Taken: 69 sec  

Which of the following statements is false about recursion?


  (1)  
Every recursive function must have a base case

  (2)  
Infinite recursion can occur if the base case isn’t properly mentioned

  (3)  
A recursive function makes the code easier to understand

 
  (4)  
Every recursive function must have a return value

Option 4: Explanation: A recursive function needn’t have a return value.


Report this question / solution
Question: 25Time Taken: 9 sec  

What will be the output of the following Python code?

l=[]
def convert(b):
    if(b==0):
        return l
    dig=b%2
    l.append(dig)
    convert(b//2)
convert(6)
l.reverse()
for i in l:
    print(i,end="")

  (1)  
11

 
  (2)  
110
  (3)  
3

  (4)  
Infinite loop

Option 2:
Explanation: The above code gives the binary equivalent of the number.

Report this question / solution


Question: 26Time Taken: 6 sec  

Observe the following Python code?

def a(n):
    if n == 0:
        return 0
    else:
        return n*a(n - 1)
def b(n, tot):
    if n == 0:
        return tot
    else:
        return b(n-2, tot-2)

  (1)  
Both a() and b() aren’t tail recursive

 
  (2)  
Both a() and b() are tail recursive

 
  (3)  
b() is tail recursive but a() isn’t

  (4)  
a() is tail recursive but b() isn’t

Option 3:
Explanation: A recursive function is tail recursive when recursive call is executed by
the function in the last.

Report this question / solution


Question: 27Time Taken: 6 sec  

What will be the output of the following Python code?

def fun(n):
    if (n > 100):
        return n - 5
    return fun(fun(n+11));
 
print(fun(45))

  (1)  
50

 
  (2)  
100

  (3)  
74

  (4)  
Infinite loop

Option 2:
Explanation: The fun(fun(n+11)) part of the code keeps executing until the value of n
becomes greater than 100, after which n-5 is returned and printed.

Report this question / solution


Question: 28Time Taken: 4 sec  

What will be the output of the following Python code?

def a(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return a(n-1)+a(n-2)
for i in range(0,4):
    print(a(i),end=" ")

 
  (1)  
0123

  (2)  
An exception is thrown

  (3)  
01123

 
  (4)  
0112

Option 4:
Explanation: The above piece of code prints the Fibonacci series.

Report this question / solution


Question: 29Time Taken: 53 sec  

Which of these is not true about recursion?

  (1)  
It’s easier to code some real-world problems using recursion than non-recursive
equivalent

  (2)  
Recursive functions are easy to debug

 
  (3)  
Recursive calls take up a lot of memory

  (4)  
Programs using recursion take longer time than their non-recursive equivalent

Option 2: Explanation: Recursive functions may be hard to debug as the logic behind
recursion may be hard to follow.
Report this question / solution
Question: 30Time Taken: 52 sec  

What happens if the base condition isn’t defined in recursive programs?

 
  (1)  
Program gets into an infinite loop

  (2)  
Program runs once

  (3)  
Program runs n number of times where n is the argument given to the function

  (4)  
An exception is thrown

Option 1: Explanation: The program will run until the system gets out of memory.
Report this question / solution

Section- Python Regular Expressions & Files


Question: 31Time Taken: 60 sec  

What happens if no arguments are passed to the seek function?

  (1)  
file position is set to the start of file

  (2)  
file position is set to the end of file

  (3)  
file position remains unchanged

  (4)  
error

Option 4: Explanation: seek() takes at least one argument.


Report this question / solution
Question: 32Time Taken: 24 sec  

Is it possible to create a text file in python?

 
  (1)  
Yes

  (2)  
No

  (3)  
Machine dependent

 
  (4)  
All of the mentioned
Option 1: Explanation: Yes we can create a file in python. Creation of file is as shown
below. file = open(“newfile.txt”, “w”) file.write(“hello world in the new file\n”)
file.write(“and another line\n”) file.close().
Report this question / solution
Question: 33Time Taken: 33 sec  

What is the pickling?

  (1)  
It is used for object serialization

  (2)  
It is used for object deserialization

 
  (3)  
None of the mentioned

  (4)  
All of the mentioned

Option 1: Explanation: Pickle is the standard mechanism for object serialization.


Pickle uses a simple stack-based virtual machine that records the instructions used
to reconstruct the object. This makes pickle vulnerable to security risks by malformed
or maliciously constructed data, that may cause the deserializer to import arbitrary
modules and instantiate any object.
Report this question / solution
Question: 34Time Taken: 26 sec  

Which of the following functions creates a Python object?

 
  (1)  
re.compile(str)

  (2)  
re.assemble(str)

  (3)  
re.regex(str)

  (4)  
re.create(str)

Option 1: Explanation: The function re.compile(srt) compiles a pattern of regular


expression into an object of regular expression. Hence re.compile(str) is the only
function from the above options which creates an object.
Report this question / solution
Question: 35Time Taken: 67 sec  

What will be the output of the following Python code?

import re
re.ASCII

  (1)  
8

  (2)  
32

  (3)  
64

 
  (4)  
256

Option 4:
Explanation: The expression re.ASCII returns the total number of ASCII characters
that are present, that is 256. This can also be abbreviated as re.A, which results in
the same output (that is, 256).

Report this question / solution


Question: 36Time Taken: 110 sec  
What will be the output of the following Python code?

re.subn('A', 'X', 'AAAAAA', count=4)

  (1)  
‘XXXXAA, 4’

  (2)  
(‘AAAAAA’, 4)

 
  (3)  
(‘XXXXAA’, 4)

  (4)  
‘AAAAAA, 4’

Option 3:
Explanation: The line of code shown above demonstrates the function re.subn. This
function is very similar to the function re.sub except that in the former, a tuple is
returned instead of a string. The output of the code shown above is: (‘XXXXAA’, 4).

Report this question / solution


Question: 37Time Taken: 26 sec  

Which of the following statements are true?

  (1)  
When you open a file for reading, if the file does not exist, an error occurs

  (2)  
When you open a file for writing, if the file does not exist, a new file is created

  (3)  
When you open a file for writing, if the file exists, the existing file is overwritten with
the new file
 
  (4)  
All of the mentioned

Option 4: Explanation: The program will throw an error.


Report this question / solution
Question: 38Time Taken: 22 sec  

Which are the two built-in functions to read a line of text from standard input, which
by default comes from the keyboard?

 
  (1)  
Raw_input & Input

  (2)  
Input & Scan

  (3)  
Scan & Scanner

  (4)  
Scanner

Option 1: Explanation: Python provides two built-in functions to read a line of text
from standard input, which by default comes from the keyboard. These functions are:
raw_input and input
Report this question / solution
Question: 39Time Taken: 126 sec  

The function re.error raises an exception if a particular string contains no match for
the given pattern.

  (1)  
True

 
  (2)  
False

  (3)  
cant say

  (4)  
None

Option 2: Explanation: The function re.error raises an exception when a string


passed to one of its functions here is not a valid regular expression. It does not raise
an exception if a particular string does not contain a match for the given pattern.
Report this question / solution
Question: 40Time Taken: 140 sec  

Which of the following functions does not accept any argument?

 
  (1)  
re.purge

  (2)  
re.compile

  (3)  
re.findall

  (4)  
re.match

Option 1: Explanation: The function re.purge is used to clear the cache and it does
not accept any arguments.
Report this question / solution

Section- Python Scope of Variables

Question: 41Time Taken: 45 sec  

What happens if a local variable exists with the same name as the global variable
you want to access?
  (1)  
Error

  (2)  
The local variable is shadowed

  (3)  
Undefined behavior

 
  (4)  
The global variable is shadowed

Option 4: Explanation: If a local variable exists with the same name as the local
variable that you want to access, then the global variable is shadowed. That is,
preference is given to the local variable.
Report this question / solution
Question: 42Time Taken: 33 sec  

What will be the output of the following Python code?

def f1():
    x=15
    print(x)
x=12
f1()

  (1)  
Error

  (2)  
12

  (3)  
15
  (4)  
1512

Option 3:
Explanation: In the code shown above, x=15 is a local variable whereas x=12 is a
global variable. Preference is given to local variable over global variable.

Hence the output of the code shown above is 15.

Report this question / solution


Question: 43Time Taken: 33 sec  

On assigning a value to a variable inside a function, it automatically becomes a


global variable.

  (1)  
True

  (2)  
False

  (3)  
None

  (4)  
cant say

Option 2: Explanation: On assigning a value to a variable inside a function, t


automatically becomes a local variable. Hence the above statement is false.
Report this question / solution
Question: 44Time Taken: 45 sec  

What will be the output of the following Python code?

def f1():
    x=100
    print(x)
x=+1
f1()
  (1)  
Error

  (2)  
100

  (3)  
101

  (4)  
99

Option 2:
Explanation: The variable x is a local variable. It is first printed and then modified.
Hence the output of this code is 100.

Report this question / solution


Question: 45Time Taken: 30 sec  

What will be the output of the following Python code?

a=10
globals()['a']=25
print(a)

  (1)  
10

 
  (2)  
25
  (3)  
Junk value

  (4)  
Error

Option 2:
Explanation: In the code shown above, the value of ‘a’ can be changed by using
globals() function. The dictionary returned is accessed using key of the variable ‘a’
and modified to 25.

Report this question / solution


Question: 46Time Taken: 34 sec  

Which of the following data structures is returned by the functions globals() and
locals()?

  (1)  
list

  (2)  
set

 
  (3)  
dictionary

  (4)  
tuple

Option 3: Explanation: Both the functions, that is, globals() and locals() return value
of the data structure dictionary.
Report this question / solution
Question: 47Time Taken: 139 sec  

What will be the output of the following Python code?

x=12
def f1(a,b=x):
    print(a,b)
x=15
f1(4)

  (1)  
Error

  (2)  
12 4

 
  (3)  
4 12

  (4)  
4 15

Option 3:
Explanation: At the time of leader processing, the value of ‘x’ is 12. It is not modified
later. The value passed to the function f1 is 4. Hence the output of the code shown
above is 4 12.

Report this question / solution


Question: 48Time Taken: 17 sec  

What will be the output of the following Python code?

def f(p, q, r):


    global s
    p = 10
    q = 20
    r = 30
    s = 40
    print(p,q,r,s)
p,q,r,s = 1,2,3,4
f(5,10,15)

  (1)  
1234

  (2)  
5 10 15 4

  (3)  
10 20 30 40

  (4)  
5 10 15 40

Option 3:
Explanation: The above code shows a combination of local and global variables. The
output of this code is: 10 20 30 40

Report this question / solution


Question: 49Time Taken: 105 sec  

What will be the output of the following Python code?

def f(): x=4


x=1
f()
x

  (1)  
Error

 
  (2)  
4

  (3)  
Junk value
 
  (4)  
1

Option 4:
Explanation: In the code shown above, when we call the function f, a new
namespace is created. The assignment x=4 is performed in the local namespace
and does not affect the global namespace. Hence the output is 1.

Report this question / solution


Question: 50Time Taken: 33 sec  

What will be the output of the following Python code?

def f1(a,b=[]):
    b.append(a)
    return b
print(f1(2,[3,4]))

  (1)  
[3,2,4]

  (2)  
[2,3,4]

  (3)  
Error

 
  (4)  
[3,4,2]

Option 4:
Explanation: In the code shown above, the integer 2 is appended to the list [3,4].
Hence the output of the code is [3,4,2]. Both the variables a and b are local
variables.

You might also like