I am sharing a Selenium and Java challenge below. These types of questions are commonly asked in Selenium- and Java-based assessments. Test your Selenium and Java knowledge and add comments for solution. Click the Solution button below to view the answer.
public class SampleProgram3{
@Test
public void testMySkills() {
System.out.println(getTotal(10));
}
public int getTotal(int n) {
int sum = 0;
for (int i=1;i<=n;i++) {
sum+=i;
}
return sum;
}
}
sum=sum+n means the program will return the answer as “110”. sum=sum+i means only it will return the answer as “55”.
Hi Deepika, Thanks for the reply!. You are right mentioning total for sum+=i to return 55 but for sum=sum+n it will return 100 by summing value 10 for ten times, correcting question. it is my mistake to add sum+=n thanks.