0% found this document useful (0 votes)
1K views

Python Multiple Choice

The document contains questions and answers related to functions, variables, strings, and lists in Python. Some key points: - Functions allow for code reusability and modularity. The def keyword is used to define functions. Variables defined inside a function are local, while those outside are global. - Strings can be concatenated using +, indexed using [], and various string methods like count(), find(), lower() etc. can be applied. - Lists allow storing multiple elements and support operations like append(), insert(), pop() etc. Lists are mutable and support negative indexing. - Common types of errors include syntax errors due to invalid syntax, name errors due to undefined variables, value errors due to type mismatches

Uploaded by

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

Python Multiple Choice

The document contains questions and answers related to functions, variables, strings, and lists in Python. Some key points: - Functions allow for code reusability and modularity. The def keyword is used to define functions. Variables defined inside a function are local, while those outside are global. - Strings can be concatenated using +, indexed using [], and various string methods like count(), find(), lower() etc. can be applied. - Lists allow storing multiple elements and support operations like append(), insert(), pop() etc. Lists are mutable and support negative indexing. - Common types of errors include syntax errors due to invalid syntax, name errors due to undefined variables, value errors due to type mismatches

Uploaded by

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

Functions

1. 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
Ans: B

2. What is the output of the following 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
Ans: C

3. Which of the following is the use of function in python?


A. Functions are reusable pieces of programs
B. Functions don’t provide better modularity for your application
C. you can’t also create your own functions
D. All of the mentioned
Ans: A
4. Which keyword is use for function?
A. fun
B. define()
C. def
D. function
Ans:C
5. What is the output of the below program?
def printMax(a, b):
if a > b:
print(a, 'is maximum')
elif a == b:
print(a, 'is equal to', b)
else:
print(b, 'is maximum')
printMax(3, 4)

A. 3
B. 4
C. 4 is maximum
D. None of the mentioned
Ans:C
6. What is the output of the below program?
x = 50
def func(x):
x=2
print(x,end=' ')
func(x)
print(x)
x is now 50
A. 2 50
B. 2 2
C. 50 2
D. 50 50
Ans:A

7. What is the output of below program?


def say(message, times = 1):
print(message * times)
say('Hello')
say('World', 5)
A. Hello
WorldWorldWorldWorldWorld
B. Hello
World 5
C. Hello
World,World,World,World,World
D. Hello
HelloHelloHelloHelloHello
Ans: A
8. What is the output of below program?
def maximum(x, y):
if x > y:
return x
elif x == y:
return 'The numbers are equal'
else:
return y
print(maximum(2, 3))

A. 2
B. 3
C. The numbers are equal
D. None of the mentioned
9. Which are the advantages of functions in python?
A. Reducing duplication of code
B. Decomposing complex problems into simpler pieces
C. Improving clarity of the code
D. All of the mentioned

Ans:D
10. What are the two main types of functions?
A. Custom function
B. Built-in function & User defined function
C. User function
D. System function
Ans:B

11. Which of the following refers to math function?


A. sqrt
B. rhombus
C. add
D. rhombus
Ans:A
12. What will be the output of the following Python code?
def cube(x):
return x * x * x
x = cube (3)
print(x)
A. 9
B. 3
C. 27
D. 30
Ans: C
13. What will be the output of the following Python code?
def func(a, b=5, c=10):
print('a is', a, 'and b is', b, 'and c is', c)
func(3, 7)
A. a is 3 and b is 7 and c is 10
B. a is 5 and b is 10 and c is 7
C. 0 is 5 and b is 10 and c is 7
D. Error
Ans:A
14. What will be the output of the following Python code?
def C2F(c):
return c * 9/5 + 32
print C2F(100)
A. 212
B. 314
C. 567
D. 0
Ans: A

15. 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
Ans:B

16. If a function doesn’t have a return statement, which of the following does the function
return?
A. int
B. Null
C. None
D. -1
Ans:C
17. 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
Ans:A
18. What will be the output of the following Python code?
def foo():
return total + 1
total = 0
print(foo())
A. 0
B. 1
C. error
D. none of the mentioned
Ans:B

19. 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
Ans:C

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

def f1():
x=100
print(x)
x=+1
f1()
A. Error
B. 100
C. 101
D. 99
Ans: B
21. Which of the following refers to math function/keywords?
A. sqrt
B. sin
C. pi
D. all the above
Ans:D

String

1. What is the output when following statement is executed?


print("a"+"bc")
A. a
B. bc
C. bca
D. abc
Ans: D

2. What is the output when following statement is executed?


s1="abcd"
print(s1[2:])
A. a
B. ab
C. cd
D. dc
Ans: C

3. What is the output when following code is executed?


str1 = 'hello'
str2 = ','
str3 = 'world'
str1[-1:]
A. olleh
B. hello
C. h
D. o
Ans: D

