Page 44 - IP
P. 44

(d)   Anju,  a  beginner  in  java  programming  has  written  following  code  with  some  4
                        mistakes:
                                 int k=0;
                                 string s="Save Earth";
                                 int l=s.length;
                                 for(int i=0;k<l;i++)
                                 {
                                 jTextArea1.append(s+\n);
                                 k++;
                                 };
                         Help her in identifying and correcting the errors.
                         Convert the correct code into do while looping statement.

                                                        OR
                             Observe the given code:
                             int ctr=10;
                             while(ctr>5)
                             {
                             ctr=ctr-2;
                             }

                         Attempt the following questions based on the above given code:
                         i. How many times the above given loop will run and what will be the value of
                         ctr, immediately after exiting from the loop.
                         ii. Out of entry controlled or exit controlled loop, above given loop will come
                         under which category and why?

                  Ans:        int k=0;
                                 String s="Save Earth";
                                 int l=s.length();
                                 for(int i=0;k<l;i++)
                                 {
                                 jTextArea1.append(s+"\n");
                                 k++;
                                 }

                         (½ Mark for each correct error identification)

                                 int i=0,k=0;
                                 String s="Save Earth";
                                 int l=s.length();
                                 do
                                 {
                                 jTextArea1.append(s+"\n");
                                 k++;i++;
                                 }while(k<l);

                         (2 Mark for correct conversion)

                                                              OR
                 i.      3 times
                         4
                         (1 Mark for each correct answer)



                                                            7
   39   40   41   42   43   44   45   46   47   48   49