ICSE Computer Applications 2016

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) Define encapsulation.
Wrapping up of data and functions into a single unit is known as encapsulation.

(b) What are keywords? Give an example.
Keywords are the reserved words that have a special meaning in the language.

(c) Name any two library packages.
java.io package
java.util package

(d) Name the type of error (syntax, runtime or logical error) in each case given below:
(i) Math.sqrt(36 – 45)
Logical 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
p = 6
(ii) q = x[2] + x[5] * x[1]
q = 37

Question 2
(a) State the difference between == operator and equals() method.
The == operator is a relational operator that checks for equality of two values. The equals() method is in String class that checks for equality of the contents of two string values.

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

(c) Differentiate between formal parameter and actual parameter.
Formal parameters are the parameters that are used in function definition to receive values. Actual parameters are the parameters that are passed to the function during function call.

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

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

Question 3
(a) Give the output of the following string functions:
(i) “MISSISSIPPI”.indexOf(‘S’) + “MISSISSIPPI”.lastIndexOf(‘I’)
12
(ii) “CABLE”.compareTo(“CADET”)
-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?
A constructor with parameter(s) is a parameterized constructor.

(d) Write down Java expression for:
Mathematical Expression ICSE Computer Applications 2016
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;
for(n = 10; n >= 1; n–)
    System.out.println(m * n);

(g) Write one difference between primitive data types and composite data types.
Primitive data types are simple data types. Composite data types are complex data types.

(h) Analyze the given program segment and answer the following questions:
(i) Write the output of the program segment.
5
10
(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;
}
3 times

(i) Give the output of the following expression:
a += a++ + ++a + –a + a–; when a = 7
a = a + (a++ + ++a + –a + a–)
a = 7 + (7 + 9 + 8 + 8)
a = 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.
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 so that the logic of the program is clearly depicted.
Flowcharts and algorithms are not required.

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 functions:
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:

PriceDiscount
Less than or equal to ₹ 10002% of price
More than ₹ 1000 and less than or equal to ₹ 300010% of price
More than ₹ 300015% of price

void display() – to display the name and price of the book after discount.
Write a main() method to create an object of the class and call the above methods.

import java.util.Scanner;
class BookFair{
    String bName;
    double price;
    public BookFair(){
        bName = "";
        price = 0.0;
    }
    public void input(){
        Scanner in = new Scanner(System.in);
        System.out.print("Book name: ");
        bName = in.nextLine();
        System.out.print("Price: ");
        price = Double.parseDouble(in.nextLine());
    }
    public void calculate(){
        if(price <= 1000)
            price -= 2.0 / 100 * price;
        else if(price <= 3000)
            price -= 10.0 / 100 * price;
        else
            price -= 15.0 / 100 * price;
    }
    public void display(){
        System.out.println("Book name: " + bName);
        calculate();
        System.out.println("Price after discount: " + price);
    }
    public static void main(String[] args){
        BookFair obj = new BookFair();
        obj.input();
        obj.display();
    }
}

Question 5
Using the switch statement, write a menu-driven program for the following:
(i) To print 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.util.Scanner;
class Menu{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.println("1. Floyd's triangle");
        System.out.println("2. ICSE Pattern");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(in.nextLine());
        switch(choice){
        case 1:
            int n = 1;
            for(int i = 1; i <= 5; i++){
                for(int j = 1; j <= i; j++){
                    System.out.print(n + " ");
                    n++;
                }
                System.out.println();
            }
            break;
        case 2:
            String s = "ICSE";
            for(int i = 0; i < s.length(); i++)
                System.out.println(s.substring(0, i + 1));
            System.out.println();
            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 a special word.

import java.util.Scanner;
class Check{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the word: ");
        String w = in.next().toUpperCase();
        String rev = "";
        for(int i = w.length() - 1; i >= 0; i--)
            rev += w.charAt(i);
        if(w.equals(rev))
            System.out.println("Palindrome");
        else{
            char first = w.charAt(0);
            char last = w.charAt(w.length() - 1);
            if(first == last)
                System.out.println("Special word");
            else
                System.out.println("Not a special word");
        }
    }
}

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:
Series Question ICSE Computer Applications 2016
(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 s = 0.0;
        for(int i = 1; i <= n; i++)
            s += x / i;
        System.out.println("Sum = " + s);
    }
    public static void sumSeries(){
        long s = 0L;
        long f = 1L;
        for(int i = 1; i <= 20; i++){
            f *= i;
            s += f;
        }
        System.out.println("Sum = " + s);
    }
}

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 the 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.util.Scanner;
class Niven{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the number: ");
        int n = Integer.parseInt(in.nextLine());
        int sum = 0;
        for(int i = n; i != 0; i /= 10)
            sum += i % 10;
        if(n % sum == 0)
            System.out.println("Niven number");
        else
            System.out.println("Not a Niven number");
    }
}

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
Example:
Country name: INDIA
Output: INDIA – TAJ MAHAL
Country name: USA
Output: Sorry not found!

import java.util.Scanner;
class Search{
    public static void main(String[] args){
        Scanner in = new Scanner(System.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 = in.nextLine().toUpperCase();
        int i = 0;
        while(i < w.length){
            if(c.equals(l[i]))
                break;
            i++;
        }
        if(i == w.length)
            System.out.println("Sorry not found!");
        else
            System.out.println(l[i] + " - " + w[i]);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *