Join Regular Classroom : Visit ClassroomTech

Python Interview Questions II | Crack With CodeWindow | Codewindow.in

16. What is a Nested loop?

In simple terms, a Loop within another loop is called a Nested loop
Example:

for i in range (4):
    for j in range (i):
        print("CodeWindow")
Output:
CodeWindow
CodeWindow
CodeWindow
CodeWindow
CodeWindow
CodeWindow

17. What is break, continue and pass?

Just like in Language C, break statement is used to terminate the current loop and resumes execution what comes next to the loop in python.

for letter in 'code':     
   if letter == 'd':
      break
   print('Current Letter :', letter)
Output:
Current Letter : c
Current Letter : o

18. Can we use else with for loop and while loop?

Yes we can, for a short example – we can use the else statement as a condition of not using the break statement
Example:

for i in range(1, 10):
    print(i)
else:  # Executed because no break in for loop
    print("No Break")

19. What is dangling else problem?

Problem encountered with nesting of if-else statement which is created when there is no matching else for every if statement. This is solved in python by indenting the the nested if statements. 
for loop The Mechanism used to repeat a task until a particular condition is True. The for loop is usually known as a determinate or definite loop because the programmer knows exactly how many times the loop will repeat.

20. What is Function?

A Function is a block of code that is executed only when it is called usually written within the ‘def’ keyword in python. It is mostly used because it can be called multiple times and hence help to reduce the code.
Example:

def call_func(name):
    """
    Statement of function
    """
    print("Hello, " + name + ". Its codewindow here")

call_func('Sir')

C QA

Mostly Asked

DS QA

Mostly Asked

DBMS QA

Mostly Asked

ML QA

Mostly Asked

21. What is a Module?

A module is a file containing a set of python codes with variables, classes, function in a executable format.
For example:
codewindow.py is a module and the name of the module is ‘codewindow’

22. What is the difference between Global Variable and Local variable?

A variable that can be accessible globally or in other terms can be used through the program without any hesitation is called a global variable.
A local variable is one that which can only be accessed to the current scope, such as temporary variables used in a single function definition.

23. What is default argument in Python?

In this syntax, the default values is specified (value2value3, …) for each parameter using the assignment operator (=).

When a function is called and an argument is passed to the parameter that has a default value, the function will use that argument instead of the default value.

If you don’t pass the argument, the default value will be used function.

To use default parameters, you need to place parameters with the default values after other parameters. Otherwise, a syntax error will occur.

def function_name(param1, param2=value2, param3=value3, ...):

24. What is lambda function or anonymous function?

Basically a single-line function declared with no name which can only take one expression but can have many arguments

x = lambda a, b : a * b
print(x(6, 7))
Output:
42

25. What is recursive function?

In simple terms, a self calling function that has a terminating condition and follows a stack order is called a recursive function.

def fa(x):
    """
    This is a recursive function
    to find the factorial of an integer
    """

    if x == 1:
        return 1
    else:
        return (x * fa(x-1))


num = 5
print("Factorial of", num, "is", fa(num))
Output:
Factorial of 5 is 120

26. Write a program in python to execute Factorial using recursion.

def fa(x):
    """
    This is a recursive function
    to find the factorial of an integer
    """

    if x == 1:
        return 1
    else:
        return (x * fa(x-1))


num = 5
print("Factorial of", num, "is", fa(num))
Output:
Factorial of 5 is 120

27. Write a program in python to execute GCD using recursion.

def gcd(a,b):
    if(b==0):
        return a
    else:
        return gcd(b,a%b)
  
a = 60
b= 48
  
print ("The gcd of 60 and 48 is : ",end="")
print (gcd(a,b))
Output:
The gcd of 60 and 48 is : 12

28. Write a program in python to execute Fibonacci series using recursion.

def fibo(n):
   if n <= 1:
       return n
   else:
       return(fibo(n-1) + fibo(n-2))

terms = 10

# check if the number of terms is valid
if terms <= 0:
   print("positive integer please")
else:
   print("Fibonacci sequence - ")
   for i in range(terms):
       print(fibo(i))
Output:
Fibonacci sequence - 
0
1
1
2
3
5
8
13
21
34

29. Write a program in python to find exponents using recursion.

def power(N, P):
  
    # if power is 0 then return 1
    if P == 0:
        return 1
      
    # if power is 1 then number is
    # returned
    elif P == 1:
        return N
      
    else:
        return (N*power(N, P-1))
  
# Driver program
N = 5
P = 2
  
print(power(N, P))
Output:
25

30. Difference between Recursion and Iteration in python?

When a loop repeatedly repeats itself it it knows as as iteration, the basic difference is that recursion is applied to a function whereas iteration is for a set of instructions which we want to be repeated.

  • Recursion uses selection structure.

  • Iteration uses repetition structure

  • Iteration uses less memory than Recursion

Categories
Pages
Recent Posts