Model Paper 3:
Question 1(i)
Assertion (A): The index of the first element in an array is always 0.
Reason (R): Array elements are stored in an indexed manner, starting from index zero.
Answer: (a) Both Assertion (A) and Reason (R) are true, and Reason (R) is a correct explanation of Assertion (A).
Question 1(ii)
"Java compiled code (byte code) can run on all operating systems"
— Name the feature.
1. Robust and Secure
2. Object Oriented
3. Platform Independent
4. Multithreaded
Answer
Platform Independent
Reason — The feature of "Platform Independence" allows Java compiled code (bytecode) to run on any
operating system. This is achieved through the Java Virtual Machine (JVM), which interprets the bytecode and
executes it on the specific platform.
Question 1(iii)
The size of '\n' is:
1. 2 bytes
2. 4 bytes
3. 8 bytes
4. 16 bytes
Answer
2 bytes
Reason — In Java, \n (newline character) is a character literal. All character literals in Java, including escape
sequences like \n, are stored as char data type, which occupies 2 bytes (16 bits) in memory.
Question 1(iv)
Identify the operator that gets the highest precedence while evaluating the given expression:
a+b%c*d-e
1. +
2. %
3. -
4. *
Answer
%
Reason — % and * have the highest precedence. Since % appears first in the expression, it takes precedence
during evaluation.
Question 1(v)
Which of the following is a valid java keyword?
1. If
2. BOOLEAN
3. static
4. Switch
Answer
static
Reason — In Java, keywords are reserved words that have a specific meaning and purpose in the language.
Keywords must always be written in lowercase. Analysing the given options:
1. If: Incorrect because Java keywords are case-sensitive, and the correct keyword is if (in lowercase).
2. BOOLEAN: Incorrect because the keyword in Java is boolean (in lowercase).
3. static: Correct. static is a valid Java keyword used to declare static methods, variables, or blocks.
4. Switch: Incorrect because the correct keyword is switch (in lowercase).
Question 1(vi)
The output of the following code is:
[Link]([Link](6.4) + [Link](-1-2));
1. 3.0
2. 4
3. 3
4. 4.0
Answer
4.0
Reason — Let's evaluate the expression step by step:
[Link]([Link](6.4) + [Link](-1 - 2));
Step 1: Evaluate [Link](6.4)
The ceil method rounds a number up to the nearest integer.
[Link](6.4) → 7.0 (as ceil returns a double).
Step 2: Evaluate -1 - 2
-1 - 2 = -3
Step 3: Evaluate [Link](-3)
[Link](-3): The floor method rounds a number down to the nearest integer.
[Link](-3) → -3.0 (as floor also returns a double).
Step 4: Add the Results
[Link](6.4) + [Link](-3) → 7.0 + (-3.0)
Result = 4.0
Question 1(vii)
Which of the following returns a String?
1. length()
2. charAt(int)
3. replace(char, char)
4. indexOf(String)
Answer
replace(char, char)
Reason — The given methods return:
Return
Method
Type
length() int
charAt(int) char
replace(char, char) String
indexOf(String) int
Question 1(viii)
Which of the following is not true with regards to a switch statement?
1. checks for an equality between the input and the case labels
2. supports floating point constants
3. break is used to exit from the switch block
4. case labels are unique
Answer
supports floating point constants
Reason — The switch statement does not support float or double constants as case labels. It supports byte,
short, int, char, String and enum.
Question 1(ix)
Consider the array given below:
char ch[] = {'A','E','I','O', 'U'};
Write the output of the following statements:
[Link](ch[0]*2);:
1. 65
2. 130
3. 'A'
4. 0
Answer
130
Reason — The given array is:
char ch[] = {'A', 'E', 'I', 'O', 'U'};
Step-by-Step Execution:
1. ch[0]:
The element at index 0 of the array is 'A'.
2. Character Multiplication in Java:
In Java, a char is treated as a numeric value based on its ASCII code when used in arithmetic
operations.
The ASCII value of 'A' is 65.
3. ch[0] * 2:
Substituting ch[0] with its ASCII value:
65 * 2 = 130
4. Output:
[Link](ch[0] * 2); will print 130.
Question 1(x)
To execute a loop 10 times, which of the following is correct?
1. for (int i=11;i<=30;i+=2)
2. for (int i=11;i<=30;i+=3)
3. for (int i=11;i<20;i++)
4. for (int i=11;i<=21;i++)
Answer
for (int i=11;i<=30;i+=2)
Reason — To execute a loop 10 times, the total number of iterations should match 10, based on the condition
and increment values. Let's analyse each option:
1. for (int i = 11; i <= 30; i += 2)
The loop starts at 11 and increments by 2.
The sequence is: 11, 13, 15, 17, 19, 21, 23, 25, 27, 29 .
This loop executes 10 times.
2. for (int i = 11; i <= 30; i += 3)
The loop starts at 11 and increments by 3.
The sequence is: 11, 14, 17, 20, 23, 26, 29.
This loop executes only 7 times, so it is incorrect.
3. for (int i = 11; i < 20; i++)
The loop starts at 11 and increments by 1.
The sequence is: 11, 12, 13, 14, 15, 16, 17, 18, 19 .
This loop executes only 9 times, so it is incorrect.
4. for (int i = 11; i <= 21; i++)
The loop starts at 11 and increments by 1.
The sequence is: 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 .
This loop executes 11 times, so it is incorrect.
Question 1(xi)
A single dimensional array has 50 elements, which of the following is the correct statement to initialize the last
element to 100.
1. x[51]=100
2. x[48]=100
3. x[49]=100
4. x[50]=100
Answer
x[49]=100
Reason — In Java, arrays use zero-based indexing, meaning the first element of the array is at index 0. For an
array with n elements, the last element is at index n - 1.
Given a single-dimensional array with 50 elements, the indices range from 0 to 49. Thus, the correct index for
the last element is 49.
Question 1(xii)
Method prototype for the method compute which accepts two integer arguments and returns true/false.
1. void compute (int a, int b)
2. boolean compute (int a, int b)
3. Boolean compute (int a,b)
4. int compute (int a, int b)
Answer
boolean compute (int a, int b)
Reason — Let's analyse the given options:
1. void compute (int a, int b) — Incorrect because void means the method does not return any value,
but the question requires the method to return true/false.
2. boolean compute (int a, int b) — Correct because it returns a boolean value (true or false) and
accepts two integer arguments.
3. Boolean compute (int a, b) — Incorrect due to syntax error (int b is not fully declared).
Also, Boolean is a wrapper class, not a primitive boolean.
4. int compute (int a, int b) — Incorrect because the return type is int, not boolean.
Question 1(xiii)
The statement that brings the control back to the calling method is:
1. break
2. [Link](0)
3. continue
4. return
Answer
return
Reason — The return statement in Java is used to bring the control back to the calling method. If the method
has a return type other than void, the return statement also passes back a value to the calling method.
Question 1(xiv)
The default value of a boolean variable is:
1. False
2. 0
3. false
4. True
Answer
false
Reason — In Java, the default value of a variable depends on its data type. For a boolean variable, the default
value is false (in lowercase). This is because Java uses the primitive boolean type, which has only two possible
values: true or false.
Question 1(xv)
The method to convert a lowercase character to uppercase is:
1. [Link]( )
2. [Link]( char )
3. [Link]( char )
4. toUpperCase ( )
Answer
[Link]( char )
Reason — The method [Link](char) is used to convert a single lowercase character to its
uppercase equivalent. It belongs to the Character class in Java.
Question 1(xvi)
Assertion (A): Integer class can be used in the program without calling a package.
Reason (R): It belongs to the default package [Link].
1. Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
2. Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion(A)
3. Assertion (A) is true and Reason (R) is false
4. Assertion (A) is false and Reason (R) is true
Answer
Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
Reason — The Integer class can indeed be used in a program without explicitly importing any package. This is
because it is part of the [Link] package, which is automatically imported in every Java program by default.
Question 1(xvii)
A student executes the following code to increase the value of a variable ‘x’ by 2.
He has written the following statement, which is incorrect.
x = +2;
What will be the correct statement?
A. x +=2;
B. x =2;
C. x = x +2;
1. Only A
2. Only C
3. All the three
4. Both A and C
Answer
Both A and C
Reason — The student's code x = +2; is incorrect because it doesn't actually increment x by 2. Instead, it simply
assigns the value +2 to x.
The correct way to increment the value of x by 2 is:
1. Option A: x += 2;
This is shorthand for x = x + 2; and correctly increments x by 2.
2. Option C: x = x + 2;
This is the full, explicit version of incrementing x by 2. It is also correct.
Option B: x = 2;
This assigns 2 to x, which is not incrementing its value. Hence, this is incorrect.
Question 1(xviii)
The statement used to find the total number of Strings present in the string array String s[] is:
1. [Link]
2. [Link]()
3. length(s)
4. len(s)
Answer
[Link]
Reason — In Java, arrays have a property called length, which gives the total number of elements in the array.
This applies to any type of array, including arrays of Strings.
For a string array String s[], the correct way to get the number of elements is:
[Link]
Analysing other options:
1. [Link]():
o Incorrect. The length() method is used for String objects to get the number of characters in a
String, not for arrays.
2. length(s):
o Incorrect. This is not a valid syntax in Java.
3. len(s):
o Incorrect. Java does not have a function called len() for arrays or strings.
Question 1(xix)
Consider the following program segment in which the statements are jumbled, choose the correct order of
statements to swap two variables using the third variable.
void swap(int a, int b)
{
a = b; → (1)
b = t; → (2)
int t = 0; → (3)
t = a; → (4)
}
1. (1) (2) (3) (4)
2. (3) (4) (1) (2)
3. (1) (3) (4) (2)
4. (2) (1) (4) (3)
Answer
(3) (4) (1) (2)
Reason — To swap two variables a and b using a third variable t, the correct sequence of statements is:
Step 1: Declare and initialize the third variable t.
int t = 0; // (3)
Step 2: Assign the value of a to t.
t = a; // (4)
Step 3: Assign the value of b to a.
a = b; // (1)
Step 4: Assign the value of t (original value of a) to b.
b = t; // (2)
Correct Order:
int t = 0; // (3)
t = a; // (4)
a = b; // (1)
b = t; // (2)
Question 1(xx)
Assertion (A): An argument is a value that is passed to a method when it is called.
Reason (R): Variables which are declared in a method prototype to receive values are called actual parameters
1. Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
2. Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion(A)
3. Assertion (A) is true and Reason (R) is false
4. Assertion (A) is false and Reason (R) is true
Answer
Assertion (A) is true and Reason (R) is false
Explanation — Arguments are the actual values passed to a method when it is invoked. For example:
void exampleMethod(int x) { ... }
exampleMethod(5); // Here, 5 is the argument.
Hence, Assertion (A) is true.
The variables declared in a method prototype (or header) to receive values are called formal parameters, not
actual parameters. For example:
void exampleMethod(int x) { // x is the formal parameter
[Link](x);
}
exampleMethod(5); // 5 is the actual parameter
Hence, Reason (R) is false.
Question 2(i)
Rewrite the following code using single if statement.
if(code=='g')
[Link]("GREEN");
else if(code=='G')
[Link]("GREEN");
Answer
The code can be rewritten using a single if statement as follows:
if(code == 'g' || code == 'G')
[Link]("GREEN");
Explanation:
The logical OR operator (||) is used to check if code is either 'g' or 'G'.
If either condition is true, the statement [Link]("GREEN"); will execute. This simplifies the
multiple if-else blocks into a single if statement.
Question 2(ii)
Evaluate the given expression when the value of a=2 and b=3
b*=a++ - ++b + ++a;
[Link]("a= "+a);
[Link]("b= "+b);
Answer
a = 4, b = 6
Explanation:
The given expression is evaluated as follows:
b*=a++ - ++b + ++a
b = b * (a++ - ++b + ++a) [a=2, b=3]
b = 3 * (2 - 4 + 4) [a=4, b=4]
b=3*2
b=6
Question 2(iii)
A student executes the following program segment and gets an error. Identify the statement which has an error,
correct the same to get the output as WIN.
boolean x = true;
switch(x)
{
case 1: [Link]("WIN"); break;
case 2: [Link]("LOOSE");
}
Answer
Statement having the error is:
boolean x = true;
Corrected code:
int x = 1; // Change boolean to int
switch(x)
{
case 1: [Link]("WIN"); break;
case 2: [Link]("LOOSE");
}
Explanation:
The error occurs because the switch statement in Java does not support the boolean data type.
A switch expression must be one of the following types:
int, byte, short, char, String, or an enum.
Replacing boolean x = true; with int x = 1; ensures that the switch statement now uses a valid data type
(int). Assigning x = 1 makes the case 1 to execute printing WIN as the output.
Question 2(iv)
Write the Java expression for x3+y3x+y
Answer
The Java expression for the given mathematical expression can be written as:
[Link](x) + [Link](y);
Question 2(v)
How many times will the following loop execute? Write the output of the code:
int x=10;
while (true){
[Link](x++ * 2);
if(x%3==0)
break;
}
Answer
The loop executes two times.
Output
20
22
Explanation:
Step-by-Step Execution of the Code
Initial Value of x:
x = 10
Iteration 1:
[Link](x++ * 2);
x++ → Use the current value of x (10), then increment x to 11.
Output: 10 * 2 = 20
if (x % 3 == 0):
x = 11, so 11 % 3 = 2 → Condition is false.
Iteration 2:
[Link](x++ * 2);
x++ → Use the current value of x (11), then increment x to 12.
Output: 11 * 2 = 22
if (x % 3 == 0):
x = 12, so 12 % 3 = 0 → Condition is true.
break;
The break statement exits the loop.
Question 2(vi)
Write the output of the following String methods:
String x= "Galaxy", y= "Games";
(a) [Link]([Link](0)==[Link](0));
(b) [Link]([Link](y));
Answer
The outputs are:
(a) true
(b) -1
Explanation:
Given Strings:
String x = "Galaxy";
String y = "Games";
(a) [Link]([Link](0) == [Link](0));
1. [Link](0): Retrieves the character at index 0 of x → 'G'.
2. [Link](0): Retrieves the character at index 0 of y → 'G'.
3. Comparison: 'G' == 'G' → true.
(b) [Link]([Link](y));
1. [Link](y): Compares x ("Galaxy") with y ("Games") lexicographically (character by character
based on Unicode values). The comparison is based on the first differing character:
o 'G' == 'G' → Equal, move to the next characters.
o 'a' == 'a' → Equal, move to the next characters.
o 'l' == 'm' → 'l' (Unicode 108) is less than 'm' (Unicode 109).
2. Result:
o [Link](y) → 108 - 109 = -1.
Question 2(vii)
Predict the output of the following code snippet:
char ch='B',
char chr=[Link](ch);
int n=(int)chr-10;
[Link]((char)n+"\t"+chr);
Answer
Output
X b
Explanation
Step-by-Step Execution:
char ch = 'B';
ch is assigned the character 'B'.
char chr = [Link](ch);
[Link](ch) converts 'B' to its lowercase equivalent 'b'.
chr = 'b'.
int n = (int) chr - 10;
(int) chr converts the character 'b' to its ASCII value.
ASCII value of 'b' is 98.
n = 98 - 10 = 88.
[Link]((char) n + "\t" + chr);
(char) n: Converts the integer 88 back to a character.
o ASCII value 88 corresponds to the character 'X'.
+ "\t" + adds a tab space between the characters.
chr: The value of chr is 'b'.
Question 2(viii)
A student is trying to convert the string present in x to a numerical value, so that he can find the square root of
the converted value. However the code has an error. Name the error (syntax / logical / runtime). Correct the code
so that it compiles and runs correctly.
String x= "25";
int y=[Link](x);
double r=[Link](y);
[Link](r);
Answer
Syntax error in the line:
int y=[Link](x);
Corrected code:
String x= "25";
double y=[Link](x); //int changed to double
double r=[Link](y);
[Link](r);
Explanation:
The error is a syntax error because the method [Link](x) returns a double, but the code is trying
to assign it to an int variable without proper casting, which is not allowed in Java.
Question 2(ix)
Consider the following program segment and answer the questions below:
class calculate
{
int a; double b;
calculate()
a=0;
b=0.0;
calculate(int x, double y)
a=x;
b=y;
void sum()
{
[Link](a*b);
}}
Name the type of constructors used in the above program segment?
Answer
The type of constructors used in the above program segment are:
1. Default constructor
2. Parameterized constructor
Explanation:
The types of constructors used in the given program are:
1. Default Constructor:
calculate() {
a = 0;
b = 0.0;
}
A default constructor is a constructor that does not take any parameters.
It initializes the instance variables a and b with default values (0 and 0.0 respectively).
2. Parameterized Constructor:
calculate(int x, double y) {
a = x;
b = y;
}
A parameterized constructor is a constructor that takes arguments (int x and double y in this case).
It allows for initializing the instance variables a and b with specific values provided during object
creation.
Question 2(x)
Consider the following program segment and answer the questions given below:
int x[][] = { {2,4,5,6}, {5,7,8,1}, {34, 1, 10, 9}};
(a) What is the position of 34?
(b) What is the result of x[2][3] + x[1][2]?
Answer
(a) The position of 34 is x[2][0]
(b) x[2][3] + x[1][2] = 9 + 8 = 17
Explanation:
(a) Position of 34
The array x is a 2D array, where elements are accessed using row and column indices.
The element 34 is located in the 3rd row and 1st column.
In zero-based indexing:
o Row index: 2
o Column index: 0
Position: x[2][0]
(b) Result of x[2][3] + x[1][2]
Step 1: Locate x[2][3]
x[2][3] refers to the element in the 3rd row and 4th column.
Value at x[2][3] = 9.
Step 2: Locate x[1][2]
x[1][2] refers to the element in the 2nd row and 3rd column.
Value at x[1][2] = 8.
Step 3: Add the Values
x[2][3] + x[1][2] = 9 + 8 = 17.
Section B
Question 3
Define a class with the following specifications:
Class name: Bank
Member variables:
double p — stores the principal amount
double n — stores the time period in years
double r — stores the rate of interest
double a — stores the amount
Member methods:
void accept () — input values for p and n using Scanner class methods only.
void calculate () — calculate the amount based on the following conditions:
Time in (Years) Rate %
Upto 1⁄2 9
> 1⁄2 to 1 year 10
> 1 to 3 years 11
> 3 years 12
a=p(1+r100)na=p(1+100r)n
void display () — display the details in the given format.
Principal Time Rate Amount
XXX XXX XXX XXX
Write the main method to create an object and call the above methods.
Soln:
import [Link];
public class Bank
{
private double p;
private double n;
private double r;
private double a;
void accept() {
Scanner in = new Scanner([Link]);
[Link]("Enter principal amount: ");
p = [Link]();
[Link]("Enter time period in years: ");
n = [Link]();
}
void calculate() {
if (n <= 0.5) {
r = 9;
} else if (n <= 1) {
r = 10;
} else if (n <= 3) {
r = 11;
} else {
r = 12;
}
a = p * [Link](1 + (r / 100), n);
}
void display() {
[Link]("Principal\tTime\tRate\tAmount");
[Link](p + "\t" + n + "\t" + r + "\t" + a);
}
public static void main(String args[]) {
Bank b = new Bank();
[Link]();
[Link]();
[Link]();
}
}
Output
_________
Question 4
Define a class to search for a value input by the user from the list of values given below. If it is found display the
message "Search successful", otherwise display the message "Search element not found" using Binary search
technique.
5.6, 11.5, 20.8, 35.4, 43.1, 52.4, 66.6, 78.9, 80.0, 95.5.
Soln:
import [Link];
public class BinarySearch
{
void search(double[] arr, double key) {
int low = 0;
int high = [Link] - 1;
boolean found = false;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) {
found = true;
break;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (found) {
[Link]("Search successful");
} else {
[Link]("Search element not found");
}
}
public static void main(String[] args) {
Scanner in = new Scanner([Link]);
double[] values = {5.6, 11.5, 20.8, 35.4, 43.1, 52.4, 66.6, 78.9, 80.0,
95.5};
[Link]("Enter the value to search: ");
double key = [Link]();
BinarySearch obj = new BinarySearch();
[Link](values, key);
}
}
Output
Question 5
Define a class to accept a string and convert the same to uppercase, create and display the new string by
replacing each vowel by immediate next character and every consonant by the previous character. The other
characters remain the same.
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024
Soln : import [Link];
public class StringConvert
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a string: ");
String str = [Link]();
str = [Link]();
int l = [Link]();
String res = "";
for (int i = 0; i < l; i++) {
char ch = [Link](i);
if ("AEIOU".indexOf(ch) != -1) {
res += (char)(ch + 1);
}
else if ([Link](ch)) {
res += (char)(ch - 1);
}
else {
res += ch;
}
}
[Link]("Output String:");
[Link](res);
}
}
Output
Question 6
Define a class to accept values into 4x4 array and find and display the sum of each row.
Example:
A[][]={{1,2,3,4},{5,6,7,8},{1,3,5,7},{2,5,3,1}}
Output:
sum of row 1 = 10 (1+2+3+4)
sum of row 2 = 26 (5+6+7+8)
sum of row 3 = 16 (1+3+5+7)
sum of row 4 = 11 (2+5+3+1)
Soln: import [Link];
public class DDARowSum
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
int arr[][] = new int[4][4];
[Link]("Enter 4x4 DDA elements:");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
arr[i][j] = [Link]();
}
}
for (int i = 0; i < 4; i++) {
int rowSum = 0;
for (int j = 0; j < 4; j++) {
rowSum += arr[i][j];
}
[Link]("Sum of Row" + (i+1)
+ " = " + rowSum);
}
}
}
Output
Question 7
Define a class to accept a number and check whether it is a SUPERSPY number or not. A number is called
SUPERSPY if the sum of the digits equals the number of the digits.
Example1:
Input: 1021
output: SUPERSPY number [SUM OF THE DIGITS = 1+0+2+1 = 4,
NUMBER OF DIGITS = 4 ]
Soln : import [Link];
public class SuperSpy
{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
int sum = 0;
int dc = 0;
int orgNum = num;
while (num > 0) {
int d = num % 10;
sum += d;
dc++;
num /= 10;
}
if (sum == dc) {
[Link](orgNum + " is a SUPERSPY number");
}
else {
[Link](orgNum + " is not a SUPERSPY number");
}
}
}
Output
Question 8
Define a class to overload the method display() as follows:
void display(): To print the following format using nested loop.
12121
12121
12121
void display (int n, int m) : To print the quotient of the division of m and n if m is greater than n otherwise print
the sum of twice n and thrice m.
double display (double a, double b, double c) — to print the value of z where
z=p×qz=p×q
p=a+bcp=ca+b
q=a+b+cq=a+b+c
Soln:
public class OverloadDisplay
{
void display() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
if (j % 2 == 0) {
[Link]("1 ");
} else {
[Link]("2 ");
}
}
[Link]();
}
}
void display(int n, int m) {
if (m > n) {
double q = m / n;
[Link]("Quotient: " + q);
} else {
int sum = 2 * n + 3 * m;
[Link]("Sum: " + sum);
}
}
double display(double a, double b, double c) {
double p = (a + b) / c;
double q = a + b + c;
double z = p * q;
[Link]("Z = " + z);
return z;
}
}
Output