## Functions

 Question 1

Consider the following C program:

```         #include <stdio.h>
int jumble (int x, int y)  {
x = 2 * x + y ;
return x ;
}
int main ( )  {
int x=2, y=5 ;
y = jumble (y, x) ;
x = jumble (y, x) ;
printf ("%d \n", x) ;
return 0 ;
}
```

The value printed by the program is ______.

 A 26 B 67 C 25 D 13
Programming-for-Output-Problems       Functions       GATE 2019
Question 1 Explanation:
////////////////////////////////////Program
#include
int jumble(int x, int y)
{
printf("Inside jumble : 2*%d + %d\n", x,y);
x = 2*x +y;
return x;
}
int main()
{
int x=2, y=5;
printf("Initial x=%d, y=%d\n",x,y);
printf("1st jumble call : jumble(%d,%d)\n",y,x);
y = jumble(y,x);
printf("Value of y after 1st jumble = %d\n", y);
printf("2nd jumble call: jumble(%d,%d)\n", y,x);
x = jumble(y,x);
printf("Value of x after 2nd jumble = %d\n", x);
printf("Final : %d\n", x);
return 0;
}
////////////////////////////////////OUTPUT
Initial x=2, y=5
1st jumble call: jumble(5,2)
Inside jumble : 2*5 + 2
Value of y after 1st jumble = 12
2nd jumble call: jumble(12,2)
Inside jumble : 2*12 + 2
Value of x after 2nd jumble = 26
Final : 26
 Question 2

Consider the following C function.

```               void convert (int n) {
if (n < 0)
printf ("%d", n);
else {
convert (n/2);
printf ("%d", n%2);
}
}
```

Which one of the following will happen when the function convert is called with any positive integer n as argument?

 A It will print the binary representation of n in the reverse order and terminate. B It will not print anything and will not terminate. C It will print the binary representation of n and terminate. D It will print the binary representation of n but will not terminate.
Programming-for-Output-Problems       Functions       GATE 2019
Question 2 Explanation:
////////////////OUTPUT
Sequence of function calls
Convert(6)
Convert(3)
Convert(1)
Convert(0)
:
Convert(0)
:
:
It will not terminate and never produce any output.
Note:
There is no instruction which stops the loop.
 Question 3

How many onto (or surjective) functions are there from an n-element (n ≥ 2) set to a 2-element set?

 A 2n B 2n-1 C 2n-2 D 2(2n– 2)
Engineering-Mathematics       Functions       GATE 2012
Question 3 Explanation: Onto function is possible if m ≥ n. So, no. of onto functions possible is,
nm - nC1 (n-1)m + nC2 (n-2)m + .......
Here in Question,
m = n, n = 2
So, the final answer will be,
= 2n - 2C1 (2-1)n + 2C2 (2-2)n
= 2n - 2 × 1 + 0
= 2n - 2
 Question 4

Let A be a set of n(>0) elements. Let Nr be the number of binary relations on A and let Nf be the number of functions from A to A.

(a) Give the expression for Nr in terms of n.
(b) Give the expression for Nf in terms of n.
(c) Which is larger for all possible n, Nr or Nf?
 A Theory Explanation is given below.
Engineering-Mathematics       Functions       GATE 2002
 Question 5

On the set N of non-negative integers, the binary operation __________ is associative and non-commutative.

 A fog
Engineering-Mathematics       Functions       GATE 1994
Question 5 Explanation:
The most important associative operation that is not commutative is function composition. If you have two functions f and g, their composition, usually denoted fog, is defined by
(fog)(x) = f(g(x))
It is associative, (fog)oh = fo(goh), but its usually not commutative. fog is usually not equal to gof.
Note that if fog exists then gof might not even exists.
 Question 6

Consider the following C functions.

```  int fun1 (int n)  {                              int fun2 (int n)  {
static int i = 0;                                static int i = 0;
if (n > 0)  {                                    if (n > 0)  {
++i;                                              i = i + fun1 (n);
fun1 (n-1);                                       fun2 (n-1);
}                                                 }
return (i);                                           return (i);
}                                                 } ```

The return value of fun2 (5) is _______.

 A 55
