Multiple choice

Consider the following program segment: main() { int a=2; printf (“a= %d”,a); modify(a); printf (“a= %d”,a); } modify(a) { a*=3; printf (“a= %d”,a); return; } The answer printed after executing this program will be

  1. a=2 a=2 a=2

  2. a=2 a=6 a=2

  3. a=2 a=6 a=6

  4. none of these

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

C passes arguments by value, so when 'modify(a)' is called, only the value of a (which is 2) is copied to the function. Inside modify(), the parameter a becomes 6 (2×3), but this change is local to the function and doesn't affect the original variable in main(). The printf statements show: a=2 (first), a=6 (inside modify), a=2 (back in main).