Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Q1: Consider the following C program. Assume parameters to a function are evaluated from right to left.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Which one of the following options is the CORRECT output of the above C program?  (2024 SET-2)
(a) 20101020
(b) 10202010
(c) 20102010
(d) 10201020
Ans: 
(a)
Sol:

Let's understand diagrammatically:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

As its mentioned "parameters to a function are evaluated from right to left".
So, h() will be evaluated first, and it will print 20 first then g() will be evaluated and it will print 10.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Now as g() and h() both function will return 10 and 20 respectively, so now f(10, 20) will be called and from there, then again g() and h() will be called and 10 and 20 will be printed respectively.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
NOTE:
"The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual
arguments is Unspecified, but there is a sequence point before the actual call".
For example:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
for this fun() we can't say order of evolution is always either from Right to Left
or Left to Right. Its totally Unspecified according to
The possible order of evolution of A(), B(), C() can be:
A(), B(), C()
A(), C(), B()
B(), A(), C()
B(), C(), A()
C(), A(), B()
C(), B(), A()

Q2: Consider the following C program:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Assume that the input to the program from the command line is 1234 followed by a newline character. Which one of the following statements is CORRECT?  (2024 SET-1)
(a) The program will not terminate
(b) The program will terminate with no output
(c) The program will terminate with 4321 as output
(d) The program will terminate with 1234 as output
Ans: 
(c)
Sol: 

The question is vague. They said they have input as 1234 but how this input is being provided?
Inside main there is no input available to support the string entered.
In the function definition itself.
a-> If the input is passed in the parameter then char a declaration is invalid.
b-> If the input is passed in the char a itself then we can't simply pass the value as 1234 with the given declaration. Because 1234 will be a string. But the given program simply declares a character variable a. All values needs to be entered one by one. But again no looping mechanism is given.
Now considering the fact that the input 1234 is given one by one then for the given input we can only pass 1 as the input as after that the program will terminate and in doing so we will get "no output in the answer".
Hence option B.
Here's an image of what points I am trying to make:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
and if the values are provided one by one.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
We are getting a blank screen as an output.
Please note that this answer is limited only up to my understandings. Please correct me if there's something wrong.

Q3: Consider the following program:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Which one of the following options represents the activation tree corresponding to the main function?  (2023)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)  Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) A
(b) B
(c) C
(d) D
Ans: 
(a)
Sol:

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The correct execution sequence is given in the above figure. inside main() we have 3 function call as f1(), f2(), f3().
f1(), f3() will return but f2() will take x = 2 and evaluate it. when X = 2 it will call f3() and else part. in next time when x value became 1 again it will call f3() and if part of given code. after that, it will return.
Option (A) is correct.

Q4: The integer value printed by the ANSI-C program given below is ______.  (2023)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 2
(b) 5
(c) 7
(d) 9
Ans: 
(c)
Sol: 
when x = funcp() is called first time it will go to the function funcp() where local variable x = 1 is define, increment it and return value 2 to local variable x inside main().
when y = funcp() + x is called it will again go to the function funcp(). Since x is a static variable here so new value of x is 2 itself.increment it and return 3.
So y = funcp() + x became y = 3+2=5.
∴ x = 2, y = 5
print(x + y) = 5 + 2 = 7
the correct answer is 7.

Q5: Consider the following ANSI C program
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The output of the program upon execution is ____  (2021 SET-2)
(a) 10
(b) 60
(c) 180
(d) 220
Ans:
(b)
Sol:

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Single child parent inherit the value of child, double child parents inherit the sum of the value of their children.
The function will return the value 60.

Q6: Consider the following ANSI C function:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The value returned by Some Function(15, 255) is __________  (2021 SET-2)
(a) 17
(b) 15
(c) 255
(d) 5
Ans:
(b)
Sol: 
This function is calculating the GCD of the two numbers by repeated subtraction.
GCD(15, 255) = 15. So it'll return 15.

Q7: Consider the following ANSI C function:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Let Z be an array of 10 elements with Z[i] = 1, for all i such that 0 < i < 9. The value returned by Simple Function(Z, 10,2) is  (2021 SET-1)
(a) 1023
(b) 511
(c) 255
(d) 2047
Ans: 
(a)
Sol: 
For index i, we get total= x * totali-1 + Y. We have Yi = 1, x = 2, total0 = 1.
So, totaln = 2 * totaln-1 + 1
= 2 * (2 * totaln-2 + 1) + 1
= 22 * totaln-2 + 2 + 1
:

= 22 * totaln-2 + 2 + 1
= 2* totaln-n + (2n-1 + 2n-2 +...+ 2 + 1)
= 2n * total+ (2- 1)
= 2n+1 -1
So, total9 210-1 = 1023.

Q8: Consider the following C functions.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

The value returned by pp(3,4) is _____  (2020)
(a) 9
(b) 81
(c) 16
(d) 125
Ans: 
(b)
Sol: 

pp(3, 4)

  • a = 3, b = 4, tot = 1;

len = tob (4, array) will return 3 with array set as 001 as array is updated only once
when (b%2 != 0). The for loop actually iterates 3 times for b = 4, b = 2 and b = 1, and only
when b = 1, arr[i] is updated.
Now pp will run for loop 3 times:

  1. arr[0] = 0. So, ex = 3 * 3 = 9
  2. arr[1] = 0. So, ex = 9 * 9 = 81
  3. arr[1] = 1. So, tot = 1 * 81 = 81


Q9: Consider the following C functions.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The return value of fun2(5) is ______  (2020)
(a) 55
(b) 25
(c) 35
(d) 10
Ans: 
(a)
Sol:

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Fig: Tracing by tree method
Now f1(5), if we observe it increments 'i', n times so f1(5) will return 5.
Similarly, f1(4) will increment 'i' 4 times but i' being static (just one memory location for entire program run instead of different memory location across function calls), it'll resume from its previous value of 5. So, f1(4) returns 9 [5 + 4]
By the same logic f1(3) will return 12 [9 + 3],
f1(2) will return 14 [12 + 2],
f1(1) will return 15 [14 + 1].
∴ Return value i = 55 and hence 55 is the output of f2(5).

Q10: Consider the following C program:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Which one of the following values will be displayed on execution of the programs?  (2019)
(a) 41
(b) 52
(c) 63
(d) 630
Ans: 
(b)
Sol:
Basic Points:-

  1. After every expression statement in the for loop, there is a sequence point.
  2.  After the return value is copied, there is a sequence point.

