Class XII : Informatics Practices : Chapter wise Assignments

Assignments
FLOW OF CONTROL
TYPE A: VERY SHORT ANSWER QUESTION
1. What is null statement? What is its use?
2. What are the three programming constructs that governs statement flow?
3. In a nested-if, how does the default matching of dangling else take place?
4. What is the significance of a break statement in a switch statement? What will happen if a
break is missing in a switch statement?
5. Write one limitation and one advantage of a switch statement?
6. What is the significance of default clause in a switch statement? Can two case labels in a
switch have identical values?               
7. Which elements are needed to control a loop?
8. Write a for loop that displays
(1) the numbers from 51 to 60.
(2) Even numbers from 1 to 100.
(3) Numbers divisible by 5 from 1 to 100.
4) Numbers from 50 to 10. 

9. What is variable? What is meant by a variable’s scope?
10. What does continue statement in a loop constructs, if used?

TYPE B: SHORT ANSWER QUESTIONS
1. Compare the following
(1) if and a ? : Operator.
(2) while and do.. while loop
(3) Entry controlled and Exit controlled loop.
(4) Sentinel and Counter controlled loop.

2. Given the following code fragment:
if (a==0)
System.out.print ln (“Zero”);
if (a==1)
System.out.print ln (“One”);
if (a==2)
System.out.print ln (“Two”);
if (a==3)
System.out.print ln (“Three”);
Write an alternative code that saves on number of comparisons.
3. Rewrite the following fragment using switch:
if (ch== ‘E’)
eastern++ ;
if (ch== ‘W’)
western++ ;
if (ch== ‘N’)
northern++ ;
if (ch== ‘S’)
southern++ ;
else
unknown++ ;
4. Write the syntax and purpose of a switch statement.
5. When does an if statement prove more advantageous over a switch statement?
6. Why is it suggested to put a break statement after the last case statement in a switch even  though it is not needed syntactically?
7. Rewrite the code given in question 3 using switch?



8. Rewrite the following set of if-else statements in terms of switch-case statements :
(a)       char code ;
code = character.readChar();
if (code == ‘A’)
System.out.print ln (“Accountant”);
else if (code == ‘C’ ll code == ‘G’)
System.out.print ln (“Grade IV”);
else if (code == ‘F’)
System.out.print ln (“Financial Advisor”);

(b)       int inputnum, calcval;
If (inputnum == 5) {
calcval = inputnum * 25 – 20 ;
System.out.print ln (inputnum + calcval);
}
else if (inputnum ==10) {
calcval = inputnum * 25 – 20
System.out.print ln (calcval – inputnum);
}
9. How many times are the following loops executed?
(a)       x = 5 ; y = 50 ;                        (b)       int m = 10, n= 7;
while(x <= y) {                                   while(m % n >= 0) {
x = y/x ;                                               . . . . . . .
. . . . . . . . . . . .                                     m = m + 1;
}                                                                       n = n + 2;
. . . . . . . . . . . . . }

10. Given the following code fragment :
i = 100 ;
while (i > 0)
System.out.print ln( I - - );
System.out.print ln(“Thank you”);
Rewrite the above code using a do…while loop.
11. Rewrite the following code using while loop
int sum = 0;
for (int i = 1 ; i <= 5 ; ++i) {
sum = sum + c;
}
12. Rewrite following while loop into a for loop
int stripes = 0;
while (stripes <= 13) {
if (stripes %2 == 2)
{ System.out.print ln(“Colour code Red”);
}          
else {
System.out.print ln(“Colour code Blue”);
}
System.out.print ln(“New Stripe”);
stripes = stripes + 1;
}
13. Rewrite following code using either while or do-while loop or both loops.
for (int i = 1 ; i < 4 ; ++i) {
for (int j = 3 ; j > 0 ; --j) {
System.out.print(“### . .”);
}
System.out.print ln();
}
14. Find the output of the following code fragments ?
(a) int s = 14;                                     (b) int s = 14;
if(s<20)                                               if(s<20)
System.out.print(“Under”);                          System.out.print(“Under”);
Else                                                    else {
System.out.print(“Over”);                             System.out.print(“Over”);
System.out.print ln(“the limit”);                   System.out.print ln(“the limit”);
}
(c) int s = 94;
If (s < 20) {
System.out.print(“Under”);
}
else {
System.out.print(“Over”);CBSE
}
System.out.print ln(“the limit”);
15. What will be the output of the following code fragment when the value of ch is
(a) ‘A’              (b) ‘B’              (c) ‘D’             (d) ‘F’
switch (ch) {
case ‘A’ : System.out.print ln (“Grade A”);
case ‘B’ : System.out.print ln (“Grade B”);
case ‘C’ : System.out.print ln (“Grade C”);
break;
case ‘D’ : System.out.print ln (“Grade D”);
default : System.out.print ln (“Grade F”);
}
16. Predict the input of following code fragment:
(a) int i, j, n;                                                    (b) int i=1, j=0, n=0;
n=0; i=1;                                                                     while (i<4) {
do {                                                                                         for(j=1; j<=I; j++) {
n++; i++;                                                                                 n+=1;
} while (i<=5);                                                                        }
i=i+1;
}
System.out.print ln(n);



 (b) int i=3, n=0;                                                                    (d) int j=1, s=0;
While (i<4) {                                                                                      while(j<10) {
n++; i--;                                                                                                           System.out.print(j+ “+”);
}                                                                                                                       s=s+j;
System.out.print ln(n);                                                                                 J=j+j%3;
}
System.out.print ln(“=”+s);
17. Find out errors if any;
(a) m=1;                                                                                  (b) while(ctr !=10) ; {
n=0;                                                                                                    ctr=1;
for(;m+n<19;++n)                                                                             sum=sum + a;
System.out.print ln(“Hello \n”);                                                       ctr=ctr + 1;
m=m+10;                                                                                            }

(c) for(a=1, a>10; a=a+1)
{ . . . . . . . . . . .
}

18. Identify the possible error(s) in the following code fragment : Discuss the reason(s) of
error(s) and correct the code.
:
f = 1;
for (int a= 40; (a); a--)
f*=a;
:
s=0;
for (int a = 1; a<40/a++)
s += a;
19. Identify the possible error(s) in the following code fragment. Discuss the reason(s) of error(s) and correct the code.
while(i< j)
System.out.println(i * j);
i++ ;
20. Identify the possible error(s) in the following code fragment. Discuss the reason(s) of
error(s) and correct the code.
while (i<j); {
System.out.println (i * j );
i++ ;


}

1 comment: