ICSE Computer Applications Year 2013

Section A (40 Marks)

Question 1
(a) What is meant by precedence of operators?
Operator precedence determines the order in which expressions are evaluated, to determine the overall value of the expression.
(b) What is a literal?
Literals are data items that are fixed data values.
(c) State the Java concept that is implemented through:
(i) a super class and a subclass.
Inheritance.
(ii) the act of representing essential features without including background details.
Abstraction.
(d) Give a difference between a constructor and a method.
A constructor has no return type, whereas a method always has a return type.
(e) What are the types of casting shown by the following examples?
(i) double x = 15.2;
int y = (int)x;

Explicit typecasting.
(ii) int x = 12;
long y = x;

Implicit typecasting.

Question 2
(a) Name any two wrapper classes.
Integer, Character
(b) What is the difference between a break statement and a continue statement when they occur in a loop?
The break statement is used to exit from a loop. And the continue statement is used to skip a particular iteration in the loop.
(c) Write statements to show how finding the length of a character array char ch[] differs from finding the length of a String object str.
int len = ch.length;
len = str.length();

(d) Name the Java keyword that:
(i) indicates that a method has no return type.
void.
(ii) stores the address of the currently calling object.
this.
(e) What is an exception?
An exception is an abnormal condition that arises in a code sequence at run time.

Question 3
(a) Write a Java statement to create an object mp4 of class Digital.
Digital mp4 = new Digital();
(b) State the values stored in the variables str1 and str2:
String s1 = "good";
String s2 = "world matters";
String str1 = s2.substring(5).replace('t', 'n');
String str2 = s1.concat(str1);

manners
good manners
(c) What does a class encapsulate?
A class encapsulates data and behavior.
(d) Rewrite the following program segment using the if..else statement:
comm = (sale > 15000)? sale * 5 / 100 : 0;
if(sale > 15000)
    comm = sale * 5 / 100;
else
    comm = 0;

(e) How many times will the following loop execute? What value will be returned?
int x = 2, y = 50;
do{
    ++x;
    y -= x++;
}while(x <= 10);
return y;

The loop will run 5 times.
The returned value will be 15.
(f) What is the data type that the following library functions return?
(i) isWhitespace(char ch)
boolean
(ii) Math.random()
double
(g) Write a Java expression for ut + 1/2 ft2
u * t + 1.0 / 2 * f * t * t
(h) If int n[] = {1, 2, 3, 5, 7, 9, 13, 16};
What are the values of x and y?
x = Math.pow(n[4], n[2]);
y = Math.sqrt(n[5] + n[7]);

x = 343.0
y = 5.0
(i) What is the final value of ctr when the iteration process given below, executes?
int ctr = 0;
for(int i = 1; i <= 5; i++)
    for(int j = 1; j <= 5; j += 2)
        ++ctr;

ctr = 15.
(j) Name the methods of Scanner class that:
(i) is used to input an integer data from the standard input stream.
nextInt()
(ii) is used to input a String data from the standard input stream.
nextLine()

Section B (60 Marks)

Accept 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 named FruitJuice with the following description:
Instance variables/data members:
int productCode: stores the product code number.
String flavour: stores the flavour of the juice (e.g. orange, apple, etc.).
String packType: stores the type of packaging (e.g. tetra-pack, PET bottle, etc.).
int packSize: stores package size (e.g. 200ml, 400ml, etc.).
int productPrice: stores the price of the product.
Member functions:
(i) FruitJuice(): default constructor to initialize integer data members to 0 and String data members to “”.
(ii) void input(): to input and store the product code, flavour, pack type, pack size and product price.
(iii) void discount(): to reduce the product price by 10.
(iv) void display(): to display the product code, flavour, pack type, pack size and product price.

import java.io.*;
class FruitJuice{
    private int productCode;
    private String flavour;
    private String packType;
    private int packSize;
    private int productPrice;
    public void input()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Product code: ");
        productCode = Integer.parseInt(br.readLine());
        System.out.print("Flavour: ");
        flavour = br.readLine();
        System.out.print("Pack type: ");
        packType = br.readLine();
        System.out.print("Pack size: ");
        packSize = Integer.parseInt(br.readLine());
        System.out.print("Product price: ");
        productPrice = Integer.parseInt(br.readLine());
    }
    public void discount(){
        productPrice -= 10;
    }
    public void display(){
        System.out.println("Product code: " + productCode);
        System.out.println("Flavour: " + flavour);
        System.out.println("Pack type: " + packType);
        System.out.println("Pack size: " + packSize);
        System.out.println("Product price: " + productPrice);
    }
    public static void main(String args[])throws IOException{
        FruitJuice obj = new FruitJuice();
        obj.input();
        obj.discount();
        obj.display();
    }
}

