Python Tutorial: MCQ Function

Monday, 25 October 2021

MCQ Function

 

Q1. 

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

Answer: a

Q2. 

2. Which keyword is used for function?
a) Fun                              b) Define                               c) Def                                             d) Function

Answer: c

Q3. 

What will be the output of the following Python code?

def sayHello():
print('Hello World!')
sayHello()
sayHello()

a)

Hello World!
Hello World!
 

b)

'Hello World!'
'Hello World!'
 

c)

Hello
Hello
d) None of the mentioned

Answer: a

Q4. 

What will be the output of the following Python code?

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

print Max(3, 4)

            a) 3                              b) 4                  c) 4 is maximum                d) None of the mentioned

Answer: c

Q5. 

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

x =50
def func(x):
     print('x is', x)
     x =2
     print('Changed local x to', x)
func(x)
print('x is now', x)

a) x is 50

Changed local x to 2
x is now 50

b)

x is 50
Changed local x to 2
x is now 2

c)

x is 50
Changed local x to 2
x is now 100
d) None of the mentioned

Answer: a

Q6. 

What will be the output of the following Python code?

x =50
def func():
    global x
    print('x is', x)
    x =2
   print('Changed global x to', x)
func()
print('Value of x is', x)

a)

x is 50
Changed global x to 2
Value of x is 50
 

b)

x is 50
Changed global x to 2
Value of x is 2
 

c)

x is 50
Changed global x to 50
Value of x is 50
 
d) None of the mentioned

Answer: b

Q7. 

What will be the output of the following Python code?

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
 
Answer : c

Q8. 

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)
func(25, c =24)
func(c =50, a =100)

a)

a is 7 and b is 3 and c is 10
a is 25 and b is 5 and c is 24
a is 5 and b is 100 and c is 50
 

b)

a is 3 and b is 7 and c is 10
a is 5 and b is 25 and c is 24
a is 50 and b is 100 and c is 5
 

c)

a is 3 and b is 7 and c is 10
a is 25 and b is 5 and c is 24
a is 100 and b is 5 and c is 50
 
d) None of the mentioned

Answer: c

 

Q9. 

What will be the output of the following Python code?

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

Answer: b

Q10.         

Which of the following is a feature of DocString?
a) Provide a convenient way of associating documentation with Python modules, functions, classes, and methods
b) All functions should have a docstring
c) Docstrings can be accessed by the __doc__ attribute on objects
d) All of the mentioned
Answer: d

Q11.         

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
Answer: d

Q12.         

What are the two main types of functions?
a) Custom function                         b) Built-in function & User defined function
c) User function                                d) System function
Answer: b

Q13.         

Where is function defined?
a) Module                                b) Class           c) Another function                d) All of the mentioned

Answer: d

Q14.         

What is called when a function is defined inside a class?
a) Module                                b) Class           c) Another function                d) Method

Answer: d

Q15.         

Which of the following is the use of id() function in python?
a) Id returns the identity of the object                   b) Every object doesn’t have a unique id
c) All of the mentioned                                                 d) None of the mentioned
Answer: a

Q16.         

Which of the following refers to mathematical function?
            a) sqrt                          b) rhombus      c) add                                      d) rhombus
Answer: a

Q17.         

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

Answer: c

Q18.         

What will be the output of the following Python code?

def C2F(c):
     return c * 9/5 + 32
print C2F(100)
print C2F(0)

a)

212
32

b)

314
24

c)

567
98

d) None of the mentioned

Answer: a

Q19.         

What will be the output of the following Python code?

def power(x, y=2):
    r =1
    for i in range(y):
           r = r * x
    return r
print (power(3))
print (power(3,3))

a)

212
32

b)

9
27

c)

567
98

d) None of the mentioned

 

Answer: b

Q20.         

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

Q21.         

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

Q22.         

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 : 4

Q23.         

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

Q24.         

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

Q25.         

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

Q26.         

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

Q27.         

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

No comments:

Post a Comment