ICSE Computer Applications Year 2011

Section A (40 Marks)

Question 1

(a) What is the difference between an object and a class?
Object is an identifiable entity with some characteristics and behavior.
Class is a group of objects that share common properties and relationships.
(b) What does the token ‘keyword’ refer to in the context of Java? Give an example for keyword.
Keywords are the words that convey a special meaning to the language compiler. Example: ‘new’ keyword.
(c) State the difference between entry controlled loop and exit controlled loop.
In entry-controlled loop, condition is checked before the loop body gets executed. Example: for loop, while loop.
In exit-controlled loop, at first the loop body gets executed, then the condition is checked. Example: do-while loop.
(d) What are the two ways of invoking functions?
The two ways of invoking functions are:
1. Call by value
2. Call by reference
(e) What is the difference between / and % operators?
The / operator is used to get the quotient after dividing two numbers, whereas the % operator gives the remainder after dividing two numbers.

Question 2

(a) State the total size in bytes, of the arrays a[4] of char data type and p[4] of float data type.
a[4] will occupy 8 bytes.
p[4] will occupy 16 bytes.
(b) (i)Name the package that contains Scanner class.
java.util package.
(ii) Which unit of the class gets called, when the object of the class is created?
Constructor
(c) Give the output of the following:
String n = "Computer Knowledge";
String m = "Computer Applications";
System.out.println(n.substring(0, 8).concat(m.substring(9)));
System.out.println(n.endsWith("e"));

ComputerApplications
true
(d) Write the output of the following:
(i)
System.out.println(Character.isUpperCase('R'));
true
(ii) System.out.println(Character.toUpperCase('j'));
J

Question 3

(a) Analyze the following program segment and determine how many times the loop will be executed and what will be the output of the program segment?
int p = 200;
while(true){
    if(p < 100)
        break;
    p = p - 20;
}
System.out.println(p);

The loop executes 6 times.
OUTPUT: 80
(b) What will be the output of the following code?
(i)
int k = 5, j = 9;
k += k++ - ++j + k;
System.out.println("k = " + k);
System.out.println("j = " + j);

k = 6
j = 10
(ii) double b = -15.6;
double a = Math.rint(Math.abs(b));
System.out.println("a = " + a);

a = 16.0
(c) Explain the concept of constructor overloading with an example.
Using constructor overloading, we can create multiple constructors for a class with different function signatures.
Example:
class Square{
    int side;
    public Square(){
        side = 0;
    }
    public Square(int s){
        side = s;
    }
}

(d) Give the prototype of a function search which receives a sentence ‘sent’ and word ‘w’ and returns 1 or 0.
int search(String sent, String w)
(e) Write an expression in Java for z = (5x3 + 2y) / (x + y).
z = (5 * Math.pow(x, 3) + 2 * y) / (x + y)
(f) Write a statement each to perform the following tasks on a string:
(i) Find and display the position of the last space in a string s.

System.out.println(s.lastIndexOf(' '));
(ii) Convert a number stored in a string variable x to double data type.
Double.parseDouble(x)
(g) Name the keyword that:
(i) informs that an error has occurred in an input/output operation.

throws
(ii) distinguishes between instance variable and class variable.
static
(h) What are library classes? Give an example.
Library classes are the predefined classes provided by Java in the form of packages. Example: Integer class.
(i) Write one difference between Linear Search and Binary Search.
Linear Search can work on both sorted and unsorted lists, whereas binary search works only on sorted lists.

Section B (60 Marks)

Question 4

Define a class Mobike with the following description:
Instance variables/data members:
int bno: to store the bike’s number.
int phno: to store the phone number of the customer.
String name: to store the name of the customer.
int days: to store the number of days the bike is taken on rent.
int charge: to calculate and store the rental charge.
Member functions/methods:
void input(): to input and store the detail of the customer.
void compute(): to compute the rental charge.
The rent for a Mobike is charged on the following basis:
First five days: Rs. 500 per day.
Next five days: Rs. 400 per day.
Rest of the days: Rs. 200 per day.
void display(): to display the details in the following format:
Bike No    Phone No    Name    No. of days    Charge
———–     ————–     ———    —————–    ———–

import java.io.*;
class Mobike{
    private int bno;
    private int phno;
    private String name;
    private int days;
    private int charge;
    public void input()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Bike number: ");
        bno = Integer.parseInt(br.readLine());
        System.out.print("Phone number: ");
        phno = Integer.parseInt(br.readLine());
        System.out.print("Customer name: ");
        name = br.readLine();
        System.out.print("Number of days: ");
        days = Integer.parseInt(br.readLine());
    }
    public void compute(){
        if(days <= 5)
            charge = days * 500;
        else if(days <= 10)
            charge = 2500 + (days - 5) * 400;
        else if(days > 10)
            charge = 4500 + (days - 10) * 300;
    }
    public void display(){
        System.out.println("Bike No. Phone No. Name No. of days  Charge");
        System.out.println(bno + "\t" + phno + "\t" + name + "\t" + charge);
    }
    public static void main(String args[])throws IOException{
        Mobike obj = new Mobike();
        obj.input();
        obj.compute();
        obj.display();
    }
}

Question 5

Write a program to input and sort the weight of ten people. Sort and display them in descending order using the selection sort technique.

import java.io.*;
class Sorting{
    public static void main(String args[])throws IOException{
        double weights[] = new double[10];
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter weights of 10 people:");
        for(int i = 0; i < weights.length; i++)
            weights[i] = Double.parseDouble(br.readLine());
        for(int i = 0; i < weights.length; i++){
            double large = weights[i];
            int pos = i;
            for(int j = i + 1; j < weights.length; j++){
                if(large < weights[j]){
                    large = weights[j];
                    pos = j;
                }
            }
            double temp = weights[i];
            weights[i] = large;
            weights[pos] = temp;
        }
        System.out.println("Weights in descending order:");
        for(int i = 0; i < weights.length; i++)
            System.out.println(weights[i]);
    }
}

Question 6

Write a program to input a number and print whether the number is a special number or not.
(A number is said to be a special number, if the sum of the factorial of the digits of the number is same as the original number).
Example: 145 is a special number, because 1! + 4! + 5! = 1 + 24 + 120 = 145.
(Where ! stands for factorial of the number and the factorial value of a number is the product of all integers from 1 to that number, example 5! = 1 * 2 * 3 * 4 * 5 = 120)

import java.io.*;
class Special{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("N = ");
        int n = Integer.parseInt(br.readLine());
        int sum = 0;
        for(int i = n; i != 0; i /= 10){
            int d = i % 10;
            sum += factorial(d);
        }
        if(n == sum)
            System.out.println(n + " is a special number.");
        else
            System.out.println(n + " is not a special number.");
    }
    public static int factorial(int num){
        int f = 1;
        for(int i = 1; i <= num; i++)
            f *= i;
        return f;
    }
}

Question 7

Write a program to accept a word and convert it into lowercase if it is in uppercase, and display the new word by replacing only the vowels with the character following it.
Example:
Sample Input: computer
Sample Output: cpmpvtfr

import java.io.*;
class Word{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Word = ");
        String w = br.readLine();
        w = w.trim();
        int index = w.indexOf(' ');
        if(index > 0)
            w = w.substring(0, index);
        w = w.toLowerCase();
        String t = new String();
        for(int i = 0; i < w.length(); i++){
            char ch = w.charAt(i);
            switch(ch){
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                t += (char)(ch + 1);
                break;
                default:
                t += ch;
            }
        }
        System.out.println(t);
    }
}

Question 8

Design a class to overload a function compare() as follows:
(a) void compare(int, int): to compare two integer values and print the greater of the two integers.
(b) void compare(char, char): to compare the numeric values of two characters and print the character with higher numeric value.
(c) void compare(String, String): to compare the length of the two strings and print the longer of the two.

class Overload{
    public static void compare(int a, int b){
        int large = (a > b)? a : b;
        System.out.println(large);
    }
    public static void compare(char a, char b){
        char large = (a > b)? a : b;
        System.out.println(large);
    }
    public static void compare(String a, String b){
        String large = (a.length() > b.length())? a : b;
        System.out.println(large);
    }
}

Question 9

Write a menu-driven program to perform the following (use switch case statement):
(a) To print the series 0, 3 7, 15, 24, … n terms (value of ‘n’ is to be an input by the user)
(b) To find the sum of the series given below:
S = 1 / 2 + 3 / 4 + 5 / 6 + 7 / 8 + … + 19 / 20.

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. 0, 3 7, 15, 24, ... n terms");
        System.out.println("2. S = 1 / 2 + 3 / 4 + 5 / 6 + ... + 19 / 20");
        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++)
                System.out.print((i * i - 1) + "\t");
            break;
            case 2:
            double sum = 0.0;
            for(double i = 1; i <= 19; i++)
                sum += i / (i + 1);
            System.out.print("Sum of the series: " + sum);
            break;
            default:
            System.out.println("Invalid choice!");
        }
    }
}

Leave a Reply

%d bloggers like this: