Computer Sample Paper
Computer Sample Paper
Section A
Question 1(i)
Encapsulation
Inheritance
Abstraction
Polymorphism
Name the feature of java depicted in the picture. ICSE 2024 Specimen Computer
Applications Solved Question Paper.
Answer
Inheritance
Reason — The given picture shows the relationship of a parent (father) and child. Just like
a child inherits some characteristics from his parents, inheritance enables new classes
(derived class) to receive or inherit the properties and methods of existing classes (base
class).
Question 1(ii)
The expression which uses >= operator is known as:
relational
logical
arithmetic
assignment
Answer
relational
Question 1(iii)
Ternary operator is a:
logical operator
arithmetic operator
relational operator
conditional operator
Answer
conditional operator
If the condition is true then result of ternary operator is the value of expression 1.
Otherwise the result is the value of expression 2.
Question 1(iv)
When primitive data type is converted to a corresponding object of its class, it is called:
Boxing
Unboxing
Answer
Boxing
Reason — Boxing is the conversion of primitive data type into an object of its
corresponding wrapper class.
Question 1(v)
20 bytes
60 bytes
40 bytes
120 bytes
Answer
20 bytes
Reason — A char data type occupies '2' bytes in the memory. Thus, 10 char type elements
occupy (10 x 2) = 20 bytes in memory.
Question 1(vi)
nextInt()
nextDouble()
next()
nextInteger()
Answer
nextDouble()
Reason — The nextDouble() function reads the next token entered by the user as a double
value.
Question 1(vii)
every
all
case
each
Answer
case
Question 1(viii)
9.0
11.0
10.0
11
Answer
11.0
Math.round(6.6) + Math.ceil(3.4)
⇒ 7 + 4.0
⇒ 11.0
Math.round() rounds off its argument to the nearest mathematical integer and returns its
value as an int or long type. Math.ceil method returns the smallest double value that is
greater than or equal to the argument and is equal to a mathematical integer. In the
addition operation, the type of result is promoted to a double as one operand is double.
Hence, the result is 11.0.
Question 1(ix)
System.out.print("HELLO")
logical
no error
runtime
syntax
Answer
syntax
Reason — The given statement is missing a terminator ( ; ) at the end. Thus, it has a
syntax error.
Question 1(x)
X[4]
X[5]
X[3]
X[0]
Answer
X[4]
Reason — Array indexes start from 0. So, X[4] refers to the 5th element of the array.
Question 1(xi)
mark
emark
marka
able
Answer
able
Question 1(xii)
Which of the following is the wrapper class for the data type char?
String
Char
Character
Float
Answer
Character
Reason — Character is the wrapper class for the data type char.
Question 1(xiii)
java.lang
java.util
java.io
java.awt
Answer
java.lang
Question 1(xiv)
Polymorphism
Abstraction
Encapsulation
Answer
Polymorphism
Question 1(xv)
i. 4
ii. 4.0
iii. 4.3f
iv. "four"
Only i
i and iii
ii and iv
i and ii
Answer
Only i
Reason — Integer constants represent whole number values only. Thus, 4 is an integer
constant. 4.0 is a double constant, 4.3f is a float constant while "four" is a String constant.
Question 1(xvi)
The method compareTo() returns ............... when two strings are equal and in lowercase :
true
false
Answer
Reason — compareTo() method compares two strings lexicographically and returns the
difference between the ASCII values of the first differing characters in the strings. Here,
the strings are equal so the difference is 0.
Question 1(xvii)
Assertion (A): In Java, statements written in lower case letter or upper case letter are
treated as the same.
Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of
Assertion (A)
Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of
Assertion (A)
Answer
Reason — In Java, statements written in lower case letter or upper case letter are treated
differently as Java is a case sensitive language.
Question 1(xviii)
A class encapsulate Data Members that contains the information necessary to represent
the class and Member methods that perform operations on the data member.
Answer
Question 1(xix)
Reason (R): The original value of variable does not change as operation is performed on
copied values.
Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of
Assertion (A)
Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of
Assertion (A)
Answer
Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of
Assertion (A)
Reason — Call by value is known as pure method as it does not modify the value of
original variables. The original value of variable does not change as operation is
performed on copied values.
Question 1(xx)
true
Answer
Question 2(i)
Answer
Math.pow((p + q) , 2)
Question 2(ii)
x = x++ + ++x + x
Answer
Output
10
Explanation
x = x++ + ++x + x
x = 2 + ++x + x (x = 3)
x=2+4+x (x = 4)
x=2+4+4 (x = 4)
x = 10
Question 2(iii)
The following code segment should print "You can go out" if you have done your
homework (dh) and cleaned your room (cr). However, the code has errors. Fix the code
so that it compiles and runs correctly.
boolean dh = True;
else
Answer
else
Explanation
Question 2(iv)
Sam executes the following program segment and the answer displayed is zero
irrespective of any non zero values are given. Name the error. How the program can be
modified to get the correct answer?
double a;
a = 1/2 * b * h;
System.out.println("Area=" + a);
Answer
Logical error.
Modified program:
void triangle(double b, double h)
double a;
a = 1.0/2 * b * h;
System.out.println("Area=" + a);
Explanation
a = 1/2 * b * h;
To avoid this error, we can replace '1/2' with '1.0/2'. Now the expression will be evaluated
as follows:
a = 1.0/2 * b * h;
This will give the correct result for the given code.
Question 2(v)
How many times will the following loop execute? What value will be returned?
int x = 2;
int y = 50;
do{
++x;
y -= x++;
return y;
Answer
The loop will execute 5 times and the value returned is 15.
Explanation
Iteration X Y Remark
2 50 Initial values
1 3 47 x = 4 (3 + 1), y = 47 (50 - 3)
2 5 42 x = 6 (5 + 1), y = 42 (47 - 5)
3 7 35 x = 8 (7 + 1), y = 35 (42 - 7)
4 9 26 x = 10 (8 + 1), y = 26 (35 - 9)
Question 2(vi)
(a) "ARTIFICIAL".indexOf('I')
(b) "DOG and PUPPY".trim().length()
Answer
(a)
Output
Explanation
indexOf() returns the index of the first occurrence of the specified character within the
string or -1 if the character is not present. First occurrence of 'I' in "ARTIFICIAL" is at index
3 (a string begins at index 0).
(b)
Output
13
Explanation
trim() removes all leading and trailing space from the string and length() returns the length
of the string i.e., the number of characters present in the string. Thus, the output is 13.
Question 2(vii)
Answer
continue statement
Question 2(viii)
String a = "20";
String b = "23";
int p = Integer.parseInt(a);
int q = Integer.parseInt(b);
Answer
Output
20*23
Explanation
Integer.parseInt() method will convert the strings a and b to their corresponding numerical
integers — 20 and 23. In the statement, System.out.print(a + "*" + b); a, b, and "*" are
strings so + operator concatenates them and prints 20*23 as the output.
Question 2(ix)
When there is no explicit initialization, what are the default values set for variables in the
following cases?
(a) 0
(b) null
Question 2(x)
Place all elements of P array and Q array in the array R one after the other.
Answer
Size of array Q[ ] = 3
Section B
Question 3
Member variables:
Member methods:
void accept(): Accept the name and the price of the item using the methods of Scanner
class.
void calculate(): To calculate the net amount to be paid by a customer, based on the
following criteria:
Price Discount
void display(): To display the name of the item and the net amount to be paid.
Write the main method to create an object and call the above methods.
import java.util.Scanner;
name = in.nextLine();
price = in.nextDouble();
double d = 0.0;
d = 0.0;
d = 5.0;
else if (price <= 57000)
d = 7.5;
d = 10.0;
else
d = 15.0;
obj.accept();
obj.calculate();
obj.display();
Output
BlueJ output of Eshop.java
Question 4
Define a class to accept values in integer array of size 10. Sort them in an ascending order
using selection sort technique. Display the sorted array.
import java.util.Scanner;
arr[i] = in.nextInt();
int idx = i;
idx = j;
}
int t = arr[i];
arr[i] = arr[idx];
arr[idx] = t;
System.out.println("Sorted Array:");
Output
Question 5
Define a class to accept a string and convert it into uppercase. Count and display the
number of vowels in it.
Input: robotics
Output: ROBOTICS
Number of vowels: 3
import java.util.Scanner;
public class KboatCountVowels
str = str.toUpperCase();
int count = 0;
char ch = str.charAt(i);
if(ch == 'A'
|| ch == 'E'
|| ch == 'I'
|| ch == 'O'
|| ch == 'U')
count++;
Output
Question 6
Define a class to accept values into a 3 × 3 array and check if it is a special array. An array
is a special array if the sum of the even elements = sum of the odd elements.
Example:
import java.util.Scanner;
arr[i][j] = in.nextInt();
}
if (arr[i][j] % 2 == 0)
evenSum += arr[i][j];
else
oddSum += arr[i][j];
if (evenSum == oddSum)
System.out.println("Special Array");
else
Output
Question 7
Define a class to accept a 3 digit number and check whether it is a duck number or not.
Input: 2083
Output: Invalid
Example 2:
Input: 103
import java.util.Scanner;
int n = num;
int count = 0;
while (n != 0) {
count++;
n = n / 10;
}
if (count == 3)
n = num;
while(n != 0)
if(n % 10 == 0)
isDuck = true;
break;
n = n / 10;
if (isDuck) {
System.out.println("Duck Number");
else {
else {
System.out.println("Invalid");
}
}
Output
Question 8
12
123
1234
12345
void display(int n): To print the square root of each digit of the given number.
Example:
n = 4329
Output – 3.0
1.414213562
1.732050808
2.0
import java.util.Scanner;
System.out.println();
while( n != 0)
int d = n % 10;
System.out.println(Math.sqrt(d));
n = n / 10;
}
public static void main(String args[])
System.out.println("Pattern: ");
obj.display();
obj.display(num);
Output
2023
Section A
Question 1(i)
A mechanism where one class acquires the properties of another class:
Polymorphism
Inheritance
Encapsulation
Abstraction
Answer
Inheritance
Reason — Inheritance enables new classes to receive or inherit the properties and
methods of existing classes.
Question 1(ii)
ternary
unary
logical
relational
Answer
logical
Reason — Logical operators operate only on boolean operands and are used to construct
complex decision-making expressions. Logical AND && operator evaluates to true only if
both of its operands are true.
Question 1(iii)
next()
nextLine()
Next()
nextString()
Answer
nextLine()
Reason — nextLine() reads the input till the end of line so it can read a full sentence
including spaces.
Question 1(iv)
extends
export
import
package
Answer
import
Reason — import keyword is used to import built-in and user-defined packages into our
Java program.
Question 1(v)
16.0
16
4.0
5.0
Answer
4.0
Reason — Math.ceil method returns the smallest double value that is greater than or equal
to the argument and Math.sqrt method returns the square root of its argument as a
double value. Thus the given expression is evaluated as follows:
Math.sqrt(Math.ceil (15.3))
= Math.sqrt(16.0)
= 4.0
Question 1(vi)
The absence of which statement leads to fall through situation in switch case statement?
continue
break
return
System.exit(0)
Answer
break
Reason — The absence of break statement leads to fall through situation in switch case
statement.
Question 1(vii)
for (int i = 5; i != 0; i -= 2)
System.out.println(i);
finite
infinite
null
fixed
Answer
infinite
Reason — The given loop is an example of infinite loop as for each consecutive iteration
of for loop, the value of i will be updates as follows:
2 3 i=5-2=3
3 1 i=3-2=1
4 -1 i = 1 - 2 = -1
5 -3 i = -1 - 2 = -3 and so on...
Question 1(viii)
Write a method prototype name check() which takes an integer argument and returns a
char:
char check()
check (int x)
Answer
return_type method_name(arguments)
Answer
Question 1(x)
int a = Integer.parseInt(P);
int b = Integer.valueOf(Q);
20
20 22
2220
22
Answer
20 22
Reason — The values of strings P and Q are converted into integers using the
Integer.parseInt() method and stored in int variables a and b, respectively.
When the statement System.out.println(a + " " + b) is executed, first the operation a + " " is
performed. Here, int variable a is converted to a string and a space is concatenated to it
resulting in "20 ".
After this, the operation "20 " + b is performed resulting in 20 22 which is printed as the
output.
Question 1(xi)
concat(String)
<string>.joint(string)
concat(char)
Concat()
Answer
concat(String)
Reason — concat() method is used to join two strings. Its syntax is as follows:
String1.concat(String2)
Question 1(xii)
POSI
POS
MPO
MPOS
Answer
POS
Reason — The substring() method returns a substring beginning from the startindex and
extending to the character at endIndex - 1. Since a string index begins at 0, the character
at index 3 is 'P' and the character at index 5 (6-1 = 5) is 'S'. Thus, "POS" is extracted.
Question 1(xiii)
implicit
automatic
explicit
coercion
Answer
explicit
Reason — In explicit type conversion, the data gets converted to a type as specified by
the programmer. Here, the float value 32.8 is being converted to int type by the
programmer, explicitly.
Question 1(xiv)
source code
object code
machine code
Answer
Reason — Java compiler converts Java source code into an intermediate binary code
called Bytecode after compilation.
Question 1(xv)
Logical
Syntax
Runtime
No error
Answer
Syntax
Reason — Syntax Errors occur when we violate the rules of writing the statements of the
programming language. Missing a semicolon in a statement is a syntax error.
Question 1(xvi)
Consider the following program segment and select the output of the same when n = 10 :
switch(n)
case 10 : System.out.println(n*2);
default : System.out.println(n);
20
40
10
20, 40
10
10
Answer
20
40
Reason — Since n = 10, case 10 will be executed. It prints 20 (10 * 2) on the screen. Since
break statement is missing, the execution falls through to the next case. Case 4 prints 40
(10 * 4) on the screen. Now the control finds the break statement and the control comes
out of the switch statement.
Question 1(xvii)
A method which does not modify the value of variables is termed as:
Impure method
Pure method
Primitive method
Answer
Pure method
Reason — A method which does not modify the value of variables is termed as a pure
method.
Question 1(xviii)
When an object of a Wrapper class is converted to its corresponding primitive data type, it
is called as ............... .
Boxing
Unboxing
Answer
Unboxing
Question 1(xix)
1 bit
2 bits
4 bits
16 bits
Answer
16 bits
1 byte = 8 bits
2 bytes = 8 * 2 = 16 bits
Question 1(xx)
Method which is a part of a class rather than an instance of the class is termed as:
Static method
Non static method
Wrapper class
String method
Answer
Static method
Reason — Method which is a part of a class rather than an instance of the class is termed
as Static method.
Question 2(i)
Answer
Math.pow(a + b, x)
Question 2(ii)
x *= --x + x++ + x
Answer
x *= --x + x++ + x (x = 4)
x *= 3 + x++ + x (x = 3)
x *= 3 + 3 + x (x = 4)
x *= 3 + 3 + 4 (x = 4)
x *= 10 (x = 4)
x = x * 10 (x = 4)
x = 4 * 10
x = 40
Question 2(iii)
int x = 10;
do
x––;
System.out.print(x);
}while (x>=1);
Answer
System.out.print(x);
Question 2(iv)
(b) Character.isLetterOrDigit('#')
Answer
Output
Explanation
(b) Character.isLetterOrDigit('#')
Output
false
Explanation
Question 2(v)
Answer
int m = 400;
double ch = 0.0;
ch = (m / 10.0) * 2;
else
ch = (m / 20.0) - 2;
Question 2(vi)
while(n > 0)
{ d = n % 10;
System.out.println(d);
n = n / 100;
Answer
Output
Explanation
Step by step explanation of the code:
int d; — Declares an integer variable d without initializing it. It will be used to store the
individual digits.
d = n % 10; — This line calculates the remainder when n is divided by 10 and stores it in d.
In the first iteration, d will be 9 because the remainder of 4279 divided by 10 is 9.
System.out.println(d); — This line prints the value of d. In the first iteration, it will print 9.
n = n / 100; — This line performs integer division of n by 100. In the first iteration, n
becomes 42. (Remember, it is integer division so only quotient is taken and fractional part
is discarded.)
System.out.println(d); — It prints 2.
Question 2(vii)
(a) "COMMENCEMENT".lastIndexOf('M')
(b) "devote".compareTo("DEVOTE")
Answer
(a) "COMMENCEMENT".lastIndexOf('M')
Output
Explanation
The lastIndexOf('M') method searches for the last occurrence of the character 'M' in the
string "COMMENCEMENT." In this string, the last 'M' appears at the index 8, counting
from 0-based indexing. So, the method returns the index 8 as the output, indicating the
position of the last 'M' in the string.
(b) "devote".compareTo("DEVOTE")
Output
32
Explanation
Question 2(viii)
Consider the given array and answer the questions given below:
Answer
(a) 7
(b) 72
Question 2(ix)
(b) The method which has same name as that of the class name.
Answer
(a) Object.
(b) Constructor.
Question 2(x)
int n = ch + 5;
Answer
Explanation
char ch = 'd'; assigns the character 'd' to the variable ch. In ASCII, the character 'd' has a
decimal value of 100.
int n = ch + 5; adds 5 to the ASCII value of 'd', which is 100. So, 100 + 5 equals 105.
Section B
Question 3
Member variables:
void accept() — Accept name, age and marks using methods of Scanner class.
mks stream
void print() – Display student name, age, mks and stream allocated.
import java.util.Scanner;
{
Scanner in = new Scanner(System.in);
name = in.nextLine();
age = in.nextInt();
mks = in.nextDouble();
else
obj.accept();
obj.allocation();
obj.print();
Output
Question 4
Define a class to accept 10 characters from a user. Using bubble sort technique arrange
them in ascending order. Display the sorted array and original array.
import java.util.Scanner;
System.out.println("Enter 10 characters:");
for (int i = 0; i < ch.length; i++) {
ch[i] = in.nextLine().charAt(0);
System.out.println("Original Array");
//Bubble Sort
char t = ch[j];
ch[j + 1] = t;
System.out.println("\nSorted Array");
}
}
Output
Question 5
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
void print(int n) - To check whether the number is a lead number. A lead number is the
one whose sum of even digits are equal to sum of odd digits.
e.g. 3669
import java.util.Scanner;
System.out.println();
int d = 0;
int evenSum = 0;
int oddSum = 0;
while( n != 0)
d = n % 10;
if (d % 2 == 0)
evenSum += d;
else
oddSum += d;
n = n / 10;
}
if(evenSum == oddSum)
System.out.println("Lead number");
else
System.out.println("Pattern: ");
obj.print();
obj.print(num);
Output
Question 6
Define a class to accept a String and print the number of digits, alphabets and special
characters in the string.
Example:
S = "KAPILDEV@83"
Output:
Number of digits – 2
Number of Alphabets – 8
import java.util.Scanner;
System.out.println("Enter a string:");
int ac = 0;
int sc = 0;
int dc = 0;
char ch;
if (Character.isLetter(ch))
ac++;
else if (Character.isDigit(ch))
dc++;
else if (!Character.isWhitespace(ch))
sc++;
Output
Question 7
Define a class to accept values into an array of double data type of size 20. Accept a
double value from user and search in the array using linear search method. If value is
found display message "Found" with its position where it is present in the array. Otherwise
display message "not found".
import java.util.Scanner;
{
public static void main(String args[])
int l = arr.length;
int i = 0;
arr[i] = in.nextDouble();
double n = in.nextDouble();
if (arr[i] == n)
break;
}
if (i == l)
System.out.println("Not found");
else
Output
Question 8
Define a class to accept values in integer array of size 10. Find sum of one digit number
and sum of two digit numbers entered. Display them separately.
Example:
Output:
import java.util.Scanner;
{
public static void main(String args[])
System.out.println("Enter 10 numbers");
int l = arr.length;
arr[i] = in.nextInt();
oneSum += arr[i];
twoSum += arr[i];
}
}
Output