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

Python Objective Question

This document contains 50 questions and answers related to Python programming concepts like data types, operators, functions, scopes, classes and objects. The questions cover basics as well as some advanced concepts to test understanding of Python fundamentals.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Python Objective Question

This document contains 50 questions and answers related to Python programming concepts like data types, operators, functions, scopes, classes and objects. The questions cover basics as well as some advanced concepts to test understanding of Python fundamentals.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1. Which of these is not a Numeric data type in python 3.x?

a) int
b) long
c) float
d) complex
Answer: b

2. What is the default return value of a function?


a) int
b) bool
c) void
d) None
Answer: d

3. What will be the output of the following Python code?

1. >>>str="hello"
2. >>>str[:2]
3. >>>

a) he
b) lo
c) olleh
d) hello
Answer: a

4. Which of the following will run without errors?


a) round(45.8)
b) round(6352.898,2,5)
c) round()
d) round(7463.123,2,1)

Answer: a

5. What is the return type of id()?


a) int
b) float
c) bool
d) dict
Answer: a

6. What error occurs when you execute the following Python code snippet?

apple = mango

a) SyntaxError
b) NameError
c) ValueError
d) TypeError

Answer: b

7. What will be the output of the following Python code snippet?

1. def example(a):
2. a = a +'2'
3. a = a*2
4. return a

5. example("hello")

a) Indentation Error
b) cannot perform mathematical operation on strings
c) hello2
d) hello2hello2

Answer: a

8. What data type is the object below?

L = [1, 23, 'hello', 1]

a) list
b) dictionary
c) array
d) tuple

Answer: a

9. In order to store values in terms of key and value we use what core data type.
a) list
b) tuple
c) class
d) dictionary

Answer: d

10. The following is displayed by a print function call. Select all of the function calls that
result in this output.

1. tom
2. dick
3. harry

a)

print('''tom

\ndick

\nharry''')

b) print(”’tomdickharry”’)
c) print(‘tom\ndick\nharry’)
d)print('tomdickharry')
Answer: c

11. Select all options that print.

hello-how-are-you

a) print(‘hello’, ‘how’, ‘are’, ‘you’)


b) print(‘hello’, ‘how’, ‘are’, ‘you’ + ‘-‘ * 4)
c) print(‘hello-‘ + ‘how-are-you’)
d) print(‘hello’ + ‘-‘ + ‘how’ + ‘-‘ + ‘are’ + ‘you’)

Answer: c

12. What is the return value of trunc()?


a) int
b) bool
c) float
d) None

Answer: a

13. Which is the correct operator for power(xy)?


a) X^y
b) X**y
c) X^^y
d) None of the mentioned

Answer: b

14. Which one of these is floor division?


a) /
b) //
c) %
d) None of the mentioned

Answer: b

15. What is the order of precedence in python?


i) Parentheses
ii) Exponential
iii) Multiplication
iv) Division
v) Addition
vi) Subtraction
a) i,ii,iii,iv,v,vi
b) ii,i,iii,iv,v,vi
c) ii,i,iv,iii,v,vi
d) i,ii,iii,iv,vi,v

Answer: a

16. What is the answer to this expression, 22 % 3 is?


a) 7
b) 1
c) 0
d) 5

Answer: b

17. Mathematical operations can be performed on a string.


a) True
b) False

Answer: b

18. Operators with the same precedence are generally evaluated in which manner?
a) Left to Right (except exponent/power)
b) Right to Left
c) Left to Right
d) None of the mentioned

Answer: a

19. What is the output of this expression, 3*1**3?


a) 27
b) 9
c) 3
d) 1

Answer: c
20. Which one of the following has the same precedence level?
a) Addition and Subtraction
b) Multiplication, Division and Addition
c) Multiplication, Division, Addition and Subtraction
d) Addition and Multiplication

Answer: a

21. What will be the output of the following Python code?


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

a) Error
b) 12
c) 15
d) 1512

Answer: c

22. What will be the output of the following Python code?


x=101
def f1():
x=100
print(x)
f1()

a) Error
b) 100
c) 101
d) 99

Answer: b

23. What will be the output of the following Python code?


def san(x):
print(x+1)
x=-2
x=4
san(12)

a) 13
b) 10
c) 2
d) 5

Answer: a

24. What will be the output of the following Python code?


def f1():
global x
x+=1
print(x)
x=12
print("x")

a) Error
b) 13
c)13x

d) x

Answer: d

25. What will be the output of the following Python code?


def f1(x):
global x
x+=1
print(x)
f1(15)
print("hello")

a) error
b) hello
c) 16
d)16hello
Answer: a

26. What will be the output of the following Python code?


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

a) Error
b) 12 4
c) 4 12
d) 4 15

Answer: c

27. What will be the output of the following Python code?


def f():
global a
print(a)
a ="hello"
print(a)
a ="world"
f()
print(a)

a)

hello

hello

world

b)

world

world

hello

c)

hello

world

world
d)

world

hello

world

Answer: b

28. What will be the output of the following Python code?


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

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

Answer: d

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

a) 1 2 3 4
b) 5 10 15 4
c) 10 20 30 40
d) 5 10 15 40

Answer: c

30. What will be the output of the following Python code?


def f(x):
print("outer")
def f1(a):
print("inner")
print(a,x)
f(3)
f1(1)

a)

outer

error

b)

inner

error

c)

outer

inner

d) error

Answer: a

31. What will be the output of the following Python code?


x =5
def f1():
global x
x =4

def f2(a,b):
global x
return a+b+x
f1()
total= f2(1,2)
print(total)

a) Error
b) 7
c) 8
d) 15

Answer: b

32. What will be the output of the following Python code?


x=100
def f1():
global x
x=90

def f2():
global x
x=80
print(x)

a) 100
b) 90
c) 80
d) Error

Answer: a

33. Read the following Python code carefully and point out the global variables?
y, z =1,2
def f():
global x
x =y+z

a) x
b) y and z
c) x, y and z
d) Neither x, nor y, nor z

Answer: c

34. What is a variable defined outside a function referred to as?


a) A static variable
b) A global variable
c) A local variable
d) An automatic variable

Answer: b

35. What is a variable defined inside a function referred to as?


a) A global variable
b) A volatile variable
c) A local variable
d) An automatic variable

Answer: c

36. What will be the output of the following Python code?


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

a) 1
b) Nothing is displayed
c) 0
d) An exception is thrown

Answer: c

37. What will be the output of the following Python code?


def a(b):
b = b + [5]

c =[1,2,3,4]
a(c)
print(len(c))

a) 4
b) 5
c) 1
d) An exception is thrown
Answer: a

38. What will be the output of the following Python code?


a=10
b=20
def change():
global b
a=45
b=56
change()
print(a)
print(b)

a)

10

56

b)

45

56

c)

10

20

d) Syntax Error

Answer: a

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

a) An exception is thrown because of conflicting values


b) 1 2
c) 3 3
d) 3 2

Answer: d

40. What will be the output of the following Python code?


def change(one, *two):
print(type(two))
change(1,2,3,4)

a) Integer
b) Tuple
c) Dictionary
d) An exception is thrown

Answer: b

41. If a function doesn’t have a return statement, which of the following does the function
return?
a) int
b) null
c) None
d) An exception is thrown without the return statement

Answer: c

42. What will be the output of the following Python code?


def display(b, n):
while n >0:
print(b,end="")
n=n-1
display('z',3)

a) zzz
b) zz
c) An exception is executed
d) Infinite loop

Answer: a
43. What will be the output of the following Python code?
def find(a, **b):
print(type(b))
find('letters',A='1',B='2')

a) String
b) Tuple
c) Dictionary
d) An exception is thrown

Answer: c

44. _____ represents an entity in the real world with its identity and behavior.
a) A method
b) An object
c) A class
d) An operator

Answer: b

45. What is used to initialize an object.


a) class
b) constructor
c) User-defined functions
d) In-built functions

Answer: b

46. What will be the output of the following Python code?


Class test:
Def __init__(self,a="Hello World"):
self.a=a

def display(self):
print(self.a)

obj=test()
obj.display()
a) The program has an error because constructor can’t have default arguments
b) Nothing is displayed
c) “Hello World” is displayed
d) The program has an error display function doesn’t have parameters

Answer: c

47. What will be the output of the following Python code?


Class test:
Def __init__(self,a):
self.a=a

def display(self):
print(self.a)
obj=test()
obj.display()

a) Runs normally, doesn’t display anything


b) Displays 0, which is the automatic default value
c) Error as one argument is required while creating the object
d) Error as display function requires additional argument

Answer: c

48. Is the following Python code correct?


class A:
def__init__(self,b):
self.b=b
def display(self):
print(self.b)
obj=A("Hello")
del obj

a) Correct
b) Incorrect

Answer: a

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

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


b) ‘New’ is printed
c) ‘Old’ is printed
d) Nothing is printed

Answer: c

50. What is Instantiation in terms of OOP terminology?


a) Deleting an instance of class
b) Modifying an instance of class
c) Copying an instance of class
d) Creating an instance of class

Answer: d

You might also like