Class 10 Computers Half Yearly 2018

Section A [40 Marks]

Q1. a) State the Java concept that is implemented through:
(i) Wrapping of data and functions of a class together as a unit. (Encapsulation)
(ii) Process of using a function for more than one purpose. (Polymorphism or Function overloading)
b) Write one advantage and one disadvantage of using the binary search algorithm.
Advantage: It is comparatively faster than linear search.
Disadvantage: It needs the data list to be sorted.
c) Write any two features of a constructor.
1. It has the same name as the class name in which it is defined.
2. It does not have a return type. Not even void.
d) What is the data type that the following library functions return?
(i) trim() – String
(ii) startsWith() – boolean
e) Name the primitive data types in Java that is:
(i) The default data type for fractional values. (double)
(ii) The default data type for integers. (int)

Q2. a) Write a Java statement to create an object android of class os.
os android = new os();
b) Rewrite the following program segment using the if-else statement:
bonus = (sales >= 50000)? sales * 4.0 / 100 : 0;
if(sales >= 50000)
bonus = sales * 4.0 / 100;
else
bonus = 0;
c) Write a Java expression for  √((a + b) / 2).
Math.sqrt((a + b) / 2)
d) If char ch[] = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’}, then what are the values of x and y?
int x = ch[0] – ch[1];
-1
int y = ch[4] – ch.length;
96
e) Name the methods of Scanner class that:
(i) is used to input a double type data from the standard input stream.
nextDouble()
(ii) is used to input a String data from the standard input stream.
nextLine()

Q3. a) What is the final value of count when the iteration process given below executes?
int count = 0;
for(int i = 0; i <= 5; i+= 2)
for(int j = 0; j <= 5; j++)
count++;
18
b) How many times will the following loop execute? What value will be returned?
int a = 1, b = 10;
do{
b -= a;
a++;
}while(b > 0);
The loop executes 4 times.
a becomes 5.
b becomes 0.
c) State the values stored in the variables s1 and s2:
String a = “String”;
String b = “Manipulation”;
String s1 = b.substring(0, 3);
String s2 = a.substring(2);
s1 = “Man”
s2 = “ring”
d) What are the types of casting shown by the following examples?
(i) char ch = ‘A’;
int i = ch;
Implicit type casting.
(ii) int x = 65;
char ch = (char)x;
Explicit type casting.
e) Name the package that contains:
(i) Math class – java.lang
(ii) BufferedReader class – java.io
f) Name the two keywords that are optional while using the switch statement.
break and default keywords.
g) Give the size in bytes for the following data types:
(i) short – 2 bytes
(ii) char – 2 bytes
h) What are the final values stored in variables x and y below?
double p = -6.5;
double q = 15.8;
double x = Math.floor(p);
double y = Math.rint(q);
x = -7.0
y = 16.0
i) Name the type of error (syntax/runtime/logical) in each case given below:
(i) double p; q;
Syntax error.
(ii) Math.sqrt(-16)
Runtime error.
j) Write the function prototype for the following: A function named arrange which takes a string argument and doesn’t return any value.
void arrange(String s)

Section B [60 Marks]

Q4. 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 the digits is equal to the original number.
Example: 153 = 13 + 53 + 33.

import java.io.*;
class Armstrong{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("N = ");
        int n = Integer.parseInt(br.readLine());
        int sum = 0;
        for(int i = n; i > 0; i /= 10){
            int d = i % 10;
            sum += d * d * d;
        }
        if(n == sum)
            System.out.println("Armstrong number.");
        else
            System.out.println("Not an Armstrong number.");
    }
}

Q5. Write a program to allow the user to store N integers into a one-dimensional array of integer type, and by creating a user-defined method, reverse the array.
Note: You are to reverse the array without using a second array in the program.

import java.io.*;
class Reverse{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("N = ");
        int n = Integer.parseInt(br.readLine());
        int a[] = new int[n];
        System.out.println("Enter array elements:");
        for(int i = 0; i < a.length; i++)
            a[i] = Integer.parseInt(br.readLine());
        reverse(a);
        System.out.println("Reversed array contents:");
        for(int i = 0; i < a.length; i++)
            System.out.print(a[i] + "\t");
    }
    public static void reverse(int a[]){
        int i = 0;
        int j = a.length - 1;
        while(i < j){
            int temp = a[i];
            a[i] = a[j];
            a[j] = temp;
            i++;
            j--;
        }
    }
}