for loop execution:- for(e1;e2;e3)
On the first iteration, expression1 executed (generally termed as initialization expression,)
next expression2 executed (generally termed as condition check expression, if it evaluates to non-zero then only, for loop body executed, otherwise for loop terminates.)
After the first iteration, expression3 is executed (generally termed as increment expression.), after that e2 evaluates and the process continues!
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
before the main starts, the execution num is initialized with 7 (note that it is stored under static memory due to it is a static number.)
on first iteration :- r() ⇒ return 7 but num changed to 6.
r() ⇒ return 6 but num changed to 5. ⇒ condition evaluate to true ⇒ for loop body executes !
printf("%d",r()); ⇒ return 5 but num changed to 4. ⇒  print 5 on the screen.
r() ⇒ return 4 but num changed to 3.
r() ⇒ return 3 but num changed to 2. ⇒ condition evaluate to true ⇒ for loop body executes !
printf("%d",r()); ⇒ return 2 but num changed to 1. ⇒ print 2 on the screen.
r()  ⇒ return 1 but num changed to 0.
r()  ⇒ return 0 but num changed to -1.  ⇒ condition evaluate to false  ⇒ for loop over!
Hence option B: 52

Q11: Consider the following C program: 

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Which one of the following will happen when the function convert is called with any positive integer n as argument?  (2019)
(a) It will print the binary representation of n and terminate.
(b) It will print the binary representation of n in the reverse order and terminate.
(c) It will print the binary representation of n but will not terminate.
(d) It will not print anything and will not terminate.
Ans:
(d)
Sol: 
As per the question, the function convert needs to be called with a positive integer n as argument.
So, when a positive int is passed as argument, function will compute(n/2) which will return the quotient of integer division.
Again this result of integer division will be passed through compute (n/2) and so on. Each time the no will be divided by 2 and will gradually become smaller and smaller and close to 0 but it will never be less than 0.
Hence, the program will not print anything (as after every function call, the value returned is greater than 0)and will not terminate.
PS: Being a C function and hence when run on a computer each recursive call will require creation of activation record consuming memory space. So, eventually the stack memory will run out and program terminates. But such an option is not there. D is the best option here.
Answer (D)

Q12: Consider the following C program:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The value printed by program is __________ .  (2019)
(a) 13
(b) 26
(c) 2
(d) 5
Ans: (b)
Sol: 
x = 2, y = 5
y = jumble(5, 2) //call by value and y will hold return value. After this call x = 2, y = 12
x = jumble(12, 2) //call by value and x will hold return value. After this call x = 26, y 12
x = 26

Q13: Consider the following program written in pseudo-code. Assume that x and y are integers.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The number of times that the print statement is executed by the call Count(1024,1024) is _____.  (2018)
(a) 1024
(b) 10230
(c) 1048576
(d) 512
Ans: 
(b)
Sol: 

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Here, for each y value print("*") will run 10 times. Once x value reaches 1, count(1024, y - 1) will be called. Variable y can take values [2 1024] i. e. total 1023 values. So,
The total number of times '*' will be printed = (Number of times '*' printed per y value)* (number of values y takes).
Number of times '*' printed = 10 * 1023 = 10230

Q14: Consider the following C program:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

The output of the program above is  (2018)
(a) Hi Bye Bye Hi
(b) Hi Bye Hi Bye
(c) Bye Hi Hi Bye
(d) Bye Hi Bye Hi
Ans: 
(a)
Sol:

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Everything is local here. So, once function completes its execution all modification go in vain.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
This will retain modification and swap pointers of string.
So output would be Hi Bye Bye Hi
Correct Answer: A

Q15: Consider the following C program:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The output of this program is _____.  (2018)
(a) 4
(b) 3
(c) 5
(d) 6
Ans: 
(a)
Sol:

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

Q16: The output of executing the following C program is ________.  (2017 SET-1)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 23
(b) 7
(c) 8
(d) 9
Ans: (a)
Sol:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
This piece of code will count the no of set bits in v.
In the main function, i values goes from 5 to 1, So there will be 5 calls to total().
Each call to total (i) counts the no of set bits in i. But the count is a static variable,
So, total(i) = Total no of set bits in all i ≤ 5.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

Q17: Consider the C functions foo and bar given below:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

Invocations of foo(3) and bar(3) will result in:  (2017 SET-1)
(a) 
Return of 6 and 6 respectively.
(b) Infinite loop and abnormal termination respectively.
(c) Abnormal termination and infinite loop respectively.
(d) Both terminating abnormally
Ans: 
(c)
Sol:

Consider foo
Initially val = 3
foo(val --)
is equivalent to

  1.  foo(val)
  2.  val = val - 1

So,

  1. foo(3)
  2. val = 2

foo(3) calls foo(3) which in turn calls foo(3) this goes on
So, here we can see that foo(3) is called infinite number of times which causes memory overflow and abrupt termination
and one more thing to observe is infinite loop is not there since the val is decremented in the first iteration.
Consider bar.
Here, we can see the val is not decrementing in the loop
So,

  1. bar (3) will call bar(2)
  2. bar(2) will call bar (1)
  3. bar (1) will call bar(0)  → Here, bar(0) return 0
  4. bar(1) will call bar (0)
  5. bar(1) will call bar (0)

This will go on so here there is a problem of infinite loop but not abrupt termination since it does not cause memory overflow.

Q18: Consider the following two functions.  (2017 SET-1)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE) 
The output printed when fun1(5) is called is
(a) 53423122233445
(b) 53423120112233
(c) 53423122132435
(d) 53423120213243
Ans: 
(a)
Sol:

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Unroll recursion up to a point where we can distinguish the given options and choose the correct one!
Options B and D are eliminated.
A is the answer.

Q19: Consider the following program:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Note: max(x, y) returns the maxi mum of x and y. The value printed by this program is____________ .  (2016 SET-2)
(a) 2
(b) 3
(c) 4
(d) 5
Ans: 
(b)
Sol:

