MENU

Chapter 11 Conditional and Looping Constructs Solutions

Question - 21 : -
What do you mean by “for loop” ?

Answer - 21 : -

A for loop is à Python statement which repeats a group of statements a specified number of times. You can use any object (such as strings, arrays, lists, tuples, diet and so on) in a for loop in Python.

Question - 22 : -
Explain If…else statements

Answer - 22 : -

If…else statements
An else statement can be combined with an if statement. An else statement contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a false value.
The else statement is an optional statement and there could be at most only one else statement following if
Syntax:
The syntax of the if…else statement is :
if expression :
statement(s)
else:
statement(s)

Question - 23 : -
What do you mean by decision making ?

Answer - 23 : -

Structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be- true, and optionally, other statements to be executed if the condition is determined to be false.

Question - 24 : -
Create a function addNumbers(x) that takes a number as an argument and adds all the integers between 1 and the number (inclusive) and returns the total number.

Answer - 24 : -

def add Numbers (num):
total = 0
i = 1
while i< = num:
total + = i
i+ = 1
return total

Question - 25 : -
Create a function addNumbers(start, end) that adds all the integers between the start and end value (inclusive) and returns the total sum.

Answer - 25 : -

def addNumbers(start, end):
total = 0
i = start
while start < = end:
total + = start
start + = 1
return total

Question - 26 : -
Create a function countPages(x) that takes the number of pages of a book as an argument and counts the number of times the digit ‘1’ appears in the page number.

Answer - 26 : -

def countPages(num):
total = 0
i = 1
while i<=num:
page_no = str(i)
total + = page_no.count(‘1’)
i+ = l
return total

Question - 27 : -
Create a function factorial(x) that takes an integer and returns the product of all the positive integers less than or equal to n.

Answer - 27 : -

def factorial(num):
product =1
i = num
while i > 0:
product *=i
i-=l
return product

Question - 28 : -
Create a function doubleFactorial(n) that takes an odd integer and returns the product of all odd values upto the value n (where n=2k-l).

Answer - 28 : -

def doubleFactorial(num):
product = 1
i = 0
k = 0
while k
k = 2*i+l
product * = k
i += 1
return product

Question - 29 : -
Create a function that computes the approximation of pi, based on the number of iterations specified, pi can be computed by 4*(l-l/3+l/5-l/7+l/9-…).

Answer - 29 : -

def piApprox(num):
i = 1
pi = 0
while i<=num:
#set ‘while’ termination condition
pi +=((4/float(2*i-l)n-l)**(i+l))
#compute the ith term of the series
i+=l    # update i
return pi

Question - 30 : -
What gets printed?
country_counter = {}
def addone(country):
if country in country_counter:
country_counter[country] + = 1
else:
country_counter[country] = 1
addone(‘China’)
addone(‘Japan’)
addone(‘china’)
print len(country_counter)

Answer - 30 : - 3

Free - Previous Years Question Papers
Any questions? Ask us!
×