ICSE 2014 Computer Application

ICSE 2014 COMPUTER APPLICATION

Answer to this icse 2014 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 on the head of this paper is the time allowed for writing the answer. This Paper is divided into Sections. Attempt all questions from Section A and any four from Section B. The intended marks for questions or parts of questions are given in brackets [ ]

SECTION A (40 MARKS)

Attempt all questions

Question 1 [2]

Which of the following are valid comments?
/*comment*/
/*comment
//comment
*/comment*/

Ans. The valid comments are
/*comment*/
// comment
Marks [2]

What is meant by a package? Name any two Java Application Programming Interface packages.
Ans. Java contains extensive library of pre-written classes that we can use in our programs. These classes are divided into groups called packages.
There are various packages in Java, from them two Java Application Programming Interface packages are: java.applet, java.awt
Marks [2]

Name the primitive data type in Java that is:
a 64-bit integer and is used when you need a range of values wider than those provided by int
a single l6-bit Unicode character whose default value is ‘\u0000’.

Ans.
long
char
Marks [2]

State one difference between the floating point literals float and double.
Ans. Floating point real number literals:
Single precission number float will be wtitten as: 6.02f
Double precission number where as for double the same will be: 6.02d or even 6.02
Marks [2]

Find the errors in the given program segment and re-write the statements correctly to assign values to an integer array.
int a = new int(5);
for(int i=0;i<=5;i++) a[i]=i;
Ans. Array initialization use brackets instead of parenthesis, so in the first line parenthesises are replaced with brackets. Array index starts from Zero to less than its size, so in the second line the condition of for statement will be i<5 instead i<=5.
So the corrected program segment will be as:
int a = new int[5];
for(int i=0;i<5;i++) a[i]=i;

Question 2

Marks [2]

Operators with higher precedence are evaluated before operators with relatively lower precedence. Arrange the operators given below in order of higher precedence
&&
%
>=
++

Ans. After arranging the given operators according to the order of higher precedence the numbering list will be changed as:
++
%
>=
&&
Marks [2]

Identify the statements listed below as assignment, increment, method invocation or object creation statements.
System.out.println(“Java”);
costPrice = 457.50;
Car hybrid = new Car( );
petrolPrice++;

Ans.
System.out.println(“Java”); – method invocation statement
costPrice = 457.50; – assignmentstatement
Car hybrid = new Car( ); – object creation statement
petrolPrice++; – incrementstatement
Marks [2]

Give two differences between the switch statement and the if-else statement.
Ans.
Both switch and the if-else are conditional control statement but with the following differences (Only 2 are stated as per question):
switch can test only equality but any other relational operaters can be used in if-else statement.
switch supports only char, int, byte and short data type but in the case of if-else supportance are regardless of data type.
Marks [2]

What is an infinite loop? Write an infinite loop statement.
Ans. In this case, the loop block never terminates. It is actually a kind of logical error because circumstances generally might not occur where infinite loops are required.
An example of an infinite loop is:
for(int i = 1; i>=0; i++)
; //empty statement
Marks [2]

What is a constructor? When is it invoked?
Ans.A constructor is a Member function that automatically called, when the object created of that class. It has the same name as that of the class name and its primary job is to initialise the object to a legal value for the class.
Constructor invoked at the time of object creation.

Question 3

Marks [2]

List the variables from those given below that are composite data types.
static int x;
arr[i]=10;
obj.display( );
boolean b;
private char chr;
String str;

Ans. variables that are composite data types from the given list:
arr[i]=10;
String str;
Marks [2]

State the output of the following program segment:
String str1 = “great”; String str2 = “minds”;
System.out.println(str1.substring(0,2).concat(str2.substring(1)));
System.out.println((“WH”+(str1.substring(2).toUpperCase( ))));
Ans. grinds
WHEAT
Marks [2]

What are the final values stored in variables x and y below?
double a = – 6.35;
double b = 14.74;
double x = Math.abs(Math.ceil(a));
double y = Math.rint(Math.max(a,b));
Ans. Final value of x: 6.0
Final value of y: 15.0
Marks [2]

Rewrite the following program segment using if-else statements instead of the ternary operator.
String grade=(mark>=90) ? “A” : (mark>=80) ? “B” : “C”;
Ans. String grade;
if(mark >= 90)
grade = �A�;
else if(mark >= 80)
grade = �B�;
else
grade = �C�;
Marks [2]

Give the output of the following method:
public static void main(String[] args){
int a = 5;
a++;
System.out.println(a);
a -= (a–) – (–a);
System.out.println(a); }
Ans. Output
6
4
[Steps are:
a = 5
a++
ie. a = a +1
= 6 (Printed)
a -= (a–) – (–a)
ie. a = a – ((a–) – (–a))
= 6 – ((6) � (4))
= 6 – 2 = 4 (Printed)
]
Marks [2]

What is the data type returned by the library functions:
compareTo( )
equals( )

Ans.
Return data type of compareTo( ) is int
Return data type of equals( )is boolean
Marks [2]

