Class Test 6 Questions With Answers
Class Test 6 Questions With Answers
The process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called as recursive function. Using recursive algorithm, certain problems
can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH),
Inorder/Preorder/Postorder Tree Traversals, Depth First Search (DFS) of Graph, etc
2 Name the two types of iterative statements supported by Python. (Nov/Dec 2022)
For Statement
While statement
3 Define string slicing. Give an example(Nov/Dec 2020)
String Slices : A segment of a string is called string slice, selecting a slice is similar to selecting a
character.
Eg:>>> s ='Monty Python'
>>> print s[0:5] Monty
>>> print s[6:12] Python
4 Write a program to display a set of strings using range( ) function
5 Do loop Statements have else clause? When it will be executed? (Nov/Dec 2019)
Loop statements may have an else clause, it is executed when the loop terminates through
exhaustion of the list (with for) or when the condition becomes false (with while), but not when the
loop is terminated by a break statement.
6 Write a program to display a set strings using range ( ) function? (Nov/Dec 2019)
for i in range(len(a)):
print(i,a[i])
The statements inside the while loop is executed only if the condition is evaluated to true.
Syntax:
While condition
statement
Example:
# Program to add natural numbers upto, sum = 1+2+3+...+10
n = 10
# initialize sum and counter
sum = 0
i=1
while i <= n:
sum = sum + i
i = i+1 # update counter
9 Comment with an example on the use of local and global variable with same identifier name
(Apr/May 2019)
The scope of a variable refers to the places that you can see or access a variable. If we define a
variable on the top of the script or module, the variable is called global variable. The variables that
are defined inside a class or function is called local variable.
10 Write a Python program to GCD of two number and print the results (Jan 2018)
11 Write a Python program to compute the power of a number and print the results (Jan 2018)
Sometimes there are more than two possibilities and we need more than two branches. One way to
express a computation like that is a chained conditional:
Eg:
if x < y:
print 'x is less than y'
elif x > y:
print 'x is greater than y'
else: print 'x and y are equal'
elif is an abbreviation of “else if.” Again, exactly one branch will be executed. There is no limit on
the number of elif statements. If there is an else clause, it has to be at the end, but there doesn’t
have to be one.
Using a pass statement is an explicit way of telling the interpreter to do nothing. Example:def bar():
pass If the function bar() is called, it does absolutely nothing.
1. Outline the loop, break and continue statement in Python with an example (Nov/Dec 2022)
2. Write a python program in linear search binary search and its details.
3. Write the python program to find the factorial of a given number without recursion and with
recursion (Jan 2018).
6. Write a python program to generate all permutations of a string using built in function (Nov/Dec
2019)