Page 274 - Computer science 868 Class 12
P. 274
Previous Years' Questions
1. With reference to the code given below, answer the questions that follow along with dry run/working. [ISC 2023]
boolean num(int x)
{
int a=1;
for(int c=x; c>0;c/=10)
a*=10;
return(x*x%a)==x;}
(a) What will the function num() return when the value of x = 25?
Ans. x = 25
a = 1
c = 25 so, a = a * 10 = 10
c = 25/10 = 2 so, a = a * 10 = 100
c = 2/10 = 0
x * x = 25 * 25 = 625
625%100 = 25 so, it will return True
(b) What is the method num() performing?
Ans. The method num() is checking whether the number x is an automorphic number or not i.e. checking whether the number x is
present in its square or not
2. With reference to the following program code, answer the questions that follow:
int solve(int p, int q)
{ for(int r=0; p>0;r=q%p,q=p,p=r);
return(p=0)?q:-1;
}
(i) What will be the output of the method solve() when the values of p=12 and q=8? [ISC 2022]
(a) 8 (b) 16
(c) 96 (d) 4
Ans. (d)
(ii) What is the method solve() performing? [ISC 2022]
(a) LCM of two numbers (b) Checking the divisibility of two numbers
(c) HCF of two numbers (d) Counting the common Prime factors of two numbers
Ans. (d)
3. (i) A class CalSeries has been defined to calculate the sum of the series: [ISC 2022]
n
sum = 1 +x+x +x .. ... +x
2
3
Some of the members of the class are given below:
Class name : CalSeries
Data Members/Instance variables
x : integer to store the value of x
n : integer to store the value of n
sum : integer to store the sum of the series
Member Functions/Methods
CalSeries() : default constructor
void input() : to accept the values of x and n
int power(int p,int q) : return the power of p raised to q (pq) using recursive technique.
void cal() : calculates the sum of the series by invoking the method power() and displays the
result with an appropriate message
Specify the class CalSeries, giving details of the constructor, void input( ), int power(int,int) and void cal(). Define the main)
function to create an object and call the member function accordingly to enable the task.
Ans. import java.util.*;
class CalSeries
{ int x, n,sum;
272272 Touchpad Computer Science-XII

