Page 217 - CA_Blue( J )_Class10
P. 217

3.  Predict the output:
                                                                                   i       j        k        output
                      int i,j,k=0;
                                                                                   1       1        1          012
                      for(i=1;i<=3;i++)
                                                                                           3        2          345
                      {  for(j=1;j<=5;j=j+2)
                                                                                           5        3          678
                           { System.out.print(k++);}
                                                                                   2       1        4
                      System.out.println();
                                                                                           3        5
                      }
                                                                                           5        6
                   Ans.  012                                                       3       1        7
                       345                                                                 3        8
                       678                                                                 5        9
                    4.  How many times the loop will execute? What will be the output?
                       int a=3, b=2;
                       for (; a>1; a--)
                       {for (; b>1; b--)
                         {System. out. print (a + b);}
                       System.out.println();
                       }
                   Ans.  5
                        The loop executes 2 times.                                           a         b        Output
                                                                                             3         2           5
                                                                                             2         1>1x
                    5.  Predict the output:                                                  1>1x
                       int i,k=3;
                               while(k>1)                                                    k         i        Output
                               {
                                                                                             3         1          1233
                                   for(i=1;i<=3;i++)
                                                                                                       2          1232
                                   { System.out.print(i);}                                             3
                                   System.out.println(k--);
                                                                                             2         1
                               }
                   Ans.   1233                                                                         2
                       1232                                                                            3
                 D.  Assertion and Reasoning based questions.
                       The following questions consist of two statements – Assertion (A) and Reason (R). Answer these questions by selecting the
                        appropriate option given below:
                        a.  Both A and R are true, and R is the correct explanation of A.
                        b.  Both A and R are true, but R is not the correct explanation of A.
                        c.  A is true, but R is false.
                        d.  A is false, but R is true.
                    1.  for (int i = 1; i <= 3; i++)
                        {
                            for (int j = 1; j <= 4; j++)
                                {
                                System.out.println("i: " + i + ", j: " + j);
                                }
                        }
                        Assertion (A): The total number of iterations of the inner loop will be the product of the number of iterations of the outer loop
                        and the inner loop .
                        Reason (R): The inner loop runs 5 times, and the outer loop runs only once.
                    2.  for (int i = 0; i <= 1; i++)
                        {
                            for (int j = 0; j < 5; j++)
                            {

                                                                                                                       215
                                                                                                         Nested Loop   215
   212   213   214   215   216   217   218   219   220   221   222