PRIP 1.3 Sumita Arora Solutions | Class 12 Computer Science

Here are PRIP 1.3 Sumita Arora Solutions for class 12 Computer Science. To view chapter 1 lecture videos, visit here.

To view Sumita Arora solutions for all chapters, visit here.

Q.1: What is the output of the following python program? First try and predict the output without the computer. Check your answer by typing it in and running it
i = 0 
while i < 6:
    j = 0 
    while j<i:
        print("*", end =' ') 
        j=j+1 
    i=i+1 
print()

Answer:

Code:
i = 0 
while i < 6:
    j = 0 
    while j<i:
        print("*", end =' ') 
        j=j+1 #increment j by 1
    i=i+1   # increment i by 1
print() #to print next line  


Output:
* * * * * * * * * * * * * * * 


Explanation:
# end = ' '  will add space after *
# outer while loop will execute till i becomes equal to 6 
# with each while loop execution i will be incremented by one
# with each loop i will increment like 0,1,2,3,4,5 when i=6 it will stop executing while loop
# in while loop j is initialized to 0
# i.e. each time when outer while loop executes j will be initialized to zero
# inner while loop will execute till j is less than i
# when i=0 j=0 as j is not less than i print statement will not execute
# when i=1 j=0 as j is less than i print statement will execute only once 
# as in next iteration j=1 and i=1 i.e. print will not be executed 
# when i=2 j=0 print statement will execute 2 time
# when i=3 j=0 print statement will execute 3 time
# when i=4 j=0 print statement will execute 4 time
# when i=5 j=0 print statement will execute 5 time
# hence total number of stars will be 15
Q.2: What is the output of the following program?   
x= 7
y=8
if x < 7 or x <= 10 and y > 8:
    print("IT IS!")
else:
    print("Oh No")

Answer:

Code:
x= 7
y=8
if x < 7 or x <= 10 and y > 8: 
    print("IT IS!")
else:
    print("Oh No")


Output:
Oh No


Explanation:
= False or True and False
= False or False
= False
# Hence else part will get executed
Q.3: What is the output of the program?
score = 40
while (score > 1):
     score = score/2 - 1 
     print(score, end =' ')

Answer:

Code:
score = 40
while (score > 1):
     score = score/2 - 1 
     print(score, end =' ')


Output:
19.0 8.5 3.25 0.625

 
Explanation:
# initially score =40
# as 40 > 1 while loop will start executing
score  = 40/2 - 1  = 20.0 - 1 = 19.0
again condition for while loop will be tested
# as 19.0>1 while loop will be executed
score  = 19.0/2 = 9.5 - 1 = 8.5
# as 8.5>1 while loop will be executed
score  = 8.5/2 = 4.25 - 1 = 3.25
# as 3.25>1 while loop will be executed
score  = 3.25/2 = 1.625 - 1 = 0.625
# as 0.625<1 execution of while loop will be stopped
Q.4: Consider the following code:
if x > 3:
    if x <= 5:
        y = 1 
    elif x = 6:
        y = 2
    else:
        y=3
else:
    y = 4

If y has the value 2 after executing the above program fragment, then what do you know about the initial value of x ?

Answer:

Code:
x =  6 #consider
if x > 3:
    if x <= 5:
        y = 1 
    elif x == 6:
        y = 2
    else:
        y=3
else:
    y = 4
print("y : {}".format(y))


Output:
y : 2


Explanation:
As given in question after execution y will be 2
hence elif x == 6: has to be true so that y will be assigned to value 2
hence value of initial x will be 6
Q.5:  Consider the following two fragments:
If  x == 5:
    x=x+1
else:
    x = 8 

if x == 5:
     x=x+1
if x != 5:
    x = 8

Are the two fragments logically equivalent ? Why or why not?

Answer:

Code:
x = 5
if  x == 5:
    x=x+1
else:
    x = 8 
print("x : {}".format(x))

x = 5
if x == 5:
    x=x+1
if x != 5:
    x = 8
print("x : {}".format(x))


output:
x : 6
x : 8


Explanation:
# if we set value of x = 5 
# in 1st code snippet only one part will be executed, either if or else.
# in 2nd code snippet both if conditions will be checked.
# in 1st code snippet if statement will be true so, x will be 6 and else block will not get    executed 
# in 2nd code snippet 1st if block will get executed and x will be incremented by 1 i.e. x=6
# for 2nd if block condition will be true as x = 6 is not equal to 5 hence x will be assigned to new value 8
# Hence two fragments are not logically equivalent
Q.6: Write a program that prints a table on two columns: on the left the integer temperatures between 0 and 100 (Fahrenheit) and in the right column the corresponding Celsius values.

Answer:

Code:
print(" Fahrenheit |  Celsius")
fahr = 0
while fahr<=100:
    celsius = 5/9 * ( fahr - 32 ) 
    print("       {}    | {}    ".format(fahr,celsius))
    fahr = fahr+1


Explanation:
formula to convert fahrenheit to celsius 
celsius = ( fahr - 32 )*(5/9) 
1. calculate corresponding celsius value for Fahrenheit
2. print both values
3. increment Fahrenheit value by one 


Output:
Fahrenheit |  Celsius
       0    | -17.77777777777778    
       1    | -17.22222222222222    
       2    | -16.666666666666668    
       3    | -16.11111111111111    
       4    | -15.555555555555557    
       5    | -15.0    
       6    | -14.444444444444445    
       7    | -13.88888888888889    
       8    | -13.333333333333334    
       9    | -12.777777777777779    
       10    | -12.222222222222223    
       11    | -11.666666666666668    
       12    | -11.11111111111111    
       13    | -10.555555555555555    
       14    | -10.0    
       15    | -9.444444444444445    
       16    | -8.88888888888889    
       17    | -8.333333333333334    
       18    | -7.777777777777779    
       19    | -7.222222222222222    
       20    | -6.666666666666667    
       21    | -6.111111111111112    
       22    | -5.555555555555555    
       23    | -5.0    
       24    | -4.444444444444445    
       25    | -3.8888888888888893    
       26    | -3.3333333333333335    
       27    | -2.7777777777777777    
       28    | -2.2222222222222223    
       29    | -1.6666666666666667    
       30    | -1.1111111111111112    
       31    | -0.5555555555555556    
       32    | 0.0    
       33    | 0.5555555555555556    
       34    | 1.1111111111111112    
       35    | 1.6666666666666667    
       36    | 2.2222222222222223    
       37    | 2.7777777777777777    
       38    | 3.3333333333333335    
       39    | 3.8888888888888893    
       40    | 4.444444444444445    
       41    | 5.0    
       42    | 5.555555555555555    
       43    | 6.111111111111112    
       44    | 6.666666666666667    
       45    | 7.222222222222222    
       46    | 7.777777777777779    
       47    | 8.333333333333334    
       48    | 8.88888888888889    
       49    | 9.444444444444445    
       50    | 10.0    
       51    | 10.555555555555555    
       52    | 11.11111111111111    
       53    | 11.666666666666668    
       54    | 12.222222222222223    
       55    | 12.777777777777779    
       56    | 13.333333333333334    
       57    | 13.88888888888889    
       58    | 14.444444444444445    
       59    | 15.0    
       60    | 15.555555555555557    
       61    | 16.11111111111111    
       62    | 16.666666666666668    
       63    | 17.22222222222222    
       64    | 17.77777777777778    
       65    | 18.333333333333336    
       66    | 18.88888888888889    
       67    | 19.444444444444446    
       68    | 20.0    
       69    | 20.555555555555557    
       70    | 21.11111111111111    
       71    | 21.666666666666668    
       72    | 22.22222222222222    
       73    | 22.77777777777778    
       74    | 23.333333333333336    
       75    | 23.88888888888889    
       76    | 24.444444444444446    
       77    | 25.0    
       78    | 25.555555555555557    
       79    | 26.11111111111111    
       80    | 26.666666666666668    
       81    | 27.222222222222225    
       82    | 27.77777777777778    
       83    | 28.333333333333336    
       84    | 28.88888888888889    
       85    | 29.444444444444446    
       86    | 30.0    
       87    | 30.555555555555557    
       88    | 31.111111111111114    
       89    | 31.666666666666668    
       90    | 32.22222222222222    
       91    | 32.77777777777778    
       92    | 33.333333333333336    
       93    | 33.88888888888889    
       94    | 34.44444444444444    
       95    | 35.0    
       96    | 35.55555555555556    
       97    | 36.111111111111114    
       98    | 36.66666666666667    
       99    | 37.22222222222222    
       100    | 37.77777777777778 
Q.7: Write a program to print the following pattern:
* * * * * * * * 
            *   
          *     
        *       
      *         
    *           
  *             
* * * * * * * *

Answer:

Code:
n = 8
for i in range(0,n):
    for j in range(0,n):
        if(i == n-1-j or i == 0 or i == n-1):
            print('*', end=' ')
        else:
            print(' ', end=' ') 
    print()#to print next line


Output:

* * * * * * * * 
            *   
          *     
        *       
      *         
    *           
  *             
* * * * * * * *

    
Explanation: 
| 0,0 | 0,1 | 0,2 | 0,3 |
| 1,0 | 1,1 | 1,2 | 1,3 |
| 2,0 | 2,1 | 2,2 | 2,3 |
| 3,0 | 3,1 | 3,2 | 3,3 |