f(a, 5)

  • Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
    max(f(p + 1,5 - 1), 3 - 5) max(f(p + 1,4), -2)
  • Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE) 
    max(max(f(p + 1,4 - 1), 5 - 2), -2) = max(max(f(p + 1, 3), 3), -2)
  • Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
    max(max(max(f(p + 1,3 - 1), 2 - 6), 3), -2) = max(max(max(f(p + 1, 2), -4
  • Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
    max(max(max(max(f(p + 1), 1), 2), -4), 3), -2)
  • n = 1, return 0

max(max(max(max(0, 2), -4), 3), -2)
= max(max(max(2, -4), 3), -2)
= max(max(2, 3), -2)
= max(3, -2)
= 3

Q20: The value printed by the following program is.  (2016 SET-2)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 15
(b) 20
(c) 25
(d) 30
Ans:
(d)
Sol:
i is called by reference and j is called by value.
So, in function f() only value of i might change,
Now, in function f(*p, m)
*p is pointing to i
Thus *p is 5.
m is 10 because of call by value of j.

  1. m = 10 + 5 hence m = 15
  2. *p = 5+15 hence *p = 20, that is, value of variable i is now 20
  3. returns nothing

Now, back to main
i = 20 and j is as it is 10
Hence, output of printf will be i + j = 20 + 10 = 30.

Q21: What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed?  (2016 SET-1)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 6,2
(b) 6,6
(c) 4,2
(d) 4,4
Ans: 
(d)
Sol: 
It is a bit confusing as variable declaration is not explicit. But we can see that "a = 3" and "a = 1" are declaring new variables, one in global and other in local space.
Main is calling m(a). Since there is no local 'a', 'a' here is the global one.
In m, we have "a = 1" which declares a local "a" and gives 1 to it. "a = y - a" assigns 3 - 1 = 2 to 'a'.
Now, in n(x), 'a' is used and as per dynamic scoping this 'a' comes from 'm()' and not the global one. So, "x = x * a" assigns "2 * 2 = 4" to "x" and 4 is printed. Being passed by reference, "a" in m() also get updated to 4. So, D is the answer here.

Q22: What will be the output of the following C program?  (2016 SET-1)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 312213444
(b) 312111222
(c) 3122134
(d) 3121112
Ans:
(a)
Sol: 

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Here, d is Static, so the value of d will persists between the function calls.

  1. count(3) will print the value of n and d and increments d and call
    count(2) prints 3 1.
  2. count(2) will print the value of n and d and increments d and call  
    count(1) prints 2 2.
  3. count(1) will print the value of n and d and increments d prints 1 3.

Now, it will return and prints the final incremented value of d which is 4, three times.
So, option (A) is correct = 3 1 2 2 1 3 4 4 4.

Q23: Consider the following C program.  (2016 SET-1)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The output of the program is _________.
(a) 2016
(b) 0
(c) 4
(d) 42
Ans:
(a)
Sol: 
The mystery about mystery function is it does not affect values in main. As in C, parameters are passed by value- even if they are pointer. So, here the pointer values are exchanged within the function only. (we can use * operator to exchange the values at the location of the
pointers and this will affect the values in main).
So, no changes in a, b, c, d.
And Answer is 2016

Q24: Consider the following C program.  (2016 SET-1)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Which one of the following expressions, when placed in the blank above, will NOT result in a type checking error?
(a) f(s,*s)
(b) i=f(i,s)
(c) f(i,*s)
(d) f(i,*p)
Ans: 
(d)
Sol:

A. f(s, *s) 1st argument is short whereas the function expects an int. But in C language short gets implicitly converted to int during a function call and so this won't be a type error. But second argument is a pointer (can be 64 bits on a x64 machine) where as the function expects a short (can be even 16 bits). So this will generate a type error. So, WRONG.
B. i = f(i, s) - return type is not void. So, WRONG.
C. f(i, *s) 1st argument is int, second is again syntax error. So, WRONG
D. f(i, *p) - Both the arguments and return type match. p is a pointer to short, so *p is value of short. So, is ANSWER.

Q25: Suppose c=(c[0],...,c[k-1]) is an array of length k, where all the entries are from the set {0,1}. For any positive integers a and n, consider the following pseudocode.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
If k = 4, c = (1,0,1,1), a = 2 and n = 8, then the output of DOSOMETHING (c,a,n) is______.  (2015 SET-3)
(a) 0
(b) 1
(c) 2
(d) 3
Ans: 
(a)
Sol:

Initially k = 4, c = [1, 0, 1, 1], a = 2, n = 8.
Now let's iterate through the function step by step:
z = 1 (at the start of do-something)
i = 0 (start of external for loop)
In the do loop
z = 1 * 1 (non zero value so considered as true and continue)
c[0] = 1, so in the if clause, z = 1 * 2%8 = 2
In the do loop
z = 2 * 2%8 = 4 (since now z = 2) (non zero value so considered as true and continue)
c[0] = 1, so in the if clause, z = 4 * 2%8 = 0
Now no need to check further:
Reason: All the operations that update Z are multiplicative operations and hence the value
of Z will never change from 0.

Q26: Consider the following C program.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The output of the program is ________.  (2015 SET-3)
(a) 228
(b) 230
(c) 229
(d) 239
Ans: 
(b)
Sol: 
The variable x is initialized to 1. First and only call to f1() returns 26. First call to f2() returns 51. First and only call to f3() returns 100. Second call to f2() returns 52 (The value of local static variable x in f2() retains its previous value 51 and is incremented by 1).
x = 1 + 26 + 51 + 100 + 52 = 230
Answer: 230

Q27: Consider the following recursive C function.  (2015 SET-3)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
If get(6) function is being called in main()then how many times will the get()function be invoked before returning to the main()?
(a) 15
(b) 25
(c) 35
(d) 45
Ans: 
(b)
Sol:

T(n) = T(n-1) + T(n-3) + 2, here T(n) denotes the number of times a recursive call is made for input n. 2 denotes the two direct recursive calls.
T(n ≤ 0) = 0
T(1) = 2
T(2) = 4
T(3) = 6
T(4) = 10
T(5) = 16
T(6) = 24
So, answer is 24 + 1 call from main = 25.

Q28: Consider the following C function.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The return value of fun(5) is ________.  (2015 SET-2)
(a) 15
(b) 51
(c) 38
(d) 64
Ans:
(b)
Sol:

fun(1) = 1;
fun(2) = 1 + fun(1) * fun(1) = 1 + 1 = 2;
fun(3) = 1 + fun (1) * fun(2) + fun(2) * fun(1) = 5;
fun(4) = 1 + fun(1) * fun(3) + fun(2) * fun(2) + fun(3) * fun(1)
= 1 + 5 + 4 + 5 = 15;
fun(5) = 1 + fun (1) * fun(4) + fun(2) * fun(3) + fun(3) * fun(2) + fun(4) * fun(1)
= 1 + 15 + 10 + 10 + 15 = 51;
More formal way:
The recurrence relation is
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
f(1) = 1
f(2) = 1 + f(1). f(1)
= 1 + 1.1 = 2
f(3) = 1 + f(1). f(2) + f(2). f(1)
= 1 + 1.2 + 2.1 = 5
f(4) = 1 + f(1). f(3) + f(2). f(2) + f(3). f(1)
= 1 + 1.5 + 2.2 + 5.1 = 15
f(5) = 1 + f(1). f(4) + f(2). f(3) + f(3). f(2) + f(4). f(1)
= 1 + 1.15 + 2.5 + 5.2 + 15.1 = 51

Q29: Consider the following function written in the C programming language.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The output of the above function on input'' ABCD EFGH'' is  (2015 SET-2)
(a) ABCD EFGH
(b) ABCD
(c) HGFE DCBA
(d) DCBA
Ans:
(d)
Sol:

Answer D as priority of! = is greater than that of && in C. The execution happens as:
if ((*a) && (*a != ' '))
So, the if breaks either when *a = 0 (not '0' but ASCII 0 or null character '\0'), or when *a =''.
So, the recursive call goes like
'A' - 'B' - 'C' - 'D' -'' (breaks) and then starts outputting
DCBA

Q30: The output of the following C program is__________.  (2015 SET-1)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) -7
(b) -5
(c) -3
(d) -1
Ans:
(b)
Sol:

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

Q31: Consider the C function given below
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Which one of the following is TRUE?  (2014 SET-2)
(a) The function returns 0 for all values of j.
(b) The function prints the string something for all values of j.
(c) The function returns 0 when j = 50.
(d) The function will exhaust the runtime stack or run into an infinite loop when j = 50

Ans: (d)
Sol:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
since for value other then 50 it will not print anything b/c not satisfy if property.
so ans is D. since always same value pass as value.

Q32: Consider the following function
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Give a value q (to 2 decimals) such that f(q) will return q:______  (2014 SET-2)
(a) 1.5
(b) 1.73
(c) 0.73
(d) 2.2
Ans: 
(b)
Sol:

(We can directly go to the "if" part to get one answer, but we need to solve "else" part too to get all possible answers which though is not asked in question)
Solving the else part:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
So, the new value of x will bePrevious Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)and we need it equal to x.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Now solving the if part.
  abs(x*x - 3) < 0.01
