# ICSE 2005 COMPUTER APPLICATION

Answer to this ICSE 2005 Computer Application ICSE Board Papers 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 is the time allowed for writing the answer.

This paper is divided into two Sections. Answer all Questions from Section A and any four questions from Section B. The intended marks for questions or parts of questions are given in [].

SECTION A(40 Marks)
Attempt all questions.

Question 1.                                                    
(a) Name any two OOP’S principles.
Ans: Encapsulation: It is the way of combining both data and the function that operates on the data under a single unit. Inheritance: It is the capability of one class of thing to inherit properties from another class.
(b) Mention two different styles of expressing a comment in a program.
Ans: The two ways of inserting a comments in a program are:
(i) using //
(ii) using /*     */
(c) Which element is num of the array num?
Ans: 10th element. Because the first index number/subscript value of an array is 0. So 9th element is treated as the 10th element in an array.

(d) Differentiate between operator and expression.
Ans: The operations are represented by operators and the object of the operations are referred to as operands. The expression is any valid combination of operators, constant and variables.
(e) If m=5 and n=2 output the values of m and n after execution in (i) and (ii).
(i)   m -= n;
(ii)  n = m + m/n;
Ans: (i) the value of m is 3 and n is 2   (ii) The value of m is 5 and n is 7

Question 2.                                                   
(a) Explain the term for loop with an example.
Ans: In Java the ‘for’ statement is the most common iterative statement. the general syntax of the for loop is,
for(initialization; test-expression; increment)
{
body of the loop
}
three statement separated by semi colons are placed with in the parenthesis. for example:
for(int i=1;i<=10;i++)
{
System.out.println(i);
}
(b) What is a compound Statement? Give an Example.
Ans: It is a block of code containing more then one executable statement. In Java the {} is called block and the statements written under {} is called compound statements or block statement. The { } opening and closing braces indicates the start and end of a compound statement.
for(int i=1;i<=5;i++)
{
System.out.println(“Hello”);
System.out.println(“How”);
System.out.println(“are you?”);
}
(c) State the difference between Constructor and Method.
Ans: The function has a return type like int. but the constructor has no return type. The function must be called in programs where as constructor automatically called when the object of that class is created.
(d) State one similarity and one difference between while and do-while loop.
Ans: Similarity: In both loops there is a chances to forget the increment statement inside the loop. Difference: In while loop the test expression is evaluated at the beginning where as in do-while loop test expression is evaluated at the bottom, after the body of the loop.

(e) Explain, with the help of an example, the purpose of default in a switch statement.
Ans: The default section is an optional part of the switch statement and the statement written under default clause are executed when no matching case is found.
switch(n)
{
case 1: System.out.println(“Sunday”); break;
case 2: System.out.println(“Monday”); break;
case 3: System.out.println(“Tuesday”); break;
case 4: System.out.println(“Wednesday”); break;
case 5: System.out.println(“Thursday”); break;
case 6: System.out.println(“Friday”); break;
case 7: System.out.println(“Saturday”); break;
default : System.out.println(“Invalid Input”);
}

Question 3.
(a) What will be the output of the following. if x=5 initially? 
(i)  5 * ++x
(ii) 5 * x++
Ans: (i) 30   (ii) 25
(b) What is the output of the following?                        
char c = ‘A’;
short m = 26;
int n = c + m;
System.out.println(n);
Ans: output is 91, because adding ‘char’ with integer data type ‘short’, the least data type ‘char’ converted to integer ‘short’. i.e. ‘A’ becomes 65, so 65+26 is 91.

(c) Explain the meaning of break and continue statement?        
Ans: Both statements are used as a jumped statement. But there is a difference between Break and Continue statement. The break statement terminate the loop, but the continue statement skip the rest of the loop statement and continued the next iteration of the loop.
e.g. of Break Statement
int i=0;
while(i<=10)
{
i++;
if(i==5)
break;
System.out.println(i);
}

e.g. of Continue Statement
int i=0;
while(i<=10)
{
i++;
if(i==5)
continue;
System.out.println(i);
}
(d) (i)  What is call by value?                                 
(ii) How are the following passed?                          
(1) Primitive types.
(2) Reference types.
Ans: (i) In call by value, the called functions creates its own work copy for the passed parameters and copies the passed values in it. Any changes that take place remain in the work copy and the original data remains intact. (ii) (1) Primitive types are passed, call by value method. (2) Reference data types are passed, call by reference method.
(e) Enter any two variables through constructor parameters and write a program to swap and print the values.                         
class swap
{
int a,b;
public swap(int x,int y)
{
a=x;
b=y;
}
public void main(String args[])
{
int t=a;
a=b;
b=t;
System.out.out.println(“the value of a and b after swaping : “+a+” “+b);
}
}
(f) What do the following functions return for:                 
String x = “hello”;
String y = “world”;
(i)   System.out.println((x + y);
(ii)  System.out.println((x.length());
(iii) System.out.println((x.charAt(3));
(iv)  System.out.println((x.equals(y));
Ans:       (i)    helloworld         (ii)  5              (iii)   l             (iv)   false

(g) Differentiate between toLowerCase() and toUpperCase() methods.
Ans: The given two string method’s change the case of the current string. The toLowerCase() method change the current string object to its equivalent Lower Case, where as toUpperCase() method change the current string object to its equivalent Upper Case.

SECTION B (60 Marks)
Attempt any four questions from this Section. The answers in this Section should consist of the Program in BlueJ environment with Java. Each program should be written in using Variable descriptions/Mnemonic Codes such that the logic of the program is clearly depicted.
Flow charts and Algorithms are not required.

Question 4.
Write a class with name employee and basic as its data member, to find the gross pay of an employee for the following allowances and deduction. Use meaningful variable.
Dearness Allowance = 25% of Basic Pay.
House Rent Allowance = 15% of Basic Pay.
Provident Fund = 8.33% of Basic Pay.
Net Pay = Basic Pay + Dearness Allowance + House Rent Allowance.
Gross Pay = Net Pay – Provident Fund.

import java.io.*;
class employee
{
static double basic;
public static void main() throws IOException
{
System.out.print(“Enter a Basic : “);
double DA=basic*0.25;
double HRA=basic*0.15;
double PF=basic*0.0833;
double NET_PAY=basic+DA+HRA;
double GROSS_PAY=NET_PAY-PF;
System.out.println(“Gross Pay : “+GROSS_PAY);
}
}

Question 5.
Write a program to input any given string to calculate the total number of characters and vowels present in the string and also reverse the string.
Example:    INPUT
Enter String        :    SNOWY
OUTPUT:
Total number of characters    : 05
Number of Vowels              : 01
Reverse String                : YWONS

import java.io.*;
class ques5
{
public static void main() throws IOException
{
System.out.println(“INPUT”);
System.out.print(“Enter a String : “);
String rev=””;
int nv=0;
int l=n.length();
for(int i=0;i<l;i++)
{
char c=n.charAt(i);
rev=c+rev;
if(c==’A’||c==’a’||c==’E’||c==’e’||c==’I’||c==’i’||c==’O’||c==’o’||c==’U’||c==’u’)
nv++;
}
System.out.println(“OUTPUT”);
System.out.println(“Total No. of Character : “+l);
System.out.println(“Number of Vowels : “+nv);
System.out.println(“Reverse String : “+rev);
}
}

Question 6.
Write a program using a function area() to compute the area of a:
(i)   circle (PI*r2)  where PI=3.14
(ii)  square (side * side)
Display the menu to output the area as per User’s choice.

import java.io.*;
class ques6
{
double area(double r)
{
double PI=3.14;
double area=PI*r*r;
return(area);
}
int area(int side)
{
int area=side*side;
return(area);
}
int area(int l,int b)
{
int area=l*b;
return(area);
}
public void main() throws IOException
{
int ch=0;
do
{
System.out.println(“1. Circle”);
System.out.println(“2. Square”);
System.out.println(“3. Rectangle”);
System.out.println(“4. Exit”);
switch(ch)
{
case 1:
System.out.println(“Area is : “+area(r));
break;
case 2:
System.out.print(“Enter Side : “);
System.out.println(“Area is : “+area(s));
break;
case 3:
System.out.print(“Enter Length : “);
System.out.println(“Area is : “+area(l,b));
break;
}
}while(ch<4);
}
}

Question 7.
Write a program to bubble sort the following set of values in ascending order. 5, 3, 8, 4, 9, 2, 1, 12, 98, 16
Output:
1
2
3
4
5
8
9
12
16
98

/* Programs to sort a set of value stored
* in array using Bubble Sort technique */
public class ques7
{
public static void main()
{
int i,j,k;
// initialize array with given value
int a[]={5,3,8,4,9,2,1,12,98,16};
int l=a.length;
for(i=0;i<l-1;i++)
{
for(j=0;j<l-i-1;j++)
{
// comparing two successive element
if(a[j]>a[j+1])
{
// swaping the two array element
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}
}
}
for(i=0;i<l;i++)
System.out.println(a[i]);
}
}

Questions 8.
Write a program to print the sum of negative numbers, sum of positive even numbers and sum of positive odd numbers from a list of numbers (N) entered by the user. The list terminates when the User enters a zero.

import java.io.DataInputStream;
import java.io.IOException;
public class ques8
{
public static void main() throws IOException
{
DataInputStream stdin = new DataInputStream(System.in);
int N,sumNeg=0,sumPosEven=0,sumPosOdd=0;
do
{
System.out.print(“Enter a Number : “);
if(N<0) sumNeg+=N;
if(N>0 && N%2==0) sumPosEven+=N;
if(N>0 && N%2!=0) sumPosOdd+=N;
}while(N!=0);
System.out.println(“Sum of Negative Number : “+sumNeg);
System.out.println(“Sum of Positive Even Number : “+sumPosEven);
System.out.println(“Sum of Positive Odd Number : “+sumPosOdd);
} // end of method
}

Questions 9.
Write a program to initialize an array of 5 names and initialize another array with their respective telephone numbers. Search for a name input by the user, in the list. If found, display “Search Successful” and print the name along with the telephone number, otherwise display “Search unsuccessful. Name not enlisted”.

import java.io.DataInputStream;
import java.io.IOException;
public class ques9
{
public static void main() throws IOException
{
DataInputStream stdin = new DataInputStream(System.in);
String name[]={“Sanjeet”,”Rishabh”,”Rahul”,”Surajit”,”Trisha”};
String telephone[]={“2222222″,”3333333″,”4444444″,”5555555″,”7777777”};
int i;
String search=null;
boolean flag=false;
System.out.print(“Enter Searching Name : “);
for(i=0;i<5;i++)
{
if(search.compareTo(name[i])==0)
{
System.out.println(“Search Successful”);
System.out.println(“Name “+name[i]+” Phone Number is “+telephone[i]);
flag=true;
}
}
if(flag==false)
System.out.println(“Search unsuccessful. Name not enlisted”);
} // end of method
}

=*=*=*=*=*=