Share on Google+Share on Google+

SCJP Module-2 Question-5

Advertisement
The given question is given below for checks the concepts and understating of core java and also helps in preparation of SCJP exams.

What should be at line number 3 to get the total sum of array "sum" ?

public int totalsum( int[] sum ){
int a, b= 0 ;
//which 'for' loop should be here(line :3)
{
b += sum[ a++ ] ;
}
return b ;
}

Which 'for' loop should be at line number 3 to calculate total sum of the array "sum" :

1. for( int a = 0 ; a< sum.length ; )    

2. for( a= 0 ; a< sum.length ; )    

3. for( a = 0 ; a< sum.length ; a++ )    

4. for( a = 1 ; i <= sum.length ; a++ )

Answer :

(2)

Explanation  :

In 1st option, variable a is declared again i.e. duplicate variable.

In 3rd option, due to extra loop iterator "a++" , the summation would not be correct.

In 4th option. due to initialization a=1  + due to extra loop iterator "a++" , the summation would not be correct.

Advertisements

Posted on: July 7, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.