ICSE Computer Application - Class X - 2020 PYQ
Table of Content:
Question 1
(a) Define Java bytecode.
Java bytecode is an intermediate code that gets generated when the Java source code is compiled. It is platform independent.
(b) Write a difference between class and an object.
A class is a collection of related objects. It acts as a data type for an object.
An object is an instance of a class.
(c) Name the following:
(i) The keyword which converts variable into constant.
The final keyword
(ii) The method which terminates the entire program from any stage.
System.exit(0);
(d) Which of the following are primitive data types?
(i) double
Primitive data type
(ii) String
Non-primitive data type
(iii) char
Primitive data type
(iv) Integer
Non-primitive data type
(e) What is an operator? Name any two types of operators in Java.
An operator is a symbol that when applied with operand(s) gives some meaningful result.
Two categories of operators: Arithmetic operators, Relational operators.
Question 2
(a) What is autoboxing in Java? Give an example.
Autoboxing is process of automatic conversion between the primitive types and their corresponding object wrapper classes.
Example:
int a = 5;
Integer i = a; //autoboxing
(b) State the difference between length and length() in Java.
The length property is used in arrays to find the size of arrays. The length() method is used with string objects to find the total number of characters present in the string.
(c) What is constructor overloading?
The process of creating more than one constructor for a class that differ in their signatures is known as constructor overloading.
(d) What is the use of import statement in Java?
The import statement is used to include packages into our program.
(e) What is an infinite loop? Give an example.
A loop that doesn’t terminate is an infinite loop.
Example:
while(true)
System.out.println(“Hello”);
Question 3
(a) Write a Java expression for the following:
\[ \sqrt{b^2 - 4ac} \]
Math.sqrt(b * b – 4 * a * c)
(b) Evaluate the following if the value of x = 7, y = 5
x += x++ + x + ++y;
x = x + (x++ + x + ++y)
x = 7 + (7 + 8 + 6)
x = 7 + 21
x = 28
(c) Write the output for the following:
String s1 = “Life is Beautiful”;
System.out.println(s1.endsWith(“L”));
false
(d) Write the output of the following statement:
System.out.println(“A picture is worth \t \”A thousand words.\””);
A picture is worth “A thousand words.”
(e) Give the output of the following program segment and mention how many times the loop will execute:
int k;
for(k = 5; k <= 20; k += 7)
if(k % 6 == 0)
continue;
System.out.println(k);
Output: 26
The loop executes 3 times.
(f) What is the data type returned by the following library methods?
(i) isWhitespace() boolean data type
(ii) compareToIgnoreCase() int data type
(g) Rewrite the following program segment using logical operators:
if(x > 5)
if(x > y)
System.out.println(x + y);
if(x > 5 && x > y)
System.out.println(x + y);
(h) Convert the following if else if construct into switch case:
if(ch == ‘c’ || ch == ‘C”)
System.out.print(“COMPUTER”);
else if(ch == ‘h’ || ch == ‘H’)
System.out.print(“HINDI”);
else
System.out.print(“PHYSICAL EDUCATION”);
switch(ch){
case ‘c’:
case ‘C’:
System.out.print(“COMPUTER”);
break;
case ‘h’:
case ‘H’:
System.out.print(“HINDI”);
break;
default:
System.out.print(“PHYSICAL EDUCATION”);
(i) Give the output of the following:
(i) Math.pow(36, 0.5) + Math.cbrt(125)
11.0
(ii) Math.ceil(4.2) + Math.floor(7.9)
12.0
(j) Rewrite the following using ternary operator:
if(n1 > n2)
r = true;
else
r = false;
r = (n1 > n2)? true : false;
SECTION B
Attempt any four questions from this section.
The answers in this section should consist of the programs in either BlueJ environment or any program environment with Java as the base.
Each program should be written using variable descriptions/mnemonic codes so that the logic of the program is clearly depicted.
Flowcharts and algorithms are not required.
Question 4
A private cab service company provides service within the city at the following rates:
AC CAR | NON AC CAR | |
UPTO 5 KM | ₹150/- | ₹120/- |
BEYOND 5 KM | ₹10/- PER KM | ₹08/- PER KM |
Design a class CabService with the following description:
Member variables/data members:
String carType – to store the type of car (AC or NON AC)
double km – to store the kilometer travelled
double bill – to calculate and store the bill amount
Member methods:
CabService() – default constructor to initialize data members. String data members to “” and double data members to 0.0.
void accept() – to accept carType and km (using Scanner class only).
void calculate() – to calculate the bill as per the rules given above.
void display() – to display the bill as per the following format
CAR TYPE:
KILOMETER TRAVELLED:
TOTAL BILL:
Create an object of the class in the main() method and invoke the member methods.
import java.util.Scanner;
class CabService{
String carType;
double km;
double bill;
public CabService(){
carType = "";
km = 0.0;
bill = 0.0;
}
public void accept(){
Scanner in = new Scanner(System.in);
System.out.print("Car type: ");
carType = in.nextLine().toUpperCase();
System.out.print("Distance travelled: ");
km = Double.parseDouble(in.nextLine());
}
public void calculate(){
if(km <= 5){
if(carType.startsWith("AC")){
carType = "AC CAR";
bill = 150.0;
}
else if(carType.startsWith("NON")){
carType = "NON AC CAR";
bill = 120.0;
}
}
else{
if(carType.startsWith("AC")){
carType = "AC CAR";
bill = 150.0 + (km - 5) * 10;
}
else if(carType.startsWith("NON")){
bill = 120.0 + (km - 5) * 8;
carType = "NON AC CAR";
}
}
}
public void display(){
System.out.println("CAR TYPE: " + carType);
System.out.println("KILOMETER TRAVELLED: " + km);
System.out.println("TOTAL BILL: " + bill);
}
public static void main(String[] args) {
CabService obj = new CabService();
obj.accept();
obj.calculate();
obj.display();
}
}
Question 5
Write a program to search for an integer value input by the user in the sorted list given below using binary search technique. If found, display “Search Successful” and print the element, otherwise display “Search Unsuccessful”
{31, 36, 45, 50, 60, 75, 86, 90}
import java.util.Scanner;
class BinarySearch{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int a[] = {31, 36, 45, 50, 60, 75, 86, 90};
System.out.print("Element to be searched: ");
int key = Integer.parseInt(in.nextLine());
int low = 0;
int high = a.length - 1;
int mid;
while(low <= high){
mid = (low + high) / 2;
if(key == a[mid])
break;
else if(key < a[mid])
high = mid - 1;
else
low = mid + 1;
}
if(low > high)
System.out.println("Search Unsuccessful");
else
System.out.println("Search Successful: " + key);
}
}
Question 6
Write a program to input a sentence and convert it into uppercase and display each word in a separate line.
Example:
Input: India is my country
Output:
INDIA
IS
MY
COUNTRY
import java.util.Scanner;
class Display{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the sentence: ");
String s = in.nextLine().toUpperCase();
String w = "";
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if(Character.isLetterOrDigit(ch))
w += ch;
else{
System.out.println(w);
w = "";
}
}
}
}
Question 7
Design a class to overload a method number() as follows:
(i) void number(int num, int d) – to count and display the frequency of a digit in a number.
Example:
num = 2565685
d = 5
Frequency of digit 5 = 3
(ii) void number(int n) – to find and display the sum of even digits of a number.
Example:
n = 29865
Sum of even digits = 16
Write a main() method to create an object and invoke the above methods.
class Overload{
public void number(int num, int d){
int f = 0;
if(num == 0 && d == 0)
f = 1;
for(int i = num; i != 0; i /= 10){
if(i % 10 == d)
f++;
}
System.out.println("Frequency of digit " + d + " = " + f);
}
public void number(int n){
int sum = 0;
for(int i = n; i != 0; i /= 10){
int d = i % 10;
if(d % 2 == 0)
sum += d;
}
System.out.println("Sum of even digits = " + sum);
}
public static void main(String[] args){
Overload obj = new Overload();
obj.number(2565685, 5);
obj.number(29865);
}
}
Question 8
Write a menu-driven program to perform the following operations as per user’s choice:
(i) To print the value of c = a2 + 2ab, where a varies from 1.0 to 20.0 with increment of 2.0 and b = 3.0 is a constant.
(ii) To display the following pattern using for loop:
A
AB
ABC
ABCD
ABCDE
Display proper message for an invalid choice.
import java.util.Scanner;
class Menu{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("1. a^2 + 2ab");
System.out.println("2. Pattern");
System.out.print("Enter your choice: ");
int choice = Integer.parseInt(in.nextLine());
switch(choice){
case 1:
double a = 1.0;
final double b = 3.0;
double c;
while(a <= 20.0){
c = a * a + 2 * a * b;
System.out.println("c = " + c);
a += 2.0;
}
break;
case 2:
for(char i = 'A'; i <= 'E'; i++){
for(char j = 'A'; j <= i; j++)
System.out.print(j);
System.out.println();
}
break;
default:
System.out.println("Invalid choice!");
}
}
}
Question 9
Write a program to input and store integer elements in a double dimensional array of size 3 × 3 and find the sum of elements in the left diagonal.
Example:
1 3 5
4 6 8
9 2 4
Output:
Sum of the left diagonal elements = (1 + 6 + 4) = 11
import java.util.Scanner;
class Matrix{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int a[][] = new int[3][3];
int sum = 0;
System.out.println("Enter elements:");
for(int i = 0; i < a.length; i++){
for(int j = 0; j < a.length; j++){
a[i][j] = Integer.parseInt(in.nextLine());
if(i == j)
sum += a[i][j];
}
}
System.out.println("Sum of the left diagonal elements = " + sum);
}
}