ICSE Computer Applications 2014

COMPUTER APPLICATIONS
(Theory)
(Two hours)

Answers to this paper 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 of this paper is the time allowed for writing the answers.
This paper is divided into two sections.
Attempt all questions from Section A and any four questions 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
(a) Which of the following are valid comments?
(i) /* comment */
(ii) /* comment
(iii) // comment
(iv) */ comment */
(i) and (iii) are valid comments

(b) What is meant by a package? Name any two Java Application Programming Interface packages.
A package is a collection of related classes and interfaces. Two Java API packages are:
java.io package
java.util package

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

(d) State one difference between floating point literals float and double.
The float data type value occupies 4 bytes in memory, whereas the double data type value occupies 8 bytes in memory.

(e) 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;
int a[] = new int[5];
for(int i = 0; i < 5; i++)
    a[i] = i;

Question 2
(a) Operators with higher precedence are evaluated before operators with relatively lower precedence. Arrange the operators given below in order of higher precedence to lower precedence.
(i) &&
(ii) %
(iii) >=
(iv) ++
++, %, >=, &&

(b) Identify the statements listed below as assignment, increment, method invocation or object creation statements.
(i) System.out.println(“Java”);
Method invocation
(ii) costPrice = 457.50;
Assignment
(iii) Car hybrid = new Car();
Object creation
(iv) petrolPrice++;
Increment

(c) Give two differences between switch statement and if-else statement.
1. The switch statement is a multi-branching statement, whereas the if-else is a bi-directional statement.
2. The switch statement can only compare for equality, whereas in if-else, all kinds of comparisons can be made.

(d) What is an infinite loop? Write an infinite loop statement.
A loop that never terminates is an infinite loop.
Example:
while(true)
    System.out.println(“Hello”);

(e) What is a constructor? When is it invoked?
A constructor is a method that is used to initialize an object with legal initial values. It is invoked during object creation.

Question 3
(a) List the variables from those given below that are composite data types.
(i) static int x;
(ii) arr[i] = 10;
(iii) obj.display();
(iv) boolean b;
(v) private char chr;
(vi) String str;
arr, obj and str are the variables that are composite data types.

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

(c) 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));
6.0
15.0

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

(e) Give 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);
}
6
4

(f) What is the data type returned by the library functions:
(i) compareTo()
int data type
(ii) equals()
boolean data type

(g) 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));
characteristic = 4
mantissa = 3756

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

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

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

SECTION B (60 Marks)
Attempt any four questions from this section.

The answers in this section should consist of the programs in either BlueJ environment or any program environment with Java as the base.
Each program should be written using variable descriptions/mnemonic codes such that the logic of the programs is clearly depicted.
Flowcharts and algorithms are not required.

Question 4
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 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.

RatingMessage to be displayed
0.0 to 2.0Flop
2.1 to 3.4Sem-hit
3.5 to 4.5Hit
4.6 to 5.0Super hit

Write a main method to create an object of the class and call the above member methods.

import java.util.Scanner;
class MovieMagic{
    int year;
    String title;
    float rating;
    public MovieMagic(){
        year = 0;
        title = "";
        rating = 0.0F;
    }
    public void accept(){
        Scanner in = new Scanner(System.in);
        System.out.print("Year: ");
        year = Integer.parseInt(in.nextLine());
        System.out.print("Title: ");
        title = in.nextLine();
        System.out.print("Rating: ");
        rating = Float.parseFloat(in.nextLine());
    }
    public void display(){
        System.out.println("Movie Title: " + title);
        if(rating <= 2.0F)
            System.out.println("Flop");
        else if(rating <= 3.4F)
            System.out.println("Semi-hit");
        else if(rating <= 4.5F)
            System.out.println("Hit");
        else
            System.out.println("Super Hit");
    }
    public static void main(String[] args){
        MovieMagic obj = new MovieMagic();
        obj.accept();
        obj.display();
    }
}

Question 5
A special two-digit number is such that when the sum of its digits is added to the product of 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 × 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 of its digits. If the value is equal to the number input, output the message “Special 2-digit number” otherwise, output the message “Not a special 2-digit number”.