State the value of characteristic and mantissa when the following code is executed.
String s = “4.3756”;
int n = s.indexOf(‘.’);
int characteristic = Integer.parseInt(s.substring(0,n));
int mantissa = Integer.valueOf(s.substring(n+1);
Ans. After executing the given code
The value of characteristic will be: 4
The value of mantissa will be: 3756
Marks [2]

Study the method and answer the given questions.
1
public void sampleMethod( )
2
{   for(int i=0;i<3;i++)
3
{   for(int j=0;j<2;j++)
4
{   int number = (int)(Math.random( )*10);
5
System.out.println(number); }}}
How many times does the loop execute?
What is the range of possible values stored in the variable number?

Ans.
The loop will execute for: Outer loop 3 times and Inner loop 2 times so, 3 x 2 =
6 times
The range of possible values stored in the variable number will be:
0 to 9
Marks [2]

Consider the following class:
public class myClass {
public static int x = 3, y = 4;
public int a = 2, b = 3; }
Name the variables for which each object of the class will have its own disstinct copy.
Name the variables that are common to all objects of the class.

Ans. The variables that will be distinct to each object are:
a and b
Marks [2]

What will be the output when the following code segments are executed?
String s = “1001”;
int x = Integer.valueOf(s);
double y = Double.valueOf(s);
System.out.println(“x=” +x);
System.out.println(“y=” +y);
System.out.println(“The king said \”Begin at the beginning!\” to me

Ans.
Output
x=1001
y=1001.0
Output
The king said “Begin at the beginning!” to me.

SECTION B (60 MARKS)

Attempt any four questions from this Section. The answers in this Section should consist of the Programs in either Blue J environment or any program environment with Java as the base. Each program should be written using Variable description/Mnemonic Codes so that the logic of the program is clearly depicted. Flow-Charts and Algorithms are not required.

Question 4
Marks [15]

Define a class named movieMagic with the following description:
Instance variables/data members:
int year – to store the year of release of a movie
String title – to store the title of the movie
float rating – to store the popularity rating of the movie

(minimum rating = 0.0 and maximum rating = 5.0)
Member methods:

movieMagic( )Default constructor to initialize numeric data members to 0 and String data member to “”.
void accept( )To input and store year, title and rating.
void display( )To display the title of a movie and a message based on the rating as per the table below.

Rating     Message to be displayed
0.0 to 2.0     Flop
2.1 to 3.4     Semi-hit
3.5 to 4.5     Hit
4.6 to 5.0     Super Hit

Write a main method to create an object of the class and call the above member methods.
Ans.
1
import java.io.BufferedReader;
2
import java.io.IOException;
3
import java.io.InputStreamReader;
4
public class movieMagic
5
{
6
int year;
7
String title;
8
float rating;
9
public movieMagic()
10
{
11
year = 0;
12
title = “”;
13
rating = 0.0f;
14
}
15
void accept( )throws IOException
16
{
17
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
18
System.out.print(” Enter Movie Title: “);
19
title = read.readLine();
20
System.out.print(” Enter release year: “);
21
year = Integer.parseInt(read.readLine());
22
System.out.print(” Enter popularity rating: “);
23
rating = Float.parseFloat(read.readLine());
24
}
25
void display()
26
{
27
//if rating entered wrongly entered negative
28
if (rating <0.0)
29
rating = 0.0f;
30
if(rating >= 0.0 && rating<=2.0)
31
{
32
System.out.println(“Flop”);
33
}
34
else if (rating >= 2.1 && rating <= 3.4)
35
{
36
System.out.println(“Semi-hit”);
37
}
38
else if (rating >= 3.5 && rating <= 4.5)
39
{
40
System.out.println(“Hit”);
41
}
42
else
43
{
44
System.out.println(“Super Hit”);
45
}
46
}
47
public static void main(String[] args) throws IOException
48
{
49
movieMagic Rates = new movieMagic();
50
Rates.accept();
51
Rates.display();
52
}
53
}
Question 5
Marks [15]

A special two-digit number is such that when the sum of its digits is added to the productof its digits, the result is equal to the original two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of its digits = 5 x 9 = 45
Sum of the sum of digits and product of digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product ofits digits. If the value is equal to the number input, output the message “Special 2-digitnumber” otherwise, output the message “Not a special2-digit number”.
Ans.
1
import java.io.*;
2
public class specialTwoDigit
3
{
4
public static void main(String[] args)throws IOException
5
{
6
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
7
int n, d;
8
int sod=0, sop=1;
9
System.out.print(” Input a two digit number: “);
10
String str = read.readLine();
11
//checking if the number is 2 digit
12
if(str.length() != 2)
13
{
14
System.out.println(” Not a 2-digit Number”);
15
return;
16
}
17
n = Integer.parseInt(str);
18
int no = n;
19
while(no!=0)
20
{
21
d = no % 10;
22
sod = sod + d;
23
sop = sop * d;
24
no = no / 10;
25
}
26
int sum = sod + sop;
27
if(sum == n)
28
System.out.println(” Special 2-digit”);
29
else
30
System.out.println(” Not a special 2-digit”);
31
}
32
}
Question 6
Marks [15]

Write a program to assign a full path and file name as given below. Using library functions,extract and output the file path, file name and file extension separately as shown.
Input C:\Users\adm in\Pictures\flower.jpg
Output Path: C:\Users\admin\Pictures\
File name: flower
Extension: jpg
Ans.
1
import java.io.*;
2
public class filePathNameExt
3
{
4
public static void main(String[] args)
5
{
6
// Extraction of File Path, File Name and File Extension
7
String inputPath = “C:\\User\\admin\\Pictures\\flower.jpg”; // One is for escape sequences and another is for String
8
System.out.print(” Input path is: “);
9
System.out.println(inputPath);
10
int len = inputPath.length();
11
int dot = inputPath.indexOf(“.”); // Dot position
12
int slash =inputPath.lastIndexOf(“\\”); //Last back slash position
13
String path = inputPath.substring(0, slash); // path name is 0 to before last back slash position
14
System.out.println(” Path:   ” +path);
15
String name = inputPath.substring(slash+1, dot); //name is after last back slash and upto before dot
16
System.out.println(” File name: ”  +name);
17
String ext = inputPath.substring(dot+1, len); //extension is after dot till before length
18
System.out.println(” Extension: ”  +ext);
19
}
20
}
Question 7
Marks [15]

Design a class to overload a function area( ) as follows:

double area(double a, double b, double c) with three double arguments, returns the area of a scalene triangle using the formula: �area =�area mathematical expressionwhere s =�mathematical expression
double area(int a, int b, int height) with three integer arguments, returns the area of a trapezium using the formula: area =�mathematical expression
double area(double diagonal1, double diagonal2) with two double arguments, returns the area of a rhombus using the formula: area =�mathematical expression

Ans.
1
import java.io.*;
2
public class overloadArea
3
{
4
double resultantArea;
5
public overloadArea()
6
{
7
resultantArea = 0.0;
8
}
9
//Area of a scalene triangle
10
double area(double a, double b, double c)
11
{
12
double s = ( a + b + c ) / 2;
13
resultantArea = Math.sqrt(s * (s – a) * (s – b) * (s – c));
14
return resultantArea;
15
}
16
//Area of a trapezium
17
double area(int a, int b, int height)
18
{
19
resultantArea =  (height * (a + b))/2;
20
return resultantArea;
21
}
22
//Area of a rhombus
23
double area(double diagonal1, double diagonal2)
24
{
25
resultantArea = ( diagonal1 * diagonal2) / 2;
26
return resultantArea;
27
}
28
public static void main(String[] args)
29
{
30
overloadArea doArea = new overloadArea();
31
double triangle = doArea.area(18.0, 16.0, 12.0);
32
double trapiziun = doArea.area(12, 8, 14);
33
double rhombus = doArea.area(18.0, 22.0);
34
System.out.println(“Area of a scalene triangle: ” +triangle);
35
System.out.println(“Area of a trapezium: ” +trapiziun);
36
System.out.println(“Area of a rhombus: ” +rhombus);
37
}
38
}
Question 8
Marks [15]

Using the switch statement, write a menu driven program to calculate the maturity amountof a Bank Deposit.
The user is given the following options:

Term Deposit
Recurring Deposit

For option (i) accept principal(P), rate of interest(r) and time period in years(n). Calculateand output the maturity amount(A) receivable using the formula

A =��maturity amount

For option (ii) accept Monthly Installment (P), rate ofinterest(r) and time period in months(n). Calculate and output the maturity amount(A) receivable using the formula

A =�maturity amount

For an incorrect option, an appropriate error message should be displayed.

import java.io.*;
import java.io.*;
public class bankDeposite
{
public static void main(String[] args)throws IOException
{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
int p = 0, n = 0;
double a = 0.0;
double r = 0.0;
System.out.println(" MENU:-");
System.out.println(" TERM DEPOSIT: PRESS 1");
System.out.println(" RECURRING DEPOSIT: PRESS 2");
System.out.print(" Enter your choice: ");
int ch = Integer.parseInt(read.readLine());
switch(ch)
{
case 1:
System.out.print(" Enter Principal Amount: ");
p = Integer.parseInt(read.readLine());
System.out.print(" Enter rate of interest: ");
r = Integer.parseInt(read.readLine());
System.out.print(" Enter time period in years: ");
n = Integer.parseInt(read.readLine());
a = p * Math.pow((1 + r/100),n);
break;
case 2:
System.out.print(" Enter Monthly Installment: ");
p = Integer.parseInt(read.readLine());
System.out.print(" Enter rate of interest: ");
r = Integer.parseInt(read.readLine());
System.out.print(" Enter time period in months: ");
n = Integer.parseInt(read.readLine());
a = p * n + p * n*(n+1)/2 * r/100 * 1/12;
break;
default:
System.out.println(" Sorry Entered Wrong Choice, Hence ");
}
System.out.print(" Maturity Amount is " +a);
}
}

Question 9

Write a program to accept the year of graduation from school as an integer value from the user. Using the Binary Search technique on the sorted array of integers given below,output the message “Record exists” if the value input is located in the array. If not, outpu tthe message “Record does not exist”.
{1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010}
Ans.

Leave a Comment