Pythontest 2
Pythontest 2
55 sec
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.
(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
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.
(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.
(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.
(1)
9997
(2)
9999
(3)
9996
(4)
None of the mentioned
Option 3:
Executable Output
(2)
Class
(3)
Another function
(4)
Method
(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.
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.
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
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.
(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’>.
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.
(1)
__div__()
(2)
__ceildiv__()
(3)
__floordiv__()
(4)
__truediv__()
(1)
True
(2)
False
(3)
Cant say
(4)
None
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.
(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
(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
(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
(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
(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.
(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
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.
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.
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.
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.
(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
(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
(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
(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
(1)
It is used for object serialization
(2)
It is used for object deserialization
(3)
None of the mentioned
(4)
All of the mentioned
(1)
re.compile(str)
(2)
re.assemble(str)
(3)
re.regex(str)
(4)
re.create(str)
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).
(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).
(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
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
(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
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
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.
(1)
True
(2)
False
(3)
None
(4)
cant say
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.
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.
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
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.
(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
(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.
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.