Programming       Functions       GATE 2020
Question 6 Explanation:
#include
int fun1(int n) {
printf("--fun1 call--\n");
static int i = 0;
if(n>0){
++i;
printf("fun1(%d-1)\n",n);
fun1(n-1);
}
printf("fun1(%d)= %d\n",n, i);
return(i);
}
int fun2(int n) {
printf("\n******* fun2 call ********\n");
static int i = 0;
if(n>0){
printf("%d + fun1(%d)\n", i,n);
i=i+fun1(n);
fun2(n-1);
}
printf("fun2(%d)= %d\n",n, i);
return(i);
}
void main()
{
printf("final = %d\n", fun2(5));
}
Check step by step hand run of the code to understand the recursion:
******* fun2 call ********
0 + fun1(5)
--fun1 call--
fun1(5-1)
--fun1 call--
fun1(4-1)
--fun1 call--
fun1(3-1)
--fun1 call--
fun1(2-1)
--fun1 call--
fun1(1-1)
--fun1 call--
fun1(0)= 5
fun1(1)= 5
fun1(2)= 5
fun1(3)= 5
fun1(4)= 5
fun1(5)= 5
******* fun2 call ********
5 + fun1(4)
--fun1 call--
fun1(4-1)
--fun1 call--
fun1(3-1)
--fun1 call--
fun1(2-1)
--fun1 call--
fun1(1-1)
--fun1 call--
fun1(0)= 9
fun1(1)= 9
fun1(2)= 9
fun1(3)= 9
fun1(4)= 9
******* fun2 call ********
14 + fun1(3)
--fun1 call--
fun1(3-1)
--fun1 call--
fun1(2-1)
--fun1 call--
fun1(1-1)
--fun1 call--
fun1(0)= 12
fun1(1)= 12
fun1(2)= 12
fun1(3)= 12
******* fun2 call ********
26 + fun1(2)
--fun1 call--
fun1(2-1)
--fun1 call--
fun1(1-1)
--fun1 call--
fun1(0)= 14
fun1(1)= 14
fun1(2)= 14
******* fun2 call ********
40 + fun1(1)
--fun1 call--
fun1(1-1)
--fun1 call--
fun1(0)= 15
fun1(1)= 15
******* fun2 call ********
fun2(0)= 55
fun2(1)= 55
fun2(2)= 55
fun2(3)= 55
fun2(4)= 55
fun2(5)= 55
final = 55
 Question 7
Consider the following C functions.
int tob (int b, int* arr) {
int i;
for (i=0; b>0; i++) {
if (b%2) arr [i] = 1;
else arr [i] = 0;
b = b/2;
}
return (i);
}

int pp (int a, int b) {
int arr ;
int i, tot = 1, ex, len;
ex = a;
len = tob (b,arr);
for (i=0; i if (arr[i] == 1)
tot = tot * ex;
ex = ex * ex;
}
return (tot);
}
The value returned by pp(3,4) is ________.
 A 81
Programming       Functions       GATE 2020
Question 7 Explanation:
pp(3,4) ⇒
a=3,b=4
tot=1
ex=a=3
len=tob(b,arr) which is 3
[
tob(4,arr)==>
b=4
b%2 =4%2=0 Condition is false then arr=0
=> b=b/2 =4/2 =2
b=2
b%2 =2%2=0 condition is false then arr=0
=>b=b/2=2/2=1
b=1
then b%2=1%2 condition is true then arr=1
=>b=b/2=1/2=0
The i value is 3 [length is 3]
]
i=0,
arr ==1 condition is false
ex=3*3=9
i=1
arr==1 condition is false
then
ex=9*9=81
i=2
then arr==1 condition is true
tot=tot*ex=1*81=81
ex=81*81
Finally it returns tot value which 81.
 Question 8

The function f(x,y) = x2y - 3xy + 2y + x has

 A no local extremum B one local minimum but no local maximum C one local maximum but no local minimum D one local minimum and one local maximum
Engineering-Mathematics       Functions       GATE 1993
Question 8 Explanation:
Note: Out of syllabus.
 Question 9

The Laplace transform of the periodic function f(t) described by the curve below, i.e., is _________ A Out of syllabus.
Engineering-Mathematics       Functions       GATE 1993
 Question 10

Let A and B be sets with cardinalities m and n respectively. The number of one-one mappings (injections) from A to B, when m < n, is:

 A mn B nPm C mCn D nCm E mPn
Engineering-Mathematics       Functions       GATE 1993
Question 10 Explanation:
Let,
A = {a1, a2, ... am} and
B = {b1, b2, ... bn}
A one-one function 'f' assigns each element ai of A a distinct element, bj=f(ai) of Bi for a, there are n choices, for a2 there are n-1 choices, for am there are (n-(m-1)) choices.
i.e., Question 11
Study the following program: //precondition: x>=0 public void demo(int x) { System.out.print(x % 10); if (x % 10 != 0) { demo(x/10); } System.out.print(x%10); } Which of the following is printed as a result of the call demo(1234)?
 A 1441 B 3443 C 12344321 D 43211234
Programming       Functions       ISRO-2007
Question 11 Explanation:
demo(1234) ---> First it will print “4” then 4!=10 condition is true again it call demo(123)
demo(123)------> It will print “3” then 3!=10 condition is true again it call demo(12)
demo(12)------> It will print “2” then 2!=10 condition is true again it call demo(1)
demo(1)------> It will print “1” then 1!=10 condition is true again it call demo(0)
It will print again 1,2,3 and 4 as there is another Print statement is in each function call.
 Question 12
What is the output of this C++ program? A 910 B 920 C 870 D 900
Programming-for-Output-Problems       Functions       ISRO-2017 May
Question 12 Explanation:
Step-1: In main function, number variable has been initialized to 30.
Step-2: We pass address of number as parameters in the square function. As per the concept of function overloading in C++ the second square function will be executed.
Step-3: --(*y) will be executed first since decrement operator has the higher precedence than multiplication.
*x = (*x) * --(*y);
*x = 30 * 29;
*x = 870
Note: x is a pointer variable and it holds the address of the variable number.
 Question 13
Consider the following C function A Call swap (a, b) B Call swap (&a, &b) C swap(a, b) cannot be used as it does not return any value D swap(a, b) cannot be used as the parameters passed by value
Programming       Functions       ISRO-2017 May
Question 13 Explanation: Question 14
What is the output of the following program? A 20 10 10 B 20 10 20 C 20 20 20 D 10 10 10
Programming       Functions       ISRO-2017 May
Question 14 Explanation:
Step-1: Every program starts execution from main() function.
Step-2: By default tmp value 20 will print initially.
Step-3: It calls fun() then it will print 10
Step-4: Again we are in main() function and printing value 20. Actually, the static value will return because its scope is a lifetime. But we are given tmp value as global. So, it prints 20 instead of 10.
 Question 15
The cubic polynomial y(x) which takes the following values:
y(0) = 1,
y(1) = 0,
y(2) = 1,
y(3) = 10 is
 A x3 +2 x2 + 1 B x3 + 3x2 + 1 C x3 + 1 D x3 – 2 x2 + 1
Engineering-Mathematics       Functions       ISRO CS 2009
Question 15 Explanation:
Substitute the values of x (0,1,2 and 3) in the options and check which option gives the corresponding y(x) values. The function x3 – 2 x2 + 1 gives the required values.
x = 0: y(x) = 1
x = 1: y(x) = 0
x = 2: y(x) = 8 - 8 + 1 = 1
x = 3: y(x) = 27 - 18 + 1 = 10
 Question 16
Consider the polynomial, p(x) = a0 + a1X + a2X2 + a3X3 where ai ≠ 0, ∀i . The minimum number of multiplications needed to evaluate p on an input X is:
 A 3 B 4 C 6 D 9
Engineering-Mathematics       Functions       ISRO CS 2009
Question 16 Explanation:
P(X)=a0+a1+a2X2+a3X3
=a0+X(a1+X(a2+a3X))
↓ ↓ ↓
③ ② ①
Here, minimum 3 multiplication needed
 Question 17
The shift operator E is defined as E[f(xi)] = f(xi + h) and E'[f(xi)] = f(xi – h) then △ (forward difference) in terms of E is
 A E-1 B E C 1 – E-1 D 1 – E
Engineering-Mathematics       Functions       ISRO CS 2009
Question 17 Explanation: Question 18
A root of equation f(x) = 0 can be computed to any degree of accuracy if a ‘good’ initial approximation x0 is chosen for which
 A f (x0) > 0 B f (x0) f (x0)” > 0 C f (x0) f (x0)” < 0 D f (x0)” > 0
Engineering-Mathematics       Functions       ISRO CS 2009
 Question 19
What is the least value of the function f(x) = 2x2– 8x – 3 in the interval [ 0 , 5] ?
 A -15 B 7 C -11 D -3
Engineering-Mathematics       Functions       ISRO CS 2013
Question 19 Explanation:
One method is trial and error method
Substitute all the value of x from 0 to 5

The value of f(x) is -3 when x=0
The value of f(x) is -9 when x=1
The value of f(x) is -11 when x=2
The value of f(x) is -9 when x=3
The value of f(x) is -3 when x=4

The value of f(x) is -7 when x=5

so the least value is -11
Second method:
→We can solve this one by using derivatives.
→Given function is f(x) = 2x2 -8x -3
→ f'(x)=4x-8 (first derivative)
a f''(x)=4 (Second derivative)
Here we got constant value which means that it has minimal value at the point x=2
So we can find the minimum value by substituting value 2 in place of “x” in the given function.
Minimum value is 2⨉(2)2 -8⨉(2) -3= -11
 Question 20

Given below are three implementations of the swap( ) function in C++ : Which of these would actually swap the contents of the two integer variables p and q ?

 A (a) only B (b) only C (c) only D (b) and (c) only
Programming-in-c++       Functions       UGC-NET CS 2018 JUNE Paper-2
Question 20 Explanation:
Module - (a) is call by value. So, by using that code w can not swap the contents.
Module - (b) is call by reference. So, the modification of content in the function reflects the changes in the main program. Swapping is done in the module - (b).
Module - (c) is passing addresses as parameters but in the functions definition. We are changing the addresses not the content. So, swapping of the values can’t be done.
 Question 21
The keyboard used to transfer control from a function back to the calling function is:
 A Switch B Go to C go back D Retun
Programming       Functions       Nielit Scientist-B IT 4-12-2016
Question 21 Explanation:
● The return statement terminates the execution of a function and it returns the control to the calling function.
● The execution resumes in the calling function at a point immediately following the call.
 Question 22
Consider the following function definition.
void greet (int n)
{
if (n>0)
{
printf("hello");
greet(n-1);
}
printf("world");
}
If you run greet(n) for some non-negative integer n, what would it print?
 A n times “hello”, followed by n + 1 times “world” B n times “hello”, followed by n times “world” C n times “helloworld” D n + 1 times “helloworld” E n times “helloworld”, followed by “world”
Programming       Functions       TIFR PHD CS & SS 2018
 Question 23
Output of following program?
#include
void dynamic(int s,..)
{
printf("%d",s);
}
int main()
{
dynamic(2,4,6,8);
dynamic(3,6,9);
return 0;
}
 A 23 B compile error C 43 D 32
Programming       Functions       Nielit Scientist-B IT 22-07-2017
Question 23 Explanation:
● The latest compiler giving compilation error ​ “error: expected declaration specifiers or ‘...’ before ‘.’ toke​ n“
● Old compiler may support that syntax, in that syntax only first argument is defined which consists of remainings arguments but not defined.
● For the function call dynamic(2,4,6,8), first argument 2 is printed.
● For the function call dynamic(3,6,9);first argument 3 is printed.
 Question 24
The following program
main()
{
inc();
inc();
inc();
}
inc()
{
static int x;
printf("%d", ++x);
}
 A prints 012 B prints 123 C prints 3 consecutive, but unpredictable numbers D prints 111
Programming-for-Output-Problems       Functions       ISRO CS 2015
Question 24 Explanation:
In this question, we have to remind one point about “static” storage class.
Static storage class by default value is 0.
Step-1: We are calling inc( ) in main function. So, it will enter into inc() function.
Here, we are using pre increment function, so it replaced 0 with 1.
Step-2: Second inc() in main function. So, it will enter into inc() function.
Here, we are using pre increment function, so it replaced 1 with 2.
Step-3: Third inc() in main function. So, it will enter into inc() function.
Here, we are using pre increment function, so it replaced 2 with 3.
Note: The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope.
 Question 25

In C language ______ is a process in which a function calls itself repeatedly until some specified condition has been satisfied

 A Infinite loop B Call by value C Call by reference D Recursion
Programming       Functions       JT(IT) 2018 PART-B Computer Science
Question 25 Explanation:
Recursion is a process in which a function calls itself repeatedly until some specified condition has been satisfied.
 Question 26
A member function can always access the data in __________ , (in C++).
 A the class of which it is member B the object of which it is member C the public part of its class D the private part of its class
Programming-in-c++       Functions       UGC NET CS 2017 Nov- paper-2
Question 26 Explanation:
→ A member function can always access the data in the class of which it is member.
→ Functions within classes can access and modify (unless the function is constant) data members without declaring them, because the data members are already declared in the class.
 Question 27
Which of the following is not correct (in C++) ?
 A Class templates and function templates are instantiated in the same way B Class templates differ from function templates in the way they are initiated C Class template is initiated by defining an object using the template argument D Class templates are generally used for storage classes E None of the above
Programming-in-c++       Functions       UGC NET CS 2017 Nov- paper-2
Question 27 Explanation:
(1) TRUE: Class templates and function templates are instantiated in the same way
(2) FALSE: Class templates differ from function templates in the way they are initiated
(3) FALSE: Class template is initiated by defining an object using the template argument
(4)FALSE: Class templates are generally used for storage classes
 Question 28
Which of the following cannot be passed to a function in C++ ?
 A Constant B Structure C Array D Header file
Programming-in-c++       Functions       UGC NET CS 2017 Jan -paper-2
Question 28 Explanation:
→ Header files contain definitions of Functions and Variables, which is imported or used into any C++ program by using the pre-processor #include statement. Header file have an extension ".h" which contains C++ function declaration and macro definition.
→ Header file is a library file, we can not passed to a function in C++.
→ We can pass constant,Structure and Array
 Question 29
If a function is friend of a class, which one of the following is wrong?
 A A function can only be declared a friend by a class itself. B Friend functions are not members of a class, they are associated with it. C Friend functions are members of a class. D It can have access to all members of the class, even private ones.
OOPS       Functions       UGC NET CS 2016 Aug- paper-2
Question 29 Explanation:
→ A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
TRUE: function can only be declared a friend by a class itself.
TRUE: Friend functions are not members of a class, they are associated with it.
FALSE:Friend functions are members of a class.
TRUE: It can have access to all members of the class, even private ones.
 Question 30
A function template in C++ provides _____ level of generalization.
 A 4 B 3 C 2 D 1
Programming-in-c++       Functions       UGC NET CS 2016 Aug- paper-2
Question 30 Explanation:
→ ​ A function template in C++ provides 2 level of generalization.
→ Templates are a feature of the C++ programming language that allows functions and classes to operate with generic types. This allows a function or class to work on many different data types without being rewritten for each one.
 Question 31
When a method in a subclass has the same name and type signatures as a method in the super class, then the method in the subclass _____ the method in the super class.
 A Overloads B Friendships C Inherits D Overrides
OOPS       Functions       UGC NET CS 2016 July- paper-2
Question 31 Explanation:
→ When a method in a subclass has the same name and type signatures as a method in the superclass, then the method in the subclass overrides the method in the superclass.
 Question 32
A function object :
 A is an instance of a class for which operator ( ) is a member function. B is an instance of a class for which operator → is a member function. C is a pointer to any function D is a member function of a class
Programming-in-c++       Functions       UGC NET CS 2004 Dec-Paper-2
Question 32 Explanation:
A function object is an object to which the function call operator can be applied. Typically, it is a class that defines the function call operator (operator()()) as a member function. When a function object is used as a function, the function call operator is invoked whenever the function is called.
 Question 33
Non modifiable procedures are called
 A Serially useable procedures B Concurrent procedures C Reentrant procedures D Top down procedures
Programming       Functions       UGC NET CS 2004 Dec-Paper-2
Question 33 Explanation:
→ A computer program or subroutine is called reentrant if it can be interrupted in the middle of its execution and then safely be called again ("re-entered") before it’s previous invocation complete execution.
→ The interruption could be caused by an internal action such as a jump or call, or by an external action such as an interrupt or signal. The previous invocations may resume correct execution before the reentered invocation completes, unlike recursion, where the previous invocations may only resume correct execution once the reentered invocation completes.
Rules:
1. Reentrant code may not hold any static or global non-constant data.
2. Reentrant code may not modify itself.
3. Reentrant code may not call non-reentrant computer programs or routines.
 Question 34
Data members and member function of a class by default is respectively :
 A private and public B public C public and private D private
Programming-in-c++       Functions       UGC NET CS 2005 Dec-Paper-2
Question 34 Explanation:
If we do not specify any access modifiers for the members inside the class then by default the access modifier for the members will be Private.
 Question 35
Nonmodifiable procedures are called :
 A Serially usable procedure B Concurrent procedure C Reentrant procedure D Top down procedure
Programming       Functions       UGC NET CS 2005 Dec-Paper-2
Question 35 Explanation:
→ A computer program or subroutine is called reentrant if it can be interrupted in the middle of its execution and then safely be called again ("re-entered") before it’s previous invocation complete execution. → The interruption could be caused by an internal action such as a jump or call, or by an external action such as an interrupt or signal. The previous invocations may resume correct execution before the reentered invocation completes, unlike recursion, where the previous invocations may only resume correct execution once the reentered invocation completes.
Rules:
1. Reentrant code may not hold any static or global non-constant data.
2. Reentrant code may not modify itself.
3. Reentrant code may not call non-reentrant computer programs or routines.
 Question 36
When a function is recursively called, all automatic variables :
 A are initialized during each execution of the function B are retained from the last execution C are maintained in a stack D are ignored
Programming       Functions       UGC NET CS 2006 Dec-paper-2
Question 36 Explanation:
→ When a function is recursively called, all automatic variables are initialized during each execution of the function. → Automatic local variables primarily applies to recursive lexically-scoped languages. An automatic variable is a local variable which is allocated and deallocated automatically when program flow enters and leaves the variable's scope. The scope is the lexical context, particularly the function or block in which a variable is defined.
 Question 37
If we define the functions f, g and h that map R into R by : f(x) = x​ 4​ , g(x) = √ x 2 + 1 , h(x) = x​ 2​ + 72, then the value of the composite functions ho(gof) and (hog)of are given as
 A x​ 8​ – 71 and x​ 8​ – 71 B x​ 8​ – 73 and x​8​ – 73 C x​ 8​ + 71 and x​ 8​ + 71 D x​ 8​ + 73 and x​ 8​ + 73
