Welcome Guest | Sign in | Register
Flow Control - Discussion Page For Q.2803 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Flow Control > Questions and Answers

Q: 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" :
A. for( int a = 0 ; a< sum.length ; )     B. for( a= 0 ; a< sum.length ; )    
C. for( a = 0 ; a< sum.length ; a++ )     D. for( a = 1 ; i <= sum.length ; a++ ) 

Answer and Explanation

Answer:for( a= 0 ; a< sum.length ; )    

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.

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.