Home Tutorial Java Scjp Part2 SCJP Module-2 Question-5

 
 

SCJP Module-2 Question-5
Posted on: July 7, 2010 at 12:00 AM
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.

Related Tags for SCJP Module-2 Question-5:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.