Here is the ICSE 2012 Computer Application paper with it’s solution
SECTION A (40 Marks)
Question 1.
(a)Give one example of a primitive data type and composite data
type. [2]
Ans. One of the primitive data
types is byte while one of the composite data types is class.
(b) Give one point of difference between unary and binary
operators. [2]
Ans. Unary operators
work on a single operand like a++ while binary operators work on two operands like
a+b
© Differentiate between call by value or pass by value and call by
reference or pass by reference [2]
Ans. When variables are
passed as argument while calling a function, it is known as call by value or
pass by value. In call be reference or pass by reference, objects are passed as
argument and objects are used as formal arguments.
In call by value both
the actual and formal arguments are variables while in call by reference,
actual and formal arguments are objects.
(d) Write java expression
for √2as+u2 [2]
Ans.
double d= Math.sqrt((2*a*s + u*u));
(e) Name the type of error
(syntax, runtime or logical error) in each case given
below: [2]
Ans. (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
Ans.
class A
{
int a; //instance variable
A() //default constructor
{
x=0;
}
A(int a) //parameterized constructor
{
x=a;
}
}
(b) Complete the code given
below to create an object of Scanner class: [2]
Ans. Scanner sc=new Scanner (System.in);
© What is an array? Write
a statement to declare an integer array of 10 elements[2]
Ans. An array can be defined as collection of similar
datas in one named space location.
int a[]=new int[10];
(d) Name the search or sort algorithm that
: [2]
(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 : Selection sort
(ii) At each stage,
compares the sought key value with the key value of the middle element of the
array: Binary search
(e) Differentiate between public and private modifiers for members
of a class [2]
Ans. public members of a class can be accessed from anywhere, even
from outside the package while private members are accessible within the class
only.
Question 3.
(a) What are the values of x
and y when the following statements are executed?[2]
int a=63, b=36;
boolean x = (a>b) ? true : false ;
int y=(a
Ans.
x=true
y=36
(b) State the values
of n and ch. [2]
char c=’A’;
int n=c+1;
char ch=(char)n;
Ans.
n=66 ,
ch=B
© What will be the result
stored in x after evaluating the following expression?[2]
int x = 4;
x+= (x++)+(++x)+x;
Ans.
x=4+4+6+6
=20
(d) Give output of the
following program segment: [2]
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));
Ans.
2.0
3.0
(e) State the output of the following program
segment: [2]
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));
Ans.
false
true
(f) State the method
that: [2]
(i) Converts a
string to a primitive float data type : parseFloat()
(ii) Determines if the
specified character is an upper case character: isUpperCase()
(g) State the data
type and values of a and b after the following segment is
executed: [2]
String s1= “Computer”, s2=“Applications”;
a = (s1.compareTo(s2));
b = (s1.equals(s2));
Ans. a=2 or
positive data type
: int
b=
false data type
: boolean
(h) What will the following code
output: [2]
String s= “malayalam”;
System.out.println(s.indexOf(‘m’));
System.out.println(s.lastIndexOf(‘m’));
Ans.
0
8
(i) Rewrite the following
program segment using while instead of for statement[2]
int f=1,I;
for(i=1; i<=5; i++)
{
f*=i; System.out.println(f);}
Ans.
int f=1, i=1;
while (i<=5)
{
f*=i
System.out.println(f);
i++;
}
(j) In the program
given below: [2]
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);
}
}
State the name and value of the :
(i) method argument or
argument variable: 5
(ii) class
variable : x
(iii) local
variable: a
(iv) instance
variable : y
SECTION B(60 Marks)
Question 4.
Define a class called Library with the following description:
Instance variables/data members:
Int acc_num :
stores the accession number of books
String
title :
stores the title of book
String author :
stores the name of author
Member methods:
Void input() : to input and store the accession number,
title and author
Void compute(): to accept the number of days late,
calculate and display the fine charged t the rate of Rs. 2 per day
Void display(): to display the details in the
following format:
Accession
number
Title Author
Write the main method to create an object of the class and
call the above member methods
[15]
Ans
import java.io.*;
class Library {
int acc_num;
String title, author;
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
void input() throws
Exception
{
System.out.println("Enter
the accession number of the book:");
acc_num=
Integer.parseInt(br.readLine());
System.out.println("Enter
the title of the book:");
title= br.readLine();
System.out.println("Enter
the author name of the book:");
author = br.readLine();
}
void compute() throws Exception
{
System.out.println("Enter
the number of days late:");
int n=
Integer.parseInt(br.readLine());
int fine=n*2;
System.out.println("Fine
Amount:"+fine);
}
void display()
{
System.out.println("Accession
Number\t\tTitle\t\tAuthor");
System.out.println(acc_num+"\t\t"+title+"\t\t"+author);
}
public static void
main(String args[]) throws Exception
{
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 yrs:
Does not exceed Rs.
1,60,000 -nil
Is > Rs. 1,60,000 & <=Rs. 50,000 – (TI-
1,60,000)*10%
Is >Rs.5,00,000 & <=Rs. 8,00,000 -
[(TI-5,00,000)*20%]+34,000
Is >Rs. 8,00,000 – [(TI-8,00,000)*30%]+94,000
Write a program to input the age, gender(male or female) and
taxable income(TI) of a person.
If the age is more than 65 yrs or the gender is female, display
“wrong category”.
If the age is less than or equal to 65 yrs and the gender is
male, compute and display the income tax payable as per the table given
above. [15]
Ans.
import java.io.*;
class Tax
{
int age, taxable;
String gender;
double tax;
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
public void takeData()
throws Exception
{
System.out.println("Enter age of the
citizen:");
age=Integer.parseInt(br.readLine());
System.out.println("Enter gender:");
gender= br.readLine();
System.out.println("Enter taxable
income:");
taxable=
Integer.parseInt(br.readLine());
show();
}
void show()
{
if((age>65) ||
(gender.equalsIgnoreCase("female")))
System.out.println("Wrong
category");
else if((age<=65)
&& (gender.equalsIgnoreCase("male")))
{
if(taxable< 160000)
tax=0;
tax=0;
else
if(taxable>160000 && taxable<=500000)
tax=(taxable-160000)*(double)10/100*taxable;
else
if(taxable>500000 && taxable<=800000)
tax=(taxable-500000)*((double)20/100*taxable)+34000;
else
tax=(taxable-800000)*((double)30/100*taxable)+94000;
}
}
public static void
main(String args[]) throws Exception
{
Tax ob=new Tax();
ob.takeData();
}
}
Question 6.
Write a program to accept a string. Convert it to uppercase.
Count and output the number of double letter words that exist in the
string. [15]
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE”
Sample Output: 4
Ans.
import java.io.*;
class Letter
{
BufferedReader br=new
BufferedReader(new InputStreamReader(System.in));
String s1;
int len,i,c=0;
void takeSentence()
throws Exception
{
System.out.println("Enter
the sentence:");
s1=br.readLine().toUpperCase();
show();
}
private void show()
{
len=s1.length();
for( i=0;
i< len; i++)
{
if(s1.charAt(i) ==
s1.charAt(i+1))
c++;
}
System.out.println(
"Number of double letter words:"+c
);
}
public static void
main(String args[]) throws Exception
{
Letter ob=new Letter();
ob.takeSentence();
}
}
Question 7.
Design a class to overload a function polygon() as
follows: [15]
(i) void polygon(int n, char ch) : with one
integer argument and one character type 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 argument that draws a
filled triangle shown below:
*
* *
* * *
Ans.
import java.io.*;
class OverLoadDemo
{
int i,j;
void polygon(int n ,
char ch)
{
for (i=0;i< n; i++)\
{
{
for(j=0;j< n;j++)
System.out.print(ch);
System.out.println();
}
}
void polygon(int x, int y)
{
for(i=0;i< x; i++)
{
for ( j=0;j< y; j++)
System.out.print("@");
System.out.println();
}
}
void polygon()
{
for(i=0;i<=n;i++)<3 i="" o:p="">3>
{
for(j=0;j<=i;j++)
System.out.print("*");
System.out.print("*");
System.out.println();
}
}
public static void
main(String args[])throws Exception
{
OverLoadDemo ob=new
OverLoadDemo();
System.out.println("First
Overloaded function is called.");
ob.polygon(4,'a');
System.out.println("Second
Overloaded function is called.");
ob.polygon(3,4);
System.out.println("Third
Overloaded function is called.");
ob.polygon();
}
}
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,….
(ii) find the sum of the digits of an integer that is input. eg
15390=18
For an incorrect choice, appropriate error message should be
displayed. [15]
Ans.
import java.io.*;
class MenuDriven
{
BufferedReader br=new
BufferedReader (new InputStreamReader (System.in));
int choice;
void takeChoice() throws
Exception
{
System.out.print("Enter
1 for Fibonacci numbers or 2 for sum of digits:");
choice=Integer.parseInt(br.readLine());
switch( choice)
{
case 1:
fib();
break;
case 2:
sum();
break;
default:
System.out.println("
Incorrect choice. Enter 1 or 2 only");
}
}
void fib()
{
int p=0,c=1,next;
System.out.print("The
series is: "+p+" "+c);
for(int i=2;i< 10-2;i++)
{
{
next=p+c;
System.out.print("
"+next);
p=c;
c=next;
}
}
void sum() throws
Exception
{
int i,s=0,n;
System.out.println("Enter
the number:");
n=Integer.parseInt(br.readLine());
while(n>0)
{
s=s+n%10;
n=n/10;
}
System.out.println("Sum
of the digits="+s);
}
public static void
main(String args[])throws Exception
{
MenuDriven ob=new
MenuDriven();
ob.takeChoice();
}
}
Question 9.
Write a program to accept the names of 10 cities in a single
dimension string array and their STD 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”. [15]
Ans.
import java.io.*;
class CityStd
{
String city[]=new
String[10];
int std[]=new int[10];
String w;
BufferedReader br=new
BufferedReader (new InputStreamReader (System.in));
int i;
void takeDatas() throws
Exception
{
for(i=0;i<10 i="" o:p="">10>
{
System.out.print("Enter the city
Name:");
city[i] = br.readLine();
System.out.print("Enter the std
code:");
std[i] =
Integer.parseInt(br.readLine());
}
search();
}
void search()throws
Exception
{
System.out.print("Enter the city to
search:");
w = br.readLine();
for(i=0;i<10 i="" o:p="">10>
{
if(city[i] .
equalsIgnoreCase(w))
break;
}
if(i==10)
System.out.print("Search
Unsuccessful. No such city in the list");
else
System.out.print("STD
code of "+w + "is "+std[i]);
}
public static void
main(String args[])throws Exception
{
CityStd ob=new
CityStd();
ob.takeDatas();
}
No comments:
Post a Comment