## Functions

Question 1 |

**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)?

1441 | |

3443 | |

12344321 | |

43211234 |

Question 1 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.

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 2 |

What is the output of this C++ program?

910 | |

920 | |

870 | |

900 |

Question 2 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.

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 3 |

Consider the following C function

Call swap (a, b) | |

Call swap (&a, &b) | |

swap(a, b) cannot be used as it does not return any value | |

swap(a, b) cannot be used as the parameters passed by value |

Question 3 Explanation:

Question 4 |

What is the output of the following program?

20 10 10 | |

20 10 20 | |

20 20 20 | |

10 10 10 |

Question 4 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.

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 5 |

The cubic polynomial y(x) which takes the following values:

y(0) = 1,

y(1) = 0,

y(2) = 1,

y(3) = 10 is

y(0) = 1,

y(1) = 0,

y(2) = 1,

y(3) = 10 is

x3 +2 x2 + 1 | |

x3 + 3x2 + 1 | |

x3 + 1 | |

x3 – 2 x2 + 1 |

Question 5 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 x

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

^{3}– 2 x^{2}+ 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 6 |

Consider the polynomial, p(x) = a

_{0}+ a_{1}X + a_{2}X^{2}+ a_{3}X^{3}where a_{i}≠ 0, ∀i . The minimum number of multiplications needed to evaluate p on an input X is:3 | |

4 | |

6 | |

9 |

Question 6 Explanation:

P(X)=a

=a

↓ ↓ ↓

③ ② ①

Here, minimum 3 multiplication needed

_{0}+a_{1}+a_{2}X^{2}+a_{3}X^{3}=a

_{0}+X(a_{1}+X(a_{2}+a_{3}X))↓ ↓ ↓

③ ② ①

Here, minimum 3 multiplication needed

Question 7 |

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

E-1 | |

E | |

1 – E ^{-1} | |

1 – E |

Question 7 Explanation:

Question 8 |

**A root of equation f(x) = 0 can be computed to any degree of accuracy if a ‘good’ initial approximation x**

**0**

**is chosen for which**

f (x _{0}) > 0 | |

f (x _{0}) f (x_{0})” > 0 | |

f (x _{0}) f (x_{0})” < 0 | |

f (x _{0})” > 0 |

Question 9 |

What is the least value of the function f(x) = 2x

^{2}– 8x – 3 in the interval [ 0 , 5] ?-15 | |

7 | |

-11 | |

-3 |

Question 9 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) = 2x

→ 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)

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) = 2x

^{2}-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= -11Question 10 |

The keyboard used to transfer control from a function back to the calling function is:

Switch | |

Go to | |

go back | |

Retun |

Question 10 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.

● The execution resumes in the calling function at a point immediately following the call.

Question 11 |

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;

}

#include

void dynamic(int s,..)

{

printf("%d",s);

}

int main()

{

dynamic(2,4,6,8);

dynamic(3,6,9);

return 0;

}

23 | |

compile error | |

43 | |

32 |

Question 11 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.

● 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 12 |

The following program

main()

{

inc();

inc();

inc();

}

inc()

{

static int x;

printf("%d", ++x);

}

main()

{

inc();

inc();

inc();

}

inc()

{

static int x;

printf("%d", ++x);

}

prints 012 | |

prints 123 | |

prints 3 consecutive, but unpredictable numbers | |

prints 111 |

Question 12 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.

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 13 |

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

Infinite loop
| |

Call by value
| |

Call by reference
| |

Recursion |

Question 13 Explanation:

Recursion is a process in which a function calls itself repeatedly until some specified condition has been satisfied.

Question 14 |

A member function can always access the data in __________ , (in C++).

the class of which it is member | |

the object of which it is member | |

the public part of its class | |

the private part of its class |

Question 14 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.

→ 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 15 |

Which of the following is not correct (in C++) ?

Class templates and function templates are instantiated in the same way | |

Class templates differ from function templates in the way they are initiated | |

Class template is initiated by defining an object using the template argument | |

Class templates are generally used for storage classes | |

None of the above |

Question 15 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

(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 16 |

Which of the following cannot be passed to a function in C++ ?

Constant | |

Structure | |

Array | |

Header file |

Question 16 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

→ Header file is a library file, we can not passed to a function in C++.

→ We can pass constant,Structure and Array

Question 17 |

If a function is friend of a class, which one of the following is wrong?

A function can only be declared a friend by a class itself. | |

Friend functions are not members of a class, they are associated with it. | |

Friend functions are members of a class. | |

It can have access to all members of the class, even private ones. |

Question 17 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.

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 18 |

A function template in C++ provides _____ level of generalization.

4 | |

3 | |

2 | |

1 |

Question 18 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.

→ 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 19 |

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.

Overloads | |

Friendships | |

Inherits | |

Overrides |

Question 19 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 20 |

A function object :

is an instance of a class for which operator ( ) is a member function. | |

is an instance of a class for which operator → is a member function. | |

is a pointer to any function | |

is a member function of a class |

Question 20 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 21 |

Data members and member function of a class by default is respectively :

private and public | |

public | |

public and private | |

private |

Question 21 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 22 |

When a function is recursively called, all automatic variables :

are initialized during each execution of the function | |

are retained from the last execution | |

are maintained in a stack | |

are ignored |

Question 22 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 23 |

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 asx ^{8} – 71 and x ^{8} – 71 | |

x ^{8} – 73 and x^{8} – 73 | |

x ^{8} + 71 and x ^{8} + 71 | |

x ^{8} + 73 and x ^{8} + 73 |

Question 23 Explanation:

Given f(x) = x

for, ho(gof)

gof=g(f(x))

=g(x

=√(x

ho(gof)=h(gof)

=h(√(x

=(√(x

=x

=x

√ x

for, (hog)of,

hog=h(g(x))

=h(√(x

=(√(x

=x

=x

(hog)of=(hog)(f(x))

=(hog)(x

=(x

=x

Hence, option-D is the correct answer

^{4} , g(x) = √ x^{2}+ 1 , h(x) = x 2 + 72for, 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} + 72for, (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} +73Hence, option-D is the correct answer

Question 24 |

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 ?

Which of these would actually swap the contents of the two integer variables p and q ?

(a) only | |

(b) only | |

(c) only | |

(b) and (c) only |

Question 24 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.

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 25 |

Match the following in List - I and List - II, for a function f :

(a)-(i), (b)-(ii), (c)-(iii) | |

(a)-(iii), (b)-(ii), (c)-(i) | |

(a)-(ii), (b)-(i), (c)-(iii) | |

(a)- (ii), (b)-(iii), (c)-(i) |

Question 25 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

∀ 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 26 |

_______ is often used to prove the correctness of a recursive function.

Diagonalization | |

Communitivity | |

Mathematical Induction | |

Matrix Multiplication |

Question 26 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

→ 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 27 |

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

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

maximum of a, b | |

positive difference of a, b | |

sum of a, b | |

minimum of a, b |

Question 28 |

How many constructors can a class have ?

zero | |

2 | |

2 | |

any number |

Question 28 Explanation:

There is no restriction to to have number of constructors in a lass.

There are 28 questions to complete.