ICSE Computer Applications Year 2009

Section A (40 Marks)

Question 1

a) Why is a class called a factory of objects?
A class is called a factory of objects because with one class definition, we can create several objects, each with their own state and behavior.
b) State the difference between a boolean literal and a character literal.
A boolean literal occupies 1 byte of storage, whereas a character literal occupies 2 bytes of storage. A boolean literal can store either true or false, whereas a character literal can store Unicode characters.
c) What is the use and syntax of a ternary operator?
A ternary operator is an alternative for if-else statement. It works with three operands.
Following is the syntax:
variable = (condition)? value 1 : value 2;
d) Write one word answer for the following:
(i) A method that converts a string to a primitive integer data type.
Integer.parseInt()
(ii) The default initial value of a boolean variable data type.
false
e) State one similarity and one difference between while and for loop.
Similarity: Both while and for loops are entry-controlled loops.
Difference: The while loop is used when the number of iterations is not known, whereas a for loop is used when the number of iterations is known.

Question 2

a) Write the function prototype for the function “sum” that takes an integer variable x as its argument and returns a value of float data type.
float sum(int x)
b) What is the use of the keyword this?
The keyword this is used to refer to the currently calling object.
c) Why is a class known as composite data type?
A class is known as a composite data type because it is generally user-defined, and it is built using one or more primitive data types.
d) Name the keyword that:
(i) is used for allocating memory to an array.
new
(ii) causes the control to transfer back to the method call.
return
e) Differentiate between pure and impure functions.
A pure function does not change the state of an object, whereas an impure function changes the state of an object.

Question 3

a) Write an expression for:
ICSE 2009 Computer Application Expression
Math.pow(a + b, n) / (Math.sqrt(3) + b)
b) The following is a segment of a program:

x = 1; y = 1;
if(n > 0)
{
    x = x + 1;
    y = y - 1;
}

What will be the value of x and y, if n assumes a value (i) 1 (ii) 0?
(i) x = 2, y = 0
(ii) x = 1, y = 1
c) Analyze the following program segment and determine how many times the body of the loop will be executed (show the working):

x = 5; y = 50;
while(x <= y)
{
    y = y / x;
    System.out.println(y);
}

y = 50 –> y = 10 –> y = 2
The loop body executes 2 times.
d) When there are multiple definitions with the same function name, what makes them different from each other?
The number of arguments.
The data types of each argument.
e) Given that int x[][] = {{2, 4, 6}, {3, 5, 7}};
What will be the value of x[1][0] and x[0][2]?
x[1][0] = 3
x[0][2] = 6
f) Give the output of the following code fragment when (i) opn = ‘b’ (ii) opn = ‘x’ (iii) opn = ‘a’:

switch(opn)
{
    case 'a':
    System.out.println("Platform Independent");
    break;
    case 'b':
    System.out.println("Object Oriented");
    case 'c':
    System.out.println("Robust and Secure");
    break;
    default:
    System.out.println("Wrong Input");
}

(i) When opn = ‘b’, the output is:
Object Oriented
Robust and Secure
(ii) When opn = ‘x’, the output is:
Wrong Input
(iii) When opn = ‘a’, the output is:
Platform Independent
g) Consider the following code and answer the questions that follow:

class academic
{
    int x, y;
    void access()
    {
        int a, b;
        academic student = new academic();
        System.out.println("Object created");
    }
}

(i) What is the object name of class academic?
The object name is student.
(ii) Name the class variables used in the program.
The class variables are x and y.
(iii) Write the local variables used in the program.
The local variables are a and b.
(iv) Give the type of function used and its name.
The type of function is void. It is used to indicate that the function doesn’t return any value.
h) Convert the following segment into an equivalent do loop:

int x, c;
for(x = 10, c = 20; c > 10; c = c - 2)
    x++;
int x = 10, c = 20;
while(c > 10){
    c = c - 2;
    x++;
}

Section B (60 Marks)

Question 4

An electronics shop has announced the following seasonal discounts on the purchase of certain items:

Purchase amount in Rs. Discount on Laptop Discount on Desktop PC
0 – 25000 0.0% 5.0%
25001 – 57000 5.0% 7.6%
57001 – 100000 7.5% 10%
More than 100000 10.0% 15.0%

Write a program based on the above criteria to input name, address, amount of purchase and the type of purchase (L for Laptop and D for Desktop) by a customer. Compute and print the net amount to be paid by a customer along with his name and address.
(Hint: Discount = (discount rate / 100) * amount of purchase, Net amount = amount of purchase – discount)

import java.io.*;
class Shop{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Name: ");
        String name = br.readLine();
        System.out.print("Address: ");
        String address = br.readLine();
        System.out.print("Amount of purchase: ");
        double amount = Double.parseDouble(br.readLine());
        System.out.print("Type of purchase: ");
        char type = Character.toUpperCase(br.readLine().charAt(0));
        double discount = 0.0;
        if(amount <= 25000){
            if(type == 'L')
                discount = 0.0;
            else if(type == 'D')
                discount = 5.0;
        }
        else if(amount <= 57000){
            if(type == 'L')
                discount = 5.0;
            else if(type == 'D')
                discount = 7.6;
        }
        else if(amount <= 100000){
            if(type == 'L')
                discount = 7.5;
            else if(type == 'D')
                discount = 10.0;
        }
        else{
            if(type == 'L')
                discount = 10.0;
            else if(type == 'D')
                discount = 15.0;
        }
        discount = discount / 100 * amount;
        double net = amount - discount;
        System.out.println("Customer's name: " + name);
        System.out.println("Address: " + address);
        System.out.println("Net Amount: " + net);
    }
}

Question 5