So,
x2 - 3 < 0.01 and - (x- 3) < 0.01 ⇒ x2 < 3.01 and x2 > 2.99 ⇒ x < 1.73
Corrected to 2 decimal places answer should be 1.73 or 1.74.

Q33: Consider the function func shown below:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The value returned by func(435)is __________.  (2014 SET-2)
(a) 8
(b) 9
(c) 10
(d) 11
Ans: 
(b)
Sol:

435 - (110110011)
num >>= 1; implies a num is shifted one bit right in every while loop execution. While loop is executed 9 times successfully and 10th time num is zero.
So count is incremented 9 times.
Note:
Shifting a number "1" bit position to the right will have the effect of dividing by 2:
8 >> 1 = $4    // In binary: (00001000) >> 1 = (00000100)

Q34: What is the return value of f(p,p), if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value.  (2013)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 3024
(b) 6561
(c) 55440
(d) 161051
Ans: 
(b)
Sol:

In GATE 2013 marks were given to all as the same code in C/C++ produces undefined behavior. This is because * is not a sequence point in C/C++. The correct code must replace:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
In this code, there will be 4 recursive calls with parameters (6, 4), (7, 3), (8, 2) and (9, 1).
The last call returns 1. But due to pass by reference, & in all the previous functions is now 9.
Hence, the value returned by f(p, p) will be 9 * 9 * 9 * 9 * 1 = 6561.

Q35: Consider the following C code segment.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
What output will be generated by the given code segment if:  (2012)
Line 1 is replaced by auto int a = 1;
Line 2 is replaced by register int a = 2;

(a)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(b)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(c)

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(d)

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Ans:
(d)
Sol:

main
a = 1

prt Fun()
a = 2
b = 1
a = a + ++b = 2 + 2 = 4
b = 2
printf → 4 2
back to main
a = a + 1 → 1 + 1 → 2

prtFun()
a = 2 //previous a is lost
b = 1
a = a+ ++b = 2 + 2 = 4
b = 2
printf → 4 2

back to main
a = 2
b = 0 (initial value of global b. in prtFun local b is only updated)
printf → 2 0
Answer is D.

Q36: Consider the following C code segment.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
What output will be generated by the given code segment?  (2012)
(a)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(b)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(c)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(d)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Ans: (c)
Sol:

main
a = 1
prt Fun()
a = 2
b = 1
a = a+ ++b = 2 + 2 = 4
b = 2
printf → 42
back to main
a = a + 1 → 1 + 1  → 2 (local static a is taken)
prt Fun()
a = 4 // previous value in the function is retained because of static
b = 1
a = a+ ++b = 4 + 2 = 6
b = 2
printf → 6 2
back to main
a = 2
b = 0 (initial value of global b. in prtFun local b is only updated)
printf → 2 0
The answer is C.

Q37: Consider the following recursive C function that takes two arguments
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
What is the return value of the function foo when it is called as foo (513, 2)?  (2011)
(a) 9
(b) 8
(c) 5
(d) 2
Ans: 
(d)
Sol: 

The function returns the sum of digits in a binary representation of the given number
so 1 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 = 2
Correct Answer: D

Q38: Consider the following recursive C function that takes two arguments
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
What is the return value of the function foo when it is called as foo (345, 10) ?  (2011)
(a) 345
(b) 12
(c) 5
(d) 3
Ans: 
(b)
Sol:  

Red colour represents return values.
Answer is 12.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

Q39: What is the value printed by the following C program?  (2010)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) -9
(b) 5
(c) 15
(d) 19
Ans:
(c)
Sol:
  
Suppose int array takes 4 bytes for each element and stored at base address100.
Follow below image. Red color shows the return value.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
So, 15 is the answer.
Correct Answer: C

Q40: What does the following program print?  (2010)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 2 2
(b) 2 1
(c) 0 1
(d) 0 2
Ans: (d)
Sol: 
int  i=0, j=1;
The above statement declares and initializes two global variables.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Now in the main function the function 'f' is called with the address of variable 'i' and 'j' as parameters.
Let us assume address of i = 100 and address of j = 200.
So the statement A in the definition of function f changes the value of the pointer 'p' from 100 to 200 as the pointer 'p' takes the address of pointer 'q'.
So after statement A the pointer 'p' also stores the address of j.
And in the statement B the value at address stored in the pointer 'p' is set to 2 this means the value of j is changed to 2.
Because of this the printf function in the main will print the value of i=0 and the value of j=2.
Therefore the output will be 0 2

Q41: Consider the program below:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The value printed is  (2009)
(a) 6
(b) 8
(c) 14
(d) 15
Ans: 
(b)
Sol:
 Let the address of x be 1000.

  1. f(5, 1000) = 8
  2. f(4, 1000) = 5
  3. f(3, 1000) = 3
  4. f(2, 1000) = 2
  5. f(1, 1000) = 1.

The evaluation is done from 5 to 1. Since recursion is used.

Q42: Consider the code fragment written in C below :
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Which of the following implementations will produce the same output for f(173) as the above code?  (2008)
P1

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
P2
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) Both P1 and P2
(b) P2 only
(c) P1 only
(d) Neither P1 nor P2
Ans: 
(c)
Sol:
  
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

Here, P1 and P2 will print opposite in direction as shown in diagram.
And given code fragment will print like P1 and not like P2
Hence, answer will be (C).

Q43: Consider the code fragment written in C below :
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
What does f(173) print?  (2008)
(a) 10110101
(b) 010101101
(c) 010110101
(d) 10101101
Ans: 
(d)
Sol: 
Here  before calling f(n/2) we need to push the printf statement into stack.
and we know stack follows LIFO order so Last in First print here.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Hence D is Ans.

Q44: Consider the C program below. What does it print?  (2008)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 5,5
(b) 5,4
(c) 4,5
(d) 4,4
Ans: 
(c)
Sol:
  
In main program num1=5, num2=4
first condition true. num1=4, num2=5
Now, second condition become true but no affect on values still num1=4, num2=5
third condition fail.
Ans: 4,5

