Class 10 Half Yearly Computer Applications 2019

Section A (40 Marks)

Question 1
a) Differentiate between = and == operators.
= is an assignment operator whereas == is a relational operator.
b) Define an identifier. Give one example.
An identifier is a named storage location in memory that holds data temporarily.
Example:
int x = 5; //x is an identifier.
c) State one difference between linear search and binary search.
Linear search does not require sorted list of data whereas binary search requires sorted list of data.
d) Define bytecode.
Bytecode is the intermediate code which is platform independent. It is generated after compiling the Java source code.
e) State the escape sequence for:
i) horizontal tab
‘\t’
ii) carriage return
‘\n’

Question 2
a) State the range of values that can be stored in:
i) int data type.
–2,147,483,648 to 2,147,483,647
ii) double data type.
4.9e–324 to 1.8e+308
b) Write two advantages of using functions in a program.
i) Functions promote code reuse.
ii) Functions reduce code complexity.
c) Differentiate between call by value and call by reference.
In call by value, the changes made in formal parameters are not reflected in actual parameters. But in call by reference, changes made in formal parameters is reflected in actual parameters.
d) What is meant by direct initialization of an array? Give one example.
Direct initialization is when we initialize an array with the values already known to us.
Example: char v[] = {'a', 'e', 'i', 'o', 'u'};
e) What is encapsulation?
Wrapping up data and functions into a single unit is called encapsulation.

Question 3
a) State the value returned by:
i) Math.abs(Math.max(-21, -13))
13
ii) Math.ceil(12.65)
13.0
b) Rewrite the following without using if-else statement:

if(a > 0){
    if(a % 2 == 0)
        x = "POSITIVE EVEN";
    else
        x = "POSITIVE ODD";
}
else
    x = "NEGATIVE NUMBER";

String x = (a > 0)? ((a % 2 == 0)? "POSITIVE EVEN" : "POSITIVE ODD") : "NEGATIVE NUMBER";
c) Arrange the operators in order of higher to lower precedence:
i) >=
ii) %
iii) ++
iv) &&
++, %, >=, &&
d) Write one Java expression to express:
Convert to Java expression
Math.sqrt(a * Math.pow(b, 7)) / (5 * Math.pow(a, 3))
e) Predict the output:

int p = 10, r = 13, t = 8;
if((p != r) || (r > 2 * t))
    System.out.print(p + t);
else
    System.out.print(r + t);

18
f) Answer the given questions after analyzing the given code:

for(int x = 5; x < 20; x += 4)
    if(x % 4 == 0){
        System.out.print("**");
        break;
    }
    else if(x % 5 == 0){
        System.out.print(x);
        continue;
    }

i) Write the output of the above code.
5
ii) How many times does the loop run?
5
g) Given:
int a = 68;
State the output of:
i) Character.isLetter((char)(a + 2));
true
ii) Character.isLowerCase(a);
false
h) Given:
int s[] = {2, 4, 7, 91, 36};
State the output of:
i) System.out.println(s[1] + s[3]);
40
ii) System.out.println(s.length);
5
i) Given:
String a = "COMFORT", b = "PASSION";
Evaluate:
i) a.indexOf('O') + b.lastIndexOf('S')
4
ii) a.substring(0, 2) + b
COPASSION
j) State the output:
i) "POT".compareTo("PET")
10
ii) "PROP".equals("Prep")
false

Section B (60 Marks)

Question 4
Write a program to accept two words and check whether they are anagrams or not. An anagram is a word that is made with the combination of the letters present in the original word. For example, The first word is LISTEN and the second word is SILENT, which is formed with the combination of the letters present in the original word. So, LISTEN and SILENT are anagrams.