Q6. Write a program to input and store all roll numbers, names, and marks in three subjects for N number of students in five single-dimensional arrays and display the remark based on average marks as given below:

Average Marks                Remark
85 - 100 EXCELLENT
75 - 84 DISTINCTION
60 - 74 FIRST CLASS
40 - 59 PASS
Less than 40 POOR

Average marks = total marks / 3

import java.io.*;
class Student{
    public static void main(String args[])
    throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("N = ");
        int n = Integer.parseInt(br.readLine());
        int roll[] = new int[n];
        String name[] = new String[n];
        int m1[] = new int[n];
        int m2[] = new int[n];
        int m3[] = new int[n];
        System.out.println("Enter details:");
        for(int i = 0; i < n; i++){
            System.out.print("Roll: ");
            roll[i] = Integer.parseInt(br.readLine());
            System.out.print("Name: ");
            name[i] = br.readLine();
            System.out.print("Marks 1: ");
            m1[i] = Integer.parseInt(br.readLine());
            System.out.print("Marks 2: ");
            m2[i] = Integer.parseInt(br.readLine());
            System.out.print("Marks 3: ");
            m3[i] = Integer.parseInt(br.readLine());
            double avg = (m1[i] + m2[i] + m3[i]) / 3;
            if(avg >= 85 && avg <= 100)
                System.out.println("EXCELLENT");
            else if(avg >= 75 && avg < 85)
                System.out.println("DISTINCTION");
            else if(avg >= 60 && avg < 75)
                System.out.println("FIRST CLASS");
            else if(avg >= 40 && avg < 60)
                System.out.println("PASS");
            else if(avg < 40)
                System.out.println("POOR");
        }
    }
}

Q7. Design a class to overload a function area() as follows:
(i) double area(double a, double b, double c) with three double arguments that returns the area of a scalene triangle using the formula:
area = (s(s – a)(s – b)(s – c))
where s = (a + b + c) / 2
(ii) double area(int a, int b, int height) with three integer arguments that returns the area of a trapezium using the formula:
area = 1 / 2 × height (a + b)
(iii) double area(double diagonal1, double diagonal2) with two double arguments that returns the area of a rhombus using the formula:
area = 1 / 2 × (diagonal1 × diagonal2)

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

Q8. Write a program that uses class members and instance members to maintain different bank accounts. Bank accounts can be of two types:
1. In which the initial amount is Rs. 25,000.
2. In which the initial amount is Rs. 45,000.
Use a class-field to keep count of the number of accounts opened. The common rate of interest is 7.1% per annum. The interest is to be calculated for 1 year and then the balance is to be updated.

import java.io.*;
class Bank{
    static int count = 0;
    int type;
    double balance;
    double interest;
    public Bank(int t){
        balance = 0.0;
        interest = 0.0;
        type = t;
        if(type == 1)
            balance = 25000;
        else if(type == 2)
            balance = 45000;
        System.out.println("Initial balance: " + balance);
        count++;
        System.out.println("Number of accounts: " + count);
    }
    public void update(){
        interest = balance * 7.1 / 100;
        balance += interest;
        System.out.println("New balance: " + balance);
    }
    public static void main(String args[]){
        Bank a1 = new Bank(1);
        a1.update();
        Bank a2 = new Bank(2);
        a2.update();
    }
}

Q9. Write a program to perform binary search on a list of integers given below, to search for an element input by the user. If it is found, then display the element along with its position, otherwise display the message “Searched element not found!”.
5, 7, 9, 11, 15, 20, 30, 45, 89, 97

import java.io.*;
class BinarySearch{
    public static void main(String args[])
    throws IOException{
        int a[] = {5, 7, 9, 11, 15, 20, 30, 45, 89, 97};
        int low = 0;
        int high = a.length - 1;
        int mid = 0;
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("Key: ");
        int key = Integer.parseInt(br.readLine());
        while(low <= high){
            mid = (low + high) / 2;
            if(key == a[mid])
                break;
            else if(key < a[mid])
                high = mid - 1;
            else 
                low = mid + 1;
        }
        if(low > high)
            System.out.println("Searched element not found.");
        else
            System.out.println(key + " found at position " + (mid + 1));
    }
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.