ICSE Computer Applications 2012

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 (40 Marks)
Attempt all questions.

Question 1 [10]
(a) Give one example each of a primitive data type and a composite data type.
int is a primitive data type.
String is a composite data type.

(b) Give one point of difference between unary and binary operators.
Unary operators act on one operand.
Binary operators act on two operands.

(c) Differentiate between call by value or pass by value and call by reference or pass by reference.
In call by value, a copy of the argument is passed to the function, so any changes made inside the function are not reflected to the original data.
In call by reference, the original data is passed to the function, so any changes made inside the function are reflected back to the original data.

(d) Write a Java expression for:
Mathematical Expression ICSE 2012 Computer Applications
Math.sqrt(2 * a * s + u * u)

(e) Name the type of error (syntax, runtime or logical error) in each case given below:
(i) Division by a variable that contains a value of zero.
Runtime error
(ii) Multiplication operator used when the operation should be division.
Logical error
(iii) Missing semicolon.
Syntax error

Question 2
(a) Create a class with one integer instance variable. Initialize the variable using:
(i) default constructor
(ii) parameterized constructor

class MyClass{
    int x;
    public MyClass(){
        x = 0;
    }
    public MyClass(int n){
        x = n;
    }
}

(b) Complete the code below to create an object of Scanner class:
Scanner sc = ____ Scanner(________);
Scanner sc = new Scanner(System.in);

(c) What is an array? Write a statement to declare an integer array of 10 elements.
An array is a collection of similar elements with fixed length.
int a[] = new int[10];

(d) Name the search or sort algorithm that:
(i) Makes several passes through the array, selecting the next smallest item in the array each time and placing it where it belongs in the array.
Selection Sort Algorithm
(ii) At each stage, compares the sought key value with the key value of the middle element of the array.
Binary Search Algorithm

(e) Differentiate between public and private modifiers for members of a class.
The public keyword makes a class member accessible from anywhere.
The private keyword makes a class member accessible from only within the class in which it is declared.

Question 3
(a) What are the values of x and y when the following statements are executed?
int a = 63, b = 36;
boolean x = (a > b)? true : false;
int y = (a < b)? a : b;
x = true
y = 36

(b) State the values of n and ch:
char c = ‘A’;
int n = c + 1;
char ch = (char)n;
n = 66
ch = ‘B’

(c) What will be the result stored in x after evaluating the following expression?
int x = 4;
x += (x++) + (++x) + x;
x = x + ((x++) + (++x) + x);
x = 4 + (4 + 6 + 6)
x = 4 + 16
x = 20

(d) Give output of the following program segment:
double x = 2.9, y = 2.5;
System.out.println(Math.min(Math.floor(x), y));
System.out.println(Math.max(Math.ceil(x), y));
2.0
3.0

(e) State the output of the following program segment:
String s = “Examination”;
int n = s.length();
System.out.println(s.startsWith(s.substring(5, n)));
System.out.println(s.charAt(2) == s.charAt(6));
false
true

(f) State the method that:
(i) Converts a string to a primitive float data type.
Float.parseFloat()
(ii) Determines if the specified character is an uppercase character.
Character.isUpperCase()

(g) State the data type and values of a and b after the following segment is executed:
String s1 = “Computer”, s2 = “Applications”;
a = s1.compareTo(s2);
b = s1.equals(s2);
a = 2
false

(h) What will the following code output?
String s = “malayalam”;
System.out.println(s.indexOf(‘m’));
System.out.println(s.lastIndexOf(‘m’));
0
8

(i) Rewrite the following program segment using while instead of for statement:
int f = 1, i;
for(i = 1; i <= 5; i++){
    f *= i;
    System.out.println(f);
}
int f = 1, i = 1;
while(i <= 5){
    f *= i;
    System.out.println(f);
    i++;
}

(j) In the program given below:

class MyClass{
    static int x = 7;
    int y = 2;
    public static void main(String args[]){
        MyClass obj = new MyClass();
        System.out.println(x);
        obj.sampleMethod(5);
        int a = 6;
        System.out.println(a);
    }
    void sampleMethod(int n){
        System.out.println(n);
        System.out.println(y);
    }
}

State the name and the value of the:
(i) method argument or argument variable
n is the method argument
(ii) class variable
x is the class variable
(iii) local variable
a is the local variable
(iv) instance variable
y is the instance variable

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 such that the logic of the program is clearly depicted.
Flowcharts and algorithms are not required.

Question 4
Define a class called Library with the following description:
Instance variables/data members:
int accNum – stores the accession number of the book
String title – stores the title of the book
String author – stores the name of the author
Member methods:
(i) void input() – To input and store the accession number, title and author
(ii) void compute() – To accept the number of days late, calculate and display the fine charged at the rate of ₹ 2 per day.
(iii) void display() – To display the details in the following format:
Accession Number Title Author
————————– ——- ———-
Write a main() method to create an object of the class and call the above member methods.

import java.util.Scanner;
class Library{
    int accNum;
    String title;
    String author;
    public void input(){
        Scanner in = new Scanner(System.in);
        System.out.print("Accession number: ");
        accNum = Integer.parseInt(in.nextLine());
        System.out.print("Title: ");
        title = in.nextLine();
        System.out.print("Author: ");
        author = in.nextLine();
    }
    public void compute(){
        Scanner in = new Scanner(System.in);
        System.out.print("Number of days late: ");
        int d = Integer.parseInt(in.nextLine());
        double fine = d * 2;
        System.out.println("Fine: Rs. " + fine);
    }
    public void display(){
        System.out.println("Accession Number Title\tAuthor");
        System.out.println(accNum + "\t" + title + "\t" + author);
    }
    public static void main(String args[]){
        Library obj = new Library();
        obj.input();
        obj.compute();
        obj.display();
    }
}