Q45: Consider the following C functions:  (2008)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
f1(8) and f2(8) return the values
(a) 1661 and 1640
(b) 59 and 59
(c) 1640 and 1640
(d) 1640 and 1661
Ans:
(c)
Sol:

Both f1 and f2 are calculating the same function in recursive and iterative fashion respectively.
So, lets solve the recurrence relation.
F(n) = 2F(n - 1) + 3F(n - 2)
Its characteristic equation will be
r2 - 2r - 3 = 0
⇒ (r - 3) (r + 1) = 0
⇒ r = 3, -1
Now, we have two roots. So the equation will be
a= C1(-1n) + C2(3n) → (1)
Now from the function f1,
F(0) = 0 and F(1) = 1
So, C+ C2 = 0
-C+ 3C2 = 1
⇒ C= -1/4 and C2 = 1/4
After putting the values in (i) the equation will become
an=(-1/4)(-1n) + (1/4)(3n)
Putting n = 8 it will become
F(8) = a8 =  1640.
Or we can do it manually
f1(2) = 2f1(1) + 3f1(0) = 2
f1(3) = 2f1(2) + 3f1(1) = 7
f1(4) = 20
f1(5) = 61
f1(6) = 182
f1(7) = 547
f1(8) = 1640 f2(8)

Q46: Choose the correct option to fill ? 1 and ? 2 so that the program below prints an input string in reverse order. Assume that the input string is terminated by a newline character.  (2008)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) ?1 is (getchar( )!= '\n')
?2 is getchar (c);
(b) ?1 is (c = getchar( ))! = '\n')
?2 is getchar (c);
(c) ?1 is (c! = '\n')
?2 is putchar (c);
(d) ?1 is ((c = getchar( ))! = '\n')
?2 is putchar (c);

Ans: (d)
Sol:
Here, we are using the '=' operator which has less priority than'! =' operator. So (c = getchar()) has to be in brackets and after reversing the string we use function putchar(c) for
printing the character.
So option (D) is the right answer.

Q47: What is printed by the following C program?  (2008)

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 18
(b) 19
(c) 21
(d) 22
Ans: 
(b)
Sol:  

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Return x + y + z = return 7 + 7 + 5 = return 19
So, option B = 19 is correct.

Q48: Consider the program below in a hypothetical programming language which allows global variables and a choice of static or dynamic scoping.  (2007)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Let x be the value printed under static scoping and y be the value printed under dynamic scoping. Then, x and y are:
(a) x=10, y=20
(b) x=20, y=10
(c) x=10, y=10
(d) x=20, y=20
Ans:
(a)
Sol:  

In static scoping, the scope of an identifier is determined by its location in the code, and since that doesn't change, the scope doesn't either. In dynamic scoping, the scope is determined by the sequence of calls that has led to the use of an identifier, and since that can be different each time that use is reached, is dynamic.
So, here:
Option A must be the answer.
As, under static scoping: x = 10(global i)
under dynamic scoping: y = 20(according to the sequence of calls, i.e. 20)

Q49: Consider the program below in a hypothetical language which allows global variable and a choice of call by reference or call by value methods of parameter passing.  (2007)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Which one of the following options represents the correct output of the program for the two parameter passing mechanisms?
(a) Call by value : i = 70, j = 10; Call by reference :i = 60, j = 70
(b) Call by value : i = 50, j = 60; Call by reference :i = 50, j = 70
(c) Call by value : i = 10, j = 70; Call by reference :i = 100, j = 60
(d) Call by value : i = 100, j = 60; Call by reference :i = 10, j = 70

Ans: (d)
Sol:
Correct answer is (D)
CALL BY VALUE :- i as global variable declared. Then in main() a local variable j as integer declared ie j = 60 And global variable i initialized to 50 by i = 50. Now procedure f called and values of i and j are passed to it. i.e., in f(i, j) → f(x, y) content of memory location of i (here 50) is copied to memory location of x (which is different from i) and content of memory location of j (here, 60) is copied to memory location of y. Then in f(x, y) i = 100 changes the global i to 100, X = 10 changes the local X from 50 to 10 and Y = y + i means y = 60 + 100 = 160. Now when
return back to main, i and i will be 100 and 60 respectively.
CALL BY REFERENCE:- Now procedure f called and passed reference of i and j to it. i.e., in f(i, j) → f(x, y) x and y are new names (aliases) pointing to the same memory location of i and j respectively. So, i = 100 changes the global i to 100 and x = 10 means as well as global i = 10 (as the i being passed is the global variable and x and i share the same address).
y = y + i means y = 60 + 10 = 70 and this changes the value of j also to 70 as j and y have the same address. Now when return back to main, i and j will be 10 and 70 respectively.

Q50: The function f is defined as follows:  (2007)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following statements.
i. The function f terminates for finitely many different values of n ≥ 1.
ii. The function f terminates for infinitely many different values of n ≥ 1.
iii. The function f does not terminate for finitely many different values of n ≥ 1.
iv. The function f does not terminate for infinitely many different values of n ≥ 1.
Which one of the following options is true of the above?

(a) i and iii
(b) i and iv
(c) ii and iii
(d) ii and iv
Ans: 
(d)
Sol:
 
The function terminates for all powers of 2 (which is infinite), hence (i) is false and (ii) is TRUE.
Let n = 5.
Now, recursive calls will go like 5 - 14 - 7 - 20 - 10 - 5-
And this goes into infinite recursion. And if we multiply 5 with any power of 2, also result will be infinite recursion. Since, there are infinite powers of 2 possible, there are infinite recursions possible (even considering this case only). So, (iv) is TRUE and (iii) is false.
So, correct answer is (D).

Q51: Consider the following C function:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
What is the value of f(5)?  (2007)

(a) 5
(b) 7
(c) 9
(d) 18
Ans: (d)
Sol: 
The answer is D.
f(5) = 18.
f(3) + 2 = 16 + 2 = 18
f(2) + 5 = 11 + 5 = 16
f(1) + 5 = 6 + 5 = 11
f(0) + 5 = 1 + 5 = 6
Consider from last to first. Since it is recursive function.

Q52: The following function computes the value ofPrevious Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)correctly for all legal values m and n (m ≥ 1, n ≥ 0 and mgtn)
In the above function, which of the following is the correct expression for E?  (2006)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) (n = = 0) || (m = = 1)
(b) (n = = 0) && (m = = 1)
(c) (n = = 0) || (m = = n)
(d) (n = = 0) && (m = = n)
Ans: 
(c)
Sol:
 
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
return 1;
(n==0) || (m==n)
Answer: C

Q53: Which one of the choices given below would be printed when the following program is executed?  (2006)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

(a) a = 0, b = 3 a = 0, b = 3
(b) a = 3, b = 0 a = 12, b = 9
(c) a = 3, b = 6 a = 3, b = 6
(d) a = 6, b = 3 a = 15, b = 12
Ans: 
(d)
Sol:
  
