Grade 10 Practice Paper 2 July 2024
Grade 10 Practice Paper 2 July 2024
Question 1(i)
Which of the following is/are principle(s) of Object Oriented Programming?
Encapsulation
Abstraction
Inheritance
All of these
Answer
All of these
Reason — Encapsulation, abstraction and inheritance are principles of Object Oriented Programming.
Question 1(ii)
If a = 8 and b = 4, the value of a % = b is
2
0
4
8
Answer
a%=b
⟹a=a%b
⟹a=8%4
⟹a=0
Question 1(iii)
Predict the output of the following code snippet:
int a = 1;
int b = 2;
if (a == b)
System.out.println("Both values are equal");
else
System.out.println("Values are not equal");
Both values are equal
Incorrect use of the == operator
Values are not equal
No output
Answer
Reason — Since the values of a and b are not equal, the condition (a == b) is false. Thus, the else
block is executed and "Values are not equal" is printed.
Question 1(iv)
Which of the following is mandatory in the switch statement?
break
continue
case
default
Answer
case
next( )
nextInt( )
nextLong( )
nextNumber( )
Answer
nextNumber( )
Question 1(vi)
What will be the output of Math.floor(-20.10);?
-20.0
-21.0
20
21
Answer
-21.0
Reason — Math.floor() method returns the largest double value that is less than or equal to the
argument and is equal to a mathematical integer. Thus, -21.0 is returned by Math.floor(-20.10);
method.
Question 1(vii)
Which of the following is an illegal identifier?
age
123abc
_value
$
$salary
Answer
123abc
Reason — 123abc is not a valid identifier because it starts with a number, which is not allowed.
Question 1(viii)
Which of the following is/are jump statement used in Java?
break
continue
return
All of these
Answer
All of these
Reason — break, continue and return are jump statements used in Java.
Question 1(ix)
Class initialisation is the ............... initialisation of class fields of values.
explicit
implicit
Both a and b
None of the above
Answer
explicit
Reason — Explicit class initialisation is the initialisation of class fields of values.
Question 1(x)
Operations like square root, sine and cosine are
impure functions
pure functions
static functions
class functions
Answer
pure functions
Reason — Operations like square root, sine and cosine are pure functions as they do not modify their
arguments.
Question 1(xi)
Given array int x[ ] = {11, 22, 33, 44}; the value of x[1 + 2] is :
11
22
33
44
Answer
44
Reason — Array index starts from 0 to Size - 1. The value of x[1 + 2] equals x[3]. The value at index
3 is 44.
Question 1(xii)
A linear search
can be used with sorted arrays only.
can be used with unsorted arrays only.
can be used with both sorted and unsorted arrays.
cannot be used with arrays.
Answer
Reason — A linear search can be used with both sorted and unsorted arrays.
Question 1(xiii)
Method that converts a character to uppercase is
toUpper()
ToUpperCase()
TOUPPERCASE()
toUpperCase(char)
Answer
toUpperCase(char)
Question 1(xiv)
Give the output of the following code
System.out.println("Good".concat("Day"));
GoodDay
Good Day
Goodday
goodDay
Answer
GoodDay
Reason — concat() method will add "Day" at the end of "Good" and "GoodDay" will be printed on
the screen.
Question 1(xv)
The array char arr[8] occupies :
32
8
16
24
Answer
16
Reason — char datatype requires 2 bytes of memory. 8 char type elements will require 2 x 8 = 16
bytes of memory.
Question 1(xvi)
This access specifier achieves the lowest level of accessibility.
Public
Protected
Private
Default
Answer
Private
Reason — Private access specifier achieves the lowest level of accessibility as a data member or
member method declared as private is only accessible inside the class in which it is declared.
Question 1(xvii)
Assertion (A) Line comment is used for a single line of comment.
Reason (R) A line comment starts with forward slash and asterisk(/*).
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).
Assertion (A) is true and Reason (R) is false.
Assertion (A) is false and Reason (R) is true.
Answer
Reason — Assertion (A) is true as a line comment is used for a single line of comment. Reason (R) is
false as a line comment starts with two forward slashes (//).
Question 1(xviii)
Read the following text and choose the correct answer:
Byte streams are used to perform input and output of 8-bytes. They are used to read bytes from the
input stream and write bytes to the output stream. Mostly, they are used to read or write raw binary
data.
FileReader
FileWriter
FileInputStream
InputStreamReader
Answer
FileInputStream
Question 1(xix)
Assertion (A) The private access specifier achieves the lowest level of accessibility.
Reason (R) The private methods and fields can be accessed only within the same class to which the
methods and fields belong.
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).
Assertion (A) is true and Reason (R) is false.
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 — Both Assertion (A) is true as private access specifier achieves the lowest level of
accessibility and Reason (R) explains Assertion (A) correctly as a data member or member method
declared as private is only accessible inside the class in which it is declared.
Question 1(xx)
State the data type after the following is executed :
char ch = '9';
res = Character.isDigit(ch);
int
char
string
boolean
Answer
boolean
Reason — isDigit() method returns true if the specified character is a digit; returns false otherwise.
Thus, the return type is boolean.
Question 2(i)
State the number of bytes and bits occupied by a character array of 20 elements.
Answer
Question 2(ii)
If
String x = "Computer";
String y = "Science";
What will the following return?
(a) System.out.print(x.substring(2,6));
(b) System.out.print(x.indexOf(x.charAt(5)));
Answer
(a)
Output
mput
Explanation
The substring() method returns a substring beginning from the startindex and extending to the
character at index endIndex - 1. Here, startindex is 2 and end index is 5 (6 - 1). Since the string starts
from index 0, the extracted substring is "mput". Thus, "mput" is printed on the screen.
(b)
Output
5
Explanation
charAt() method returns a character from the string at the index specified as its argument. indexOf()
method returns the index of the first occurrence of the specified character within the string or -1 if the
character is not present. Thus, the given expression is evaluated as follows:
Question 2(iii)
Write the Java statement for the following mathematical expression :
x=
(
𝑎
+
𝑏
)
2
𝑎
+
𝑏
a+b
(a+b)
2
Answer
Answer
Question 2(v)
State the type of errors, if any in the following statements.
(a)
switch (x < 2)
(b)
int a = 100, b = 0;
System.out.println (a / b);
Answer
(a) There is a syntax error in the switch statement. In a switch statement, the expression inside the
parentheses should evaluate to an integer, a character, or an enumerated type. However, x < 2 is a
boolean expression, and it cannot be used directly as the expression in a switch statement.
(b) The statement System.out.println (a / b); will result in a runtime error as the user is trying to divide
a number by 0 which is not possible.
Question 2(vi)
What will be the output of the following code?
Output
10
Explanation
Since the condition (num < 20) is true, the if block is executed. Postfix operator first uses the value
and then increments the value, so the value of num is first printed (10) and then incremented.
Question 2(vii)
Find the output of the given code.
int a, b = 100;
for (a = 10; a <= 12; a++)
{
b += a;
}
System.out.print("a:" + a + " " + "b:" + b);
Answer
Output
a:13 b:133
Explanation
The values of a and b are evaluated as follows in the for loop:
Iteration a b Remarks
100 Initial value
1 10 110 b = b + a = 100 + 10 = 110
2 11 121 b = b + a = 110 + 11 = 121
3 12 110 b = b + a = 121 + 12 = 133, Loop terminates
The print statements print the final values of a and b.
Question 2(viii)
The following code has some error(s). Identify the errors and write the corrected code.
Int x = 4, y = 8;
{
y = y + (x++)
} while (x <= 10)
System.out.println(y);
Answer
int x = 4, y = 8;
do
{
y = y + (x++);
} while (x <= 10);
System.out.println(y);
Statement Error
Int x = 4, y = 8; int is a keyword and should be written in lowercase
The user needs to use do while loop The do-while loop structure is used incorrectly, as the do
keyword is missing.
y = y + (x++) Semicolon should be written at the end of the statement
while (x <= 10) A semicolon should be written after the while(x <= 10)
Question 2(ix)
State the method that determines, if the specified character is an uppercase character.
Answer
Question 2(x)
Write the return data type of the following functions.
(a) startsWith( )
(b) log( )
Answer
(a) boolean
(b) double
Section B
Question 3
Define a class to declare an array of size 20 of double datatype, accept the elements into the array and
perform the following:
Calculate and print the sum of all the elements.
import java.util.Scanner;
System.out.println("Enter 20 numbers:");
for (int i = 0; i < 20; i++) {
arr[i] = in.nextDouble();
}
sum += arr[i];
}
(i)
12345
1234
123
12
1
(ii)
A
BA
CBA
DCBA
EDCBA
public class KboatPattern
{
public static void main(String args[]) {
System.out.println("Pattern 1: ");
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
System.out.println();
System.out.println("Pattern 2: ");
char ch = 'A';
for (int i = 0; i < 5; i++) {
for (int j = i; j >= 0; j--) {
System.out.print((char)(65 + j) + " ");
}
System.out.println();
}
}
}
Output
BlueJ output of KboatPattern.java
Question 5
Write a program to input and store integer elements in a double dimensional array of size 4 x 4 and
find the sum of all the elements.
7345
5461
6942
3275
Sum of all the elements: 73
import java.util.Scanner;
An automorphic number is a number whose square "ends" in the same digits as the number itself.
import java.util.Scanner;
/*
* Count the number of
* digits in num
*/
while(num > 0) {
d++;
num /= 10;
}
/*
* Extract the last d digits
* from square of num
*/
int ld = (int)(sq % Math.pow(10, d));
if (ld == numCopy)
System.out.println(numCopy + " is automorphic");
else
System.out.println(numCopy + " is not automorphic");
}
}
Output
BlueJ output of KboatAutomorphic.java
BlueJ output of KboatAutomorphic.java
Question 7
Design a class RailwayTicket with following description:
import java.util.Scanner;
import java.util.Scanner;
}
}
Output