ICSE Computer Applications Year 2017

Section A (40 Marks)

Attempt all questions

Question 1

(a) What is inheritance?

Inheritance is a mechanism that allows a class to derive properties from another class.

(b) Name the operators listed below:
(i) < (Less than operator)
(ii) ++ (Increment operator)
(iii) && (Logical AND operator)
(iv) ?: (Conditional or Ternary operator)

(c) State the number of bytes occupied by char and int data types.

char (2 bytes)
int (4 bytes)

(d) Write one difference between / and % operator.

/ operator gives us the quotient, whereas % operator gives us the remainder.

(e) String x[] = {"SAMSUNG", "NOKIA", "SONY", "MICROMAX", "BLACKBERRY"};
Give the output of the following statements:

(i) System.out.println(x[1]);
NOKIA

(ii) System.out.println(x[3].length());
8

Question 2

(a) Name the following:

(i) A keyword used to call a package in the program.
import

(ii) Any one reference data type.
String

(b) What are the two ways of invoking functions?

Call by value and call by reference.

(c) State the data type and value of res after the following is executed:
char ch = 't';
res = Character.toUpperCase(ch);

Data type is char.
Value is ‘T’.

(d) Give the output of the following program segment and also mention the number of times the loop is executed:
int a, b;

for(a = 6, b = 4; a <= 24; a = a + 6){
    if(a % b == 0)
        break;
}
System.out.println(a);

The output is 12.
The loop executes 2 times.

(e) Write the output:
char ch = 'F';
int m = ch;
m = m + 5;
System.out.println(m + " " + ch);

75 F

Question 3

(a) Write a Java expression for the following:
ax5 + bx3 + c

a * Math.pow(x, 5) + b * Math.pow(x, 3) + c

(b) What is the value of x1 if x = 5?

x1 = ++x - x++ + --x;
= 6 – 6 + 6
= 0 + 6
= 6

(c) Why is an object called an instance of a class?

An object is called an instance of a class because, from a class, multiple objects can be created.

(d) Convert following do-while loop into for loop.

int i = 1;
int d = 5;
do{
    d = d * 2;
    System.out.println(d);
    i++;
}while(i <= 5); int i, d;
for(i = 1, d = 5; i <= 5; i++){
    d = d * 2;
    System.out.println(d);
}

(e) Differentiate between constructor and function.

(i) Constructor has the same name as the class name, whereas functions have different names from their class names.
(ii) Constructors have no return type (not even void), whereas functions always have a return type.

(f) Write the output for the following:
String s = "Today is Test";
System.out.println(s.indexOf('T'));
System.out.println(s.substring(0, 7) + " " + "Holiday");

0
Today i Holiday

(g) What are the values stored in variables r1 and r2?

(i) double r1 = Math.abs(Math.min(-2.83, -5.83));
5.83

(ii) double r2 = Math.sqrt(Math.floor(16.3));
4.0

(h) Give the output of the following code:
String A = "26", B = "100";
String D = A + B + "200";
int x = Integer.parseInt(A);
int y = Integer.parseInt(B);
int d = x + y;
System.out.println("Result 1 = " + D);
System.out.println("Result 2 = " + d);

Result 1 = 26100200
Result 2 = 126

(i) Analyze the given program segment and answer the following questions:

for(int i = 3; i <= 4; i++){
    for(int j = 2; j <i; j++){
        System.out.print("");
    }
    System.out.println("WIN");
}

(i) How many times does the inner loop execute?

3 times
(ii) Write the output of the program segment.

WINWIN

(j) What is the difference between the Scanner class functions next() and nextLine()?

The next() function lets us input a string with a single word from the user, whereas nextLine() lets us input a string with multiple words from the user.

Section B (60 Marks)

Attempt any four questions from this Section.

Question 4

Define a class ElectricBill with the following specifications:
class: ElectricBill
Instance variables/data members:
String n: to store the name of the customer.
int units: to store the number of units consumed.
double bill: to store the amount to be paid.
Methods:
void accept(): to accept the name of the customer and number of units consumed.
void calculate(): to calculate the bill as per the following tariff:
Number of units – Rate per unit
First 100 units – Rs. 2.00
Next 200 units – Rs. 3.00
Above 300 units – Rs. 5.00
A surcharge of 2.5% charged if the number of units consumed is above 300 units.
void print(): To print the details as follows:
Name of the customer: ________
Number of units consumed: ________
Bill amount: ________
Write a main() method to create an object of the class and call the above member functions.

