Exactly one of the following correctly sets total to the number of p's grandchildren. Which one?
A.
Person p = new Person( "Fred" );
int total = p.numChildren();
B.
Person p = new Person( "Fred" );
int total = p.numChildren( p.numChildren() );
C.
Person p = new Person( "Fred" );
int total = 0;
for ( int k = 1 ; k <= p.numChildren() ; k++ )
{
total += p.numChildren();
}
D.
Person p = new Person( "Fred" );
int total = 0;
for ( int k = 1 ; k <= p.numChildren() ; k++ )
{
total += p.child( k ).numChildren();
}
E.
Person p = new Person( "Fred" );
int total = 0;
for ( int k = 1 ; k <= p.numChildren() ; k++ )
{
total += p.numChildren().child( k );
}
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
D is the correct one.