Functions & Recursion

MCQS on Functions & Recursion

Q) Consider the following code:

function modify(y,z)

{

y = y + 1;

z = z + 1;

return y - z

}

function calculate( )

{

integer a = 5, b = 10, c

c = modify(a, b);

print a

print space

print c

}

Assume that a and b were passed by value. What will be the output on executing function calculate( )?

Op 1: 11 -5 Op 2: 10 -5 Op 3: 6 -5 Op 4: 5 -5


Correct Op : 4

Q) Consider the following code:

function modify(b,a)

{

return a - b

}

function calculate( )

{

integer a = 5, b = 12, c

c = modify(a, b);

print c

}Assume that a and b were passed by reference. What will be the output of the program on executing function calculate( )?

Op 1: 7 Op 2: -7 Op 3: Error Op 4: 8


Correct Op : 1

Q) Consider the following code:

function modify(y,z)

{

y = y + 1

z = z + 1

return y - z

}

function calculate( )

{

integer a = 12, b = 20, c

c = modify(a, b);

print a

print space

print c

}

Assume that a and b were passed by reference. What will be the output of the function calculate( ) ?

Op 1: 12 -8 Op 2: 13 -8 Op 3: 12 8 Op 4: 13 8


Correct Op : 2

Q) Afzal writes a piece of code, where a set of three lines occur around 10 times in different parts of the program. What programming concept can he use to shorten his program code length?

Op 1: Use for loops Op 2: Use functions

Op 3: Use arrays Op 4: Use classes


Correct Op : 2

Q) If you want to write a function that swaps the values of two variables, you must pass them by:

Op 1: Value only Op 2: Reference only

Op 3: Either A or B Op 4: Neither A nor B


Correct Op : 2

Q) Geetika writes a piece of code, where a set of eight lines occur around 10 times in different parts of the program (Code A). She passes on the code to Deva. Deva puts the set of eight lines in a function definition and calls them at the 10 points in the program (Code B). Which code will run faster using an interpreter?

Op 1: Code A Op 2: Code B

Op 3: Code A and Code B will run with the same speed

Op 4: None of these


Correct Op : 1

Q) Consider the following code:

function modify(a,b)

{

integer c, d = 2

c = a*d + b

return c

}

function calculate( )

{

integer a = 5, b = 20, c

integer d = 10

c = modify(a, b);

c = c + d

print c

}

Assume that a and b were passed by value. What will be the output of the function calculate( ) ?

Op 1: 80 Op 2: 40 Op 3: 32 Op 4: 72


Correct Op : 2

Q) Consider the following code:

function modify(w,u)

{

w = w + 2

u = u - 3

return (w - u)

}

function calculate( )

{

integer a = 10, b = 20, c

c = modify(a, b);

print a

print space

print b

}

Assume that a was passed by value and b was passed by reference. What will be the output of the program on executing function calculate( ) ?

Op 1: 12 17 Op 2: 10 17

Op 3: 12 20 Op 4: 10 20


Correct Op : 2

Q) Consider the following function:

function run( )

{

integer a = 0 // Statement 1

while (a < 5)

{

integer c = 0 // Statement 2

c = c + 1 // Statement 3

a = a + 1

}

print c // Statement 4

}

At which statement in this program will the compiler detect an error?

Op 1: Statement 1 Op 2: Statement 2

Op 3: Statement 3 Op 4: Statement 4


Correct Op : 4

Q) I have a problem to solve which takes as input a number n.The problem has a property that given the solution for(n-1), I can easily solve the problem for n. Which programming technique will I use to solve such a problem?

Op 1: Iteration

Op 2: Decision-making

Op 3: Object Oriented Programming

Op 4: Recursion


Correct Op : 4

Q) Shrishti writes the code for a function that computes the factorial of the inputted number n.

function factorial(n)

{

if(n equals 1)

return 1

else

-- MISSING STATEMENT --

end

}

Fill in the missing statement.

Op 1: return factorial(n-1) Op 2: return n*factorial(n)

Op 3: return n*(n-1) Op 4: return n*factorial(n-1)


Correct Op : 4

Q) Tanuj writes the code for a function that takes as input n and calculates the sum of first n natural numbers.

Function sum( n )

{

if(??)

return 1

else

return (n + sum(n-1))

end

}

Fill in ?? in the code.

Op 1: n equals 1 Op 2: n equals 2 Op 3: n >= 1 Op 4: n > 1


Correct Op : 1

Q) Saloni writes the code for a function that takes as input n, an even integer and calculates the sum of first n even natural numbers.

function sum( n )

{

if(n equals 2)

return 2

else

return (n + sum(n-2))

end

}

She then calls the function by the statement, sum(30). How many times will the function sum be called to compute this sum.

Op 1: 1 Op 2: 30 Op 3: 15 Op 4: 16

Correct Op : 3

Q) Consider the following function

function calculate( n )

{

if(n equals 5)

return 5

else

return (n + calculate(n-5))

end

}

Shishir calls the function by the statement, calculate(20). What value will the function return?

Op 1: 50 Op 2: 200 Op 3: 35 Op 4: 20


Correct Op : 1