Write a program to generate a triangle or an inverted triangle till n terms based upon the user’s choice of triangle to be displayed.
Example 1:
INPUT:
Type 1 for a triangle and type 2 for an inverted triangle
1
Enter the number of terms:
5
OUTPUT:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Example 2:
INPUT:
Type 1 for a triangle and type 2 for an inverted triangle
2
Enter the number of terms:
6
OUTPUT:
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

import java.io.*;
class Triangle{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Type 1 for Triangle");
        System.out.println("Type 2 for Inverted Triangle");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(br.readLine());
        switch(choice){
            case 1:
            System.out.print("N = ");
            int n = Integer.parseInt(br.readLine());
            for(int i = 1; i <= n; i++){
                for(int j = 1; j <= i; j++){ 
                    System.out.print(i + " "); 
                } 
                System.out.println(); 
            } 
            break; 
            case 2: 
            System.out.print("N = "); 
            n = Integer.parseInt(br.readLine()); 
            for(int i = n; i >= 1; i--){
                for(int j = 1; j <= i; j++){
                    System.out.print(i + " ");
                }
                System.out.println();
            }
            break;
            default:
            System.out.println("Invalid Input");
        }
    }
}

Question 6

Write a program to input a sentence and print the number of characters found in the longest word of the given sentence.
For example, if s = “India is my country” then the output should be 7.

import java.io.*;
class Longest{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Sentence: ");
        String s = br.readLine().trim() + " ";
        String word = "";
        String longest = "";
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(ch == ' '){
                if(longest.length() < word.length())
                    longest = word;
                word = "";
            }
            else
                word += ch;
        }
        int len = longest.length();
        System.out.println("Length = " + len);
    }
}

Question 7

Design a class to overload a function num_calc() as follows:
a) void num_calc(int num, char ch) with one integer argument and one character argument computes the square of integer argument if choice ch is ‘s’ otherwise finds its cube.
b) void num_calc(int a, int b, char ch) with two integer arguments and one character argument, computes the product of integer arguments if ch is ‘p’ else adds the integers.
c) void num_calc(String s1, String s2) with two string arguments, which prints whether the strings are equal or not.

class Overload{
    public void num_calc(int num, char ch){
        if(ch == 's' || ch == 'S'){
            int s = num * num;
            System.out.println("Square = " + s);
        }
        else{
            int c = num * num * num;
            System.out.println("Square = " + c);
        }
    }
    public void num_calc(int a, int b, char ch){
        if(ch == 'p' || ch == 'P'){
            int p = a * b;
            System.out.println("Product = " + p);
        }
        else{
            int s = a + b;
            System.out.println("Sum = " + s);
        }
    }
    public void num_calc(String s1, String s2){
        if(s1.equalsIgnoreCase(s2))
            System.out.println("They are equal");
        else
            System.out.println("They are unequal");
    }
}

Question 8

Write a menu-driven program to accept a number from the user and check whether it is a BUZZ number or to accept any two numbers and print the GCD of them.
a) A BUZZ number is the number which either ends with 7 or is divisible by 7.
b) GCD (Greatest Common Divisor) of two integers is calculated by continued division method. Divide the larger number by the smaller; the remainder then divides the previous divisor. The process is repeated until the remainder is zero. The divisor then results the GCD.

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. Buzz number");
        System.out.println("2. GCD of two numbers");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(br.readLine());
        switch(choice){
            case 1:
            System.out.print("N = ");
            int n = Integer.parseInt(br.readLine());
            if(n % 10 == 7 || n % 7 == 0)
                System.out.println("Buzz number");
            else
                System.out.println("Not a buzz number");
            break;
            case 2:
            System.out.print("First number: ");
            int a = Integer.parseInt(br.readLine());
            System.out.print("Second number: ");
            int b = Integer.parseInt(br.readLine());
            while(a % b != 0){
                int rem = a % b;
                a = b;
                b = rem;
            }
            System.out.println("GCD = " + b);
            break;
            default:
            System.out.println("Invalid Input");
        }
    }
}

Question 9

The annual examination results of 50 students in a class is tabulated as follows:

Roll No. Subject A Subject B Subject C
…….. …….. …….. ……..

Write a program to read the data, calculate and display the following:
a) Average marks obtained by each student.
b) Print the roll number and average marks of the students whose average mark is above 80.
c) Print the roll number and average marks of the students whose average mark is below 40.

import java.io.*;
class Exam{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int roll[] = new int[50];
        int a[] = new int[50];
        int b[] = new int[50];
        int c[] = new int[50];
        double avg[] = new double[50];
        for(int i = 0; i < roll.length; i++){
            System.out.print("Enter roll: ");
            roll[i] = Integer.parseInt(br.readLine());
            System.out.print("Marks in Subject A: ");
            a[i] = Integer.parseInt(br.readLine());
            System.out.print("Marks in Subject B: ");
            b[i] = Integer.parseInt(br.readLine());
            System.out.print("Marks in Subject C: ");
            c[i] = Integer.parseInt(br.readLine());
            avg[i] = (a[i] + b[i] + c[i]) / 3.0;
        }
        System.out.println("Average marks obtained by each student:");
        for(int i = 0; i < avg.length; i++)
            System.out.print(avg[i] + "\t");
        System.out.println();
        System.out.println("Those scoring above 80:");
        for(int i = 0; i < avg.length; i++) if(avg[i] > 80)
                System.out.print("Roll " + roll[i] + ": " + avg[i] + "\t");
        System.out.println();
        System.out.println("Those scoring below 40:");
        for(int i = 0; i < avg.length; i++)
            if(avg[i] < 40)
                System.out.print("Roll " + roll[i] + ": " + avg[i] + "\t");
        System.out.println();
    }
}

Leave a Reply