ICSE Computer Applications 2017

COMPUTER APPLICATIONS
(Theory)
(Two hours)

Answers to this paper must be written on the paper provided separately.
You will not be allowed to write during the first 15 minutes.
This time is to be spent in reading the question paper.
The time given at the head of this paper is the time allowed for writing the answers.
This paper is divided into two sections.
Attempt all questions from Section A and any four questions from Section B.
The intended marks for questions or parts of questions are given in brackets [ ].

SECTION A
Attempt all questions.

Question 1
(a) What is inheritance?
The process of deriving properties and behavior by a child class from a parent class is known as inheritance.

(b) Name the operators listed below:
(i) <
Less than operator (relational operator)
(ii) ++
Increment operator
(iii) &&
Logical AND operator
(iv) ?:
Conditional operator (Ternary operator)

(c) State the number of bytes occupied by char and int data types.
char occupies 2 bytes.
int occupies 4 bytes.

(d) Write one difference between / and % operator.
/ is used to find the quotient during integer division, whereas % is used to find the remainder.

(e) String x[] = {“SAMSUNG”, “NOKIA”, “SONY”, “MICROMAX”, “BLACKBERRY”};
Give the output of the following statements:
(i) System.out.println(x[1]);
NOKIA
(ii) System.out.println(x[3].length());
8

Question 2
(a) Name the following:
(i) A keyword used to call a package in the program.
import keyword
(ii) Any one reference data type.
String

(b) What are the two ways of invoking functions?
Call by value and call by reference

(c) State the data type and value of res after the following is executed:
char ch = ‘t’;
res = Character.toUpperCase(ch);
Data type = char
Value = ‘T’

(d) Give the output of the following program segment and also mention the number of times the loop is executed:
int a, b;
for(a = 6, b = 4; a <= 24; a = a + 6){
    if(a % b == 0)
        break;
}
System.out.println(a);
Output: 12
Number of times the loop executed: 2

(e) Write the output:
char ch = ‘F’;
int m = ch;
m = m + 5;
System.out.println(m + ” ” + ch);
75 F

Question 3
(a) Write a Java expression for the following:
ax5 + bx3 + c
a * Math.pow(x, 5) + b * Math.pow(x, 3) + c

(b) What is the value of x1 if x = 5?
x1 = ++x – x++ + –x;
x1 = 6 – 6 + 6
x1 = 6

(c) Why is an object called an instance of a class?
An object is called an instance of a class because each object is unique and may have different state at any point of time.

(d) Convert the following do-while loop into for loop:
int i = 1;
int d = 5;
do{
    d = d * 2;
    System.out.println(d);
    i++;
}while(i <= 5);
int d = 5;
int i;
for(i = 1; i <= 5; i++){
    d = d * 2;
    System.out.println(d);
}

(e) Differentiate between constructor and function.
A constructor has no return type, whereas a function always has some return type.

(f) Write the output for the following:
String s = “Today is Test”;
System.out.println(s.indexOf(‘T’));
System.out.println(s.substring(0, 7) + ” ” + “Holiday”);
0
Today i Holiday

(g) What are the values stored in variables r1 and r2:
(i) double r1 = Math.abs(Math.min(-2.83, -5.83));
r1 = 5.83
(ii) double r2 = Math.sqrt(Math.floor(16.3));
r2 = 4.0

(h) Give the output of the following code:
String A = “26”, B = “100”;
String D = A + B + “200”;
int x = Integer.parseInt(A);
int y = Integer.parseInt(B);
int d = x + y;
System.out.println(“Result 1 = ” + D);
System.out.println(“Result 2 = ” + d);
Result 1 = 26100200
Result 2 = 126

(i) Analyze the given program segment and answer the following questions:
for(int i = 3; i <= 4; i++){
    for(int j = 2; j < i; j++){
        System.out.print(“”);
    }
    System.out.println(“WIN”);
}
(i) How many times does the inner loop execute?
3 times
(ii) Write the output of the program segment.
WIN
WIN

(j) What is the difference between the Scanner class functions next() and nextLine()?
The next() method finds and returns the next complete token.
The nextLine() method advances the scanner past the current line and returns the input that was skipped.

SECTION B (60 Marks)
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
Define a class ElectricBill with the following specifications:
Class name: ElectricBill
Instance variables/data members:
String n – to store the name of the customer
int units – to store the number of units consumed
double bill – to store the amount to be paid
Member functions:
void accept() – to accept the name of the customer and number of units consumed
void calculate() – to calculate the bill as per the following tariff:

Number of unitsRate per unit
First 100 units₹ 2.00
Next 200 units₹ 3.00
Above 300 units₹ 5.00

A surcharge of 2.5% is charged if the number of units consumed is above 300 units.
void print() – to print the details as follows:
Name of the customer: …………….
Number of units consumed: …………….
Bill amount: …………….
Write a main() method to create an object of the class and call the above methods.

