python_qus
python_qus
1)A ---- error does not casuse the program abort, but produces incorrect result.
Ans: logic error
2)for i in range(3):
print i,
for i in range (4,6):
print i,
Ans:01245
3)def foo():
print "IronPython"'
if_name_ == '_main_':
foo()
print "Jython"
import mod
print 'cython'
mod foo()
Ans: CythonIronPython
4)def foo()
print "is half the battle",
if_name_ == '_main_"
print "makes a good endings",
app.y
Import mod
print "A good beginning,",
mod.foo()
5)x =1
y = x = x+1
print("y is", y)
x,y = 1,2
x,y = y,x
print(x,y)
Ans: Y is 2, 1,2
Ans: pytho
Ans: 300
8)To read the remainig lines of the file from a file object in file, use----
which statement should be used to get the mod of the file which was opened with
open statement?
11)
#!user/bin/python
#opena file
fo=open("foo.txt", "rt")
str = fo.read(10):
print "read string is: ", str
position = fo.seek(0,0):
str = fo.read(10):
print :read strings is :", str
fo.close()
15)Each time a function is invoked the system parameters and local variables in an
area of memory, known as ------, which stores elementry in last in first out
Ans: Stack
Ans: Sets
17)
temperature = 50
if tep >= 100:
print ("too hot")
elif tep <= 40:
print ("too cold")
else:
print ("just right")
***********************************************************************************
***********************************************************************************
***********
Question Set 2:
1. os.rename(current_file_name, new_file_name)
#!/usr/bin/python
import os
3. #!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
Explanation:
The seek(offset[, from]) method changes the current file position. The offset
argument indicates the number of bytes to be moved.
The from argument specifies the reference position from where the bytes are to be
moved.
Ans:
Function blocks begin with the keyword def followed by the function name and
parentheses ( ( ) ).
Any input parameters or arguments should be placed within these parentheses. You
can also define parameters inside these parentheses.
The first statement of a function can be an optional statement - the documentation
string of the function or docstring.
The code block within every function starts with a colon (:) and is indented.
The statement return [expression] exits a function, optionally passing back an
expression to the caller. A return statement with no arguments is the same as
return None.
5. x=1
x=x*2+1
print x
Ans: 3
6. eval(1+2*3)
Ans: "Python"
a) python Inheritance
b) python Encapsulation
c) python Polymorphism
***********************************************************************************
********************************************************************************
Question Set 3:
Ans: a)
Ans: Assembly
3. x = y = 10
if x is y:
x = 20
else:
x = 30
print x
Ans: 20
4. when the user reads the input from user as 70, 72, 68 and what will be the
output of the below pgm
get marks()
sem1 = input()
sem2 = input()
sem3 = input()
return (sem1, sem2, sem3)
Ans: a (We can return multiple values in python and displayed as tuples)
Ans: elif
Ans:
Ans: 2
Ans: Error
a) Sets
b) Tuples
c) Lists
d) Dictionary
Ans: Tuples
13. Which among the following does not support indexing and slicing
a) Sets
b) Lists
c) Dictionary
d) Tuples
Ans: Sets
a) eval
b) exec
c) expr
d) none
Ans: eval
15. Which among the following are syntax for opening a file "C:\test.txt" in
reading mode.
Ans: option a
a) Nothing
b) 0
c) Null
d) []
Ans: None
***********************************************************************************
***********************************************************************************
************************
Question Set 4:
1. If the element in the python list is known, but index is not known, the command
to remove element is _____
a) pop()
b) del()
c) remove()
d) purge()
Ans: remove()
a) Hardware
b) Software
c) Programs
d) Keyboard
Ans: Programs
except IOerror:
print ("Error cannot write file")
else:
print ("write contents successfully")
fh.close()
Ans: option c
Ans: option b
Ans: Option a
7. O\p of a program:
a) 4
b) 5
c) 6
d) Syntax error
Ans: 5
8. Which python module offers more sophiticated control over print in both build in
and userdefined objects.
a) pprint
b) rprint
c) uprint
d) usrprint
Ans: pprint
9. x = True
y = True
z = False
if (x or y) and z:
print "Yes"
else:
print "No"
Ans: No
a) infile.read(2)
b) infile.read()
c) infile.readline()
c) infile.readlines()
Ans: infile.read(2)
a) read
b) input
c) load
d) receive
Ans: read
12. fh = open("temp.txt","rb+")
Ans: Option a
a) fo.tell()
b) fo.write(data)
c) fo.seek(0,0)
d) fo.close()
Ans: Option b
print result
a) 300
b) 100200
c) 100 200
d) Error as strings cannot be added
Ans: 300
***********************************************************************************
***********************************************************************************
***
Question Set 5
a) exit()
b) end()
c) quit()
d) exit() and quit()
Ans: option d
2. for i in range(10)
for j in range(10)
print (i*j)
a) 100
b) 20
c) 10
d) 45
Ans: 100
3. a = 10
def foobar(a):
a = 20
x = 30
foobar(x);
print x
a) 10
b) 20
c) 30
d) Error
a) 0.2
b) 0.199999
c) 0.199
d) 0.3
Ans: 0.2
a) x++
b) x+=1
c) x!=1
d) x = 1
Ans: Option b since increment / decrement operator are not supported in python.
6. 45//4
a) 10
b) 11
c) 11.25
d) 12
Ans: 11
a) Lists
b) Dictionary
c) Tuples
d) Class
Ans: Class
Ans: Yield
Ans: Option b
***********************************************************************************
***********************************************************************************
******
Question Set 6
1. _____is a function used to execute python statement which are stored in a string
or file as opposed to written in the program itself.
a) exec
b) repr
c) eval
d) str
a) sys.terminate()
b) sys.halt()
c) sys.exit()
d) sys.stop()
Ans: Option C
3. print "Good"+1+32+3
What is o/p:
4. x = 10
def foobar();
global x;
x = 20
x = 30
foobar()
print x
Ans: 30
5. What is output:
x = y = 10
if (x is y):
x = 20
else(x>=y)
x = 30
print x, y
a)20 10
b)10 20
Ans: 20 10
a) u
b) e
c) ur
d) ru
Ans: ur
7. Output:
print Employeedata[-3:5]
8. Output:
d = lambda p: p*2
f = lambda p: p*3
x = 2
x = d(x)
x = f(x)
x = d(x)
print x
a) 7
b) 12
c) 24
d) 36
Ans: 24
9. i = 0
for i in range(10, 1, -2):
y+=i;
print y
a) 10
b) 40
c) 30
d) 20
Ans: 30
10.values = [2,3,2,4]
def transform(num):
return num/2
for i in map(transform, values)
print i
a) 2 3 2 4
b) 4 6 4 8
c) 1 1 1 2
Ans: option C
a) Lists
b) Dictionary
c) Tuples
d) Set
***********************************************************************************
******************************************************************************