import java.io.*;
class ElectricBill{
    String n;
    int units;
    double bill;
    public void accept()throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Customer name: ");
        n = br.readLine();
        System.out.print("Units consumed: ");
        units = Integer.parseInt(br.readLine());
    }
    public void calculate(){
        if(units <= 100)
            bill = units * 2.0;
        else if(units >= 200 && units <= 300)
            bill = 200.0 + (units - 100) * 3.0;
        else{
            bill = 800.0 + (units - 300) * 5.0;
            bill = bill + 2.5 / 100 * bill;
        }
    }
    public void print(){
        System.out.println("Name of the customer: " + n);
        System.out.println("Number of units consumed: " + units);
        System.out.println("Bill amount: " + bill);
    }
    public static void main(String args[])
    throws IOException{
        ElectricBill obj = new ElectricBill();
        obj.accept();
        obj.calculate();
        obj.print();
    }
}

Question 5

Write a program to accept a number and check and display whether it is a spy number or not. A number is spy if the sum of its digits equals the product of its digits.
Example: Consider the number 1124.
Sum of the digits = 1 + 1 + 2 + 4 = 8.
Product of the digits = 1 × 1 × 2 × 4 = 8.

import java.io.*;
class Spy{
    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;
        int product = 1;
        for(int i = n; i != 0; i /= 10){
            int digit = i % 10;
            sum += digit;
            product *= digit;
        }
        if(sum == product)
            System.out.println("Spy number.");
        else
            System.out.println("Not spy number.");
    }
}

Question 6

Using switch statement, write a menu-driven program for the following:
(i) To find and display the sum of the series given below:
S = x1 – x2 + x3 – x4 + x5 … – x20, where x = 2.
(ii) To display the following series:
1 11 111 1111 11111
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. Series Sum");
        System.out.println("2. Series");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(br.readLine());
        switch(choice){
            case 1:
            int sum = 0;
            int x = 2;
            for(int i = 1; i <= 20; i++)
                sum += (int)Math.pow(x, i);
            System.out.println("Sum = " + sum);
            break;
            case 2:
            for(int i = 1; i <= 5; i++){
                int t = ((int)Math.pow(10, i) - 1) / 9;
                System.out.print(t + "\t");
            }
            break;
            default:
            System.out.println("Invalid choice!");
        }
    }
}

Question 7

Write a program to input integer elements into an array of size 20 and perform the following operations:
Display the largest number from the array.
Display the smallest number from the array.
Display sum of all the elements of the array.

import java.io.*;
class MyArray{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        int a[] = new int[20];
        int sum = 0;
        int largest = 0;
        int smallest = 0;
        System.out.println("Enter 20 elements:");
        for(int i = 0; i < 20; i++){
            a[i] = Integer.parseInt(br.readLine());
            if(i == 0)
                largest = smallest = a[0];
            if(largest < a[i])
                largest = a[i];
            if(smallest > a[i])
                smallest = a[i];
            sum += a[i];
        }
        System.out.println("Sum = " + sum);
        System.out.println("Largest element = " + largest);
        System.out.println("Smallest = " + smallest);
    }
}

Question 8

Design a class to overload a function check() as follows:
void check(String str, char ch) – to find and print the frequency of a character in a string.
Example:
INPUT:
str = “success”
ch = ‘s’
OUTPUT:
Number of s present is = 3.
void check(String s1) – to display only vowels from s1, after converting it to lowercase.
Example:
INPUT:
s1 = “computer”
OUTPUT:
o u e

class Overload{
    public static void check(String str, char ch){
        int count = 0;
        for(int i = 0; i < str.length(); i++){
            if(ch == str.charAt(i))
                count++;
        }
        System.out.println("Frequency = " + count);
    }
    public static void check(String s1){
        s1 = s1.toLowerCase();
        for(int i = 0; i < s1.length(); i++){
            char ch = s1.charAt(i);
            switch(ch){
                case 'a':
                case 'e':
                case 'i':
                case 'o':
                case 'u':
                System.out.print(ch + "\t");
            }
        }
    }
}

Question 9

Write a program to input forty words in an array. Arrange these words in descending order of alphabets, using selection sort technique. Print the sorted array.

import java.io.*;
class Selection{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        String words[] = new String[40];
        System.out.println("Enter 40 words: ");
        for(int i = 0; i < words.length; i++)
            words[i] = br.readLine();
        for(int i = 0; i < words.length; i++){
            String large = words[i];
            int pos = i;
            for(int j = i + 1; j < words.length; j++){
                if(large.compareTo(words[j]) < 0){
                    large = words[j];
                    pos = j;
                }
            }
            String temp = words[i];
            words[i] = large;
            words[pos] = temp;
        }
        for(int i = 0; i < words.length; i++)
            System.out.print(words[i] + "\t");
    }
}

Leave a Reply

%d bloggers like this: