ICSE Computer Applications 2010

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
(a) Define the term bytecode.
Bytecode is the intermediate code which is platform-independent. It is generated when the Java source code is successfully compiled.

(b) What do you understand by type conversion?
How is implicit conversion different from explicit conversion?
Converting the data type of one variable into another for compatibility is known as type conversion. Implicit type conversion takes place automatically, whereas explicit type conversion needs to be done forcibly.

(c) Name two jump statements and their use.
The break keyword is used to exit from a loop or a switch case. The return keyword is used to exit from a method.

(d) What is Exception? Name two exception handling blocks.
Exception means runtime error. The two exception handling blocks are try and catch.

(e) Write two advantages of using functions in a program.
Functions helps in reusing the code.
Functions helps in reducing the code complexity.

Question 2
(a) State the purpose and return data type of the following String functions:
(i) indexOf()
It is used to find the index of the first occurrence of a given character in a string. The return type is int.
(ii) compareTo()
It is used to compare two strings character by character.
The return type is int.

(b) What is the result stored in x, after evaluating the following expression:
int x = 5;
x = x++ * 2 + 3 * –x;
x = 5 * 2 + 3 * 5
x = 10 + 15
x = 25

(c) Differentiate between static and non-static data members.
The static data member remains common (only one copy) for all the objects, whereas the non-static data members are separate (multiple copies) for multiple objects.

(d) Write the difference between length and length() functions.
The length is used to find the length of an array, whereas the length() is used to find the length of a string.

(e) Differentiate between private and protected visibility modifiers.
The private data members can be accessed from only within the class. The protected data members can be accessed from within the class as well as from its sub-classes.

Question 3
(a) What do you understand by the term data abstraction? Explain with an example.
Data abstraction is the process of hiding unnecessary details and showing only necessary information. Example: Math.sqrt() method in which we don’t have to bother how this function works internally.

(b) What will be the output of the following code?
(i) int m = 2;
int n = 15;
for(int i = 1; i < 5; i++);
m++; –n;
System.out.println(“m = ” + m);
System.out.println(“n = ” + n);
m = 3
n = 14
(ii) char x = ‘A’; int m;
m = (x == ‘a’)? ‘A’ : ‘a’;
System.out.println(“m = ” + m);
m = 97

(c) 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 k = 1, i = 2;
while(++i < 6)
k *= i;
System.out.println(k);
The loop will execute 3 times.
The output will be 60.

(d) Give the prototype of a function check which receives a character ch and an integer n and returns true or false.
boolean check(char ch, int n)

(e) State two features of a constructor.
A constructor has the same name as its class name.
A constructor doesn’t have any return type.

(f) Write a statement each to perform the following task on a string:
(i) Extract the second last character of a word stored in the variable wd.
char ch = wd.charAt(wd.length() – 2);
(ii) Check if the second character of a string str is in uppercase.
if(Character.isUpperCase(str.charAt(1)))

(g) What will the following function return when executed?
(i) Math.max(-17, -19)
-17
(ii) Math.ceil(7.8)
8.0

(h) (i) Why is an object called an instance of a class?
An object is called an instance of a class because each and every object created from a class are unique objects even though they are similar to one another.
(ii) What is the use of the keyword import?
The import keyword is used to include a package into our program.

SECTION B
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
Write a program to perform binary search on a list of integers given below, to search for an element input by the user. If it is found display the element along with its position, otherwise display the message “Search element not found”.
5, 7, 9, 11, 15, 20, 30, 45, 89, 97

import java.util.Scanner;
class Binary{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        int a[] = {5, 7, 9, 11, 15, 20, 30, 45, 89, 97};
        System.out.print("Element to be searched: ");
        int s = Integer.parseInt(in.nextLine());
        int low = 0;
        int high = a.length - 1;
        int mid = 0;
        while(low <= high){
            mid = (low + high) / 2;
            if(s == a[mid])
                break;
            else if(s < a[mid])
                high = mid - 1;
            else
                low = mid + 1;
        }
        if(low > high)
            System.out.println("Search element not found");
        else
            System.out.println("Found at position " + (mid + 1));
    }
}

Question 5
Define a class Student described as below:
Data members/instance variables:
name, age, m1, m2, m3 (marks in 3 subjects), maximum, average
Member methods:
(i) A parameterized constructor to initialize the data members.
(ii) To accept the details of a student.
(iii) To compute the average and the maximum out of three marks.
(iv) To display the name, age, marks in three subjects, maximum and average.
Write a main() method to create an object of a class and call the above member methods.

import java.util.Scanner;
class Student{
    String name;
    int age;
    int m1;
    int m2;
    int m3;
    int maximum;
    double average;
    public Student(String n, int a, int m1, int m2, int m3){
        name = n;
        age = a;
        this.m1 = m1;
        this.m2 = m2;
        this.m3 = m3;
        maximum = 0;
        average = 0.0;
    }
    public void accept(){
        Scanner in = new Scanner(System.in);
        System.out.print("Name: ");
        name = in.nextLine();
        System.out.print("Age: ");
        age = Integer.parseInt(in.nextLine());
        System.out.print("Marks 1: ");
        m1 = Integer.parseInt(in.nextLine());
        System.out.print("Marks 2: ");
        m2 = Integer.parseInt(in.nextLine());
        System.out.print("Marks 3: ");
        m3 = Integer.parseInt(in.nextLine());
    }
    public void compute(){
        maximum = Math.max(m1, m2);
        maximum = Math.max(maximum, m3);
        average = (m1 + m2 + m3) / 3.0;
    }
    public void display(){
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Marks 1: " + m1);
        System.out.println("Marks 2: " + m2);
        System.out.println("Marks 3: " + m3);
        System.out.println("Maximum: " + maximum);
        System.out.println("Average: " + average);
    }
    public static void main(String args[]){
        Student obj = new Student("", 0, 0, 0, 0);
        obj.accept();
        obj.compute();
        obj.display();
    }
}

Question 6
Shasha Travels Pvt. Ltd. gives the following discount to its customers:

Ticket amountDiscount
Above Rs. 7000018%
Rs. 55001 to Rs. 7000016%
Rs. 35001 to Rs. 5500012%
Rs. 25001 to Rs. 3500010%
Less than Rs. 250012%

Write a program to input the name and ticket amount for the customer and calculate the discount amount and net amount to be paid. Display the output in the following format for each customer:

Sl.No.NameTicket ChargesDiscountNet amount
1
import java.util.Scanner;
class Travel{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.print("Name: ");
        String name = in.nextLine();
        System.out.print("Ticket amount: ");
        double amt = Double.parseDouble(in.nextLine());
        double dp = 0.0;
        if(amt > 70000)
            dp = 18.0;
        else if(amt > 50000)
            dp = 16.0;
        else if(amt > 35000)
            dp = 12.0;
        else if(amt > 25000)
            dp = 10.0;
        else
            dp = 2.0;
        double d = dp / 100 * amt;
        double net = amt - d;
        System.out.println("Sl.No. Name Ticket Charges Discount Net amount");
        System.out.println("1. " + name + " " + amt + " " + d + " " + net);
    }
}

Question 7
Write a menu-driven program to accept a number and check and display whether it is a prime number or not OR an automorphic number or not (use switch-case statement).
(a) Prime number: A number is said to be a prime number if it is divisible only by 1and itself and not by any other number.
Example: 3, 5, 7, 11, etc.
(b) Automorphic number: An automorphic number is the number which is contained in the last digit(s) of its square.
Example: 25 is an automorphic number as its square is 625 and 25 is present as the last two digits.

import java.util.Scanner;
class Menu{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.println("1. Prime number");
        System.out.println("2. Automorphic number");
        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());
                int f = 0;
                for(int i = 2; i <= n / 2; i++){
                    if(n % i == 0){
                        f = 1;
                        break;
                    }
                }
                if(f == 0 && n > 1)
                    System.out.println("Prime number!");
                else
                    System.out.println("Not a prime number.");
                break;
            case 2:
                System.out.print("N = ");
                n = Integer.parseInt(in.nextLine());
                long s = n * n;
                int digits = 0;
                for(int i = n; i != 0; i /= 10)
                    digits++;
                int divisor = (int)Math.pow(10, digits);
                if(s % divisor == n)
                    System.out.println("Automorphic number!");
                else
                    System.out.println("Not an Automorphic number.");
                break;
            default:
                System.out.println("Invalid choice!");
        }
    }
}

Question 8
Write a program to store 6 elements in an array P, and 4 elements in an array Q and produce a third array R, containing all elements of array P and Q. Display the resultant array.
Example:
INPUT:
P[] = {4, 6, 1, 2, 3, 10}
Q[] = {19, 23, 7, 8}
OUTPUT:
R[] = {4, 6, 1, 2, 3, 10, 19, 23, 7, 8}

import java.util.Scanner;
class MyArray{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        int p[] = new int[6];
        int q[] = new int[4];
        int r[] = new int[10];
        System.out.println("Enter 6 elements:");
        for(int i = 0; i < 6; i++)
            p[i] = Integer.parseInt(in.nextLine());
        System.out.println("Enter 4 elements:");
        for(int i = 0; i < 4; i++)
            q[i] = Integer.parseInt(in.nextLine());
        int i = 0;
        int k = 0;
        for(i = 0; i < 6; i++)
            r[k++] = p[i];
        for(i = 0; i < 4; i++)
            r[k++] = q[i];
        System.out.println("Elements in the third array:");
        for(i = 0; i < 10; i++)
            System.out.println(r[i]);
    }
}

Question 9
Write a program to input a string in uppercase and print the frequency of each character.
Example:
INPUT: COMPUTER HARDWARE
OUTPUT:

CHARACTERSFREQUENCY
A2
C1
D1
E2
H1
M1
O1
P1
R3
T1
U1
W1
import java.util.Scanner;
class Frequency{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the string: ");
        String s = in.nextLine().toUpperCase();
        System.out.println("CHARACTERS\tFREQUENCY");
        for(char i = 'A'; i <= 'Z'; i++){
            int f = 0;
            for(int j = 0; j < s.length(); j++){
                if(i == s.charAt(j))
                    f++;
            }
            if(f > 0)
                System.out.println(i + "\t\t" + f);
        }
  }
}