• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JQPlus ID: 952983719810

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Which checkboxes will initially be selected when the following program is run?
import java.awt.*;
public class TestClass extends Frame
{
TestClass( )
{
setLayout(new FlowLayout());
CheckboxGroup[] cbgs = new CheckboxGroup[] { new CheckboxGroup(), new CheckboxGroup()};
for (int i=0; i<5; i++) add( new Checkbox("Checkbox"+i, true, cbgs[i%2] ) ); //1
setSize(300, 300);
setVisible(true);
}
public static void main(String[ ] args)
{
new TestClass();
}
}
It says that checkboxes 3 and 4.
But I think that at line marked 1 when it will calculate cbgs[i%2] in its fourth iteration, program should throw an ArrayIndexOutOfBoundsException as result will be 2.
Correct me if wrong.
Tanveer
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tanveer ...I was also thinking to post this q.
pls,explain someone.
thanks.
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i%2 can NEVER return anything other than 0 or 1 for any value of i >= 0.
-Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

Try out the world's only WebCompiler!
www.jdiscuss.com
 
Tanveer Mehmood
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ooooops!!! sorry for putting this question.....just misjudged it....Tanks anyway...
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tanveer Mehmood:

Which checkboxes will initially be selected when the following program is run?
import java.awt.*;
public class TestClass extends Frame
{
TestClass( )
{
setLayout(new FlowLayout());
CheckboxGroup[] cbgs = new CheckboxGroup[] { new CheckboxGroup(), new CheckboxGroup()};
for (int i=0; i<5; i++) add( new Checkbox("Checkbox"+i, true, cbgs[i%2] ) ); //1
setSize(300, 300);
setVisible(true);
}
public static void main(String[ ] args)
{
new TestClass();
}
}
It says that checkboxes 3 and 4.
But I think that at line marked 1 when it will calculate cbgs[i%2] in its fourth iteration, program should throw an ArrayIndexOutOfBoundsException as result will be 2.
Correct me if wrong.
Tanveer


Sorry tanveer u are making a mistake.AnyNumber%2 will result in either 1 or 0.I hope now u will get it for there will now just be two and in this case the last two checkboxes added which will be selected.
If u have still have got any doubt do tell
sandeep
 
Tanveer Mehmood
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ratul, I think Sandeep has made it very clear. Anynumber%2 can yield in 0 or 1. There is no otherway. Thanks Sandeep its clear now.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic