ICSE Computer Applications Year 2008

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 (40 Marks)

Attempt all questions.

Question 1

(a) Mention any two attributes required for class declaration.
Characteristics and behavior.

(b) State the difference between token and identifier.
Token is the smallest individual unit in a program.
Identifiers are fundamental building blocks of a program and are used as the general terminology for the names given to different parts of the program.

(c) Explain instance variable. Give an example.
A data member that is created for every object of the class is an instance variable.
Example:

class Example{
    int num; //instance variable
}

(d) What is inheritance and how is it useful in Java?
Inheritance is a mechanism that allows a class to derive properties from another class. It is useful in Java because:
(i) It gives us the capability to express the inheritance relationship.
(ii) It allows reuse of code.

(e) Explain any two types of access specifier.
public makes the variables and methods visible to all classes.
private makes the variables and methods visible only within the class.

Question 2

(a) What is meant by an infinite loop? Give an example.
An infinite loop is a loop which keeps on executing repeatedly because its terminating condition is either missing or is never reached.
Example:

while(true)
    System.out.println("Google");

(b) State the difference between == operator and equals() method.
The == operator is a relational operator that is used to compare the contents of primitive data type values for equality. The equals() method is a member of the String class and is used to compare the contents of two String values for equality.

(c) Differentiate between actual parameter and formal parameter.
The parameters that appear in function call are called actual parameters. The parameters that appear in function definition are called formal parameters.

(d) What is the use of exception handling in Java?
It separates error-handling code from normal code.
It makes the programs clear, robust and fault-tolerant.

(e) Differentiate between base and derived class.
A class from which another class is inheriting its properties is called base class. The class inheriting properties from another class is called derived class.

Question 3

(a) Explain the function of each of the following with an example:
(i) break;

The break keyword is used to terminate a loop or a switch statement.

for(int i = 1; i <= 10; i++){
    if(i % 5 == 0)
        break;
    System.out.println(i);
}

(ii) continue;
The continue keyword is used to skip the current iteration and moves the program flow to the next iteration of the loop.

for(int i = 1; i <= 10; i++){
    if(i % 3 == 0)
        continue;
    System.out.println(i);
}

(b) Convert the following segment into equivalent for loop:

{
    int i, l = 0;
    while(i <= 20)
        System.out.print(i + " ");
    l++;
}
{
    int i, l = 0;
    for(;l <= 20;)
        System.out.print(i + " ");
    l++;
}

(c) If a = 5, b = 9 calculate the value of a += a++ – ++b + a;
a = 6

(d) Give the output of the following expressions:
(i) If x = -9.99, calculate Math.abs(x); 9.99
(ii) If x = 9.0, calculate Math.sqrt(x); 3.0

(e) If String x = “Computer”;
String y = “Applications”;
What do the following functions return?
(i) System.out.println(x.substring(1, 5));
(ii) System.out.println(x.indexOf(x.charAt(4)));
(iii) System.out.println(y + x.substring(5));
(iv) System.out.println(x.equals(y));

ompu
4
Applicationster
false

(f) If array[] = {1, 9, 8, 5, 2};
(i) What is array.length?
5
(ii) What is array[2]?
8

(g) What does the following mean?

Employee staff = new Employee();

An object named “staff” is being created of class “Employee”.

(h) Write a Java statement to input/read the following from the user using the keyboard.
(i) Character

char ch = in.readLine().charAt(0);

(ii) String

String s = in.readLine();

Section B (60 Marks)

Attempt any four questions from this section.

Question 4

Define a class Employee having the following description:
Data members/instance variables:
int pan: to store personal account number.
String name: to store name.
double taxIncome: to store annual taxable income.
double tax: to store tax that is calculated.
Member functions:
input(): store the pan number, name, taxable income.
calc(): calculate tax for an employee.
display(): output details of an employee.
Write a program to compute the tax according to the given conditions and display the output as per given format.

Total Annual Taxable IncomeTax Rate
Up to Rs. 1,00,000No tax
From 1,00,001 to 1,50,00010% of the income exceeding Rs. 1,00,000
From 1,50,001 to 2,50,000Rs. 5000 + 20% of the income exceeding Rs. 1,50,000
Above Rs. 2,50,000Rs. 25,000 + 30% of the income exceeding Rs. 2,50,000.

Output:

Pan Number        Name        Tax-income        Tax
- - - -
- - - -
- - - -
- - - -
import java.io.*;
class Employee{
    private int pan;
    private String name;
    private double taxIncome;
    private double tax;
    public void input()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("PAN Number: ");
        pan = Integer.parseInt(br.readLine());
        System.out.print("Name: ");
        name = br.readLine();
        System.out.print("Taxable Income: ");
        taxIncome = Double.parseDouble(br.readLine());
    }
    public void calc(){
        if(taxIncome <= 100000.0)
            tax = 0.0;
        else if(taxIncome <= 150000.0)
            tax = 10.0 / 100 * (taxIncome - 100000.0);
        else if(taxIncome <= 250000.0)
            tax = 5000.0 + 20.0 / 100 * (taxIncome - 150000.0);
        else
            tax = 25000.0 + 30.0 / 100 * (taxIncome - 250000.0);
    }
    public void display(){
        System.out.println("PAN Number: " + pan);
        System.out.println("Name: " + name);
        System.out.println("Taxable Income: Rs. " + taxIncome);
        System.out.println("Tax: Rs. " + tax);
    }
    public static void main(String args[])throws IOException{
        Employee obj = new Employee();
        obj.input();
        obj.calc();
        obj.display();
    }
}

Question 5

Write a program to input a string and print out the text with the uppercase and lowercase letters reversed, but all other characters should remain the same as before.
Example:
INPUT: WelComE TO School
OUTPUT: wELcOMe to sCHOOL

import java.io.*;
class Reversed{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the string: ");
        String s = br.readLine();
        String rev = new String();
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(Character.isLetter(ch)){
                if(Character.isUpperCase(ch))
                    rev += Character.toLowerCase(ch);
                else
                    rev += Character.toUpperCase(ch);
            }
            else
                rev += ch;
        }
        System.out.println(rev);
    }
}

Question 6

Define a class and store the given city names in a single dimensional array. Sort these names in alphabetical order using the bubble sort technique only.
INPUT: Delhi, Bangalore, Agra, Mumbai, Calcutta
OUTPUT: Agra, Bangalore, Calcutta, Delhi, Mumbai

class Cities{
    public static void main(String args[]){
        String c[] = {"Delhi", "Bangalore", "Agra", "Mumbai", "Calcutta"};
        for(int i = 0; i < c.length; i++){
            for(int j = 0; j < c.length - 1 - i; j++){
                if(c[j].compareTo(c[j + 1]) > 0){
                    String temp = c[j];
                    c[j] = c[j + 1];
                    c[j + 1] = temp;
                }
            }
        }
        for(int i = 0; i < c.length; i++)
            System.out.print(c[i] + " ");
        System.out.println();
    }
}

Question 7

Write a menu-driven class to accept a number from the user and check whether it is a palindrome or a perfect number.
(a) Palindrome number – a number is a palindrome which when read in reverse order is same as read in the right order. Example: 11, 101, 151, etc.
(b) Perfect number – a number is called perfect if it is equal to the sum of its factors other than the number itself. Example: 6 = 1 + 2 + 3.

import java.io.*;
class Menu{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("1. Palindrome Number");
        System.out.println("2. Perfect Number");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(br.readLine());
        switch(choice){
            case 1:
            System.out.print("Number: ");
            int n = Integer.parseInt(br.readLine());
            if(isPalindrome(n))
                System.out.println(n + " is palindrome.");
            else
                System.out.println(n + " is not palindrome.");
            break;
            case 2:
            System.out.print("Number: ");
            n = Integer.parseInt(br.readLine());
            if(isPerfect(n))
                System.out.println(n + " is perfect number.");
            else
                System.out.println(n + " is not perfect number.");
            break;
            default:
            System.out.println("Invalid choice");
        }
    }
    public static boolean isPalindrome(int n){
        int rev = 0;
        for(int i = n; i != 0; i /= 10)
            rev = rev * 10 + i % 10;
        return (n == rev);
    }
    public static boolean isPerfect(int n){
        int sum = 0;
        for(int i = 1; i < n; i++){
            if(n % i == 0)
                sum += i;
        }
        return (n == sum);
    }
}

Question 8

Write a class with the name Volume using function overloading that computes the volume of a cube, a sphere and a cuboid.
Formula:
volume of a cube (vc) = s * s * s.
volume of a sphere (vs) = 4 / 3 * π * r * r * r where π = 3.14 or 22 / 7.
volume of a cuboid (vcd) = l * b * h.

class Volume{
    public static double volume(double s){
        return s * s * s;
    }
    public static double volume(double p, double r){
        return 4.0 / 3 * p * r * r * r;
    }
    public static double volume(double l, double b, double h){
        return l * b * h;
    }
}

Question 9

Write a program to calculate and print the sum of each of the following series:
(a) Sum (S) = 2 – 4 + 6 – 8 + … – 20.
(b) Sum (S) = x / 2 + x / 5 + x / 8 + x / 11 + … + x / 20 where the value of x is to be input by the user.

import java.io.*;
class Series{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("x = ");
        double x = Double.parseDouble(br.readLine());
        double sum = 0.0;
        int sign = 1;
        for(int i = 1; i <= 10; i++){
            sum += sign * i * 2;
            sign *= -1;
        }
        System.out.println("Series 1 sum: " + sum);
        sum = 0.0;
        double d = 2;
        while(d <= 20){
            sum += x / d;
            d += 3;
        }
        System.out.println("Series 2 sum: " + sum);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.