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

python_qus

The document consists of a series of question sets related to Python programming, covering topics such as error types, file handling, data types, functions, and control structures. Each question includes a prompt followed by an answer, with some questions requiring code snippets or explanations. The content is structured in a quiz format, aimed at testing knowledge of Python concepts and syntax.

Uploaded by

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

python_qus

The document consists of a series of question sets related to Python programming, covering topics such as error types, file handling, data types, functions, and control structures. Each question includes a prompt followed by an answer, with some questions requiring code snippets or explanations. The content is structured in a quiz format, aimed at testing knowledge of Python concepts and syntax.

Uploaded by

8695862277
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Question Set 1:

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

6)print "python level:",


for num in range(5):
print num,

Ans: pytho

7)import synargs = len(sys.argv)


if(nargs<= 1):
print "insuficient no of argument passed)
"sys.exit(-1)
total =ocnt =0
while (cnt < nargs):
total += sys.argv{cnt] cnt += 1
print total

$python sum.py 100 200

Ans: 300

8)To read the remainig lines of the file from a file object in file, use----

9) def a (b,c,d): Pass

which statement should be used to get the mod of the file which was opened with
open statement?

Ans: file.mode (file is the file pointer of the file)

10) to write data into a file which statement is used?

file.write("string") (file is the file pointer of the file)

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

What seek function fo.seek(0,0) will do when it gets executed

Ans: Changes the current position of file to the beginning

12) #open a file


fo = open("foo.txt", wb)
print "name of the file:", fo.name
fo.close()

what open function will do:

Ans: opens the file in write oly binary format

13) ---- method is called when the instance is about to be destroyed

Ans: __del__ method

14) To write data into a file which statement is used?


Ans: write()

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

16) which of these do not support indexing and slicing

Ans: Sets

17)
temperature = 50
if tep >= 100:
print ("too hot")
elif tep <= 40:
print ("too cold")
else:
print ("just right")

Ans: Just Right

***********************************************************************************
***********************************************************************************
***********

Question Set 2:

1. os.rename(current_file_name, new_file_name)
#!/usr/bin/python
import os

os.rename( "test1.txt", "test2.txt" )


what rename wil do?

Ans: Renames a file from test1.txt to test2.txt

2. How many list of the different modes of opening a file?


Ans: = 12
which are r, rb, r+, rb+
w, wb, w+, wb+
a, ab, a+, ab+

3. #!/usr/bin/python

# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str

position = fo.seek(0, 0);


str = fo.read(10);
print "Again read String is : ", str
# Close opend file
fo.close()

What Seek function will do in this function?

Ans: Reposition pointer at the beginning once again

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.

4. def lang(a, b, c):


Pass

how will define the function?

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: TypeError: eval() arg 1 must be a string or code object

7. lang = ("How is your Python")


lang[0] = "C"
print lang

Ans: TypeError: 'str' object does not support item assignment

8. lang = ("How is your Python")


lang[-6:]

Ans: "Python"

9. Python Lists are different from linked lists because of


a) Insert/Delete the elements at the beginning of Lists.
b) Insert/Delete the elements at the end of Lists.
c) Insert/Delete the elements at the Middle of Lists.
d) Each Element is assigned by its Position or Index.

Ans: b Insert/Delete the elements at the end of Lists

9. Which Statement Immediately Terminates the Loop.

Ans: break statement

10. Question from in operator:


in-->Evaluates to true if it finds a variable in the specified sequence and false
otherwise.
x in y, here in results in a 1 if x is a member of sequence y.
11. len("Python")
6
len("123", "HI")
2
len("SDFLSDFSDFSDFK")
14
What kind of function this is?

a) python Inheritance
b) python Encapsulation
c) python Polymorphism

Ans: Python Polymorphism


Reason: Same function calling with diff parameters or inputs

12. Python string is Enclosed with


a) Single Quote
b) Double Quotes

Ans: Single Quotes

***********************************************************************************
********************************************************************************

Question Set 3:

1. Which of the following statement is true


a) Python 3 is the newer version, but it is backward compatible with python 2.
b) Python 3 is the newer version, but it is not backward compatible with python 2.
c) A python 2 program always run on python 3 interpreter
d) A python 3 program always run on python 2 interperter

Ans: a)

2. A computer can execute any written code in___________ format

a) Machine Language b) Assembly Language c) High Level Language d) Common


Language

Ans: Assembly

3. x = y = 10
if x is y:
x = 20
else:
x = 30
print x

What is output of this program:

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)

sem_marks = get marks()


print sem_marks

a) 70, 72, 68 is returned as tuple


b) 72, 72, 72 is returned since most recent value it will take
c) 70, 70, 70 is returned first value
d) Returning muliple values is not supported in python.

Ans: a (We can return multiple values in python and displayed as tuples)

5. Python Keyword used for elseif is

a) else if b)elif c)elsif d)elf

Ans: elif

6. Python variables must be defined

a) After they are used


b) Before they are used
c) Before/After
d) No Need

