Q. 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)


Answer =


(a)

Output:-


Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello

>>>


(b)

Output: -


10 0
9 1
8 2
7 3
6 4

>>>


(c)

Output: -


100
90
80
70
60
50

>>>


(d)

Output: -


45
45
45
45
45
45
45
45
45 infinite times.


(e)

Output: -


1
2
3
4
5

>>>


(f)

Output: -


0
1
2
3
4

>>>


(g)

Output: -


1
2
3
4
5
6
7
8
9

>>> 


(h)

Output: -


100
90
80
70
60

>>>


(i)

Output: -


-500
-400
-300
-200
-100
0
100
200
300
400

>>> 


(j)


It will give no output.
After some changes in code like:-
 
for y in range (500, 100, -100) :
    print ("*", y)
 
This code gives the output: -
 
* 500
* 400
* 300
* 200
>>>


(k)


It will give no output because the precedence of the * operator is more than the - operator.
So for value in the range becomes zero after solving.


(l)

Output: -


1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6

>>>


(m)

Output: -


0 0 0
0 1 1
0 2 2
0 3 3
1 0 1
1 1 2
1 2 3
1 3 4
2 0 2
2 1 3
2 2 4
2 3 5

>>> 


(n)

Output: -


50
>>> 


13 Comments

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

Post a Comment

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

Previous Post Next Post