Question 5
The International Standard Book Number (ISBN) is a unique numeric book identifier which is printed on every book. The ISBN is based upon a 10-digit code. The ISBN is legal if: 1 × digit1 + 2 × digit2 + 3 × digit3 + 4 × digit4 + 5 × digit5 + 6 × digit6 + 7 × digit7 + 8 × digit8 + 9 × digit9 + 10 × digit10 is divisible by 11.
Example: For an ISBN 1401601499, sum = 1 × 1 + 2 × 4 + 3 × 0 + 4 × 1 + 5 × 6 + 6 × 0 + 7 × 1 + 8 × 4 + 9 × 9 + 10 × 9 = 253 which is divisible by 11.
Write a program to:
(i) Input the ISBN code as a 10-digit integer.
(ii) If the ISBN is not a 10-digit integer, output the message “Illegal ISBN” and terminate the program.
(iii) If the number is divisible by 11, output the message “Legal ISBN”. If the sum is not divisible by 11, output the message “Illegal ISBN”.

import java.io.*;
class ISBN{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("ISBN: ");
        long isbn = Long.parseLong(br.readLine());
        String s = Long.toString(isbn);
        if(s.length() != 10){
            System.out.println("Illegal ISBN");
            return;
        }
        long sum = 0L;
        long num = 10;
        while(isbn != 0L){
            sum += isbn % 10L * num;
            isbn /= 10L;
            num--;
        }
        if(sum % 11 == 0)
            System.out.println("Legal ISBN");
        else
            System.out.println("Illegal ISBN");
    }
}

Question 6
Write a program that encodes a word into Piglatin. To translate word into Piglatin word, convert the word into uppercase and then place the first vowel of the original word as the start of the new word along with the remaining alphabets. The alphabets present before the vowel being shifted towards the end followed by “AY”.
Sample Input 1: London
Output: ONDONLAY
Sample Input 2: Olympics
Output: OLYMPICSAY

import java.io.*;
class Piglatin{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Word: ");
        String word = br.readLine();
        word = word.trim();
        if(word.indexOf(' ') > 0)
            word = word.substring(0, word.indexOf(' '));
        word = word.toUpperCase();
        String p = "";
        int pos = 0;
        loop:
        for(int i = 0; i < word.length(); i++){
            char ch = word.charAt(i);
            switch(ch){
                case 'A':
                case 'E':
                case 'I':
                case 'O':
                case 'U':
                pos = i;
                break loop;
            }
        }
        p = word.substring(pos);
        p += word.substring(0, pos);
        p += "AY";
        System.out.println(p);
    }
}

Question 7
Write a program to input 10 integer elements in an array and sort them in descending order using bubble sort technique.

import java.io.*;
class Bubble{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a[] = new int[10];
        System.out.println("Enter 10 integers:");
        for(int i = 0; i < a.length; i++)
            a[i] = Integer.parseInt(br.readLine());
        for(int i = 0; i < a.length; i++){
            for(int j = 0; j < a.length - 1 - i; j++){
                if(a[j] < a[j + 1]){
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
        System.out.println("Sorted elements in descending order:");
        for(int i = 0; i < a.length; i++)
            System.out.print(a[i] + "\t");
        System.out.println();
    }
}

Question 8
Design a class to overload a function series() as follows:
(i) double series(double n) with one double argument and returns the sum of the series, sum = 1 / 1 + 1 / 2 + 1 / 3 + … + 1 / n.
(ii) double series(double a, double n) with two double arguments and returns the sum of the series, sum = 1 / a2 + 4 / a5 + 7 / a8 + 10 / a11 + … to n terms.

class Overload{
    public double series(double n){
        double sum = 0.0;
        for(int i = 1; i <= n; i++)
            sum += 1.0 / i;
        return sum;
    }
    public double series(double a, double n){
        double sum = 0.0;
        int count = 1;
        int p = 2;
        for(int i = 1; i <= n; i++){
            sum += count / Math.pow(a, p);
            count += 3;
            p += 3;
        }
        return sum;
    }
}

Question 9
Using the switch statement, write a menu-driven program:
(i) To check and display whether a number input by the user is a composite number or not. (A number is said to be composite if it has one or more than one factor excluding 1 and the number itself.). Example: 4, 6, 8, 9, …
(ii) To find the smallest digit of an integer that is input:
Sample Input: 6524
Output: Smallest digit is 2.
For an incorrect choice, an appropriate error message should be displayed.

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. Composite Number");
        System.out.println("2. Smallest Digit");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(br.readLine());
        switch(choice){
            case 1:
            System.out.print("Number: ");
            int num = Integer.parseInt(br.readLine());
            int f = 0;
            for(int i = 1; i <= num; i++){
                if(num % i == 0)
                    f++;
            }
            if(f > 2)
                System.out.println(num + " is composite.");
            else
                System.out.println(num + " is not composite.");
            break;
            case 2:
            System.out.print("Number: ");
            num = Integer.parseInt(br.readLine());
            int small = num % 10;
            while(num != 0){
                int digit = num % 10;
                if(small > digit)
                    small = digit;
                num /= 10;
            }
            System.out.println("Smallest digit: " + small);
            break;
            default:
            System.out.println("Invalid choice!");
        }
    }
}

Leave a Reply