Question 5
Given below is a hypothetical table showing rates of income tax for male citizens below the age of 65 years:

Taxable Income in ₹Income Tax in ₹
Does not exceed ₹ 160000NIL
Is greater than ₹ 160000 & less than or equal to ₹ 500000(TI – 160000) * 10%
Is greater than ₹ 500000 & less than or equal to ₹ 800000[(TI – 500000) * 20%] + 34000
Is greater than ₹ 800000[(TI – 800000) * 30%] + 94000

Write a program to input the age, gender (male or female) and taxable income of a person.
If the age is more than 65 years or the gender is female, display “wrong category”.
If the age is less than or equal to 65 years and the gender is male, compute and display the income tax payable as per the table given above.

import java.util.Scanner;
class Tax{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.print("Age: ");
        int age = Integer.parseInt(in.nextLine());
        System.out.print("Gender: ");
        char gender = in.nextLine().charAt(0);
        System.out.print("Taxable income: ");
        double i = Double.parseDouble(in.nextLine());
        if(age > 65 || (gender != 'm' && gender != 'M')){
            System.out.println("Wrong category!");
            return;
        }
        double t = 0.0;
        if(i <= 160000)
            t = 0.0;
        else if(i <= 500000)
            t = (i - 160000) * 0.1;
        else if(i <= 800000)
            t = (i - 500000) * 0.2 + 34000;
        else
            t = (i - 800000) * 0.3 + 94000;
        System.out.println("Income tax: Rs. " + t);
    }
}

Question 6
Write a program to accept a string. Convert the string to uppercase. Count and output the number of double-letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
Sample Output: 4

import java.util.Scanner;
class Sequence{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the string: ");
        String s = in.nextLine().toUpperCase();
        int count = 0;
        for(int i = 0; i < s.length() - 1; i++){
            char a = s.charAt(i);
            char b = s.charAt(i + 1);
            if(a == b)
                count++;
        }
        System.out.println(count);
    }
}

Question 7
Design a class to overload a function polygon() as follows:
(i) void polygon(int n, char ch) – with one integer argument and one character type argument that draws a filled square of side n using the character stored in ch.
(ii) void polygon(int x, int y) – with two integer arguments that draws a filled rectangle of length x and breadth y, using the symbol ‘@’.
(iii) void polygon() – with no arguments that draws a filled triangle shown below:
Example:
(i) Input value of a = 2, ch = ‘O’
Output:
OO
OO
(ii) Input value of x = 2, y = 5
Output:
@@@@@
@@@@@
(iii) Output:
*
**
***

class Overload{
    public static void polygon(int n, char ch){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++)
                System.out.print(ch);
            System.out.println();
        }
    }
    public static void polygon(int x, int y){
        for(int i = 1; i <= y; i++){
            for(int j = 1; j <= x; j++)
                System.out.print("@");
            System.out.println();
        }
    }
    public static void polygon(){
        for(int i = 1; i <= 3; i++){
            for(int j = 1; j <= i; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

Question 8
Using the switch case statement, write a menu-driven program to:
(i) Generate and display the first 10 terms of the Fibonacci series 0, 1, 1, 2, 3, 5, …
The first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two.
(ii) Find the sum of the digits of an integer that is input:
Sample Input: 15390
Sample Output: Sum of the digits = 18
For an incorrect choice, 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. Fibonacci series");
        System.out.println("2. Sum of digits");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(in.nextLine());
        switch(choice){
        case 1:
            int a = 0;
            int b = 1;
            int c = 0;
            System.out.print(a + " " + b);
            for(int i = 3; i <= 10; i++){
                c = a + b;
                System.out.print(" " + c);
                a = b;
                b = c;
            }
            System.out.println();
            break;
        case 2:
            System.out.print("Enter the number: ");
            int n = Integer.parseInt(in.nextLine());
            int sum = 0;
            for(int i = n; i != 0; i /= 10)
                sum += i % 10;
            System.out.println("Sum of digits: " + sum);
            break;
        default:
            System.out.println("Invalid choice!");
        }
    }
}

Question 9
Write a program to accept the names of 10 cities in a single-dimensional string array and their STD (Subscribers Trunk Dialing) codes in another single dimensional integer array. Search for a name of a city input by the user in the list. If found, display “Search successful” and print the name of the city along with its STD code, or else display the message “Search unsuccessful. No such city in the list”.

import java.util.Scanner;
class Search{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        String c[] = new String[10];
        int s[] = new int[10];
        int i;
        for(i = 0; i < c.length; i++){
            System.out.print("City: ");
            c[i] = in.nextLine();
            System.out.print("STD Code: ");
            s[i] = Integer.parseInt(in.nextLine());
        }
        System.out.print("City name to be searched: ");
        String key = in.nextLine();
        i = 0;
        while(i < c.length){
            if(key.equalsIgnoreCase(c[i]))
                break;
            i++;
        }
        if(i == c.length)
            System.out.println("Search unsuccessful. No such city in the list");
        else{
            System.out.println("Search successful");
            System.out.println(c[i] + " - " + s[i]);
        }
    }
}