MENU

Chapter 11 Conditional and Looping Constructs Solutions

Question - 31 : -
What gets printed?
namesl = [‘Amir’, ‘Barry’, ‘Chales’, ‘Dao’]
if ‘amir’ in namesl:
print 1
else:
print 2

Answer - 31 : - 2

Question - 32 : -
What gets printed?
x = 0
y = l
a = cmp(x,y)
if a < x:
print “a”
elif a = = x:
print “b”
else:
print “c”

Answer - 32 : - a

Question - 33 : -
Explain Flow Diagram of “for loop”

Answer - 33 : -

If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to the iterating variable iterating_var. Next, the statements bloc is executed. Each item in the list is assigned t iterating_var, and the statements(s) block is executed until the entire sequence is exhausted.

Question - 34 : -
Write an example to illustrates the combination of an else statement with a for statement

Answer - 34 : -

The following example illustrates the combination of an else statement with a for statement that searches for prime numbers from 10 through 20.
# !/usr/bin/python
for num in range(10,20):
#to iterate between 10 to 20
for i in range(2,num):
#to iterate on the factors of the number
if num%i == 0:
#to determine the first factor
j=num/i
#to calculate the second factor
print ‘%d equals %d * %d’ % (num,i,j)
break
#to move to the next number, the
#first for
else:
# else part of the loop
print num, ‘is a prime number’
When the above code is executed, it produces  following result :
10 equals 2*5
11 is a prime number
12 equals 2*6
13 is a prime number
14 equals 2*7
15 equals 3*5
16 equals 2*8
17 is a prime number
18 equals 2*9
19 is a prime number

Question - 35 : -
Write a program to explain “for loop”

Answer - 35 : -

#!/usr/bin/python
for letter in ‘Python’:    # First Example
print ‘Current Letter :’, letter
fruits = [‘banana’, ‘apple’, ‘mango’]
for fruit in fruits:    # Second Example
print ‘Current fruit :’, fruit
print “Good bye!”
When the above code is executed, it produces following result:
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!

Question - 36 : -
Create a function that takes in a positive integer and return a list of prime numbers. A prime number is only divisible by 1 and itself.

Answer - 36 : -

def primeNumbers(num):
primes = [ ]
i = 2
# iterates through range from 2 to
num(inclusive)
while i< =num:
# add ‘while’ condition
k = 2
is Prime = True
# check if prime number
while k
if i%k==0:
isPrime = False
k+ = l   # update k
if isPrime = =True:
primes.append(i)
i+ = l # update i
return primes

Question - 37 : -
Create a function that takes in a positive number and return 2 integers such that the number is between the squares of the 2 integers. It returns the same integer twice if the number is a square of an integer.

Answer - 37 : -

import math
def sqApprox(num):
i = 0
minsq = 0 # set lower bound
maxsq = math.ceil(num’l’num)
# set upper bound
while i< maxsq :
# set ‘while’ termination condition
if i*i< =num and i>minsq:
# complete inequality condition
minsq = i
if i*i> =num and i
# complete inequality condition
maxsq = i
i+=l
# update i so that ‘while’ will terminate
return (minsq, maxsq)

Question - 38 : -
Given a positive integer, write a function that computes the prime factors that can be multplied together to get back the same integer.

Answer - 38 : -

def primeFactorization(num):
factors = [ ]
lastresult = num
# 1 is a special case
if num == 1:
return [ ]
while True:
if lastresult == 1:
break
c = 2
while True:
if lastresult % c = = 0:
break
c += 1
factors.append(c)
lastresult/= c
return factors

Question - 39 : -
The smallest common multiple of two or more numbers is called the lowest common multiple (LCM). Given a list of integers, find the lowest common multiple.

Answer - 39 : -

def LCM(nums):
nums.sort()
biggest=nums[-l]
multiplier=1
while sum([(multiplier’|,biggest)%num for num in nums])!=0:
multiplier + = 1
return biggest*multiplier

Question - 40 : -
Write a program to explain “if statement”

Answer - 40 : -

# !/usr/bin/python
var1=100
if var1:
print “1-Got a true expression Value”
print var1
else:
print ” 1-Got a false expression value”
print var1 var2=0
if var2 :
print “2-Got a true expression value”
print var2
else :
print “2-Got a false expression value”
print var2
print “Good bye!”
When the above code is executed, it produces following result:
1- Got a true expression value 100
2- Got a false expression value 0
Good bye!

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