Ad Code

Responsive Advertisement

MOST ASKED PYTHON INTERVIEW QUESTIONS

Python Interview Questions

Q.1 Define the term (in Python)?

• User-Defined Functions


• Built-In Functions


• Modules


Answer-:

1. User-Defined Functions-:

User defined functions are the functions defined by the user to perform a specific task in a large system.

know more about User-Defined Functions 

They are defined using def followed by function name and arguments.

For example -:

def add(x,y):   // func definition # here x and y are formal parameters
    sum = x+y
    return sum
print(add(3,4))  // Func calling   #here 3 and 4 are actual parameters

//Output -: 7



know more about Actual and Formal Parameters

2. Built-In Functions

Functions which are already available in Python are called Built-In Functions.

For example-:

all()  ,  print()  ,  bin()

3. Modules


Module is a file containing a set of functions you want to include in your Program.

To create a module you have to save your file with the extension .py.



Q.2 Which of the following statements is true about Recursion?

 i. Every recursive function must have a base case.

 ii. Infinite recursion may occur if the base case is not properly mentioned.

 iii. A recursive function makes the code easier to understand.

 iv. Every recursive function must have a return value


Answer-:     i,ii,iii


Q.3 What will be the output of the following code:


def fun(n):
 if n>100:
       return n-5 return fun(fun(n+11))
print(fun(45))

1. 50 ii.100 iii.74 iv. infinite loop


Answer-: 

100

For Full Explanation Comment Below

Q.4 Which of the following keywords marks the beginning of the function block?
A. fun
B. define
C. def
D. function


Answer-:

 def 


Q.5 What is the output of the following code snippet? 


def func(message, num=1):
     print(message * num)

func('Welcome')
func('Viewers', 3)


A. Welcome
Viewers
B. Welcome
ViewersViewersViewers
C. Welcome
Viewers,Viewers,Viewers
D. Welcome

Answer-:  

B. Welcome
ViewersViewersViewers


Q.6 What is the output of the following code snippet? 


def func(x = 1, y = 2):
 x = x + y
 y += 1 print(x, y)
func(y = 2, x = 1)

A. 1 3
B. 2 3
C. The program has a run-time error because x and y are not defined.
D. 3 2
E. 3 3

Answer-: 

3 3


Q.7 What is the output of the following code snippet? 


num = 1def func():
 num = num + 3 print(num)
func()
print(num)


A. 1 4
B. 4 1
C. The program has a run-time error because the local variable ‘num’ referenced before assignment. D. 1 1
E. 4 4


Answer-:

C. The program has a run-time error because the local variable ‘num’ referenced before assignment.


Q.8 Write a Python function that accepts a string and calculate the number of upper case letters and lower case letters 

Sample String : 'The quick Brow Fox' 

Expected Output : 

No. of Upper case characters : 3 

No. of Lower case Characters : 12 


Answer-:

def up(u):
    global x
    x=len(u)
    c=0    for i in range (0,x,1):
         if u[i].isupper():
               c=c+1    return c
def low(u):
    ct = 0    for j in range(0, x, 1):
        if u[j].islower():
            ct = ct + 1    return ct
p=input("Enter the String : ")
print("Number of upper case are : ",up(p))
print("Number of upper case are : ",low(p))



Output-:


Enter the String : The quick Brow Fox

Number of upper case are :  3
Number of upper case are :  12

Process finished with exit code 0

Screenshot of the Program





comment for full solution