Class 9 Half Yearly Computer Applications 2019

Section A (40 Marks)

Question 1
a) Name any two object-oriented programming principles.
i) Polymorphism
ii) Encapsulation
b) Name any two Java Application Programming Interface packages.
java.util
java.io
c) What is message passing?
Objects communicate with one another using methods by passing required arguments. This is called message passing.
d) Write any two differences between a class and an object.
A class is a blueprint of an object.
An object is an instance of a class.
e) Give one example each of a primitive data type and a composite data type.
int is a primitive data type.
String is a composite data type.

Question 2
a) int vinc = 'U'; What is the value of vinc?
85
b) 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.
c) Name the type of error in each case given below:
i) Division by a variable that contains a value zero.
Runtime error
ii) Missing semicolon.
Syntax error
d) Give two differences between switch statement and if-else statement.
switch-case is a multi-directional statement whereas if-else is a bi-directional statement.
switch-case only checks for equality whereas if-else can check for other comparisons also.
e) What is an infinite loop? Write an infinite loop statement.
An infinite loop is a loop that never terminates.
Example:

while(true)
    System.out.println("Hello");

Question 3
a) Rewrite the following code snippet by using while loop and give the output:

int p = 0;
for(i = 1; i <= 5; i++){
    System.out.print(i + " ");
    p = p + 1;
}
System.out.println(p);
int p = 0;
int i = 1;
while(i <= 5){
    System.out.print(i + " ");
    p = p + 1;
    i++;
}
System.out.println(p);

OUTPUT:
1 2 3 4 5 5
b) What are the two jump statements used in Java?
break and continue.
c) Write the Java expression for:
Class 9 Half Yearly 2019 Java Expression
Math.pow(a + b, n) / (Math.sqrt(3) + b)
d) What will be the output of the following code?
int m = 2;
int n = 15;
for(int i = 1; i < 5; i++);
m++;
--n;
System.out.println("m = " + m);
System.out.println("n = " + n);

m = 3
n = 14
e) What is ‘unfixed’ iterative loop?
When we are not sure how many times the loop is going to execute, we call it an ‘unfixed’ iterative loop.
f) Rewrite the following program segment using if-else statement instead of ternary operator:
String grade = (marks >= 90)? "A" : (marks >= 80)? "B" : "C";

String grade = "";
if(marks >= 90)
    grade = "A";
else if(marks >= 80)
    grade = "B";
else
    grade = "C";

g) What are the types of casting in the following:
double x = 15.2;
int a = 12;

i) int y = (int)x;
Explicit Type Casting
ii) long b = a;
Implicit Type Casting
h) Rewrite the code snippet using ternary operator:
if(basic > 100000)
tax =0;
else
tax = 1000;

Using ternary operator:
tax = (basic > 100000)? 0 : 1000;
i) Write at least two differences between entry-controlled loop and exit-controlled loop.
Entry-controlled loops check the condition before executing the loop body. Examples include for loop and while loop. Exit-controlled loop checks the condition after executing the loop body. Example: do-while loop.
j) Analyze the following program segment and determine how many times the loop will be executed and what will be the output of the program segment.

int k = 1, i = 2;
while(++i < 6)
    k *= i;
System.out.println(k);

The loop will execute 3 times.
Output will be 60.

Section B (60 Marks)

Question 4
a) Write a program to enter a number and check whether the number is an Armstrong number or not. A number is said to be Armstrong, if the sum of the cubes of its digits is equal to the original number.
Example: 153 = 13 + 53 + 33
b) Write the variable list for the above program.

Refer to Question 4 of Class 10 Half Yearly 2018

Question 5
a) Write a menu-driven program to convert Fahrenheit to Celsius and Celsius to Fahrenheit.
b) Write the variable list for the above program.

Refer to Question 15 of Class 9 Conditional Statements in Java Chapter

Question 6
a) Write a program to calculate and print the sum of each of the following series:
i) Sum = 2 – 4 + 6 – 8 + … – 20.
ii) Sum = x/2 + x/5 + x/8 + x/11 + … + x/20, where the value of x is to be input by the user.
b) Write the variable list for the above program.

import java.io.*;
class Series{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("2 - 4 + 6 - 8 + ... - 20");
        System.out.println("x/2 + x/5 + x/8 + x/11 + ... + x/20");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(br.readLine());
        switch(choice){
            case 1:
            int sum = 0;
            int sign = 1;
            for(int i = 2; i <= 20; i++){
                sum += i * sign;
                sign = -sign;
            }
            System.out.println("Sum = " + sum);
            break;
            case 2:
            double result = 0.0;
            System.out.print("x = ");
            int x = Integer.parseInt(br.readLine());
            for(double i = 2; i <= 20; i += 3)
                result += x / i;
            System.out.println("Sum = " + result);
            break;
            default:
            System.out.println("Invalid choice!");
        }
    }
}

