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)
{
System.out.print(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)
{
System.out.println(s);
char temp;
if(x<s.length/2)
{
temp=s[x];
s[x]=s[s.length-x-1];
s[s.length-x-1 ]=temp;
fun1(s, x+1);
}
}
void fun2(String n)
{
char c[ ]=new char[n.length()];
for(int i=0;i<c.length; i++)
c[i]=n.charAt(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)
{
System.out.print (n + " " );
if(n%2 !=0)
{
n = 3* n + 1;
System.out.print(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)
System.out.println(" ");
else
{
System. out .printing. char At (p) + " . ");
witty (n, p - 1);
System.out.print[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)
System.out. println(” ");
else
{
System.out.println ("Hello" +counter);
mymethod (--counter);
System.out.println (" " +counter);
}
}
Answer:
Counter-3,2,1
Output- Hello3, Hello2, Hello1