Categories
Class 10

Computer Applications Specimen Paper 2017

Section A

Question 1

a) Name any four tokens of Java.
Identifiers, Keywords, Literals, Operators.
b) Give the difference between actual parameter and formal parameter.
The parameters passed during function call are actual parameters. The parameters used during function definition are formal parameters.
c) What is an identifier?
Identifiers are the variables that are the named storage locations to store values temporarily in a program.
d) Write an expression in Java for .
Math.cos(x) + Math.pow(a * a + b * b)
e) What is the result produced by 2 – 10 * 3 + 100 / 11? Show the steps.
2 – 10 * 3 + 100 / 11
= 2 – 30 + 100 / 11
= 2 – 30 + 9
= -28 + 9
= -19.

Question 2

a) What is the difference between local variable and instance variable?
The variables declared inside a function are called local variables. The variables declared inside a class so that each object gets its own copy of data members are called instance variables.
b) int x = 20, y = 10, z;
z = ++x * (y–) – y?
Show the steps.
z = 21 * 10 – 9
z = 210 – 9
z = 201.
c) What is the purpose of default in a switch?
The default case provides a case that gets executed when no other case is matched.
d) Give the difference between linear search and binary search.
Linear search can be applied on both sorted and unsorted lists. Binary search can only be applied on sorted lists.
e) What will be the output of the following code?
float x = 7.87F;
System.out.println(Math.ceil(x));
System.out.println(Math.floor(x));
8.0
7.0

Question 3

a) State the difference between if-else-if ladder and switch case.
The if-else-if ladder is a bi-directional conditional statement. It can do a variety of comparisons. The switch case is a multi-branching conditional statement. It can only compare for equality of values.
b) Explain the concept of constructor with an example.
A constructor is a method in class with the same name as the class name. It is used to create objects and initialize them with legal initial values.
Example:

class Rectangle{
    int length;
    int breadth;
    public Rectangle(int a, int b){
        length = a;
        breadth = b;
    }
}

c) What will be the output of the following program segments?
i) String s = "application";
int p = s.indexOf('a');
System.out.println(p);
System.out.println(p + s);
0
0application
ii) String st = "PROGRAM";
System.out.println(st.indexOf(st.charAt(4)));
1
iii) int a = 0;
if(a > 0 && a < 20)
a++;
else
a--;
System.out.println(a);
-1
iv) int a = 5, b = 2, c;
if(a > b || a != b)
System.out.print(c + " " + a + " " + b);
7 6 1
v) int i = 1;
while(i++ <= 1){
i++;
System.out.print(i + " ");
}
System.out.print(i);
3 4
d) Differentiate between isUpperCase(char) and toUpperCase(char).
The isUpperCase(char) returns a boolean value whereas the toUpperCase(char) returns a char data type value.
e) What is the difference between a constructor and a member function of a class?
A constructor doesn’t have a return type (not even void). A function always has a return type.
f) What is the difference between a static member function and a member function which is not static?
A static member function is invoked without an object. A non-static function is invoked through an object.

Section B

Question 4

Define a class TaxiMeter having the following description:
Data members/instance variables:
int taxiNo: to store the taxi number.
String name: to store the passenger’s name.
int km: to store the number of kilometers traveled.
double bill: to store the total bill amount.
Member functions:
TaxiMeter(): constructor to initialize taxiNo to 0, name to “” and km to 0.
input(): to store taxi number, name, number of kilometers.
calculate(): to calculate bill for a customer according to given conditions:

Kilometers Traveled (km) Rate per km
≤ 1 km Rs. 25
1 < km ≤ 6 Rs. 10
6 < km ≤ 12 Rs. 15
12 < km ≤ 18 Rs. 20
> 18 Rs. 25

display(): to display the details in the following format:

Taxi No. Name Kilometers Traveled Bill Amount

Create an object in the main() method and call all the above methods in it.

import java.io.*;
class TaxiMeter{
    int taxiNo;
    String name;
    int km;
    double bill;
    public TaxiMeter(){
        taxiNo = 0;
        name = "";
        km = 0;
        bill = 0.0;
    }
    public void input()throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Taxi Number: ");
        taxiNo = Integer.parseInt(br.readLine());
        System.out.print("Name: ");
        name = br.readLine();
        System.out.print("Number of km: ");
        km = Integer.parseInt(br.readLine());
    }
    public void calculate(){
        if(km <= 1)
            bill = 25 * km;
        else if(km > 1 && km <= 6)
            bill = 10 * km;
        else if(km > 6 && km <= 12)
            bill = 15 * km;
        else if(km > 12 && km <= 18)
            bill = 20 * km;
        else
            bill = 25 * km;
    }
    public void display(){
        System.out.println("TaxiNo.\tName\tKilometers Traveled\tBill Amount");
        System.out.println(taxiNo + "\t" + name + "\t" + km + "\t" + bill);
    }
    public static void main(String args[])throws IOException{
        TaxiMeter obj = new TaxiMeter();
        obj.input();
        obj.calculate();
        obj.display();
    }
}

Question 5

Write a menu-driven program to find the sum of the following series depending on the user choosing 1 or 2:
1. s = 1/4 + 1/8 + 1/12 + … up to N terms.
2. s = 1/1! – 2/2! + 3/3! – … up to N terms.
where ! stands for factorial of the number and the factorial value of a number is the product of all integers from 1 to that number, e.g. 5! = 1 × 2 × 3 × 4 × 5 = 120. Use switch-case.

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. 1/4 + 1/8 + 1/12 + ... N terms");
        System.out.println("2. 1/1! - 2/2! + 3/3! - ... N terms");
        System.out.print("Enter your choice: ");
        int choice = Integer.parseInt(br.readLine());
        switch(choice){
            case 1:
            System.out.print("N = ");
            int n = Integer.parseInt(br.readLine());
            double sum = 0.0;
            for(int i = 1; i <= n; i++)
                sum += 1.0 / (4 * i);
            System.out.println("Sum = " + sum);
            break;
            case 2:
            System.out.print("N = ");
            n = Integer.parseInt(br.readLine());
            sum = 0.0;
            int sign = 1;
            for(int i = 1; i <= n; i++){
                int f = 1;
                for(int j = 1; j <= i; j++)
                    f *= j;
                sum += sign * (double)i / f;
                if(sign == 1)
                    sign = -1;
                else
                    sign = 1;
            }
            System.out.println("Sum = " + sum);
            break;
            default:
            System.out.println("Invalid choice!");
        }
    }
}

Question 6

Write a program to accept a sentence and print only the first letter of each word of the sentence in capital letters separated by a full stop.
Example:
INPUT: This is a cat.
OUTPUT: T.I.A.C.

import java.io.*;
class Sentence{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the sentence: ");
        String s = br.readLine();
        s = s.trim();
        s = s.toUpperCase();
        String t = "";
        for(int i = 0; i < s.length(); i++){
            if(i == 0 || s.charAt(i - 1) == ' ')
                t += s.charAt(i) + ".";
        }
        System.out.println(t);
    }
}

Question 7

Write a program to create an array to store 10 integers and print the largest integer and the smallest integer in that array.

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[10];
        System.out.println("Enter " + a.length + " numbers:");
        for(int i = 0; i < a.length; i++)
            a[i] = Integer.parseInt(br.readLine());
        int small = a[0];
        int large = a[0];
        for(int i = 1; i < a.length; i++){ if(small > a[i])
                small = a[i];
            if(large < a[i])
                large = a[i];
        }
        System.out.println("Smallest number: " + small);
        System.out.println("Largest number: " + large);
    }
}

Question 8

Write a program to calculate the sum of all the prime numbers between the range of 1 and 100.

class Prime{
    public static void main(String args[]){
        int sum = 0;
        for(int i = 1; i <= 100; i++){
            int f = 0;
            for(int j = 1; j <= i; j++){
                if(i % j == 0)
                    f++;
            }
            if(f == 2)
                sum += i;
        }
        System.out.println("Sum = " + sum);
    }
}

Question 9

Write a program to store 10 names in an array. Arrange these in alphabetical order by sorting. Print the sorted list. Take single word names, all in capital letters, e.g. SAMSON, AJAY, LUCY, etc.

import java.io.*;
class Menu{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String a[] = new String[10];
        System.out.println("Enter " + a.length + " numbers:");
        for(int i = 0; i < a.length; i++){
            a[i] = br.readLine().toUpperCase();
            a[i] = a[i].trim();
            if(a[i].indexOf(' ') > 0)
                a[i] = a[i].substring(0, a[i].indexOf(' '));
        }
        for(int i = 0; i < a.length; i++){
            for(int j = 0; j < a.length - 1 - i; j++){
                if(a[j].compareTo(a[j + 1]) > 0){
                    String temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
        System.out.println("Sorted List of names:");
        for(int i = 0; i < a.length; i++)
            System.out.print(a[i] + "\t");
    }
}

By admin

I am a Computer Science teacher in one of the renowned schools in India. I teach Java programming language to my students, and I maintain a website happycompiler.com

Leave a Reply

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