Question 7
CESC, an electric supply company, calculates electricity bill for their consumers according to the units consumed (per month) as per the given tariff:

Units Consumed Charges
Up to 100 units Rs. 1.50/unit
More than 100 units & up to 300 units Rs. 2.30/unit
More than 300 units & up to 500 units Rs. 2.80/unit
More than 500 units Rs. 3.50/unit

Write a program to input name of the consumer, consumer number, month and units consumed. Calculate and display the electricity bill with all the details.
b) Write the variable list for the above program.

import java.io.*;
class Electricity{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Consumer name: ");
        String name = br.readLine();
        System.out.print("Consumer number: ");
        int num = Integer.parseInt(br.readLine());
        System.out.print("Month: ");
        String month = br.readLine();
        System.out.print("Number of units consumed: ");
        int units = Integer.parseInt(br.readLine());
        double bill = 0.0;
        if(units <= 100)
            bill = units * 1.50;
        else if(units > 100 && units <= 300)
            bill = units * 2.30;
        else if(units > 300 && units <= 500)
            bill = units * 2.80;
        else
            bill = units * 3.50;
        System.out.println("Consumer name: " + name);
        System.out.println("Consumer number: " + num);
        System.out.println("Month: " + month);
        System.out.println("Number of units: " + units);
        System.out.println("Electricity bill: " + bill);
    }
}

Question 8
A cloth showroom has announced festival discounts and the gifts on the purchase of items based on the total cost as given below:

Total Cost Discount Gift
Up to Rs. 2000 5% Calculator
Rs. 2001 to Rs. 5000 10% School Bag
Rs. 5001 to Rs. 10,000 15% Wall Clock
Above Rs. 10,000 20% Wrist Watch

Write a program to input the total cost. Compute and display the amount to be paid by the customer along with the gift.
b) Write the variable list for the above program.

import java.io.*;
class Showroom{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Total cost: ");
        double cost = Double.parseDouble(br.readLine());
        String gift = "";
        double d = 0.0;
        double amt = 0.0;
        if(cost <= 2000){
            d = 5.0;
            gift = "Calculator";
        }
        else if(cost > 2000 && cost <= 5000){
            d = 10.0;
            gift = "School Bag";
        }
        else if(cost > 5000 && cost <= 10000){
            d = 15.0;
            gift = "Wall Clock";
        }
        else{
            d = 20.0;
            gift = "Wrist Watch";
        }
        d = d / 100 * cost;
        amt = cost - d;
        System.out.println("Amount: " + amt);
        System.out.println("Gift: " + gift);
    }
}

Question 9
a) Using switch statement, write a menu-driven program to:
i) To check and display whether a number input by the user is a composite number or not. A number is said to be composite if it has at least one factor excluding 1 and itself.
Example: 4, 6, 8, 9, …
ii) To find the smallest digit of an integer that is input.
Example:
INPUT: 6524
OUTPUT: Smallest digit is 2.
For an incorrect choice, an appropriate error message should be displayed.
b) Write the variable list for the above program.

import java.io.*;
class Menu{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("1. Composite Number");
        System.out.println("2. Smallest Digit");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(br.readLine());
        switch(choice){
            case 1:
            System.out.print("Enter the number: ");
            int num = Integer.parseInt(br.readLine());
            int f = 0;
            for(int i = 1; i <= num; i++){
                if(num % i == 0)
                    f++;
            }
            if(f > 2)
                System.out.println(num + " is composite.");
            else
                System.out.println(num + " is not composite.");
            break;
            case 2:
            System.out.print("Enter the number: ");
            num = Integer.parseInt(br.readLine());
            int small = num % 10;
            while(num != 0){
                if(small > num % 10)
                    small = num % 10;
                num /= 10;
            }
            System.out.println("Smallest digit: " + small);
            break;
            default:
            System.out.println("Invalid choice!");
        }
    }
}

1 thought on “Class 9 Half Yearly Computer Applications 2019”

  1. Sir,a program is given which states that:
    WAP in Java to accept a paragraph containing n no of sentences where n>=1&& n<4. The words are to be separated with a single blank space and are in upper case.A sentence may be terminated either with a '.', '?' ,'!' only. Any other character may be ignored. Perform the following operations:
    1) Accept the no.of sentences. If the number of sentences exceeds the limit, an appropriate error message must be displayed.
    2)Find the number of words in the whole paragraph.
    3) Display the words in ascending order of their frequency. Words with same frequency may appear in any order.
    Eg:
    Sample Input:
    Enter number of sentences:1
    Enter sentences:TO BE OR NOT TO BE.
    Sample Output:
    Total number of words:6
    WORD FREQUENCY
    OR 1
    NOT 1
    TO 2
    BE 2

Leave a Reply

%d bloggers like this: