# ICSE Computer Applications Year 2012

## Section A [40 Marks]

### Question 1

(a) Give one example each of a primitive data type and a composite data type.
Primitive data type: int
Composite data type: array
(b) Give one point of difference between unary and binary operators.
Unary operators act on one operand.
Binary operators act on two operands.
(c) Differentiate between call by value or pass by value and call by reference or pass by reference.
In call by value, a copy of the actual parameters are passed as arguments.
In call by reference, original actual parameters are passed as arguments.
(d) Write a Java expression for √(2as + u2).
Math.sqrt(2 * a * s + u * u)
(e) Name the type of error (syntax, runtime or logical error) in each case given below:
(i) Division by a variable that contains a value of zero.
Runtime error.
(ii) Multiplication operator used when the operation should be division.
Logical error.
(iii) Missing semicolon.
Syntax error.

### Question 2

(a) Create a class with one integer instance variable. Initialize the variable using:
(i) default constructor
(ii) parameterized constructor

`class Number{    int num;    public Number(){        num = 0;    }    public Number(int n){        num = n;    }}`
(b) Complete the code below to create an object of Scanner class:
`Scanner sc = ____ Scanner(__________);Scanner sc = new Scanner(System.in);`
(c) What is an array? Write a statement to declare an integer array of 10 elements.
An array is one of the composite data types that stores a collection of elements of the same type in contiguous memory locations.
`int a[] = new int;`
(d) Name the search or sort algorithm that:
(i) Makes several passes through the array, selecting the next smallest item in the array each time and placing it where it belongs in the array.

Exchange Selection Sort Algorithm.
(ii) At each stage, compares the sought key value with the key value of the middle element of the array.
Binary Search Algorithm.
(e) Differentiate between public and private modifiers for members of a class.
The public modifier makes the members accessible from anywhere.
The private modifier makes the members accessible only from within the class.

### Question 3

(a) What are the values of x and y when the following statements are executed?
`int a = 63, b = 36;boolean x = (a > b)? true : false;int y = (a < b)? a : b;`

x = true
y = 36
(b) State the values of n and ch.
`char c = 'A';int n = c + 1;char ch = (char)n;`

n = 66
ch = ‘B’
(c) What will be the result stored in x after evaluating the following expression?
`int x = 4;x += (x++) + (++x) + x;`

x = x + x++ + ++x + x;
x = 4 + 4 + 6 + 6;
x = 20
(d) Give output of the following program segment:
`double x = 2.9, y = 2.5;System.out.println(Math.min(Math.floor(x), y));System.out.println(Math.max(Math.ceil(x), y));`

2.0
3.0
(e) State the output of the following program segment:
`String s = "Examination";int n = s.length();System.out.println(s.startsWith(s.substring(5, n)));System.out.println(s.charAt(2) == s.charAt(6));`

nation
true
(f) State the method that:
(i) Converts a string to a primitive float data type.

`Float.parseFloat()`
(ii) Determines if the specified character is an uppercase character.
`Character.isUpperCase()`
(g) State the data type and values of a and b after the following segment is executed:
`String s1 = "Computer", s2 = "Applications"a = (s1.compareTo(s2));b = (s1.equals(s2));`

Data type of a is int, and value is 2.
Data type of b is boolean, and value is false.
(h) What will the following code output:
`String s = "malayalam";System.out.println(s.indexOf('m'));System.out.println(s.lastIndexOf('m'));`

0
8
(i) Rewrite the following program segment using while instead of for statement.
`int f = 1, i;for(i = 1; i <= 5; i++){    f *= i;    System.out.println(f);}`

`int f = 1, i = 1;while(i <= 5){    f *= i;    System.out.println(f);    i++;}`
(j) In the program given below, state the name and the value of the:
(i) method argument or argument variable
(ii) class variable
(iii) local variable
`class MyClass{    static int x = 7;    int y = 2;    public static void main(String args[]){        MyClass obj = new MyClass();        System.out.println(x);        obj.sampleMethod(5);        int a = 6;        System.out.println(a);    }    void sampleMethod(int n){        System.out.println(n);        System.out.println(y);    }}`
Method argument is n, and its value is 5.
Class variable is x, and its value is 7.
Local variable is a, and its value is 6.

## Section B [50 Marks]

### Question 4

Define a class called Library with the following description:
Instance variables/data members:
int accNum: stores the accession number of the book.
String title: stores the title of the book.
String auhor: stores the name of the author.
Member functions:
(i) void input(): to accept and store the accession number, title and author.
(ii) void compute(): to accept the number of days late, calculate and display the fine charged at the rate of Rs. 2 per day.
(iii) void display(): to display the details in the following format:
Accession Number    Title    Author
Write a main() method to create an object of the class and call the above member functions.

``````import java.io.*;
class Library{
private int accNum;
String title;
String author;
public void input()throws IOException{
System.out.print("Accession number: ");
System.out.print("Title: ");
System.out.print("Author: ");
}
public void compute()throws IOException{
System.out.print("Number of days late: ");
int days = Integer.parseInt(br.readLine());
double fine = days * 2.0;
System.out.println("Fine charged: Rs. " + fine);
}
public void display(){
System.out.println("Accession Number\tTitle\tAuthor");
System.out.println(accNum + "\t\t" + title + "\t" + author);
}
public static void main(String args[])throws IOException{
Library obj = new Library();
obj.input();
obj.compute();
obj.display();
}
}``````

### Question 5

Given below is a hypothetical table showing rates of income tax for male citizens below the age of 65 years:

 Taxable Income (TI) in Rs Income Tax in Rs Does not exceed Rs. 1,60,000 NIL Is greater than Rs. 1,60,000 and less than or equal to Rs. 5,00,000. (TI – 1,60,000) x 10% Is greater than Rs. 5,00,000 and less than or equal to Rs. 8,00,000 [(TI – 5,00,000) x 20%] + 34,000 Is greater than Rs. 8,00,000 [(TI – 8,00,000) x 30%] + 94,000

Write a program to input the age, gender (male or female) and Taxable Income of a person.
If the age is more than 65 years or the gender is female, display “wrong category”. If the age is less than or equal to 65 years and the gender is male, compute and display the income tax payable as per the table given above.

``````import java.io.*;
class Tax{
public static void main(String args[])throws IOException{
System.out.print("Age: ");
int age = Integer.parseInt(br.readLine());
System.out.print("Gender: ");
char gender = br.readLine().charAt(0);
System.out.print("Taxable income: ");
double ti = Double.parseDouble(br.readLine());
if(age > 65 || (gender != 'M' && gender != 'm')){
System.out.println("Wrong category");
return;
}
double it = 0.0;
if(ti > 160000.0 && ti <= 500000.0)
it = (ti - 160000.0) * 10.0 / 100;
else if(ti > 500000.0 && ti <= 800000.0)
it = (ti - 500000.0) * 20.0 / 100 + 34000.0;
else if(ti > 800000.0)
it = (ti - 800000.0) * 30.0 / 100 + 94000.0;
System.out.println("Income tax payable: Rs. " + it);
}
}``````

### Question 6

Write a program to accept a string. Convert the string to uppercase. Count and output the number of double letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
Sample Output: 4

``````import java.io.*;
class Sequence{
public static void main(String args[])throws IOException{
System.out.print("String: ");
String s = br.readLine().toUpperCase();
int len = s.length();
int count = 0;
if(len > 1){
for(int i = 0; i < len - 1; i++){
char ch1 = s.charAt(i);
char ch2 = s.charAt(i + 1);
if(Character.isLetter(ch1) && Character.isLetter(ch2))
if(ch1 == ch2)
count++;
}
}
System.out.println(count);
}
}``````

### Question 7

Design a class to overload a function polygon() as follows:
(i) void polygon(int n, char ch): with one integer argument and one character argument that draws a filled square of side n using the character stored in ch.
(ii) void polygon(int x, int y): with two integer arguments that draws a filled rectangle of length x and breadth y, using the symbol ‘@’.
(iii) void polygon(): with no arguments that draws a filled triangle shown below.
Example:
(i) Input value of n = 2, ch = ‘O’
Output:
OO
OO
(ii) Input value of x = 2, y = 5
Output:
@@@@@
@@@@@
(iii) Output:
*
**
***

``````class Overload{
public static void polygon(int n, char ch){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
System.out.print(ch);
}
System.out.println();
}
}
public static void polygon(int x, int y){
for(int i = 1; i <= y; i++){
for(int j = 1; j <= x; j++){
System.out.print("@");
}
System.out.println();
}
}
public static void polygon(){
System.out.println("*");
System.out.println("**");
System.out.println("***");
}
}``````

### Question 8

Using the switch statement, write a menu-driven program to:
(i) Generate and display the first 10 terms of the Fibonacci series 0, 1, 1, 2, 3, 5, …
The first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two.
(ii) Find the sum of the digits of an integer that is input.
Sample Input: 15390
Sample Output: Sum of the digits = 18.
For an incorrect choice, an appropriate error message should be displayed.

``````import java.io.*;
public static void main(String args[])throws IOException{
System.out.println("1. First 10 Fibonacci terms");
System.out.println("2. Sum of the digits");
System.out.print("Enter your choice: ");
int choice = Integer.parseInt(br.readLine());
switch(choice){
case 1:
int a = 0;
int b = 1;
int c = 1;
System.out.print(a + "\t" + b);
for(int i = 3; i <= 10; i++){
System.out.print("\t" + c);
a = b;
b = c;
c = a + b;
}
break;
case 2:
System.out.print("N = ");
int n = Integer.parseInt(br.readLine());
int sum = 0;
while(n != 0){
sum += n % 10;
n /= 10;
}
System.out.println("Sum of the digits: " + sum);
break;
default:
System.out.println("Invalid choice!");
}
}
}``````

### Question 9

Write a program to accept the names of 10 cities in a single dimension array and their STD (Subscriber Trunk Dialing) codes in another single dimension integer array. Search for a name of a city input by the user in the list. If found, display “Search successful” and print the name of the city along with its STD code, or else display the message “Search unsuccessful, no such city in the list”.

``````import java.io.*;
class STD{
public static void main(String args[])throws IOException{
String names[] = new String;
int codes[] = new int;
int i = 0;
for(; i < names.length; i++){
System.out.print("City: ");
System.out.print("STD code: ");
}
System.out.print("City to search: ");
String key = br.readLine();
for(i = 0; i < names.length; i++){
if(key.equalsIgnoreCase(names[i]))
break;
}
if(i < names.length){
System.out.println("Search successful");
System.out.println(names[i] + " - " + codes[i]);
}
else
System.out.println("Search unsuccessful, no such city in the list");
}
}``````