Answer Key
Question 1
No. Answer Explanation
i b) Inheritance Picture generally shows parent–child relationship between classes.
ii b) double y = 2 * 4.6 Result becomes double automatically (implicit casting).
iii d) All are correct All expressions evaluate true, so none return false.
iv d) 8 bytes long occupies 8 bytes in Java.
v d) m + n * 9 Valid arithmetic expression. Others are relational/unary.
vi b) method A self-contained block of instructions is called a method.
vii d) 4.0 [Link](10,12)=12, [Link](2,3)=8, 12−8 = 4.0
viii b) ABC No break → execution falls through all cases.
ix a) Both statements are true and reason explains assertion.
x a) Valid Condition eventually becomes false.
xi a) Logical operators combine conditions and return true/false.
xii a) source code Program written in high-level language.
xiii b) 4.0 [Link](9,16)=16, sqrt(16)=4.0.
xiv c) object Object is a non-primitive type.
xv d) 8 bytes Size of long.
xvi b) ten times Loop runs from 1 to 10.
xvii b) Runtime error Division by zero occurs during execution.
xviii b) nextLine() Reads a full line including spaces.
xix b) infinite i becomes negative but never 0.
xx b) infinite i keeps increasing and condition always true.
Question 2
i. Difference between (a) and (b)
Code (a)
Uses separate if statements.
All conditions are checked one after another.
Example
If x = 1
First condition true → x becomes 2
Second condition true → x becomes 4
Third condition false
Multiple blocks may execute.
Code (b)
Uses else-if ladder.
Only one condition executes.
Example
If x = 1
First condition true → x becomes 2
Remaining conditions skipped.
ii. Errors in the switch statement
Errors:
1. ? symbol is invalid.
2. Duplicate case value case 3.
3. Statement z=x*y after break will never execute.
4. Variables may be used without declaration.
Correct idea:
Each case must have unique values and proper syntax.
iii. Number of Iterations
(a)
int a=5, x=50;
while(a<x)
a=x/a;
Execution:
1st iteration
a = 50 / 5 = 10
2nd iteration
a = 50 / 10 = 5
Now condition again true → loop repeats forever
Answer: Infinite loop
(b)
int a=10, x=7;
while(a%x>=0)
{
a++;
x+=2;
}
Since a % x is always ≥ 0, condition always true.
Answer: Infinite loop
iv. Output
a) [Link](4.2) → 5.0
b) [Link](-4) → 4
c) [Link](-0.88) → -1.0
v. Output
int k=1, n=2, m=4, y;
for(y=1;y<=m;y++)
k*=n;
[Link]("k="+k);
Execution:
k = 1×2 = 2
k = 2×2 = 4
k = 4×2 = 8
k = 8×2 = 16
Output
k=16
vi. Convert into for loop
Original
int s=1, a=12;
while (a>=3)
{
s*=a;
a-=2;
}
Converted
int s=1;
for(int a=12; a>=3; a-=2)
{
s*=a;
}
[Link]("Result="+s);
vii. Rewrite using do-while
Corrected version
class Pattern
{
public static void main()
{
int i=5, j;
do
{
j=1;
do
{
[Link](i);
j++;
}
while(j<=5);
[Link]();
i--;
}
while(i>=1);
}
}
viii. Output
(i) a=2, b=6
a>3 → false
Go to else
b==6 → true
Output
True
(ii) a=8, b=0
a>3 → true
b<=6 → true
Output
Yes
(iii) a=3, b=4
a>3 → false
b==6 → false
Output
False
ix. Value of expression
Given
p = 10
n=7
m = --p + p * ++n
Step-1
--p → p becomes 9
Step-2
++n → n becomes 8
Step-3
m=9+9*8
m = 9 + 72
m = 81
Answer: m = 81
SECTION B
Question 3 – Special Number
import [Link].*;
class SpecialNumber
{
public static void main()
{
Scanner sc = new Scanner([Link]);
int n, d1, d2, sum, product;
[Link]("Enter a two digit number");
n = [Link]();
d1 = n / 10;
d2 = n % 10;
sum = d1 + d2;
product = d1 * d2;
if(sum + product == n)
[Link]("Special Number");
else
[Link]("Not a Special Number");
}
}
Question 4
(a) Series Program
Series
S = (1×2²) + (2×3²) + (3×4²) + … n terms
import [Link].*;
class Series1
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int n,i,sum=0;
[Link]("Enter number of terms");
n=[Link]();
for(i=1;i<=n;i++)
{
sum = sum + i * (int)[Link](i+1,2);
}
[Link]("Sum="+sum);
}
}
(b) Reverse of 4 digit number
import [Link].*;
class ReverseNumber
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int n,rev=0,d;
[Link]("Enter a 4 digit number");
n=[Link]();
while(n>0)
{
d=n%10;
rev=rev*10+d;
n=n/10;
}
[Link]("Reverse number = "+rev);
}
}
Question 5
(a) Series
S = 1 + (x+2)/2! + (2x+3)/3! + …
import [Link].*;
class Series2
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int x,n,i,fact=1;
double sum=1;
[Link]("Enter x and n");
x=[Link]();
n=[Link]();
for(i=1;i<=n;i++)
{
fact=fact*(i+1);
sum=sum+(i*x+(i+1))/(double)fact;
}
[Link]("Sum="+sum);
}
}
(b) Pattern
class Pattern
{
public static void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
[Link](j+" ");
}
[Link]();
}
}
}
Output
1
12
123
1234
12345
Question 6 – Smith Number
import [Link].*;
class SmithNumber
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int n,i,sum1=0,sum2=0,temp;
[Link]("Enter number");
n=[Link]();
temp=n;
while(temp>0)
{
sum1+=temp%10;
temp/=10;
}
temp=n;
for(i=2;i<=temp;i++)
{
while(temp%i==0)
{
int t=i;
while(t>0)
{
sum2+=t%10;
t/=10;
}
temp/=i;
}
}
if(sum1==sum2)
[Link]("Smith Number");
else
[Link]("Not a Smith Number");
}
}
Question 7 – Menu Driven Program
import [Link].*;
class MenuProgram
{
public static void main()
{
Scanner sc=new Scanner([Link]);
int n,i,count=0,sum=0,ch;
[Link]("1. Number of Factors");
[Link]("2. Sum of Proper Factors");
[Link]("Enter your choice");
ch=[Link]();
[Link]("Enter number");
n=[Link]();
switch(ch)
{
case 1:
for(i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
[Link]("Total factors="+count);
break;
case 2:
for(i=1;i<n;i++)
{
if(n%i==0)
sum+=i;
}
[Link]("Sum of proper factors="+sum);
break;
default:
[Link]("Invalid choice");
}
}
}