Flow of Control || 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 (input"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. 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 ()





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





Q14. 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")


 

 

 

Q15. Find the error. Consider the following program:

 

a = int(input("Enter a value: "))

while a != 0:

    count = count + 1

    a = int(input("Enter a value")

print ("You entered", count, "value" )

 

It is supposed to count the number of values entered by the user until the user enters and then display the count (not including the 0). However, when the program is run, it crashes with the following error message after the first input value is read :

 

Enter a value: 14

Traceback (most recent call last):

  File "count.py", line 4, in <module>

    count = count + 1

NameError: name 'count' is not defined

>>> 

What change should be made to this program so that it will run correctly? Write the modification that is needed into the program above, crossing out any code that should be removed and clearly indicating where any new code should be inserted.

7 Comments

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

  1. Thanks a lot for the one who is extending his/her support to us in this form
    Really I appreciate you for your aid

    ReplyDelete
  2. I can find only question where are solutions

    ReplyDelete
    Replies
    1. Click on question then you will get solutions of that question.

      Delete
  3. really helpful with the tricky questions...don't have to type in IDLE for everything now thank you

    ReplyDelete
  4. Thank you for this help your solving this b ques
    Really thank you so much for you help respected path walla

    ReplyDelete
  5. Can you explain little bit more ques 3 type B

    ReplyDelete

Post a Comment

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

Previous Post Next Post