First of all, the swap function just swaps the pointers inside the function and has no effect on the variables being passed.
Inside printab, a and b are added odd integers from 1-5, i.e., 1 + 3 + 5 = 9. So, in firs call to printab, a = -3 + 9 = 6 and b = -6 + 9 = 3.
Static variables have one memory throughout program run (initialized during program start) and they keep their values across function calls. So, during second call to printab, a = 6 + 9 = 15, b = 3 + 9 = 12.
Hence, (D) is choice.

Q54: Consider the C code to swap two integers and these five statements: the code  (2006)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
S1: will generate a compilation error
S2: may generate a segmentation fault at runtime depending on the arguments passed
S3: correctly implements the swap procedure for all input pointers referreing to integers stored in memory locations accessible tot he process
S4: implements the swap procedure correctly for some but not all valid input pointers
S5: may add or subtract integers and pointers

(a) S1
(b)  S2 and  S3
(c)  S2 and  S4
(d)  S2 and  S5
Ans:
(c)
Sol: 

S1 is false.
S2 is true, depending on the argument passed it may generate segmentation fault.
S3 is false because implementation is having some problem. Let x = 3 and I want to implement SWAP[x, x]. Now ans would be 0 but that must be x. Problem is because we are not checking whether both pointer are pointing the same address or different
So, S4 is true.
S5 is obviously false so, option (C) is right.

Q55: Consider the following code written in a pass-by reference language like FORTAN and these statements about the code.  (2006)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
S1: The complier will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell to swap
S2: On execution the code will generate a runtime error on line L1
S3: On execution the code will generate a runtime error on line L2
S4: The program will print 13 and 8
S5: The program will print 13 and -2
Exactly the following set of statement (s) is correct:
(a) S1 and S2
(b) S1 and S4
(c) S3
(d) S1 and S5
Ans: 
(b)
Sol:

 call swap (ia, ib+5)
The first parameter is passed by reference. For the second parameter, ib isn't passed by reference, rather a temporary memory cell is passed, whose value if 8 + 5 = 13. Let's name it p.
p swaps with a, hence a gets the value 13; p gets the value 3. We'll never refer to p again now.
 print *, ia, ib
This would print 13, 8 because ib was never passed by reference, its value stays intact.
S1 and S4 are correct
Line 1 and Line 2 won't generate a runtime error, as ints are being assigned to ints. So, S2 and S3 are incorrect.
And since S4 is correct, S5 is incorrect.

Q56: What is the output printed by the following program?  (2005)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

(a) 5
(b) 8
(c) 9
(d) 20
Ans: 
(c)
Sol: 

See the following calling sequence. Boxed values show the return values.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Hence, answer is option C.

Q56: Consider the following C-program:  (2005)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The above code compiled without any error or warning. If Line 1 is deleted, the above code will show:
(a) no compile warning or error
(b) some compiler-warnings not leading to unintended results
(c) some compiler-warnings due to type-mismatch eventually leading to unintended results
(d) compiler errors

Ans: (d)
Sol:
I executed the code in code:blocks
I execute the following code:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Above code will execute successfully even without warnings. The reason might be compiler implicitly assumes that the function foo has a return type of int and also at the end when it sees the function as what he assumes there will be no problem and therefore code executes correctly.
What I also observe is you have to make code:blocks to follow c99 standard. and when I set it and again execute code:
Compiler produces warning: implicit declaration of function 'foo'
but the code executes successfully and prints 20 as output.
but in the question, without a prototype as compiler assumes that foo() return int but as it goes it sees at the end function returns double type. therefore returns an error. therefore option (D) is correct and option (c) is not correct as it says some compiler-warning and not error.
Please correct me if what I am assuming is wrong.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

Q57: Consider the following C-program:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
What does the above program print?  (2005)
(a) 8, 4, 0, 2, 14
(b) 8, 4, 0, 2, 0
(c) 2, 0, 4, 8, 14
(d) 2, 0, 4, 8,0
Ans: 
(d)
Sol:

foo is printing the lowest digit. But the print f inside it is after the recursive call. This forces the output to be in reverse order
2, 0, 4, 8
The final value sum printed will be 0 as C uses pass by value and hence the modified value inside foo won't be visible inside main.

Q58: What does the following C-statement declare?  (2005)
int ( * f) (int * ) ;
(a) A function that takes an integer pointer as argument and returns an integer
(b) A function that takes an integer as argument and returns an integer pointer
(c) A pointer to a function that takes an integer pointer as argument and returns an integer.
(d) A function that takes an integer pointer as argument and returns a function pointer
Ans:
(c)
Sol:

(a) A function that takes an integer pointer as argument and returns an integer int f(int*)
(b) A function that takes an integer as argument and returns an integer pointer int* f(int)
(c) A pointer to a function that takes an integer pointer as argument and returns an integer
int (*f) (int * );
So, answer is C.

Q59: Consider the following C program:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
What is the output generated by the program ?  (2004)
(a) 
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(b)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(c)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(d)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Ans: (b)
Sol:
  
First print A B
f1 is call by value the changes applicable only for local
from f1, U V is printed
back in main A B is printed
then in f2, V W is printed
Hence, answer is (B).

Q60: Choose the correct option to fill the ?1 and ?2 so that the program prints an input string in reverse order. Assume that the input string is terminated by a new line character.  (2004)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) ?1 is getchar()!= '\n'
?2 is getchar(c);
(b) ?1 is (c=getchar());!= '\n'
?2 is getchar(c);
(c) ?1 is c!= '\n'
?2 is putchar(c);
(d) ?1 is (c=getchar())!= '\n'
?2 is putchar(c);

Ans: (d)
Sol:
getchar() - reads a single character at a time from the stdin.
putchar(c) - writes a character specified by the argument to stdout.
As getchar() and putchar() both are needed to read the string and print its reverse and only option D
contains both the function. D is the answer.
:P
Now coming to the code.
wrt_it(void) is calling itself recursively. when \n is encountered putchar() gets executed and prints the last
character and then the function returns to its previous call and prints last 2nd character and so on.

Q61: What is the output of the following program?  (2004)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 43 80
(b) 42 74
(c) 33 37
(d) 32 32
Ans: 
(a)
Sol:
  
funcf(x) + funcg(x)
funcf or funcg can be executed first as whether the first operand or the second operand to '+' operator is first executed is not defined in C language and it depends on the compiler implementation. But here the order does not matter as both the operands are not affecting each other and '+' is commutative. Lets assume funcf is executed first. It calls funcg - so even if the order of call is reversed, result will be same.
In first call of funcg, y becomes 11 and it returns 5 + 11 = 16.
In second call of funcg, y becomes 12 and it returns 5 + 12 = 17.
So, in main y is incremented by 16 + 17 = 33 to become 10 + 33 = 43. (Choice A)
In the second iteration y will be incremented by 18 + 19 = 37 to give 43 + 37 = 80.