import java.util.Scanner;
class ElectricBill{
    String n;
    int units;
    double bill;
    public void accept(){
        Scanner in = new Scanner(System.in);
        System.out.print("Customer name: ");
        n = in.nextLine();
        System.out.print("Units consumed: ");
        units = Integer.parseInt(in.nextLine());
    }
    public void calculate(){
        if(units <= 100)
            bill = units * 2.0;
        else if(units <= 300)
            bill = 200 + (units - 100) * 3.0;
        else{
            bill = 800 + (units - 300) * 5.0;
            bill += 2.5 / 100 * bill;
        }
    }
    public void print(){
        System.out.println("Name of the customer: " + n);
        System.out.println("Number of units consumed: " + units);
        System.out.println("Bill amount: " + bill);
    }
    public static void main(String[] args){
        ElectricBill obj = new ElectricBill();
        obj.accept();
        obj.calculate();
        obj.print();
    }
}

Question 5
Write a program to accept a number and check and display whether it is a spy number or not. A number is spy if the sum of its digits equals the product of its digits.
Example: Consider the number 1124.
Sum of the digits = 1 + 1 + 2 + 4 = 8
Product of the digits = 1 × 1 × 2 × 4 = 8

import java.util.Scanner;
class Spy{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n = Integer.parseInt(in.nextLine());
        int sum = 0;
        int product = 1;
        for(int i = n; i != 0; i /= 10){
            sum += i % 10;
            product *= i % 10;
        }
        if(sum == product)
            System.out.println("Spy number");
        else
            System.out.println("Not a Spy Number");
    }
}

Question 6
Using switch statement, write a menu-driven program for the following:
(i) To find and display the sum of the series given below:
S = x1 – x2 + x3 – x4 + x5 … – x20
(where x = 2)
(ii) To display the following series:
1 11 111 1111 11111
For an incorrect choice, an appropriate error message should be displayed.

import java.util.Scanner;
class Menu{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("1. S = x^1 - x^2 + x^3 ... - x^20");
        System.out.println("2. 1 11 111 1111 11111");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(in.nextLine());
        switch(choice){
        case 1:
            int x = 2;
            double sum = 0.0;
            for(int i = 1; i <= 20; i++){
                if(i % 2 == 1)
                    sum += Math.pow(x, i);
                else
                    sum -= Math.pow(x, i);
            }
            System.out.println("Sum = " + sum);
            break;
        case 2:
            int n = 0;
            for(int i = 1; i <= 5; i++){
                n = n * 10 + 1;
                System.out.print(n + " ");
            }
            System.out.println();
            break;
        default:
            System.out.println("Invalid choice!");
        }
    }
}

Question 7
Write a program to input integer elements into an array of size 20 and perform the following operations:
(i) Display the largest number from the array.
(ii) Display the smallest number from the array.
(iii) Display the sum of all the elements of the array.

import java.util.Scanner;
class Find{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int a[] = new int[20];
        int large = 0;
        int small = 0;
        int sum = 0;
        System.out.println("Enter 20 integers:");
        for(int i = 0; i < a.length; i++){
            a[i] = Integer.parseInt(in.nextLine());
            if(i == 0){
                large = a[i];
                small = a[i];
                sum = a[i];
            }
            else{
                if(large < a[i])
                    large = a[i];
                if(small > a[i])
                    small = a[i];
                sum += a[i];
            }
        }
        System.out.println("Largest number: " + large);
        System.out.println("Smallest number: " + small);
        System.out.println("Sum: " + sum);
    }
}

Question 8
Design a class to overload a function check() as follows:
void check(String str, char ch) – to find and print the frequency of a character in a string.
Example:
Input:
str = “success”
ch = ‘s’
Output:
number of s present is = 3
void check(String s1) – to display only the vowels from string s1, after converting it to lowercase.
Example:
Input:
s1 = “computer”
Output:
o u e

class Overload{
    public static void check(String str, char ch){
        int f = 0;
        for(int i = 0; i < str.length(); i++){
            if(ch == str.charAt(i))
                f++;
        }
        System.out.println("number of " + ch + " present is = " + f);
    }
    public static void check(String s1){
        s1 = s1.toLowerCase();
        for(int i = 0; i < s1.length(); i++){
            if("aeiou".indexOf(s1.charAt(i)) >= 0)
                System.out.print(s1.charAt(i) + " ");
        }
        System.out.println();
    }
}

Question 9
Write a program to input forty words in an array. Arrange these words in descending order of alphabets, using selection sort technique. Print the sorted array.

import java.util.Scanner;
class SelectionSort{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        String a[] = new String[40];
        System.out.println("Enter 40 words:");
        for(int i = 0; i < a.length; i++)
            a[i] = in.nextLine();
        for(int i = 0; i < a.length; i++){
            String large = a[i];
            int pos = i;
            for(int j = i + 1; j < a.length; j++){
                if(large.compareToIgnoreCase(a[j]) < 0){
                    large = a[j];
                    pos = j;
                }
            }
            String temp = a[i];
            a[i] = a[pos];
            a[pos] = temp;
        }
        System.out.println("Sorted array:");
        for(int i = 0; i < a.length; i++)
            System.out.print(a[i] + " ");
        System.out.println();
    }
}