Previous Questions and Answers: 2019

COMPUTER APPLICATIONS

SECTION A (40 Marks)
Attempt all questions

Question 1

(a) Name any two principles of Objetc-oriented Programming. [2]

1) Abstraction
2) Encapsulationm

(b) Write a difference between unary and binary operator. [2]

Operator that operates on one operand is known as unary operator. E.g.: ++, --
Operator that operates on two operands is known as binary operator. E.g.: = + etc

(c) Name the keyword which:
(i) indicates that a method has no return type.
(ii) makes the variable as class variable [2]

(i) void
(ii) static

(d) Write memory capacity of short and float data type in bytes. [2]

short: 2 bytes
float: 4 bytes

(e) Identify and name the following tokens: [2]

(i) public
(ii) ‘a’
(iii) = =
(iv) { }

public: Keyword
‘a’: Literal
== operator
{ } Punctuator

Question 2

(a) Differentiate beween if else if and switch case statements. [2]

switch
1) Only equality checking can be done.
2) Only integer and character values can be checked.
3) Only one variable can be used to test.

if
1) Relational or logical expressions can be used.
2) Any type of value can be tested including floating point or string or boolean.
3) Various variables can be used for condition checking.

(b) Give output:
String P=”20”, Q=”19”;
int a=Integer.parseInt(P);
int b =Integer.parseInt(P);
System.out.println(a+""+b); [2]

2019

(c) What are the various types errors in Java? [2]

1) Syntax errors. E.g.: Class Sum
2) Run time error. E.g.: System.out.println(5/0);
3) Logical error: remainder=a/b;

(d) State the data type and value of res after the following is executed:
char ch=’9’;
res=Character.isDigit(ch); [2]

Data type: boolean
Value: true

(e) What is the difference between linear search and binary search? [2]

Linear Search: Search can be done in srted or unsorted elements. It searches data sequentially from index 0 to index n-1. So the searching speed is lower than binary search.
Binary search: The search can be done only in sorted elements. Its first search is in the middle of the array. If the serch element is found then the search is finished. If it is not there and it is greater than the middle element then the next search is the middle of the right half of the array. If the search element is lower than the middle element is the search is in the middle of of left half of the array. Such a way the search is done.

Question 3

(a) Write a Java expression for the following:
|x2+2xy| [2]

Math.abs(x*x+2*x*y)

(b) Write return type of the following functions:
(i) startsWith()
(ii) random() [2]

(i) boolean
(ii) double

(c) If the basic=1500, what will be the value of tax after the following is executed?
tax=basic>1200?200:100; [2]

200

(d) Give output of the following and mention how may times the loop will execute.

int i;
for(i=5;i>=1;i--)
{
if(i%2==1)
continue;
System.out.print(i+" ");
}

[2]

4
2
Loop executes 5 times

(e) State a difference between call by value and call by reference. [2]

(1) When a method is called by passing primitive values then it is call by value. When a method is called by passing reference values like objects then it is known as call by reference.
(2) In call by value copies of values are passed to method definition and these copies are received by formal parameters. Process are done with these copies and any change that takes place reflects in the copy only and original data remain unchanged.

(f) Give output:
Math.sqrt(Math.max(9,16)); [2]

4

(g) Write the output of the following:
String s1=”phoenix”, s2=”island”;
System.out.println(s1.substring(0).concat(s2.substring(2)));
System.out.println(s2.toUpperCase()); [2]

phoenixland
ISLAND

(h) Evaluate the following expression if the value of x=2,y=3 and z=1.:
v=x+--z+y+++y [2]

9

(j) What is meant by a package? Give an example. [2]

Package is group of related classes and interfaces. E.g.: java.lang is a package

SECTION B (60 Marks)

Attempt any four questions

Question 4

Design a class ShowRoom with following description:

Instance variables/data members:
String name : To store the name of the customer
long mobno : To store customer’s mobile number
double cost : To store cost of the items purchased
double dis : To store discount
double amount : To store amount to be paid after discount

Member methods:
ShowRoom - default constructor to initialize data members
void input() - To input customer name, mobile number and cost.
void calculate() - To calculate dicount on cost of purchased items based on following citeria:

(extra amount to be added in the amount as follows)

CostDiscount Rate
Less than equal to Rs. 100005%
More than 10000 and less than equal to 2000010%
More than 20000 and less than equal to 3500015%
More than 3500020%

void display() - To display customer name, mobile number, amount to be paid after discount
Write a main method also to create an object of the class and call the above member methods. [15]

import java.io.*;
public class ShowRoom
{
String name;
long mobno;
double cost;
double dis;
double amount;
public ShowRoom()
{
name=””;
mobno=0L;
cost=0.0;
dis=0.0;
amount=0.0;
}
public void input()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name, moile number and cost");
name= br.readLine();
mobno=Long.parseLong(br.readLine());
cost=Double.parseDouble(br.readLine());
}
public void calculat()
{
if(cost=10000)
amount=cost-cost*5/100.0;
else if(cost=10000)
amount=cost-cost*10/100.0;
else if(cost=10000)
amount=cost-cost*15/100.0;
else
amount=cost-cost*20/100.0;
}
public void display()
{
System.out.println("Name: "+name);
System.out.println("Moile number: "+mobno);
System.out.println("Amount to be paid: "+amount);
}
public static void main()throws IOException
{
ShowRoom ob=new ShowRoom();
ob.input();
ob.calculate();
ob.display();
}
}

Question 5

Using switch statement write a menu driven program to do the following:
(a) To generate and print Letters from A to Z and their Unicode

LettersUnicode
A65
B66
....
Z90

b) Display the following pattern using iteration statement:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 [15]

import java.io.*;
public class MenuDriven
{
public static void unicode()
{
System.out.println("Letters\tUnicode");
for(char i='A';i<='Z';i++)
System.out.println(i+"\t"+(int)i);
}
public static void pattern()
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
System.out.print(j+"\t");
System.out.println();
}
}
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch;
do
{
System.out.println("\t\t\t\tMenu");
System.out.println("1 Unicode");
System.out.println("2 Pattern");
System.out.println("0 Exit");
System.out.println("enter your choice");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1: sumOfSeries();
break;
case 2: printSeries();
break;
case 0: break;
default: System.out.println("Wrong choice. Tryt again!");
}
}
while(ch!=0);
}
}

Question 6

Write a program to input 15 integers in an array and sort them in ascending order using bubble sort. [15]

import java.io.*;
public class Sort
{
public static void main()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[]=new int[10];
int i;
for(i=0;i<15;i++)
{
System.out.println("Enter number ");
a[i]=Integer.parseInt(br.readLine());
}
//Sorting Process
int j,temp;
for(i=0;i<15-1;i++)
{
for(j=0;j<10-1-i;j++)
{
if(a[ j]>a[ j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println("Sorted Array");
for(i=0;i<15;i++)
System.out.print( a[i]+"\t");
}
}

Question 7

Design a class to overload a function series() as follows

(a) void series(int x,int n) – To display the sum of the series given below:
x1 + x2 + x3 + …. xn terms

(b) void series(int p) – To display the following series:
0, 7, 26, 63 … p terms

(c) void series() – To display sum of series:
1/2 + 1/3 + 1/4 … 1/10 [15]

public class Overload
{
public static void series(int x,int n)
{
double s=0;
for(int i=1;i<=n;i++)
s+=Math.pow(x,i);
System.out.println("Sum of first series: "+s);
}
public static void series(int p)
{
for(int i=1;i<=p;i++)
System.out.print((i*i*i-1)+", ");
}
public static void series()
{
double s=0;
for(int i=2;i<=10;i++)
s+=1.0/i;
System.out.println("Sum of third series: "+s);
}
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter value for x and n");
int x=Integer.parseInt(br.readLine());
int n=Integer.parseInt(br.readLine());
System.out.println("Enter value for pr");
int p=Integer.parseInt(br.readLine());
series(x,n);
series(p);
series();
}
}

Question 8

Write a program to input a sentence and convert it into uppercase and count and display the total number of words starting with letter ‘A’.
Sample input: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING
Sample output: Total number of words starting with letter ‘A’= 4. [15]

import java.io.*;
public class Strings
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a sentence");
String s= br.readLine();
s=s.toUpperCase();
s=“ ”+s;
int c=0;
for(int i=0;i
{
char ch=s.charAt(i);
if(s.charAt(i)==‘ ‘&&s.charAt(i+1)==‘A‘)
c++;
}
System.out.println("Total number of words starting with letter ‘A’= "+c);
}
}

Question 9

A tech number has even number of digits. If the number is split in two equal halves, then square of sum of these halves is equal to the number itself. Write a program to generate and print all four digits tech numbers.
Example: Consider the number 3025
Square of sum of the halves of 3025 = (30+25)2 = (55)2 = 3025 is a tech number [15]

public class Numbers
{
public static void main()
{
for(int i=1000;i<=9999;i++)
{
int h1=i/100;
int h2=i%100;
int s=h1+h2;
if(s*s==n)
System.out.println(i);
}
}
}