above is the 4X4 matrix
each cell consist of 2 values i,j respectively
to print intended pattern 
cells which follows pattern i = n-1-j or i=0 or i=n-1
should print '*' 
and remaining cells should print ' '(empty space)

two nested for loops are created to increment 
value of i and j respectively from range 0 to n-1

with each pair of i,j 
if condition is checked 
if any condition is true * will be printed
else empty space will be printed
Q.8: Write programs that generate each of the patterns given below:
a)
* * * * * * * * * * 
* * * * * * * * *   
* * * * * * * *     
* * * * * * *       
* * * * * *         
* * * * *           
* * * *             
* * *               
* *                 
*   

Answer:

Code:
n=10
for i in range(0,n):
    for j in range(0,n):
        if(i <= n-1-j):
            print('*', end=' ')
        else:
            print(' ', end=' ') 
    print()#to print next line


Output:
* * * * * * * * * * 
* * * * * * * * *   
* * * * * * * *     
* * * * * * *       
* * * * * *         
* * * * *           
* * * *             
* * *               
* *                 
*     


Explanation:
| 0,0 | 0,1 | 0,2 | 0,3 |
| 1,0 | 1,1 | 1,2 | 1,3 |
| 2,0 | 2,1 | 2,2 | 2,3 |
| 3,0 | 3,1 | 3,2 | 3,3 |

above is the 4X4 matrix
each cell consist of 2 values i, j respectively
to print intended pattern 
cells which follows pattern i <= n-1-j 
should print '*' 
and remaining cells should print ' '(empty space)

two nested for loops are created to increment 
value of i and j respectively from range 0 to n-1

with each pair of i,j 
if condition is checked 
if any condition is true * will be printed
else empty space will be printed
Q.8: Write programs that generate each of the patterns given below:
b)
          * * * * * 
          * * * * * 
          * * * * * 
          * * * * * 
          * * * * * 
* * * * *           
* * * * *           
* * * * *           
* * * * *           
* * * * *

Answer:

Code:
n=10
for i in range(0,n):
    for j in range(0,n):
        if((i <= (n+1)/2 - 1 and j > (n+1)/2 - 1) or (i > (n+1)/2 - 1 and j <= (n+1)/2 - 1)):
            print('*', end=' ')
        else:
            print(' ', end=' ') 
    print()#to print next line


Output:
          * * * * * 
          * * * * * 
          * * * * * 
          * * * * * 
          * * * * * 
* * * * *           
* * * * *           
* * * * *           
* * * * *           
* * * * *


Explanation:
| 0,0 | 0,1 | 0,2 | 0,3 |
| 1,0 | 1,1 | 1,2 | 1,3 |
| 2,0 | 2,1 | 2,2 | 2,3 |
| 3,0 | 3,1 | 3,2 | 3,3 |

above is the 4X4 matrix
each cell consist of 2 values i,j respectively
to print intended pattern 
cells which follows pattern
(i <= (n+1)/2 - 1 and j > (n+1)/2 - 1) 
or 
(i > (n+1)/2 - 1 and j <= (n+1)/2 - 1)) 
should print '*' 
and remaining cells should print ' '(empty space)

two nested for loops are created to increment 
value of i and j respectively from range 0 to n-1

with each pair of i,j 
if condition is checked 
if any condition is true * will be printed
else empty space will be printed
Q.8: Write programs that generate each of the patterns given below:
c)
* * * * * * * * * *
* * * *     * * * * 
* * *         * * * 
* *             * * 
*                 * 
*                 * 
* *             * * 
* * *         * * * 
* * * *     * * * * 
* * * * * * * * * *

Answer:

Code:
n=10
half = int((n/2))
# for upper part of pattern
for i in range(1,half+1):
    for j in range(i,half+1): # this code will print upper left part of pattern
        print("*", end = ' ')

    for j in range(1,2*i-1):# this code fills the unwanted part with space
        print(" ",end = ' ')
        
    for j in range(i,half+1):
        print("*", end = ' ') # this code will print upper right part of pattern
    print();

    
# loop for lower half part  of the pattern
for i in range(1,half+1):
    for j in range(1,i+1): # this code will print lower left part of pattern
        print("*", end = ' ')

    for j in range(2*i-2,2*half-2):# this code fills the unwanted part with space
        print(" ",end = ' ')
        
    for j in range(1,i+1):
        print("*", end = ' ') # this code will print lower right part of pattern
    print();
   
 
Output:
* * * * * * * * * * 
* * * *     * * * * 
* * *         * * * 
* *             * * 
*                 * 
*                 * 
* *             * * 
* * *         * * * 
* * * *     * * * * 
* * * * * * * * * *

Clear Doubts with Computer Tutor
In case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You cannot copy content of this page