Engineering-Mathematics       Functions       UGC NET CS 2014 Dec-Paper-2
Question 37 Explanation:
Given f(x) = x​ 4​ , g(x) = √ x 2 + 1 , h(x) = x​ 2​ + 72
for, ho(gof)
gof=g(f(x))
=g(x​ 4​ )
=√(x​ 8​ +1)
ho(gof)=h(gof)
=h(√(x​ 8​ +1))
=(√(x​ 8​ +1)​ 2​ +72
=x​ 8​ +1+72
=x​ 8​ +73
√ x 2 + 1 , h(x) = x​ 2​ + 72
for, (hog)of,
hog=h(g(x))
=h(√(x​ 2​ +1)
=(√(x​ 2​ +1)​ 2​ +72
=x​ 2​ + 1+72
=x​ 2​ +73
(hog)of=(hog)(f(x))
=(hog)(x​ 4​ )
=(x​ 4​ )​ 2​ +73
=x​ 8​ +73
Hence, option-D is the correct answer
 Question 38
When we pass an array as an argument to a function, what actually gets passed ?
 A Address of the array B Values of the elements of the array C Base address of the array D Number of elements of the array
Programming       Functions       UGC NET CS 2014 June-paper-2
Question 38 Explanation:
When we pass an array as an argument to a function it actually gets passed base address of the array.
 Question 39
A friend function can be used to
 A avoid arguments between classes. B allow access to classes whose source code is unavailable. C allow one class to access an unrelated class. D None of the above
Programming-in-c++       Functions       UGC NET CS 2014 June-paper-2
Question 39 Explanation:
A friend function of a class is defined outside that class scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.
→A function can only be declared a friend by a class itself.
→It can have access to all members of the class, even private ones.
→ A friend function can be used to allow one class to access an unrelated class.
 Question 40
Which of the following is the correct value returned to the operating system upon the successful completion of a program ?
 A 0 B 1 C -1 D Program do not return a value.
Programming       Functions       UGC NET CS 2014 June-paper-2
Question 40 Explanation:
The operating system will return 0 when it’s successfully execute a program.
 Question 41
Given below are three implementations of the swap( ) function in C++ : Which of these would actually swap the contents of the two integer variables p and q ?
 A (a) only B (b) only C (c) only D (b) and (c) only
Programming       Functions       UGC NET CS 2018 JUNE Paper-2
Question 41 Explanation:
Module -(a) is call by value , so by using that code w can not swap the contents.
Module -(b) is call by reference , So the modification of content in the function reflects the changes in the main program. Swapping is done in the module -(b).
Module -(c) is passing addresses as parameters but in the functions definition , We are changing the addresses not the content So swapping of the values can’t be done.
 Question 42
Match the following in List - I and List - II, for a function f : A (a)-(i), (b)-(ii), (c)-(iii) B (a)-(iii), (b)-(ii), (c)-(i) C (a)-(ii), (b)-(i), (c)-(iii) D (a)- (ii), (b)-(iii), (c)-(i)
Engineering-Mathematics       Functions       UGC NET CS 2018 JUNE Paper-2
Question 42 Explanation:
● The function is injective (one-to-one) if each element of the codomain is mapped to by at most one element of the domain. An injective function is an injection. Notationally:
∀ x, x ′ ∈ X , f (x) = f (x ′ ) ⇒ x = x ′
Or, equivalently (using logical transposition),
∀ x, x ′ ∈ X , x = / x ′ ⇒ f (x) = / f (x ′ )
● The function is surjective (onto) if each element of the codomain is mapped to by at least one element of the domain. (That is, the image and the codomain of the function are equal.) A surjective function is a surjection. Notationally:
∀ y ∈ Y , ∃x ∈ X such that y = f (x)
A constant function is a ​ function​ whose (output) value is the same for every input value. For example, the function y(x) =4 is a constant function because the value y(x) is 4 regardless of the input value x
 Question 43
_______ is often used to prove the correctness of a recursive function.
 A Diagonalization B Communitivity C Mathematical Induction D Matrix Multiplication
Programming       Functions       UGC NET CS 2013 Dec-paper-2
Question 43 Explanation:
→ Mathematical Induction is often used to prove the correctness of a recursive function.
→ Mathematical Induction is a special way of proving things. It has only 2 steps:
Step-1: Show it is TRUE for the first one
Step-2: Show that if any one is TRUE then the next one is true
Then all are true
 Question 44
Consider the function
find (int x, int y)
{return ((x < y) ? 0 : (x - y)) ; }
Let a, b be two non-negative integers. The call find {a, find(a, b)} can be used to find the
 A maximum of a, b B positive difference of a, b C sum of a, b D minimum of a, b
Programming       Functions       NIELIT Technical Assistant_2016_march
 Question 45
How many constructors can a class have ?
 A zero B 2 C 2 D any number
Programming-in-c++       Functions       UGC NET CS 2008 Dec-Paper-2
Question 45 Explanation:
There is no restriction to to have number of constructors in a lass.
 Question 46
Consider the code below, defining the function mystery: mystery(a,b){ if (a < 0 or b < 0) return 0; else if (a == 0) return b+1; else if (b == 0) return mystery(a-1,1); else return mystery(a-1, mystery(a,b-1)); } (i) Express mystery(1, n) as a function of n. (ii) Express mystery(2, n) as a function of n. (iii) Compute mystery(3, 2) and mystery(3, 3).
 A Descriptive Explanation
Programming       Functions       CHENNAI MATHEMATICAL INSTITUTE (M.Sc. / Ph.D. Programme in Computer Science)15 May 2013
Question 46 Explanation:
(i) mystery(1, n) = n + 2. We prove this by induction on n.
Clearly mystery(1, 0) = mystery(0, 1) = 1 + 1 = 2 = 0 + 2. Assuming that mystery(1, n − 1) = n + 1, mystery(1, n) = mystery(0, mystery(1, n − 1)) = mystery(0, n + 1) = (n + 1) + 1 = n + 2.
(ii) mystery(2, n) = 2n + 3, and we again prove this by induction on n.
Clearly mystery(2, 0) = mystery(1, 1) = 1 + 2 = 3 = 2 · 0 + 3. Assuming that
mystery(2, n−1) = 2(n−1)+3 = 2n+1, mystery(2, n) = mystery(1, mystery(2, n−1)) = mystery(1, 2n + 1) = (2n + 1) + 2 = 2n + 3.
(iii) mystery(3, 0) = mystery(2, 1) = 2 + 3 = 5.
mystery(3, 1) = mystery(2, mystery(3, 0)) = mystery(2, 5) = 2 · 5 + 3 = 13.
mystery(3, 2) = mystery(2, mystery(3, 1)) = mystery(2, 13) = 2 · 13 + 3 = 29.
mystery(3, 3) = mystery(2, mystery(3, 2)) = mystery(2, 29) = 2 · 29 + 3 = 61.
 Question 47
Consider the code below, defining the functions f and g: f(m, n) { if (m == 0) return n; else { q = m div 10; r = m mod 10; return f(q, 10*n + r); } } g(m, n) { if (n == 0) return m; else { q = m div 10; r = m mod 10; return g(f(f(q, 0), r), n-1); } } (a) Compute g(3, 7), g(345, 1), g(345, 4) and g(345, 0). (b) What does g(m, n) compute, for nonnegative numbers m and n? (c) How much time does it take to compute f (m, n) and g(m, n)?
 A Descriptive Explanation
Programming       Functions       CHENNAI MATHEMATICAL INSTITUTE(M.Sc. / Ph.D. Programme in Computer Science) 18May 2015
Question 47 Explanation:
In the following, by “reverse of a number a” we mean the number got by reversing the digits of a.
We first prove by induction on the number of digits in m that the result of f (m, n) is the reverse of m appended to n.
• If m = 0, then f (m, n) = n, as we require.
• If m < 10, then q = 0 and r = m, and a recursive call is made to f (0, 10n + m) = 10n + m, which is the reverse of the digits of m appended to n.
• If m > 10, then r is the last digit of m, and q is all the other digits. There is a recursive call to f (q, 10n + r), whose output (by induction hypothesis applied on q, which has one fewer digit than m) is the reverse of q appended to 10n + r, which is exactly the reverse of m appended to n.
Now f (a, 0) is the reverse of a, and f (f (a, 0), b) is the reverse of reverse of a appended to b, or in other words, a appended to b. Thus g(m, 1) appends to the last digit of m the other digits of m—performing a right rotate of m, in other words. g(m, n) is just n right rotates performed on m.
It follows that g(3, 7) = 3, g(345, 1) = 534 and g(345, 0) = 345.
It is also easy to see from the code that f (m, n) takes O(log m) time and g(m, n) takes O(n · log m) time.
 Question 48
What does the following function compute in terms of n and d, for integer values of d? Note that the operation / denotes floating point division, even if the arguments are both integers. function foo(n,d){ if (d == 0){ return 1; }else{ if (d < 0){ return foo(n,d+1)/n; }else{ return n*foo(n,d-1); }}}
 A log d n if d < 0, n d if d > 0. B n d for all values of d. C n × d if d > 0, n ÷ d if d < 0. D n × d for all values of d.
Programming       Functions       CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science (18 May 2017)
Question 48 Explanation:
For d > 0, this computes n d by repeated multiplication. For d negative, the answer is 1/nd by repeated division.
 Question 49
Consider the following functions f() and g(). f(){ w = 5; w = 2*z + 2; } g(){ z = w+1; z = 3*z - w; print(z); } We start with w and z set to 0 and execute f() and g() in parallel—that is, at each step we either execute one statement from f() or one statement from g(). Which of the following is not a possible value printed by g()?
 A -2 B -1 C 2 D 4
Programming       Functions       CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science (18 May 2017)
Question 49 Explanation:
The possible values are : {-1,-2,3,4,7,13}, corresponding to the interleavings below. Question 50
Consider the following function that takes as input a sequence A of integers with n elements, A, A, . . . , A[n] and an integer k and returns an integer value. The function length(S) returns the length of sequence S. Comments start with //. function mystery(A, k){ n = length(A); if (k > n) return A[n]; v = A; AL = [ A[j] : 1 <= j <= n, A[j] < v ]; // AL has elements < v in A Av = [ A[j] : 1 <= j <= n, A[j] == v ]; // Av has elements = v in A AR = [ A[j] : 1 <= j <= n, A[j] > v ]; // AR has elements > v in A if (length(AL) >= k) return mystery(AL,k); if (length(AL) + length(Av) >= k) return v; return mystery(AR, k - (length(AL) + length(Av))); } (a) Explain what the function computes. (b) What is the worst-case complexity of this algorithm in terms of the length of the input sequence A? (c) Give an example of a worst-case input for this algorithm.
 A Descriptive Explanation
Programming       Functions       CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science (18 May 2017)
Question 50 Explanation:
(a) mystery(A, k) finds the kth element in A in ascending order. This is a divide-and- conquer selection algorithm, similar to quicksort.
(b) The worst case complexity is O(n 2 ) because of the fixed choice of pivot, as in quicksort.
(c) A typical worst case input would be, for instance, where A is in ascending order and k = n.
 Question 51
In the code fragment to the right, start and end are integer values and prime(x) is a function that returns true if x is a prime number and false otherwise. At the end of the loop: i := 0; j := 0; k := 0; for (m := start; m <= end; m := m+1){ if (prime(m)){ i := i + m; k := k - m; }else{ j := j - m; k := k + m; } }
 A k == i-j. B k == j-i. C k == -j-i. D Depends on start and end.
Programming       Functions       CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science (18 May 2016)
Question 51 Explanation:
The value of i + j + k is 0 initially, and also at the end of each iteration of the loop. Thus k = −j − i at the end of the loop.
 Question 52
Consider the function M defined as follows: (a) Compute the following. i. M (101) ii. M (99) iii. M (87) (b) Give a constant-time algorithm that computes M (n) on input n. (A constant- time algorithm is one whose running time is independent of the input n.)
 A Explanation
Programming       Functions       CHENNAI MATHEMATICAL INSTITUTE M.Sc. / Ph.D. Programme in Computer Science (18 May 2016)
Question 52 Explanation:
(a) M (101) = M (99) = M (87) = 91
(b) The following is a simple algorithm for computing M . It just compares n with 100, and either subtracts 10 from n or returns a constant, all of which are constant- time operations.
if (n> 100) return (n-10) else return 91
To prove that the algorithm is correct, we need to show that M (n) = 91 for n < 101. Notice first that M (101) = 91. From the following claim, it follows that M (n) = 91 for all n ≤ 100
Claim: For all k ≥ 1 and all n such that 101 − 11k ≤ n < 101 − 11(k − 1), M (n) = 91.
The proof of the claim is by induction on k.
Suppose k = 1 and 90 ≤ n < 101.
M (n)
= M (M (n + 11))
= M (n + 11 − 10) ( since n + 11 ≥ 101)
= M (n + 1)
Therefore, M (90) = M (91) = · · · = M (100) = M (101) = 91.
Assume the claim holds for k ≤ m. Consider n such that 101 − 11k ≤ n < 101 − 11(k − 1) for k = m + 1.
M (n) = M (M (n + 11))
= M (91) (M (n + 11) = 91, by IH )
= 91 by base case
Thus the claim holds for all k, and hence M (n) = 91 for all n < 101.
 Question 53
What are the possible values of gcd(7p + 94, 7p 2 + 97p + 47), where p is an arbitrary integer?
 A Either 1 or 94 B Either 94 or 47 C Either 1 or 47 D None of these
Programming       Functions       CHENNAI MATHEMATICAL INSTITUTE - M.Sc. / Ph.D. Programme in Computer Science ( 15 May 2014 )
Question 53 Explanation:
Use Euclid’s algorithm for gcd.
 Question 54
Consider the code below, defining the function A: A(m, n, p) { if (p == 0) return m+n; else if (n == 0 && p == 1) return 0; else if (n == 0 && p == 2) return 1; else if (n == 0) return m; else return A(m, A(m,n-1,p), p-1); } (a) Express A(m, n, 1) as a function of m and n. (b) Express A(m, n, 2) as a function of m and n. (c) Compute A(2, 2, 3) and A(2, 3, 3).
 A Explanation
Programming       Functions       CHENNAI MATHEMATICAL INSTITUTE - M.Sc. / Ph.D. Programme in Computer Science ( 15 May 2014 )
Question 54 Explanation:
(a) Evaluating A(m, n, 1) for small values of n suggests that A(m, n, 1) = mn. We verify it using induction. The base case is clear: A(m, 0, 1) = 0 = m · 0. For the induction step, assuming that A(m, n − 1, 1) = m(n − 1), we have
A(m, n, 1) = A(m, A(m, n − 1, 1), 0) = A(m, m(n − 1), 0) = m + m(n − 1) = mn.
(b) Evaluating A(m, n, 2) for small values of n suggests that A(m, n, 2) = m n . We verify it using induction. The base case is clear: A(m, 0, 2) = 1 = m 0 . For the induction step, assuming that A(m, n − 1, 2) = m n−1 , we have
A(m, n, 2) = A(m, A(m, n − 1, 2), 1) = A(m, m n−1 , 1) = m · m n−1 = m n.
(c) A(2, 0, 3) = 2.
A(2, 1, 3) = A(2, A(2, 0, 3), 2) = A(2, 2, 2) = 2 2 = 4. A(2, 2, 3) = A(2, A(2, 1, 3), 2) = A(2, 4, 2) = 2 4 = 16. A(2, 3, 3) = A(2, A(2, 2, 3), 2) = A(2, 16, 2) = 2 16 = 65536.
 Question 55
Consider the following C++ function f() :
unsigned int f(unsigned int n)
{
unsigned int b=0;
while(n)
{
b+=n&1;
n>>=1;
}
return b;
}
The function f() returns the int that represents the___p___in the binary representation of positive integer n, where P is
 A number of 0's B number of bits C number of consecutive 1's D number of 1's
Programming-in-c++       Functions       UGC NET June-2019 CS Paper-2
Question 55 Explanation:
& operator does the binary and operation of both value and 1
when b += n & 1 is given
n and 1, both will be converted to binary and binary & operation is calculated
any number & 000000001
it will only compare the last bit of both numbers, if both right most bit is 1 then the value is incremented by 1 and the number n is right shifted that means it loses the value of right most bit and again new right most bit is binary anded with 1.
So the value is nothing but the number of 1's of the binary format of number.
 Question 56
Consider the following recursive C function that takes two arguments
unsigned int rer ( unsigned int n, unsigned int r){
If (n > 0) return ( n%r + rer (n/r,r));
else return 0;
}
What is the return value of the function rer when it is called as rer (513, 2 )?
 A 9 B 8 C 5 D 2
Programming       Functions       ISRO CS 2020
Question 56 Explanation:
Given input function is rer(513, 2)
Condition: n>0 Sum of bits=2 if they given 513. It represented in binary.
 Question 57
In the following procedure
Integer procedure P(X,Y);
Integer X,Y;
value x;
Begin
K=5;
L=8
P=x+y;
End
X is called by value and Y is called by name. If the procedure were invoked by the following program fragment
K=0
L=0
Z=P(K,L);
then the value Z would be set equal to
 A 5 B 8 C 13 D 0
Programming       Functions       ISRO CS 2020
 Question 58

The time complexity of the following C function is (assume n>0)

```int recursive (int n)
{
if (n==1) return(1);
else return(recursive (n-1) + recursive(n-1));
}```
 A O(n) B O(n log n) C O(n2) D O(2n)
Programming       Functions       APPSC-2016-DL-CA
Question 58 Explanation:
T(n) = 2T(n-1)+a is the recurrence equation found from the code given.
Note ‘a’ is a constant O(1) cost that the non-recursive part of the function takes.
Solving the recurrence by Back Substitution:
T(n) = 2T(n-1)+a
T(n-1) = 2T(n-2)+a
T(n-2) = 2T(n-3)+a
:
:
Thus we can write the equation as follows
T(n) = 2(2T(n-2)+a)+a = 4T(n-2) + 2a + a
T(n) = 4(2T(n-3)+a) + 2a + a = 8T(n-3) + 4a + 2a + a
:
:
:
T(n) = 2k T(n-k) + (2k - 1)a
On substituting limiting condition,
T(1) = 1
=> n - k = 1
=> k = n-1
Therefore, our solution becomes
2n-1 + (2n-1 - 1)a
= O(2n)
 Question 59
```int function (int x, int y)
{
if(y<=0) return x;
return function (y, x%y);
} ```

The above recursive function computes ______

 A GCD of X and Y B xy C yx D HCF of x and y E Both A and D
Programming       Functions       CIL 2020
Question 59 Explanation:
Let’s take x=3, y=5 There are 59 questions to complete.