A computer science problem by Raj Maiti

Observe the following coding in java and find how many times will "And it's good!" printed when disp(5) --

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
         class r{ 
           public void disp(int a)
          {
          if (a>0);
            {
             System.out.println("And it's good!");
               disp(a-1);
             }
            }
             }

log on to comprogzz.in to learn computer programming:))

5 times 10 times infinite times 0 times

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

2 solutions

The method called by itself and the value disp(a-1) is changed any more!

No, it's because of the semicolon after the "if". It's a null statement. That means the program will print even if a<0, because the print isn't inside the if.

Kenny Lau - 6 years, 9 months ago
Sonali Srivastava
Jul 21, 2014

since value of a is never being changed hence it will print infinitely.Only disp(a-1) is being called,value of a is not updated.

No, it's because of the semicolon after the "if". It's a null statement. That means the program will print even if a<0, because the print isn't inside the if.

Kenny Lau - 6 years, 9 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...