ICSE Computer Applications Year 2016

Computer Applications

Section A (40 Marks)

Attempt all questions

Question 1

(a) Define encapsulation.
Wrapping up of data and functions into a single unit called class is known as encapsulation.

(b) What are keywords? Give an example.
Keywords are the words that convey a special meaning to the language compiler. Example: void.

(c) Name any two library packages.
java.lang and java.io are two library packages.

(d) Name the type of error (syntax, runtime or logical error) in each case given below:
(i) Math.sqrt(36 - 45) – runtime error.
(ii) int a;b;c; – syntax error.

(e) If int x[] = {4, 3, 7, 8, 9, 10}; what are the values of p and q?
(i) p = x.length
6
(ii) q = x[2] + x[5] * x[1]
7 + 10 * 3
= 7 + 30
= 37.

Question 2

(a) State the difference between == operator and equals() method.
The == operator compares two numeric or character values for equality.
The equals() method compares the contents of two String values for equality.

(b) What are the types of casting shown by the following examples?
(i) char c = (char)120; (Explicit type casting)
(ii) int x = 't'; (Implicit type casting)

(c) Differentiate between formal parameter and actual parameter.
Formal parameters appear in the method definition.
Actual parameter appear in the method call statement.

(d) Write the function prototype of the following:
A function posChar which takes a string argument and a character argument and returns an integer value.
int posChar(String s, char ch)

(e) Name any two types of access specifiers.
public and private are two types of access specifiers.

Question 3

(a) Give the output of the following string functions:
(i) "MISSISSIPPI".indexOf('S') + "MISSISSIPPI".lastIndexOf('I')
2 + 10 = 12
(ii) "CABLE".compareTo("CADET")
66 – 68 = -2

(b) Give the output of the following Math functions:
(i) Math.ceil(4.2)
5.0
(ii) Math.abs(-4)
4

(c) What is a parameterized constructor?
Parameterized constructor are the ones that receive parameters and initialize objects with received values.

(c) Write down Java expression for:
T = √(A2 + B2 + C2)
T = Math.sqrt(A * A + B * B + C * C)

(e) Rewrite the following using ternary operator:

if(x % 2 == 0)
    System.out.print("EVEN");
else
    System.out.print("ODD");

System.out.print(((x % 2 == 0)? "EVEN":"ODD"));

(f) Convert the following while loop to the corresponding for loop:

int m = 5, n = 10;
while(n >= 1){
    System.out.println(m * n);
    n--;
}
int m = 5, n = 10;
for(; n >= 1; n--)
    System.out.println(m * n);

(g) Write one difference between primitive data types and composite data types.
Primitive data types come as part of the language.
Composite data types are constructed from primitive data types.

(h) Analyze the given program segment and answer the following questions:
(i) Write the output of the program segment.
(ii) How many times does the body of the loop gets executed?

for(int m = 5; m <= 20; m += 5){
    if(m % 3 == 0)
        break;
    else
        if(m % 5 == 0)
            System.out.println(m);
    continue;
}

OUTPUT:
5
10

The body of the loop executes 3 times.

(i) Give the output of the following expression:
a += a++ + ++a + --a + a--; when a = 7.

a = a + a++ + ++a + –a + a–;
= 7 + 7 + 9 + 8 + 8
= 14 + 9 + 8 + 8
= 23 + 8 + 8
= 31 + 8
= 39.

(j) Write the return type of the following library functions:
(i) isLetterOrDigit(char)boolean
(ii) replace(char, char)String

Section B (60 Marks)

Attempt any four questions from this section.

Question 4

Define a class named BookFair with the following description:
Instance variables/data members:
String bName: stores the name of the book.
double price: stores the price of the book.
Member methods:
BookFair(): default constructor to initialize data members.
void input(): to input and store the name and the price of the book.
void calculate(): to calculate the price after discount. Discount is calculated based on the following criteria:

Price Discount
Less than or equal to Rs. 1000 2% of price
More than Rs. 1000 and less than or equal to Rs. 3000 10% of price
More than Rs. 3000 15% of price

void display(): to display the name and price of the book after discount.

import java.io.*;
class BookFair{
    String bName;
    double price;
    public BookFair(){
        bName = new String();
        price = 0.0;
    }
    public void input()throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Book name: ");
        bName = br.readLine();
        System.out.print("Book price: ");
        price = Double.parseDouble(br.readLine());
    }
    public void calculate(){
        if(price <= 1000.0) price -= 2.0 / 100 * price; else if(price > 1000.0 && price <= 3000.0)
            price -= 10.0 / 100 * price;
        else
            price -= 15.0 / 100 * price;
    }
    public void display(){
        System.out.println("Book name: " + bName);
        System.out.println("Book price: Rs. " + price);
    }
    public static void main(String args[])
    throws IOException{
        BookFair obj = new BookFair();
        obj.input();
        obj.calculate();
        obj.display();
    }
}

Question 5

Using the switch statement, write a menu-driven program for the following:
(i) To print the Floyd’s triangle given below:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
(ii) To display the following pattern:
I
IC
ICS
ICSE
For an incorrect option, an appropriate error message should be displayed.

import java.io.*;
class Menu{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.println("1. Floyd's Triangle");
        System.out.println("2. ICSE");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(br.readLine());
        switch(choice){
            case 1:
            int term = 1;
            for(int i = 1; i <= 5; i++){
                for(int j = 1; j <= i; j++){
                    System.out.print(term + " ");
                    term++;
                }
                System.out.println();
            }
            break;
            case 2:
            String s = "ICSE";
            for(int i = 1; i <= s.length(); i++)
                System.out.println(s.substring(0, i));
            break;
            default:
            System.out.println("Invalid choice!");
        }
    }
}

Question 6

Special words are those words which starts and ends with the same letter.
Examples:
EXISTENCE
COMIC
WINDOW

Palindrome words are those words which read the same from left to right and vice-versa.
Examples:
MALAYALAM
MADAM
LEVEL
ROTATOR
CIVIC

All palindromes are special words, but all special words are not palindromes.

Write a program to accept a word and check and print whether the word is a palindrome or only special word.

import java.io.*;
class Special{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.println("Enter the word: ");
        String word = br.readLine();
        word = word.toUpperCase();
        word = word.trim();
        if(word.indexOf(' ') > -1)
            word = word.substring(0, word.indexOf(' '));
        if(isSpecial(word)){
            if(isPalindrome(word))
                System.out.println(word + " is a palindrome.");
            else
                System.out.println(word + " is special.");
        }
        else
            System.out.println(word + " is not special.");
    }
    public static boolean isSpecial(String w){
        char first = w.charAt(0);
        char last = w.charAt(w.length() - 1);
        if(first == last)
            return true;
        return false;
    }
    public static boolean isPalindrome(String w){
        String p = new String();
        for(int i = 0; i < w.length(); i++)
            p = w.charAt(i) + p;
        if(w.equals(p))
            return true;
        return false;
    }
}

Question 7

Design a class to overload a function sumSeries() as follows:
(i) void sumSeries(int n, double x): with one integer argument and one double argument to find and display the sum of the series given below:
s = x / 1 – x / 2 + x / 3 – x / 4 + x / 5 … N terms.
(ii) void sumSeries(): to find and display the sum of the following series:
s = 1 + (1 × 2) + (1 × 2 × 3) + … + (1 × 2 × 3 × 4 … × 20)

class Overload{
    public static void sumSeries(int n, double x){
        double sum = 0.0;
        int sign = 1;
        for(int i = 1; i <= n; i++){
            double term = sign * (double)x / i;
            sum += term;
            sign *= -1;
        }
        System.out.println("Sum = " + sum);      
    }
    public static void sumSeries(){
        long sum = 0L;
        for(int i = 1; i <= 20; i++){
            long f = 1;
            for(int j = 1; j <= i; j++)
                f *= j;
            sum += f;
        }
        System.out.println("Sum = " + sum);      
    }
}

Question 8

Write a program to accept a number and check and display whether it is a Niven number or not.

Niven number is that number which is divisible by its sum of digits.

Example:
Consider the number 126.
Sum of its digits is 1 + 2 + 6 = 9, and 126 is divisible by 9.

import java.io.*;
class Niven{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Enter the number: ");
        int n = Integer.parseInt(br.readLine());
        int sum = 0;
        for(int i = n; i != 0; i /= 10)
            sum += i % 10;
        if(n % sum == 0)
            System.out.println(n + " is Niven.");
        else
            System.out.println(n + " is not Niven.");
    }
}

Question 9

Write a program to initialize the seven Wonders of the World along with their locations in two different arrays. Search for a name of the country input by the user. If found, display the name of the country along with its Wonder, otherwise display “Sorry not found!”.

Seven Wonders: CHICHEN ITZA, CHRIST THE REDEEMER, TAJ MAHAL, GREAT WALL OF CHINA, MACHU PICCHU, PETRA, COLOSSEUM

Locations: MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY

Examples:
Country name: INDIA
Output: TAJ MAHAL

Country name: USA
Output: Sorry not found!

import java.io.*;
class Wonders{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        String w[] = {"CHICHEN ITZA", "CHRIST THE REDEEMER", "TAJ MAHAL", "GREAT WALL OF CHINA", "MACHU PICCHU", "PETRA", "COLOSSEUM"};
        String l[] = {"MEXICO", "BRAZIL", "INDIA", "CHINA", "PERU", "JORDAN", "ITALY"};
        System.out.print("Country name: ");
        String c = br.readLine();
        c = c.trim();
        c = c.toUpperCase();
        int i = 0;
        for(; i < w.length; i++){
            if(c.equals(l[i]))
                break;
        }
        if(i == w.length)
            System.out.println("Sorry not found!");
        else
            System.out.println(l[i] + " - " + w[i]);
    }
}

2 thoughts on “ICSE Computer Applications Year 2016

    • Since I manage this website all alone, it takes time to add more content. I assure you that with time, the website will be more content rich, with explanations of each of the programs. By the way, thanks for your feedback!

Leave a Reply