0% found this document useful (0 votes)
15 views8 pages

Palindrome and Recursion Functions Explained

The document contains a series of programming questions and answers related to recursive functions and algorithms. It includes dry runs for various functions, explanations of outputs, and the identification of missing code segments. The questions cover topics such as palindromes, Armstrong numbers, and string manipulations.

Uploaded by

jreeva26
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Palindrome and Recursion Functions Explained

The document contains a series of programming questions and answers related to recursive functions and algorithms. It includes dry runs for various functions, explanations of outputs, and the identification of missing code segments. The questions cover topics such as palindromes, Armstrong numbers, and string manipulations.

Uploaded by

jreeva26
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Q.

1-

The following function Mystery( ) is a part of some class. What will the
function Mystery( ) return when the value of num=43629, x=3 and
y=4 respectively? Show the dry run/working. [5]

int Mystery (int num, int x, int y)


{
if(num<10)
return num;
else
{
int z = num % 10;
if(z%2 == 0)
return z*x + Mystery (num/10, x, y);
else
return z*y + Mystery(num/10, x, y);
}
}

Answer:
Given n = 43629, x = 3, y = 4

Step 5 returns 4
Step 4 returns 12 + 4 = 16
Step 3 returns 18 + 16 = 34
Step 2 returns 6 + 34 = 40
Step 1 returns 36 + 40 = 76

Q.2-

The following is a function of some class which checks if a positive


integer is a Palindrome number by returning true or false. (A number is
said to be palindrome if the reverse of the number is equal to the
original number.) The function does not use the modulus (%) operator
to extract digit. There are some places in the code marked by ?1?, ?2?,
?3?, ?4?, ?5? which may be replaced by a statement/expression so that
the function works properly.

boolean PalindromeNum(int N)
{
int rev=?1?;
int num=N;
while(num>0)
{
int f=num/10;
int s= ?2?;
int digit = num-?3?;
rev= ?4? + digit;
num/= ?5?;
}
if(rev==N)
return true;
else
return false;
}

1. What is the statement or expression at ?1?


2. What is the statement or expression at ?2?
3. What is the statement or expression at ?3?
4. What is the statement or expression at ?4?
5. What is the statement or expression at ?5?

Answer:

1. 0
2. 0
3. 1
4. rev*10
5. 10

Q.3-

The following function magicfun() is a part of some class. What will the
function magicfun() return, when the value of n=7 and n=10,
respectively? Show the dry run/working: [5]

int magicfun (int n)


{
if(n = = 0)
return 0;
else
return magicfim(n/2) * 10 + (n%2);
}

At n = 7 ⇒ 111
Answer:

At n = 10 ⇒ 1010
Binary equivalent of N

Q.4-

The following function Check() is a part of some class. What will the
function Check() return when the values of both ‘m’ and ‘n’ is equal to
5? Show the dry run/working. [5]

int Check (int m, int n)


{
if(n = = 1)
return - m --;
else
return + + m + Check (m, -- n);
}

Answer:
The value of m and n are 5 which is not equal to 1. It is, for this reason,
the first if block is not executed as a result it will jump to the else part
where the value of m will be 6 and n will be 4. As m is having Pre
Increment operator so the value of m is changed to 6 and the value of
n to 4 as it is the Pre Decrement operator.

Q.5-

The following function is a part of some class. Assume ‘x’ and ‘y’ are
positive integers, greater than 0. Answer the given questions along
with dry run/working.

void someFun(int x, int y) {


if(x>1)
{
if(x%y == 0)
{
[Link](y+ "");
someFun(x/y, y);
}
else
someFun(x, y+1);
}
}

(i) What will be returned by someFun(24, 2)? [2]


(ii) What will be returned by someFun(84, 2)? [2]
(iii) State in one line what does the function someFun() do, apart from
recursion? [1]
(b) The following is a function of some class which checks if a positive
integer is an Armstrong number by returning true or false. (A number
is said to be Armstrong of the sum of the cubes of all its digits is equal
to the original number.) The function does not use modulus (%)
operator to extract digit. There are some places in the code marked by
?1?, ?2?, ?3?, ?4?, ?5? which may be replaced by a
statement/expression so that the function works properly.

Answer:
(a) (i) 2 2 2 3 24, 2
(ii) 2 2 3 7 84, 2
(iii) someFunOcalculates the L.C.M (Lowest Common Multiple).

Q.6-

The following is a function of some class which checks if a positive


integer is an Armstrong number by returning true or false. (A number
is said to be Armstrong of the sum of the cubes of all its digits is equal
to the original number.) The function does not use modulus (%)
operator to extract digit. There are some places in the code marked by
?1?, ?2?, ?3?, ?4?, ?5? which may be replaced by a
statement/expression so that the function works properly.

boolean ArmstrongNum(int N)
{
int sum = ?1?;
int num= N;
while(num>0)
{
int f=num/10;
int s= ?2?;
int digit = num-s;
sum+=?3?;
num= ?4?;
}
if(?5?)
return true;
else
return false;
}

(i) What is the statement or expression at ?1? [1]


(ii) What is the statement or expression at ?2? [1]
(iii) What is the statement or expression at ?3? [1]
(iv) What is the statement or expression at ?4? [1]
(v) What is the statement or expression at ?5? [1]

Answer:

(i) 0
(ii) 0
(iii) (f * f * f);
(iv) num/10;
(v) s == num

Q.7-

The following functions are part of some class:

void fun 1 (char s[ ],int x)


{
[Link](s);
char temp;
if(x<[Link]/2)
{
temp=s[x];
s[x]=s[[Link]-x-1];
s[[Link]-x-1 ]=temp;
fun1(s, x+1);
}
}
void fun2(String n)
{
char c[ ]=new char[[Link]()];
for(int i=0;i<[Link]; i++)
c[i]=[Link](i);
fun1(c,0);
}

(i) What will be the output of fun1() when the value of


s[ ]={‘J’,‘U’,‘N’,‘E’} and x = 1? [2]
(ii) What will be the output of fun2( ) when the value of n = ‘SCROLL”?
(iii) State in one line what does the function fim1() do apart from
recursion. [1]
(b) The following is a function of some class which sorts an integer
array a[ ] in ascending order using selection sort technique. There are
some places in the code marked by ?1?, ?2?, ?3?, ?4?, ?5? which may
be replaced by a statement/expression so that the function works
properly:

Answer:

(i) JUNE
JNUE
(ii) SCROLL
LCROLS
LLROCS
LLORCS
(iii) Reverses the part of a string from a specified location.

Q.8-

The following function Recur is a part of some class. What will be the
output of the function Recur () when the value of n is equal to 10. Show
the dry run / working. [5]

void Recur (int n)


{
if (n>1)
{
[Link] (n + " " );
if(n%2 !=0)
{
n = 3* n + 1;
[Link](n + " ");
}
Recur (n/2);
}
}
Answer:

Recur (10)
10 Recur (5)
5
16 Recur (8)
8 Recur (4)
4 Recur (2)
2 Recur (1)
OUTPUT: 10 5 16 8 4 2

Q.9-

The following function witty() is a part of some class. What will be the
output of the function witty( ) when the value of n is ‘SCIENCE’ and the
value of p is 5. Show the dry run/working: [5]

void witty (String n, int p)


{
if (p < 0)
[Link](" ");
else
{
System. out .printing. char At (p) + " . ");
witty (n, p - 1);
[Link][n. char At (p)]:
}
}
Answer-

C.
N.
E.
I.
C.
S.
SCIENCE.

Q.10-

The following is a part of some class. What will be the output of the
function mymethod( ) when the value of the counter is equal to 3?
Show the dry run/working. [5]

void mymethod (int counter)


{
if (counter == 0)
[Link]. println(” ");
else
{
[Link] ("Hello" +counter);
mymethod (--counter);
[Link] (" " +counter);
}
}
Answer:
Counter-3,2,1

Output- Hello3, Hello2, Hello1

Common questions

Powered by AI

The `Recur()` function processes input `n=10` by printing `n`, transforming odd numbers by `3*n+1`, and recursively calling itself with `n/2`. It outputs the sequence `10 5 16 8 4 2 1` by halving or transforming numbers until `n` becomes 1 .

The `Mystery()` function recursively processes each digit of a number, multiplying even-positioned digits by `x` and odd-positioned digits by `y`, then adds them up. For `num=43629`, `x=3`, and `y=4`, it returns 76. The detailed computation involves splitting the number into digits and applying these operations, resulting in a step-by-step return of 76 .

The `ArmstrongNum()` function verifies if a number is an Armstrong number by adding the cubes of its digits. The placeholders are replaced as: `?1?`: 0, `?2?`: 0, `?3?`: `(digit*digit*digit)`, `?4?`: `num/10`, `?5?`: `sum==N`. It reconstructs each digit and compares the sum of cubes to the original number .

`someFun()` prints all the prime factors of `x` starting from `y`. For `someFun(24, 2)`, it prints `2223`, and for `someFun(84, 2)`, it prints `2237`. This function recursively divides `x` by `y` if `x` is divisible by `y`, otherwise it increments `y` and tries again, effectively factorizing `x` by its smallest divisors .

The `Check()` function recursively increments `m` and decrements `n` until `n` equals 1, accumulating the result in the process. When both `m` and `n` are 5, the function ultimately returns 30. This is calculated by incrementing `m`, reducing `n` through recursion, and returning the sum as 30 after five recursive calls .

The `witty()` function prints each character of `n` from index `p` to `0` in reverse order, then reconstructs the string by appending characters from `0` to `p`. For `n="SCIENCE"` and `p=5`, it outputs: `C. N. E. I. C. S. SCIENCE` .

The `fun1()` function recursively reverses a section of an array, while `fun2()` initializes this process across the whole string. For the input `"SCROLL"`, `fun2()` reverses it to `"LLORCS"`. It first converts the string into a char array and then calls `fun1()` to reverse the order of characters .

The `magicfun()` function converts an integer to its binary representation. For `n=7`, it returns 111, and for `n=10`, it returns 1010. It does this by recursively dividing the number by 2 and constructing the binary result by appending the remainder (either 0 or 1).

The `PalindromeNum()` function constructs the reverse of a number by extracting each digit and building the reverse sequentially. The expressions to replace placeholders are: `?1?`: 0, `?2?`: 0, `?3?`: 1, `?4?`: `rev*10`, `?5?`: 10. The function checks if the reversed number equals the original to confirm if it is a palindrome .

For `counter=3`, `mymethod()` prints `Hello3`, `Hello2`, `Hello1` during the downward recursive calls, and then prints `2`, `1`, `0` during the return unwinding of the recursive calls .

You might also like