Conditional and Iterative Statements || Type B || Sumita Arora || Class 11 || Computer science || Information practices || Solution



 Q1. Rewrite the following code fragment that saves on the number of comparisons:

if (a == 0) :
    print ("Zero")
if (a == 1) :
    print("One")
if (a == 2) :
    print ("Two")
if (a == 3) :
    print ("Three")






Q2. Under what conditions will this code fragment print "water"?

if temp < 32 :
    print ("ice")
elif temp < 212 :
    print ("water")
else :
    print ("steam")



 


Q3. What is the output produced by the following code?

x = 1
if x > 3 :
    if x > 4 :
        print ("A", end = ' ')
    else :
        print ("B", end = ' ')
elif x < 2:
    if (x != 0) :
        print ("C", end = ' ')
print ("D")






Q4. What is the error in following code? Correct the code:

weather = 'raining'
if weather = 'sunny' :
    print ("wear sunblock")
elif weather = "snow" :
    print ("going skiing")
else :
    print (weather)






Q5. What is the output of the following lines of code?

if int('zero') == 0 :
    print ("zero")
elif str(0) == 'zero' :
    print (0)
elif str(0) == '0' :
    print (str(0))
else :
    print ("none of the abo
ve")





Q6. Find the errors in the code given below and correct the code:

if n == 0
    print ("zero")
elif :n == 1
    print ('one')
elif
    n == 2:
    print("two")
else n == 3 :
    print ("three")






Q7. What is following code doing? What would it print for input as 3?

n = int (route "Enter an integer : "))
if n < 1 :
    print ("invalid value")
else :
    for i in range (1, n + 1) :
        print (i * i)






Q8. How are following two code fragments different from one another? Also, predict the output of following code fragments:

(a)
n = int (input ("Enter an integer : "))
if n > 0 :
    for a in range(1, n + n) :
        print (a / (n / 2))
    else :
        print ("Now quitting")

(b)
n = int (input( "Enter an integer :"))
if n > 0 :
    for a in range(1, n + n) :
        print (a / (n / 2))
    else:
        print ("Now quitting")


 



Q9. Rewrite the following code fragments using for loop:

(a)
i = 10
while (i > 0) :
    print (i)
    i -= 3

(b)
while num > 0 :
    print (num % 10)
    num = num / 10

(c)
while (num > 0) :
    count += 1
    sum += num
        num -= 2
        if count == 10 :
            print ( sum / float(count))
            break






Q10. Rewrite following code fragments using while loops:

(a)
min = 0
max = num
if num < 0 :
    min = num
    max = 0 # compute sum of integers from min to max
    for i in range (min, max + 1) :
        sum += i
        

(b)
for i in range(1, 16) :
    if i % 3 == 0 :
        print (i)

(c)
for i in range(4) :
    for j in range(5) :
        if i +1 == j or j + i == 4 :
            print ("+", end = " ")
    else :
        print ("o", end = " ")
print ()






Q11. Predict the output of the following code fragments:

(a)
count = 0
while count < 10 :
    print ("Hello")
    count += 1

(b)
x = 10
y = 0
while x > y :
    print (x, y)
    x = x - 1
    y += 1

(c)
keepgoing = True
x = 100
while keepgoing :
    print (x)
    x = x - 10
    if x < 50 :
        keepgoing = False

(d)
x = 45
while x < 50 :
    print (x)

(e)
for x in [1,2,3,4,5] :
    print (x)

(f)
for x in range (5) :
    print (x)

(g)
for p in range(1, 10) :
    print (p)

(h)
for q in range (100, 50, -10) :
    print (q)

(i)
for z in range (-500, 500, 100) :
    print (z)

(j)
for y in range (500, 100, 100) :
    print ("*", y)

(k)
x = 10
y = 5
for i in range (x - y * 2) :
    print ("%", i)

(l)
for x in [1, 2, 3] :
    for y in [4, 5, 6] :
        print (x, y)

(m)
for x in range(3) :
    for y in range (4) :
        print (x, y, x + y)
    
(n)
c = 0
for x in range (10) :
    for y in range (5) :
        c += 1
print (c)






Q12. Which of the following Python programs implement the control flow graph shown?

 

a)
while True :
    n == int (input("Enter an int : "))
    if == A1 :
        continue
    elif n == A2 :
        break
    else :
        print ("what")
else :
    print ("ever")

(b)
while True :
    n = int ( input ( "Enter an int : "))
    if n == A1 :
        continue
    elif :
        n == A2 :
            break
    else :
        print ("what")
print ("ever")

(c)
while True :
    n == int ( input ("Enter an int : "))
    if n == A1 :
        continue
    elif n == A2 :
        break
print ("what")
print ("ever")





Q13. What is the output of the following code?

for i in range (4) :
    for j in range (5) :
        if i + 1 == j or j + i == 4 :
            print ("+", end = ' ')
        else :
            print ("o", end = ' ')
print ()





Q14. In the nested for loop code above (Q. 13), how many times is the condition of the if clause evaluated?




Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post