Q62: Consider the following C function:  (2004)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The value returned by f(1) is  
(a) 5
(b) 6
(c) 7

(d) 8
Ans: 
(c)
Sol:

Static storage class allows to initialize a variable for one time and re-initialization can't be done till end of program.
Here Iteration1: 
n(1)<=5; and i=1;
n(2)=n(1)+i(1);
i(2)=i(1)+1;
Iteration2: 
n(2)<=5; and i=2;
n(4)=n(2)+i(2);
i(3)=i(2)+1;
Iteration3: 
n(4)<=5; and i=3;
n(7)=n(4)+i(3),
i(4)=i(3)+1;
Iteration2: n(7)<=5; and program execution terminates. So 7 is returned.

Q63: Consider the following C function
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
In order to exchange the values of two variables x and y.  (2004)
(a) call swap (x, y)
(b) call swap (&x, &y)
(c) swap (x,y) cannot be used as it does not return any value
(d) swap (x,y) cannot be used as the parameters are passed by value

Ans: (d)
Sol: 
Option A will not exchange the values of x and y because parameters are passed by value in C. i.e., the code is exchanging a' and y' which are having the values of x and y respectively.
Option B will not swap the value
void swap(int a, int b)
Here, it is wrong to pass in address (int*) as the parameters are of int type, even sizeof int and int* varies depending on the compiler. Now, even if ignoring this error, the given code would not exchange the values of x and y as it is merely exchanging p'1 and p'2 where p'1 and p'2 are containing the copies of the addresses of x and y respectively. (Even addresses are passed by value in C language).
Option C is false, return value is not required for exchanging the variables.
Option D is correct. We cannot use swap(x, y) because parameters are passed by value. Only way now to exchange the variables are by passing their addresses and then modifying the contents using the de-referencing operator (*).

Q64: Consider the C program shown below.  (2003)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The output of this program is
(a) 12 7 6
(b) 22 12 11
(c) 14 6 6
(d) 7 6 6
Ans: 
(a)
Sol:

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Correct Answer: A

Q65: The following program fragment is written in a programming language that allows variables and does not allow nested declarations of functions.  (2003)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
If the programming language uses dynamic scoping and call by name parameter passing mechanism, the values printed by the above program are
(a) 115, 220

(b) 25, 220
(c) 25, 15
(d) 115, 105
Ans: 
(a)
Sol:

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Thus, A is the correct option as 115 and 220 are printed.

Q66: The following program fragment is written in a programming language that allows variables and does not allow nested declarations of functions.  (2003)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
If the programming language uses static scoping and call by need parameter passing mechanism, the values printed by the above program are

(a) 115, 220
(b) 25, 220
(c) 25, 15
(d) 115, 105
Ans: 
(d)
Sol:

First refer the following question on Call-by-name parameter passing technique then solve this question.
Call by Name vs. Call by Need :
Assume X is the formal and e the corresponding actual expression.
Call-by-Name:

  1. Delays evaluation of arguments past call until a reference to the formal.
  2. Re-evaluates argument e on each reference to X in environment of caller.
  3. No local variable X is allocated

Call-by-Need :

  1. Delays evaluation of arguments past call until a reference to the formal.
  2. Evaluates e on 1st reference in environment of caller & loads local variable X, no re-evaluation: subsequent references use local X

Since "Call by need"  parameter passing technique, it is almost same as Call-by-name But the difference is that Actual argument is evaluated only once(on the first reference) and then that value is saved and re-used on further references But the actual argument is Not re-evaluated.
Caller function's Actual argument contains variable i which clashes with called function P's local variable i, hence, we rename called function P's local variable i and change it to i'.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
In case of Static scoping: 115, 105
In case of Dynamic scoping: 115, 105
Note that there are no local variable i, j in main function, so, when we say that i, j refer to the i, j in main's environment, we mean that If i, j were accessed/updated in main function then depending on the scoping, which i, j would they refer.
Here, in this question, in both static and dynamic scoping case, i, j will refer to the Global variables.
And in function P, in the 4th statement (i.e. j = 20), the Global variable j will be updated.

Q67:  The results returned by function under value-result and reference parameter passing conventions  (2002)
(a) Do not differ
(b) Differ in the presence of loops
(c) Differ in all cases
(d) May differ in the presence of exception
Ans:
(d)
Sol: 

D. May differ in the presence of exceptions.
In call by reference, the called function is working with the memory location of the passed variables. So, any update to the variables are immediately effective.
In call by value-result, the called function is working with a copy of the passed variables. On return, the updated values are copied back to the original variables.
So, during a function execution if an exception happens, in call-by-value-result, the passed variables won't be getting updated values.

Q68: Consider the following program
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
If the language has dynamic scooping and parameters are passed by reference, what will be printed by the program ?  (2001)
(a) 10
(b) 11
(c) 3 
(d) None of the above
Ans: (d)
Sol: here, because of dynamic scoping the value of n passed in w(n) will be n = 3.
therefore o/p will be 4, which is not in any option.

Q69: Consider the following three functions :
[P1]
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
[P2]
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
[P3]
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Which of the above three functions are likely to cause problems with pointers ?  (2001)
(a) Only P3
(b) Only P1 and P3
(c) Only P1 and P2
(d) P1, P2 and P3
Ans
: (c)
Sol: 

[P1] may cause an error because the function is returning the address of a locally declared variable.
[P2] will cause a problem because px is an int pointer that is not assigned with any address and we are doing dereferencing.
[P3] will work because the memory in bytes of the size of an int will be reserved and its address will be stored in px that can be further used, once function execution completes, this m/m will still exist in Heap until we free it using free() function.
Hence, the answer is (C).

Q70: What is printed by the print statements in the program P1 assuming call by reference parameter passing ?  (2001)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 10, 3
(b) 31, 3
(c) 27, 7
(d) None of the above
Ans: 
(b)
Sol:

Here, variable x of func1 points to address of variable y.
and variables y and z of func1 points to address of variable x.
Therefore, y = y + 4 ⇒ y = 10 + 4 = 14
and z = x + y + z ⇒ z = 14 + 14 + 3 = 31
z will be stored back in x. Hence, x = 31 and y will remain as it is. (y = 3)
Answer is 31, 3

Q71: The value of j at the end of the execution of the following C program:  (2000)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
is:
(a) 10
(b) 4
(c) 6
(d) 7
Ans: 
(a)
Sol:

  • At i = 0, j = 0
  • At i = 1, j = 1
  • At i = 2, j = 3
  • At i = 3, j = 6
  • At i = 4, j = 10

Answer: (A)

Q72: Consider the following C function definition  (1999)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The functional Trial:
(a) Finds the maximum of a, b, and c
(b) Finds the minimum of a, b, and c
(c) Finds the middle number of a, b, c
(d) None of the above
Ans: (d)
Sol:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Answer D.

