ICSE Computer Applications Year 2010

Section A (40 Marks)

Question 1

(a) Define the term Bytecode.
Bytecode is a machine instruction for a Java processor chip called Java Virtual Machine (JVM).
(b) What do you understand by type conversion? How is implicit conversion different from explicit conversion?
The process of converting one predefined type into another is called type conversion.
Implicit conversion takes place automatically.
Explicit conversion is done forcefully.
(c) Name two jump statements and their use.
The break statement is used to exit from a loop or a switch case.
The continue statement is used to move to the next iteration of the loop.
(d) What is Exception? Name two exception handling blocks.
An exception is an abnormal condition that arises in a code sequence at run-time.
Two exception handling blocks are try and catch.
(e) Write two advantages of using functions in a program.
Functions lets us reuse code.
Functions reduce code complexity.

Question 2

(a) State the purpose and return data type of the following String functions:
(i) indexOf()

Purpose: to find the index of the first occurrence of a given character or string.
Return data type: int
(ii) compareTo()
Purpose: to compare the contents of two strings.
Return data type: int
(b) What is the result stored in x, after evaluating the following expression?
int x = 5;
x = x++ * 2 + 3 * --x;

= 5 * 2 + 3 * 5
= 10 + 15
= 25.
(c) Differentiate between static and non-static data members.
The static data member is common for all the objects for a given class.
The non-static data members are created separately for each object for a given class.
(d) Write the difference between length and length().
The length is used to find the size of an array.
The length() is used to find the length of a string object.
(e) Differentiate between private and protected visibility modifiers.
The private data members can only be accessed from within the class.
The protected data members can be accessed from within the class as well as from the sub-classes.

Question 3

(a) What do you understand by the term data abstraction? Explain with an example.
The act of representing essential features without including background details is known as data abstraction.
Example:
class Rectangle{
    private double length;
    private double width;
    public double area(){
        return length * width;
    }
}

In the above code, the inner details of the class are hidden through encapsulation, which leads to data abstraction.
(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 executes 3 times.
OUTPUT: 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.
Constructors have the same name as their class name.
Constructors don’t have any return type (not even void).
(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.

wd.charAt(wd.length() - 2)
(ii) Check if the second character of a string str is in uppercase.
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 an object materialize the abstraction represented by the class. Every object will have its own state, even though they have same structure and behavior.
(ii) What is the use of the keyword import?
The import keyword is used to include packages and interfaces into the current file.

Section B (60 Marks)

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.io.*;
class BinarySearch{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a[] = {5, 7, 9, 11, 15, 20, 30, 45, 89, 97};
        System.out.print("Element to search: ");
        int key = Integer.parseInt(br.readLine());
        int low = 0;
        int high = a.length - 1;
        int mid = (low + high) / 2;
        while(low <= high){
            if(key == a[mid])
                break;
            else if(key < a[mid])
                high = mid - 1;
            else
                low = mid + 1;
            mid = (low + high) / 2;
        }
        if(low > high)
            System.out.println("Search element not found.");
        else
            System.out.println(key + " found at index " + mid);
    }
}

Question 5

Define a class Student as given below:
Data members/instance variables:
name: to store the student’s name.
age: to store the age.
m1, m2, m3: to store the marks in three subjects.
maximum: to store the highest marks among three subjects.
average: to store the average marks.
Member functions:
(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 all the details of the student.
Write a main() method to create an object of the class and call the above methods accordingly to enable the task.

import java.io.*;
class Student{
    private String name;
    private int age;
    private int m1;
    private int m2;
    private int m3;
    private int maximum;
    double average;
    public Student(String n, int a, int x, int y, int z){
        name = n;
        age = a;
        m1 = x;
        m2 = y;
        m3 = z;
        maximum = m1;
        average = 0.0;
    }
    public void input()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Name: ");
        name = br.readLine();
        System.out.print("Age: ");
        age = Integer.parseInt(br.readLine());
        System.out.print("Marks 1: ");
        m1 = Integer.parseInt(br.readLine());
        System.out.print("Marks 2: ");
        m2 = Integer.parseInt(br.readLine());
        System.out.print("Marks 3: ");
        m3 = Integer.parseInt(br.readLine());
    }
    public void compute(){
        average = (m1 + m2 + m3) / 3.0;
        maximum = Math.max(m1, m2);
        maximum = Math.max(maximum, m3);
    }
    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("Average: " + average);
        System.out.println("Maximum: " + maximum);
    }
    public static void main(String args[])throws IOException{
        Student obj = new Student("", 0, 0, 0, 0);
        obj.input();
        obj.compute();
        obj.display();
    }
}

Question 6

Shasha Travels Pvt. Ltd. gives the following discount to its customers:
Ticket Amount                        Discount
Above Rs. 70000                     18%
Rs. 55001 to Rs. 70000           16%
Rs. 35001 to Rs. 55000           12%
Rs. 25001 to Rs. 35000           10%
Less than Rs. 25001               2%
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:
SNo.    Name    Ticket charges    Discount    Net amount
____     ______    _______________    _________     ____________
Assume that there are 15 customers, first customer is given the serial number 1, next customer 2… and so on.

import java.io.*;
class Ticket{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String name[] = new String[15];
        double amt[] = new double[15];
        double dp[] = new double[15];
        double net[] = new double[15];
        for(int i = 0; i < 15; i++){
            System.out.print("Name: ");
            name[i] = br.readLine();
            System.out.print("Ticket amount: ");
            amt[i] = Double.parseDouble(br.readLine());
            double da = 0.0;
            if(amt[i] > 70000)
                dp[i] = 18.0;
            else if(amt[i] > 55000 && amt[i] <= 70000)
                dp[i] = 16.0;
            else if(amt[i] > 35000 && amt[i] <= 55000)
                dp[i] = 12.0;
            else if(amt[i] > 25000 && amt[i] <= 35000)
                dp[i] = 10.0;
            else
                dp[i] = 2.0;
            da = dp[i] / 100.0 * amt[i];
            net[i] = amt[i] - da;
        }
        System.out.println("SNo. Name Ticket Charges Discount Net Amount");
        for(int i = 0; i < 15; i++)
            System.out.println((i + 1) + "\t" + name[i] + "\t" + amt[i] + "\t" + dp[i] + "%\t" + net[i]);
    }
}

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 1 and itself and not by any other number.
Example: 3, 5, 7, 11, 13, 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.io.*;
class Menu{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("1. Prime Number");
        System.out.println("2. Automorphic Number");
        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());
            int f = 0;
            for(int i = 1; i <= n; i++){
                if(n % i == 0)
                    f++;
            }
            if(f == 2)
                System.out.println(n + " is prime.");
            else
                System.out.println(n + " is not prime.");
            break;
            case 2:
            System.out.print("N = ");
            n = Integer.parseInt(br.readLine());
            int square = n * n;
            String a = Integer.toString(square);
            String b = Integer.toString(n);
            if(a.endsWith(b))
                System.out.println(n + " is automorphic.");
            else
                System.out.println(n + " is not automorphic.");
            break;
            default:
            System.out.println("Invalid input!");
        }
    }
}

Question 8

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

import java.io.*;
class MyList{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int p[] = new int[6];
        int q[] = new int[4];
        int r[] = new int[10];
        System.out.println("Elements in 1st array:");
        for(int i = 0; i < p.length; i++)
            p[i] = Integer.parseInt(br.readLine());
        System.out.println("Elements in 2nd array:");
        for(int i = 0; i < q.length; i++)
            q[i] = Integer.parseInt(br.readLine());
        int index = 0;
        for(int i = 0; i < p.length; i++)
            r[index++] = p[i];
        for(int i = 0; i < q.length; i++)
            r[index++] = q[i];
        System.out.println("Resultant array:");
        for(int i = 0; i < r.length; i++)
            System.out.print(r[i] + "\t");
    }
}

Question 9

Write a program to input a string in uppercase and print the frequency of each character.
Example:
INPUT: COMPUTER HARDWARE
OUTPUT:
Characters    Frequency
A                     2
C                     1
D                     1
E                     2
H                     1
M                     1
O                     1
P                     1
R                     3
T                     1
U                     1
W                    1

import java.io.*;
class Frequency{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("String: ");
        String s = br.readLine().toUpperCase();
        for(char ch = 'A'; ch <= 'Z'; ch++){
            int count = 0;
            for(int i = 0; i < s.length(); i++){
                char x = s.charAt(i);
                if(x == ch)
                    count++;
            }
            if(count > 0)
                System.out.println(ch + "\t" + count);
        }
    }
}

Leave a Reply

%d bloggers like this: