What does the following code do?fn(int n, int p, int r){static int a=p;switch(n)  {   case 4:a+=a*r;   case 3:a+=a*r;   case 2:a+=a*r;   case 1:a+=a*r;   }}A. computes simple interest for one yearB. computes amount on compound interest for 1 to 4 yearsC. computes simple interest for four yearD. computes compound interest for 1 year

This question is related to TCS Interview

Showing Answers 1 - 1 of 1 Answers

the answer to the question ius option B
Reason being
if this function is called from within a loop with n=0 to n<4
then cases 1,2,3,4 in switch will be called one by one
as int a is static ,first time it will be initialized with the value p but after words
its memeory location will be updated every time the fun is called with the latest value of a calculated within the case
but r should be a float value
example:
main()
int p=100;
float r=0.10;
{
for(n=1;n<=4;n++)
{
fun(n,p,r);
}

}

than execution will be:
first iteration
a=p=100
n=1
case(1) selected
a=a+a*r=>a=100+100*.1
a=110

second iteration
a=110;
n=2
case(2)
a=a+a*r=>a=110+110*.1
a=121

third iteration
a=121
n=3
case(3)
a=a+a*r=> a=121+121*.1
a=133.1

fourth iteration
a=133
n=4
case(4)
a=133+133*.1
a=146

in this way we can see this is the amount for compound interest for 1-4 years

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions