ICSE Computer Applications 2011

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
Attempt all questions.

Question 1
(a) What is the difference between an object and a class?
An object is an instance of a class. A class is a data type for an object.

(b) What does the token ‘keyword’ refer to in the context of Java? Give an example for keyword.
Keywords are the reserved words in Java that can’t be used as identifiers. Example: void.

(c) State the difference between entry-controlled loop and exit-controlled loop.
An entry-controlled loop checks the condition first before executing the loop body. An exit-controlled loop checks the condition after executing the loop body.

(d) What are the two ways of invoking functions?
Call by value.
Call by reference.

(e) What is the difference between / and % operator.
The / operator is used to find the quotient. The % operator is used to find the remainder.

Question 2
(a) State the total size in bytes for the arrays a[4] of char data type and p[4] of float data type.
a[4] of char data type will occupy 8 bytes.
p[4] of float data type 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

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

(e) What is the role of keyword void in declaring functions?
The keyword ‘void’ indicates that the function doesn’t return any value.

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 will execute 7 times.
The output will be 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 = 8
j = 8
(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.
The process of creating multiple constructors for a given class that differ in function signature is known as constructor overloading.
Example:

class Test{
    int num;
    public Test(){
        num = 0;
    }
    public Test(int n){
        num = n;
    }
}

(d) Give the prototype of a function search which receives a sentence sentnc and a word wrd and returns 1 or 0?
public static int search(String sentnc, String wrd)

(e) Write an expression in Java for:
Mathematical Expression ICSE Computer Applications 2011
z = (5 * Math.pow(x, 3) + 2 * y) / (x + y);

(f) Write a statement each to perform the following task 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 d = Double.parseDouble(x);

(g) Name the keyword that:
(i) Informs that an error has occurred in an input/output operation.
throw IOException;
(ii) distinguishes between instance variable and class variable.
static

(h) What are library classes? Give an example.
Library classes are the in-built classes in Java. Example: Math class.

(i) Write one difference between linear search and binary search.
Linear search can be applied on both sorted as well as unsorted lists. Binary search can be applied only in sorted lists.

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 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 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 – ₹ 500 per day
Next five days – ₹ 400 per day
Rest of the days – ₹ 200 per day
void display() – to display the details in the following format:
Bike No. Phone No. Name No. of days Charge
—            —                —        —                  —

import java.util.Scanner;
class Mobike{
    int bno;
    int phno;
    String name;
    int days;
    int charge;
    public void input(){
        Scanner in = new Scanner(System.in);
        System.out.print("Bike number: ");
        bno = Integer.parseInt(in.nextLine());
        System.out.print("Phone number: ");
        phno = Integer.parseInt(in.nextLine());
        System.out.print("Name: ");
        name = in.nextLine();
        System.out.print("Number of days: ");
        days = Integer.parseInt(in.nextLine());
    }
    public void compute(){
        if(days <= 5)
            charge = 500 * days;
        else if(days <= 10)
            charge = 2500 + (days - 5) * 400;
        else
            charge = 4500 + (days - 10) * 200;
    }
    public void display(){
        System.out.println("Bike No. Phone No. Name No. of days Charge");
        System.out.println(bno + "\t" + phno + "\t" + name + "\t" + days + "\t" + charge);
    }
    public static void main(String args[]){
        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.util.Scanner;
class Weight{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        double w[] = new double[10];
        System.out.println("Weight of 10 people:");
        for(int i = 0; i < w.length; i++)
            w[i] = Double.parseDouble(in.nextLine());
        for(int i = 0; i < w.length; i++){
            double large = w[i];
            int pos = i;
            for(int j = i + 1; j < w.length; j++){
                if(large < w[j]){
                    large = w[j];
                    pos = j;
                }
            }
            double temp = w[i];
            w[i] = large;
            w[pos] = temp;
        }
        System.out.println("Weight in descending order:");
        for(int i = 0; i < w.length; i++)
            System.out.println(w[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.
Here ! 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.util.Scanner;
class Special{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n = Integer.parseInt(in.nextLine());
        int sum = 0;
        for(int i = n; i != 0; i /= 10)
            sum += factorial(i % 10);
        if(n == sum)
            System.out.println("Special number!");
        else
            System.out.println("Not a special number.");
    }
    public static int factorial(int n){
        int f = 1;
        for(int i = 1; i <= n; 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.util.Scanner;
class Replace{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the word: ");
        String w = in.next().toLowerCase();
        String c = "";
        for(int i = 0; i < w.length(); i++){
            char ch = w.charAt(i);
            if("aeiou".indexOf(ch) >= 0)
                ch++;
            c += ch;
        }
        System.out.println(c);
    }
}

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){
        if(a > b)
            System.out.println(a);
        else
            System.out.println(b);
    }
    public static void compare(char a, char b){
        if(a > b)
            System.out.println(a);
        else
            System.out.println(b);
    }
    public static void compare(String a, String b){
        if(a.length() > b.length())
            System.out.println(a);
        else
            System.out.println(b);
    }
}

Question 9
Write a menu-driven program to perform the following (use switch-case statement):
(a) To print the series 0, 3, 8, 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.util.Scanner;
class Menu{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.println("1. Series 1");
        System.out.println("2. Series 2");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(in.nextLine());
        switch(choice){
        case 1:
            System.out.print("n = ");
            int n = Integer.parseInt(in.nextLine());
            for(int i = 1; i <= n; i++)
                System.out.print((i * i - 1) + " ");
            System.out.println();
            break;
        case 2:
            double sum = 0.0;
            for(int i = 1; i <= 19; i++)
                sum += i / (i + 1.0);
            System.out.println("Sum = " + sum);
            break;
        default:
            System.out.println("Invalid choice!");
        }
    }
}