ICSE Computer Applications Year 2007

Question 1

a) Name two types of Java programs.
Java Applications and Java Applets.
b) Define instance variable. Give an example of the same.
The variables defined within the class without the static keyword are instance variables. They represent the state of different objects.
Example:
class Square{
double area;
double side;
}
c) Differentiate between binary search and linear search.
Binary search only works on sorted lists, whereas linear search can work on both sorted as well as unsorted lists.
d) Assign the value of pie (i.e. 3.142) to a variable with requisite data type.
double pie = 3.142;
e) Explain with an example the if-else-if construct.
The if-else-if is a conditional construct in which one out of several possible conditions execute.
Example:
if(marks >= 80)
grade = ‘A’;
else if(marks >= 50)
grade = ‘B’
else
grade = ‘C’;

Question 2

a) Differentiate between formal parameter and actual parameter.
The parameters used in method prototype are formal parameters whereas the parameters used in method call are actual parameters.
b) Why do we need a constructor as a class member?
A constructor is used to initialize the data members of an object with legal initial values. It helps in object creation.
c) Explain the term typecasting.
The process of converting the data type of a value from one type to another is known as typecasting.
d) Name the following:
i) A package that is invoked by default.
java.lang
ii) A keyword to use the classes defined in a package.
import
e) Name the class that is used for different mathematical functions. Give an example of a mathematical function.
Math class is used for different mathematical functions. Example: Math.sqrt().

Question 3

a) State the difference between = and ==.
The = is an assignment operator whereas == is a relational operator.
b) Write an equivalent Java syntax for the following expression:
mathematical expression
a = (0.05 – 2 * Math.pow(y, 3)) / (x – y);
c) Rewrite the following using ternary operator:
if(income <= 10000)
tax = 0;
else
tax = 12;
tax = (income <= 10000)? 0:12;
d) Write a statement for each of the following:
i) Store a number 275 as a String.
String num = “275”;
ii) Convert a string to a numeric value.
int n = Integer.parseInt(num);
iii) Add it to the existing total of 1000 to update the total.
total = 1000;
total += n;
e) i) What is the role of the keyword void in declaring functions?
The keyword ‘void’ indicates that the method doesn’t return any value.
ii) If a function contains several return statements, how many of them will be executed?
Only one of those return statements is executed.
iii) Which OOP principle implements function overriding?
Inheritance.
f) What is the output of the following?
i) System.out.println(“four: ” + 4 + 2);
System.out.println(“four: ” + (2 + 2));
four: 42
four: 4
ii) String s1 = “Hi”;
String s2 = “Hi”;
String s3 = ” there”;
String s4 = “HI”;
System.out.println(s1 + ” equals ” + s2 + “->” + s1.equals(s2));
System.out.println(s1 + ” equals ” + s3 + “->” + s1.equals(s3));
System.out.println(s1 + ” equals ” + s4 + “->” + s1.equals(s4));
System.out.println(s1 + ” equalsIgnoreCase ” + s4 + “->” + s1.equalsIgnoreCase(s4));
Hi equals Hi -> true
Hi equals there -> false
Hi equals -> false
Hi equalsIgnoreCase HI -> true
g) Evaluate the following expressions, if the values of the variables are a = 2, b = 3 and c = 9.
i) a – (b++) * (–c)
2 – 3 * 10 = 2 – 30 = -28.
ii) a * (++b) % c
2 * 4 % 9 = 8 % 9 = 8.

Question 4

Define a class Salary described as below:
Data members:
name, address, phone, subject specialization, monthly salary, income tax.
Methods:
i) to accept the details of a teacher including the monthly salary.
ii) to display the details of the teacher.
iii) to compute the annual income tax as 5% of the annual salary above Rs. 1,75,000.
Write a main() method to create an object of the class and call the above methods accordingly.

import java.io.*;
class Salary{
    String name;
    String address;
    int phone;
    String subject;
    double salary;
    double tax;
    public void input()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Name: ");
        name = br.readLine();
        System.out.print("Address: ");
        address = br.readLine();
        System.out.print("Phone Nnumber: ");
        phone = Integer.parseInt(br.readLine());
        System.out.print("Subject: ");
        subject = br.readLine();
        System.out.print("Monthly salary: ");
        salary = Double.parseDouble(br.readLine());
    }
    public void compute(){
        tax = 5.0 / 100 * (salary * 12 - 175000.0);
    }
    public void display(){
        System.out.println("Name: " + name);
        System.out.println("Address: " + address);
        System.out.println("Phone: " + phone);
        System.out.println("Subject: " + subject);
        System.out.println("Monthly Salary : Rs. " + salary);
        System.out.println("Annual income tax: Rs. " + tax);
    }
    public static void main(String args[])throws IOException{
        Salary obj = new Salary();
        obj.input();
        obj.compute();
        obj.display();
    }
}

Question 5

Write a program to compute and display the sum of the following series:
mathematical series

import java.io.*;
class Series{
    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());
        double sum = 0.0;
        for(int i = 2; i <= n + 1; i++){
            int p = 1;
            double s = 0;
            for(int j = 1; j <= i; j++){
                p *= i;
                s += i;
            }
            sum += s / p;
        }
        System.out.println("Sum = " + sum);
    }
}

Question 6

Write a program to initialize the given data in an array and find the minimum and maximum values along with the sum of the given elements.
Numbers: 2, 5, 4, 1, 3
Output:
Minimum value: 1
Maximum value: 5
Sum of the elements: 15

class List{
    public static void main(String args[]){
        int a[] = {2, 5, 4, 1, 3};
        int min = a[0];
        int max = a[0];
        int sum = a[0];
        for(int i = 1; i < a.length; i++){
            if(max < a[i]) max = a[i]; if(min > a[i])
                min = a[i];
            sum += a[i];
        }
        System.out.println("Minimum element: " + min);
        System.out.println("Maximum element: " + max);
        System.out.println("Sum of the elements: " + sum);
    }
}

Question 7

Write a program to enter a sentence from the keyboard and count the number of times a particular word occurs in it. Display the frequency of the search word.
Example:
INPUT:
Enter the sentence: the quick brown fox jumps over the lazy dog.
Enter a word to be searched: the
OUTPUT:
Searched word occurs 2 times.

import java.io.*;
class Frequency{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the sentence: ");
        String s = br.readLine();
        System.out.print("Enter the word to be searched: ");
        String key = br.readLine();
        String word = "";
        int count = 0;
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(Character.isLetterOrDigit(ch))
                word += ch;
            else{
                if(word.equalsIgnoreCase(key))
                    count++;
                word = "";
            }
        }
        System.out.println("Searched word occurs " + count + " times.");
    }
}

Question 8

Using a switch statement, write a menu-driven program to convert a given temperature from Fahrenheit to Celsius and vice-versa. For an incorrect choice, an appropriate error message should be displayed.
fahrenheit to celsius and vice-versa

import java.io.*;
class Conversion{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("1. Fahrenheit to Celsius");
        System.out.println("2. Celsius to Fahrenheit");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(br.readLine());
        switch(choice){
            case 1:
            System.out.print("Temperature in Fahrenheit: ");
            double f = Double.parseDouble(br.readLine());
            double c = 5.0 / 9 * (f - 32);
            System.out.println("Temperature in Celsius: " + c);
            break;
            case 2:
            System.out.print("Temperature in Celsius: ");
            c = Double.parseDouble(br.readLine());
            f = 1.8 * c + 32;
            System.out.println("Temperature in Fahrenheit: " + f);
            break;
            default:
            System.out.println("Invalid choice!");
        }
    }
}

Question 9

Write a program using a method palin(), to check whether a string is a palindrome or not. A palindrome is a string that reads the same from left to right and vice-versa.
Example: MADAM, ARORA, ABBA

import java.io.*;
class Palindrome{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("String: ");
        String s = br.readLine();
        if(palin(s))
            System.out.println(s + " is palindrome.");
        else
            System.out.println(s + " is NOT palindrome.");
    }
    public static boolean palin(String w){
        String rev = "";
        for(int i = w.length() - 1; i >= 0; i--)
            rev += w.charAt(i);
        return w.equalsIgnoreCase(rev);
    }
}