4. What arithmetic operators cannot be used with strings?


A. +
B. *
C. –
D. All of the mentioned
Ans: C

5. Which of the following function convert a string to an int in python?


A. set(x)
B. str()
C. int()
D. string()

Ans: C
6. Which of the following function checks in a string that all characters are alphanumeric?
A. isalpha()
B. capitalize()
C. isalnum()
D. isdigit()
Ans: C

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


print("xyyzxyzxzxyy".count('yy'))
A. 2
B. 0
C. error
D. none of the mentioned

Ans: A

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


print("CSECSE".count('CSE', 2))
A. 2
B. 1
C. error
D. none of the mentioned

Ans: B

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


print("I love india".endswith("india"))
A. 1
B. True
C. 3
D. 7

Ans: B

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


print("I love india".find("india"))
A. 5
B. True
C. False
D. 7

Ans: D

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


print('CSE12'.isalnum())
A. True
B. False
C. None
D. Error
Ans: A

12. What is the output of the following?


print(' '.isdigit())
A. True
B. False
C. None
D. Error
Ans: B

13. What will be the output of the following Python code snippet?
print('a@ 1,'.islower())
A. True
B. False
C. None
D. Error
Ans: A

14. What will be the output of the following Python code snippet?
print('abcefd'.replace('cd', '2'))
A. ab1ef2
B. abcefd
C. ab1efd
D. ab12ed2
Ans:B

15. What will be the output of the following Python code snippet?
print('abcdefcdghcd'.split('cd'))

A. [‘ab’, ‘ef’, ‘gh’]


B. [‘ab’, ‘ef’, ‘gh’, ”]
C. (‘ab’, ‘ef’, ‘gh’)
D. (‘ab’, ‘ef’, ‘gh’, ”)
Ans:B

16. What will be the output of the following Python code snippet?
print('ab cd-ef'.title())
A. Ab cd-ef
B. Ab Cd-ef
C. Ab Cd-Ef
D. None of the mentioned
Ans:C

17. What is the output of the following code?


example = "snow world"
example[3] = 's'
print (example)
A. snow
B. snow world
C. Error
D. snos world
Ans: C
18. What is the output of the following code?
s="India"
print(s in "I love India")
A. False
B. 8
C. Error
D. True
Ans: D

19. What is the output of the following code?


s="India"
print(s[::-1])
A. aidnI
B. india
C. a
D. Error
Ans: A

20. What is the output of the following code?


S1="India"
S2=”Japan”
print(S1<S2)
A. True
B. 8
C. Error
D. False
Ans: A
List
1. What will be the output of the following Python code?

a=[10,23,56,[78]]
b=a
print(b)
a[3][0]=95
a[1]=34
print(b)
A. [10,34,56,[95]]
B. [10,23,56,[78]]
C. [10,23,56,[95]]
D. [10,34,56,[78]]

Ans:A

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

a=[1,2,3]
b=a.append(4)
print(b)

A. [1, 2, 3, 4]
B. None
C. [1,2,3]
D. Error

Ans: B

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

a= [1, 2, 3, 4, 5]
for i in range(1, 5):
a[i-1] = a[i]
for i in range(0, 5):
print(a[i],end = " ")
A. 55123
B. 51234
C. 23451
D. 23455
Ans:D

4. Which of the following Python statements will result in the output: 6?


A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
A. A[2][3]
B. A[2][1]
C. A[1][2]
D. A[3][2]
Ans:C

5. How to find the last element of list in Python? Assume `bikes` is the name of list.
A. bikes[0]
B. bikes[-1]
C. bikes[lpos]
D. bikes[:-1]
Ans:B

7. Which of the following commands will create a list?


A. alist1 = list()
B. list1 = []
C. list1 = list([1, 2, 3])
D. all of the mentioned
Ans:D

8. To add a new element to a list1 we use which command?


A. list1.add(5)
B. list1.append(5)
C. list1.addLast(5)
D. list1.addEnd(5)

Ans:B

9. To insert 5 to the third position in list1, we use which command?


A. list1.insert(3, 5)
B. list1.insert(2, 5)
C. list1.add(3, 5)
D. list1.append(3, 5)

Ans: B

10. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count (5)?
A. 0
B. 4
C. 1
D. 2
Ans:D

General
1. What type of error the following code generates?

while i<=10:

s=s+1

i=i+1

a) Syntax error b) Name error c) Value error d) Type error

2. What type of error the following code generates?

i=0

while i=10:

a) Syntax error b) Name error c) Value error d) Type error

3. What type of error the following code generates?

i=0

s=''

s=s+i

a) Syntax error b) Name error c) Value error d) Type error

[‘ab’, ‘ef’, ‘gh’]

You might also like