10.7k views
2 votes
If r is an instance of the above Person class and oddNum has been declared as a variable of type boolean, which of the following correctly sets oddNum to true if Person object r has an odd number of children and to false otherwise?

oddNum = ( ( r.numChildren() % 2 ) != 0 );

if ( ( r.numChildren() % 2 ) == 0 )

oddNum = false;

else

oddNum = true;

oddNum = false;

for ( int k = 0 ; k < r.numChildren() ; k++ )

oddNum = !oddNum;

I only

II only

III only

I and II only

I, II, and III

User Fluminis
by
4.1k points

1 Answer

7 votes

Answer:

I, II, and III

Step-by-step explanation:

The three are almost saying the same thing. the loop is one am a bit concerned about. but since the oddnum is set to false, the loop will work.

The one and two are pretty clear. to test for odd number, the easiest is to divide by two to see if there will be a remainder. That is what both first and second statement is trying to do.

User Alex MacArthur
by
4.3k points