Q73: Consider the following program in a language that has dynamic scooping:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Then the output of the program is:  (1999)
(a) 0.125 0.125
(b) 0.25 0.25
(c) 0.25 0.125
(d) 0.125 0.25
Ans:
(c)
Sol: 
In dynamic scoping, if a variable is not found in the local scope it is looked up on at the function from which the call is made.

Q74: What is the result of the following program?  (1998)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 5
(b) 25
(c) 36
(d) 42
Ans:
(c)
Sol:

Call by value: 36,
Call by reference: undefined behaviour for C/C++ but 42 for languages having * as a sequence point.
f(x) * f(x);
If the value of x is being modified inside the function (call by reference) we cannot be sure if this modified value or the old value will be passed as argument for the second call to f(). This is because left and right operand of any arithmetic expression in C/C++ can be evaluated in any order. For languages like Java, strict left-right order is maintained.

Q75: What value would the following function return for the input x = 95?  (1998)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 89
(b) 90
(c) 91
(d) 92
Ans: (c)
Sol:
 
Value returned by fun (95) = fun(fun(106))
= fun(96)
= fun(fun(107))
= fun(97)
= fun(fun(108))
= fun(98)
= fun(fun(109))
= fun(99)
= fun(fun(110))
= fun(100)
= fun(fun(111))
= fun(101) = 91.
Correct Answer: C

Q76: Which of the following macros can put a macro assembler into an infinite loop?  (1996)
I.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE) 
II.
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) II only
(b) I only
(c) both I and I
(d) None of the above
Ans: 
(a)
Sol: 
The macro that can put a macro assembler into an infinite loop is (ii) only.
Here's why:
Let's analyze macro M2:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
In macro M2, if the condition .IF EQ, X is true, it makes a recursive call to itself with the same argument x. This creates an infinite loop because the recursive call will keep expanding with the same argument, leading to an endless cycle.
On the other hand, macro M1 does not have a recursive call within the macro itself. It uses recursion in a way that avoids infinite looping.

Q77: A language with string manipulation facilities uses the following operations  (1995)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
For the string "acbc" what will be the output of
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) ac
(b) bc
(c) ab
(d) cc
Ans: 
(c)
Sol:

  • concat(a,head(tail(tail(acbc))))
  • concat(a,head(tail(cbc)))
  • concat(a, head(bc))
  • concat(a, b)
  • ab.

Q78: What is the value of X printed by the following program?  (1995)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) 2
(b) √2
(c) Run time error
(d) None of the above
Ans: 
(a)
Sol: 
The program is in Pascal, for which default function call is call-by-value, so value 2 is passed to the procedure FIND which performs operation on it's local variable X (which is defined as real) therefore changes would not be reflected on the global variable, the value of X = 2 will thus be written/printed.

Q79:  What are x and y in the following macro definition?  (1995)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
(a) Variables
(b) Identifiers
(c) Actual parameters
(d) Formal parameters

Ans: (d)
Sol:

  • formal parameter the identifier used in a method to stand for the value that is passed into the method by a caller.
    • For example, amount is a formal parameter of calculate
  • actual parameter - the actual value that is passed into the method by a caller.
    • For example, the see used when calculate is called is an actual parameter.
    • actual parameters are often called arguments

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

Q80: In which of the following cases is it possible to obtain different results for call-by-reference and call-by-name parameter passing methods?  (1994)
(a) Passing a constant value as a parameter
(b) Passing the address of an array as a parameter
(c) Passing an array element as a parameter
(d) Passing an array
Ans: 
(c)
Sol: 
Passing an array element as a parameter is the answer.
Consider this function call
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
Output:

  • call-by-reference: 8
  • call-by-name: 0

In Call-by-name, each occurrence of the formal parameter is replaced by the actual argument text. So, the
function fun will be executed like:
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

Q81: Refer to the PASCAL program shown below.  (1993)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

What is the scope of m declared in the main program?
(a) PARAM, P, Q
(b) PARAM, P
(c) PARAM, Q
(d) P, Q (e)
(e) none of the above
Ans: (a)

Q82: Refer to the PASCAL program shown below.  (1993)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The value of n, output by the program PARAM is:
(a) 0, because n is the actual parameter corresponding to x in procedure Q.
(b) 0, because n is the actual parameter to y in procedure Q.
(c) 1, because n is the actual parameter corresponding to x in procedure Q.
(d) 1, because n is the actual parameter corresponding to y in procedure Q.
(e) none of the above

Ans: (e)

Q83: Refer to the PASCAL program shown below.  (1993)
Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)
The value of m, output by the program PARAM is:
(a) 1, because m is a local variable in P
(b) 0, because m is the actual parameter that corresponds to the formal parameter in p
(c) 0, because both x and y are just reference to m, and y has the value 0
(d) 1, because both x and y are just references to m which gets modified in procedure P
(e) none of the above
Ans: 
(b)

Q84: What type of parameter passing mechanism (call-by-value, call-by-reference, call-by-name, or-by-value result) is the following sequence of actions trying to implement for a procedure call P(A[i]) where P (i:integer) is a procedure and A is an integer array?
Is the implementation correct? Explain and correct it if necessary. You are supposed to make only small changes  (1992)
Create a new local variable, say z.  
Assign to z the value of A[i].
Execute the body of P using z for A[i]
Set A[i] to z.?
(a) call-by-value
(b) call-by-reference
(c) call-by-name
(d) call-by-value result
Ans: (d)
Sol:
implement the call by value result parameter passing technique
as value is first copied
all changes done in local variable
and then again value copied in actual variable

The document Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Previous Year Questions: Function - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is a function in computer science?
Ans. A function in computer science is a block of code that performs a specific task. It can take input parameters, perform computations, and return a result.
2. How are functions helpful in programming?
Ans. Functions help in organizing code, promoting reusability, and making it easier to debug and maintain programs. They also allow for modularity and abstraction in programming.
3. What is the syntax for defining a function in most programming languages?
Ans. The syntax for defining a function typically includes the function keyword, followed by the function name, input parameters (if any), and the code block that defines the function's behavior.
4. Can a function return multiple values in programming languages?
Ans. Yes, some programming languages allow functions to return multiple values by using data structures like arrays, tuples, or objects to package and return multiple values.
5. What is a recursive function in programming?
Ans. A recursive function is a function that calls itself within its definition. It is often used to solve problems that can be broken down into smaller, similar sub-problems.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Objective type Questions

,

mock tests for examination

,

pdf

,

Important questions

,

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

,

Exam

,

Free

,

Semester Notes

,

practice quizzes

,

study material

,

ppt

,

Previous Year Questions with Solutions

,

shortcuts and tricks

,

Viva Questions

,

Sample Paper

,

video lectures

,

Summary

,

Extra Questions

,

MCQs

,

past year papers

,

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

,

Previous Year Questions: Function | Programming and Data Structures - Computer Science Engineering (CSE)

;