import java.util.Scanner;
class Special{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int num = Integer.parseInt(in.nextLine());
        if(num > 99 || num < 10){
            System.out.println("Not a special 2-digit number");
            return;
        }
        int first = num / 10;
        int last = num % 10;
        int sum = first + last;
        int product = first * last;
        if(num == sum + product)
            System.out.println("Special 2-digit number");
        else
            System.out.println("Not a special 2-digit number");
    }
}

Question 6
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\admin\Pictures\flower.jpg
Output:
Path: C:\Users\admin\Pictures\
File name: flower
Extension: jpg

class Path{
    public static void main(String[] args){
        String fp = "C:\\Users\\admin\\Pictures\\flower.jpg";
        int last = fp.lastIndexOf('\\');
        String p = fp.substring(0, last + 1);
        String fn = fp.substring(last + 1, fp.lastIndexOf('.'));
        String e = fp.substring(fp.lastIndexOf('.') + 1);
        System.out.println("Path: " + p);
        System.out.println("File name: " + fn);
        System.out.println("Extension: " + e);
    }
}

Question 7
Design a class to overload a function area() as follows:
(i) double area(double a, double b, double c) with three double arguments, returns the area of a scalene triangle using the formula:
Triangle Area Formula
(ii) double area(int a, int b, int height) with three integer arguments, returns the area of a trapezium using the formula:
Trapezium Area Formula
(iii) double area(double diagonal1, double diagonal2) with two double arguments, returns the area of a rhombus using the formula:
Rhombus Area Formula

class Overload{
    public static double area(double a, double b, double c){
        double s = (a + b + c) / 2;
        return Math.sqrt(s * (s - a) * (s - b) * (s - c));
    }
    public static double area(int a, int b, int height){
        return 1.0 / 2 * height * (a + b);
    }
    public static double area(double diagonal1, double diagonal2){
        return 1.0 / 2 * (diagonal1 * diagonal2);
    }
}

Question 8
Using the switch statement, write a menu-driven program to calculate the maturity amount of a bank deposit. The user is given the following options:
(i) Term Deposit
(ii) Recurring Deposit
For option (i) accept principal (p), rate of interest (r) and time period (n). Calculate and output the maturity amount (a) receivable using the formula:
Term Deposit Formula
For option (ii) accept monthly installment (p), rate of interest (r) and time period in months (n). Calculate and output the maturity amount (a) receivable using the formula:
Recurring Deposit Formula

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

import java.util.Scanner;
class Deposit{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("1. Term Deposit");
        System.out.println("2. Recurring Deposit");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(in.nextLine());
        switch(choice){
        case 1:
            System.out.print("Principal: ");
            double p = Double.parseDouble(in.nextLine());
            System.out.print("Rate of interest: ");
            double r = Double.parseDouble(in.nextLine());
            System.out.print("Time period: ");
            double n = Double.parseDouble(in.nextLine());
            double a = p * Math.pow(1 + r / 100, n);
            System.out.println("Maturity amount: " + a);
            break;
        case 2:
            System.out.print("Monthly installment: ");
            p = Double.parseDouble(in.nextLine());
            System.out.print("Rate of interest: ");
            r = Double.parseDouble(in.nextLine());
            System.out.print("Time period: ");
            n = Double.parseDouble(in.nextLine());
            a = p * n + p * n * (n + 1) / 2 * r / 100 * 1 / 12;
            System.out.println("Maturity amount: " + a);
            break;
        default:
            System.out.println("Invalid choice!");
        }
    }
}

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, output the message “Record does not exist”.
{1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010}

import java.util.Scanner;
class BinarySearch{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int y[] = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
        System.out.print("Year of graduation: ");
        int key = Integer.parseInt(in.nextLine());
        int low = 0;
        int high = y.length - 1;
        int mid = 0;
        while(low <= high){
            mid = (low + high) / 2;
            if(key == y[mid])
                break;
            else if(key < y[mid])
                high = mid - 1;
            else
                low = mid + 1;
        }
        if(low <= high)
            System.out.println("Record exists");
        else
            System.out.println("Record does not exist");
    }
}