Page 220 - Computer science 868 Class 12
P. 220
C. Answer the following questions:
1. The following function is a part of some class that computes and returns the greatest common divisor of any two numbers. There
are some places in the code marked as ?1?, ?2?, ?3?, ?4?, and ?5? which must be replaced by a statement/expression so that the
function works correctly. Answer the questions given after the function.
int gcd(int a, int b)
{
int r;
while(?1?)
{
r=?2?;
b=?3?;
a=?4?;
}
if(a==0)
return ?5?;
else
return -1;
}
(i) What is the expression or statement at ?1?
(ii) What is the expression or statement at ?2?
(iii) What is the expression or statement at ?3?
(iv) What is the expression or statement at ?4?
(v) What is the expression or statement at ?5?
2. Rewrite the following while loop using for loop.
a=50;
while(a>=1)
{
System.out.println(a);
a--;
}
3. What will be the output of the following code?
void function(int a)
{
int b;
for(a=1; a<=4; a=a+1)
{
for(b=1; b<=a; b++)
{
System.out.print (a+ "," +b);
}
System.out.println();
}
}
4. What will be the output?
public static void main(String args[])
{
int r; char d='1';
for(int n=0;n<=9;n++)
{ r = n++;
for(int a=0; a<=r; a++)
d++;
System.out.println(d);
}
}
5. Convert the following statement to if-else.
c=(x>='A' && x<='Z')?"Uppercase" : "Lowercase";
218218 Touchpad Computer Science-XII