7. String Literals are enclosed in_______

a) Parenthesis b)Single Quotes c)Double Quotes d)None

Ans: Single Quotes

8. Which function do u need to read a string?


a) input("Enter a String")
b) eval(imput("Enter a string")

Ans:

9. print "Enter 3 numbers"


num1 = eval(str(input()))
num2 = eval(str(input()))
num3 = eval(str(input()))
compute average
average = (num1+num2+num3)/2
print average
a) 1 b)2 c)3 d)4

Ans: 2

10. name = "Show Storm"


name[5] = 'X'
print name
a) ShowXStorm b)ShowxStorm c)ShowX d)Error

Ans: Error

11. names1 = ['Amir', 'Berry', 'Charles', 'Daniel']


names2 = [name.lower() for name in names1]
print names2[2][0]
a)a
b)C
c)c
d)An Exception is thrown

Ans: option c (c)

12. Which among the options are read only lists

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

14. Which function used to evaluate python expression stored in a string

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.

a) infile = open("C:\\test.txt", 'r')


b) infile = open("C:\test.txt", 'r')
c) infile = open("C:\\test.txt", 'r+')
d) infile = open("C:\test.txt", 'r+')

Ans: option a

16. A function with no return statement returns_____

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

2. ______ are the instructions given to computer.

a) Hardware
b) Software
c) Programs
d) Keyboard

Ans: Programs

3. O/P of the program:


fh = open("test.txt", "w")
fh = write("Error cannot find file")

except IOerror:
print ("Error cannot write file")
else:
print ("write contents successfully")
fh.close()

a) Error cannot find file


b) write contents successfully
c) Error cannot write file
d) both a) and b)

Ans: option c

4. which of the following will prints "Welcome \ to \ Wipro Technolgies"

a) print("Welcome \to\Wipro Technologies")


b) print("Welcome\\to\\Wipro Technolgies")

Ans: option b

5. What is type of variable *args and **kargs in the below program?

def inner(*args, **kargs):


print(args)
print(**kargs)

a) List and Dictionary


b) List and Tuple
c) String and List
d) Tuple and Dictionary

Ans: Option a

6. The Word True is ________


a) Python Keyboard
b) Boolean Literal
c) Same as value as 0
d) Not supported in python

Ans: Boolean Literal

7. O\p of a program:

a = [1, 2, 3, None, ()]


print len(a)

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

10. To read 2 char from file object infile use

a) infile.read(2)
b) infile.read()
c) infile.readline()
c) infile.readlines()

Ans: infile.read(2)

11. Which function do u use to read data using binary input.

a) read
b) input
c) load
d) receive
Ans: read

12. fh = open("temp.txt","rb+")

a) open the file temp in read mode


b) open the file temp in reading oly binary format
c) open the file temp in reading & writing

Ans: Option a

13. To write data into a file which stmt is used.

a) fo.tell()
b) fo.write(data)
c) fo.seek(0,0)
d) fo.close()

Ans: Option b

14. Difference between input() and raw_input()

15. import sys #sum.py


result = sys.argv[1] + sys.argv[2]

print result

python sum.py 100 200

a) 300
b) 100200
c) 100 200
d) Error as strings cannot be added

Ans: 300

***********************************************************************************
***********************************************************************************
***

Question Set 5

1. What is the cmd to exit in python interpreter?

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)

How many times the loop will execute?

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

Ans: Error sicne x is not defined

4. print 1.2 - 1.0

a) 0.2
b) 0.199999
c) 0.199
d) 0.3

Ans: 0.2

5. To add value 1 to existing.

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

7. Which is not a core data type

a) Lists
b) Dictionary
c) Tuples
d) Class

Ans: Class

8. A function that contains a _____statement is generator function.


a) yield
b) generate
c) iterator
d) __iter__

Ans: Yield

9. Difference between file.closed and file.close()

a) Both are same


b) file.closed returns true if file is closed otherwise false

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

Ans: exec (Doubt pls cross check)

2. ____function immediately termiantes the program.

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:

Ans: Erro since we cannot concatenate string and integer in python

4. x = 10
def foobar();
global x;
x = 20
x = 30
foobar()
print x

a) 10 b)20 c) 30 d) syntax error

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

6. In python the raw_unicode escape mode is___

a) u
b) e
c) ur
d) ru

Ans: ur

7. Output:

Employeedata = ["data", 65, 75, 84]

print Employeedata[-3:5]

Ans: 65, 78, 84

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

11. What is true:

a) An iterator is an object that provides a uniformed way for traversing the


element in a container object.
b) To enable the traversal using a loop in container class must implement the
__iter__(self) method that returns an iterator
c) An iterator class must contains __next__ method that returns next element in
container object.
d) All the above

Ans: All the above

12. Which of the below data types will execute faster.

a) Lists
b) Dictionary
c) Tuples
d) Set

Ans: Tuples (Doubt pls cross check)

***********************************************************************************
******************************************************************************

You might also like