import java.io.*;
class Anagrams{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("First word: ");
        String first = br.readLine();
        System.out.print("Second word: ");
        String second = br.readLine();
        first = process(first);
        second = process(second);
        String s = sort(first);
        String t = sort(second);
        if(s.equals(t))
            System.out.println(first + " and " + second + " are anagrams.");
        else
            System.out.println(first + " and " + second + " are NOT anagrams.");
    }
    public static String process(String s){
        s = s.trim();
        if(s.indexOf(' ') > 0)
            s = s.substring(0, s.indexOf(' '));
        s = s.toUpperCase();
        return s;
    }
    public static String sort(String s){
        char a[] = new char[s.length()];
        for(int i = 0; i < s.length(); i++)
            a[i] = s.charAt(i);
        for(int i = 0; i < a.length; i++){
            for(int j = 0; j < a.length - 1 - i; j++){
                if(a[j] > a[j + 1]){
                    char temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
        String result = "";
        for(int i = 0; i < a.length; i++)
            result += a[i];
        return result;
    }
}

Question 5
Write a program to enter a letter. Change the case of the letter and display the new letter and its ASCII code.
For example:
INPUT: d
OUTPUT:
D
The ASCII code of D is 68.

import java.io.*;
class Letter{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter a letter: ");
        char alphabet = br.readLine().charAt(0);
        if(Character.isLetter(alphabet)){
            char upper = Character.toUpperCase(alphabet);
            int ascii = upper;
            System.out.println(upper);
            System.out.println("The ASCII code of " + upper + " is " + ascii);
        }
        else
            System.out.println("Please enter a letter.");
    }
}

Question 6
Write a program to allow the user to enter a positive integer and find and display the sum of the digits of the number using a user-defined method. The method prototype is:
public static int sum(int num)

import java.io.*;
class Find{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the number: ");
        int n = Math.abs(Integer.parseInt(br.readLine()));
        int result = sum(n);
        System.out.println("Sum of the digits: " + result);
    }
    public static int sum(int num){
        int s = 0;
        while(num > 0){
            s += num % 10;
            num /= 10;
        }
        return s;
    }
}

Question 7
Write a program to perform binary search on the following array, by allowing the user to enter the value to be searched: {"Fedora", "Linux Mint", "Mandriva", "OpenSUSE", "Ubuntu"}. If found, then display “Available” otherwise display “Unavailable”.

import java.io.*;
class Search{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String a[] = {"Fedora", "Linux Mint", "Mandriva", "OpenSUSE", "Ubuntu"};
        System.out.print("Name to be searched: ");
        String key = br.readLine();
        int low = 0;
        int high = a.length - 1;
        int mid = (low + high) / 2;
        while(low <= high){
            if(key.equalsIgnoreCase(a[mid]))
                break;
            else if(key.compareToIgnoreCase(a[mid]) < 0)
                high = mid - 1;
            else
                low = mid + 1;
            mid = (low + high) / 2;
        }
        if(low > high)
            System.out.println("Unavailable");
        else
            System.out.println("Available");
    }
}

Question 8
Write a program to input a number and check whether it is a pronic number or not. Use a function int pronic(int n) to accept a number. The function returns 1 if the number is pronic, otherwise it returns 0. [Hint: Pronic number is the number which is the product of two consecutive integers.] Example: 12 = 3 × 4 or 20 = 4 × 5.

import java.io.*;
class Pronic{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the number: ");
        int n = Math.abs(Integer.parseInt(br.readLine()));
        if(pronic(n) == 1)
            System.out.println(n + " is a Pronic Number.");
        else
            System.out.println(n + " is not a Pronic Number.");
    }
    public static int pronic(int n){
        int i = 1;
        while(i * (i + 1) < n)
            i++;
        if(i * (i + 1) == n)
            return 1;
        return 0;
    }
}

Question 9
Write a program in Java to store the numbers in a 4 × 4 matrix in a 2-dimensional array. Find the highest and lowest numbers of the matrix and print them.

import java.io.*;
class Find{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a[][] = new int[4][4];
        int highest = 0;
        int lowest = 0;
        System.out.println("Enter elements in the array:");
        for(int i = 0; i < 4; i++){
            for(int j = 0; j < 4; j++){
                a[i][j] = Integer.parseInt(br.readLine());
                if(i == 0 && j == 0){
                    highest = a[i][j];
                    lowest = a[i][j];
                }
                else{
                    if(highest < a[i][j])
                        highest = a[i][j];
                    if(lowest > a[i][j])
                        lowest = a[i][j];
                }
            }
        }
        System.out.println("Highest element: " + highest);
        System.out.println("Lowest element: " + lowest);
    }
}

Leave a Reply

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

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