SCJP Module-2 Question-5


 

SCJP Module-2 Question-5

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

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.

Ads