Previous Questions and Answers: 2018

COMPUTER APPLICATIONS

SECTION A (40 Marks)
Attempt all questions

Question 1

(a) Define abstraction. [2]

Abstraction refers to the act of representing essential features without including the background details or explanations in a class.
E.g.: A student object has so many attributes like roll, name, marks, caste, address etc. To find result all these attributes are not required. The essential features are roll, name and marks. All other background details is avoided.

(b) Differentiate between the searching and sorting. [2]

Searching: Finding an element in an arrray is known as searching. If an element is found the result is displayed with its position etc. If it is not found “Element is not found message is displayed”.
Sorting: Arranging element in ascending or descending order is known as sorting. If the elements are string the sorting is in alphabetical order.

(c) Write a difference between the functions isUpperCase() and toUpperCase(). [2]

isUpperCase(): It is Character class function that checks whether a character is upper case or not. It returns boolean value true or false. E.g.: Character.isUpperCase(‘N’) returns true.
toUpperCase(): It is Character class function that converts a character into upper. It returns character value. E.g.: Character.toUpperCase(‘a’) returns A.

(d) How are private members of a class different from public members? [2]

Private members of a class can be accessed by the members of the same class only. E.g.: private int a; This varibale can be accessed by a method of the same class only.
Public members of a class can be accessed by the members of all class in any package. E.g.: public int a; This varibale can be accessed by any method of any class in any package.

(e) Classify the following as primitive or non-primitive data types:
(i) char (ii) arrays (iii) int (iv) classes. [2]

Primitive data types
(i) char (iii) int
Non-primitive data types
(ii) arrays (iv) classses

Question 2.

(a) (i) int res = ‘A’;
What is the value of res?

65

(ii) Name the package that contains wrapper classes. [2]

java.lang

(b) State the difference between while and do while loops. [2]

The while is an entry controlled loop that means the condition is checks before entering to the loop body. If the condition is true only the loop body executes other wise loop terminates.
The do while is an exit controlled loop that means the condition is checks only before exiting the loop body. If the condition is true then the loop body executes other wise loop terminates. The loop body executes atleast once.

(c) System.out.print(“BEST”);
System.out.print(“OF LUCK”);
Choose the correct option for the output of the above statements
(i) BEST OF LUCK
(ii) BEST
OF LUCK [2]

BEST OF LUCK

(d) Write the prototype of a function check which takes an integer argument and returns a character. [2]

public static char check(int x)

(e) Write the return data type of the following.
(i) endsWith() (ii) log() [2]

(i) boolean
(ii) double

Question 3.

(a) Write a Java expression for the following: [2]
√(3x+x^2)/(a+b)

Math.sqrt(3*x+Math.pow(x,2))/(a+b)

(b) What is the value of y after evaluating the expression given below?
y+= ++y + y-- + --y; when int y = 8 [2]

33

(c) Give the output of the following:
(i) Math.floor(-4.7) (ii) Math.ceil(3.4) + Math.pow(2,3) [2]

(i) -5.0
(ii) 12.0

(d) Write two characteristics of a constructor. [2]

(i) A constructor is member function with its class name and it is used to create objects by initialising proper values to data memebrs.
(ii) A constructor has no return type and it is invoked at the time of object creation.

(e) Write the output of the following:
System.out.println(“Incredible”+”\n”+”India”); [2]

Incredible
India

(f) Convert the following if else if construct into switch case
If(var==1)
System.out.println(“good”);
else if(var==2)
System.out.println(“better”);
else if(var==3)
System.out.println(“best”);
else
System.out.println(“Invalid”); [2]

switch(var)
{
case 1: System.out.println("good");
break;
case 2: System.out.println("better ");
break;
case 3: System.out.println("best ");
break;
default: System.out.println("Invalid ");
}

(g) Give the output of the following string functions:
(i) “ACHIEVEMENT”.replace(‘E’,’A’) (ii) “DEDICATE”.compareTo(“DEVOTE”) [2]

(i) ACHIAVEMANT
(ii) -18

(h) Consider the following String array and give the output:
String arr[]= {“DELHI”,”CHENNAI”,”MUMBAI”,”LUCKNOW”,”JAIPUR”];
System.out.println(arr[0].length()>arr[3].length());
System.out.print(arr[4].substring(0,3)); [2]

false
JAI

(i) Rewrite the following using ternary operator:
if(bill>10000)
discount=bill*10.0/100;
else
discount=bill*5.0/100; [2]

discount=(bill>10000)? bill*10.0/100 : bill*5.0/100;

(j) Give the output of the following program segment and also mention how many times the loop is executed:
int i;
for(i=5;i>10;i++)
System.out.println(i);
System.out.println(i*4); [2]

20
Loop body does not execute.

SECTION B (60 Marks)

Attempt any four questions

Question 4

Design a class RailwayTicket with following description:
Instance variables/data members:
String name : To store the name of the customer
String coach : To store the type of coach customer wants to travel
long mobno : To store customer’s mobile number
int amt : To store basic amount of ticket
int totalamt : To store the amount to be paid after updating the original amount
Member methods:
void accept() - To take input for name, coach, mobile number and amount.
void update() - To update the amount as per the coach selected
(Extra amount to be added in the amount as follows)
Types of Coach - Amount
First_AC - 700
Second_AC - 500
Third_AC - 250
Sleeper - None
void display() - To display all details of a customer such as name, coach, total amount and mobile number
Write a main method to create an object of the class and call the above member methods. [15]

import java.io.*;
public class RailwayTicket
{
String name;
String coach;
long mobno;
int amt;
int totalamt;
public void accept()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
name= br.readLine();
coach= br.readLine();
mobno=Long.parseLong(br.readLine());
amt=Integer.parseInt(br.readLine());
}
public void update()
{
if(coach.equalsIgnoreCase(“First_AC”))
totalamt=amt+700;
else if(coach.equalsIgnoreCase(“Second_AC”))
totalamt=amt+500;
else if(coach.equalsIgnoreCase(“Third_AC”))
totalamt=amt+250;
else if(coach.equalsIgnoreCase(“Sleeper”))
totalamt=amt+0;
}
public void display()
{
System.out.println("Name: "+name);
System.out.println("Coach: "+coach);
System.out.println("Mobile Number: "+mobno);
System.out.println("Total amount: "+totalamt);
}
public static void main()throws IOException
{
RailwayTicket ob=new RailwayTicket();
ob.accept();
ob.update();
ob.display();
}
}

Question 5

Write a program to input a number and check and print whether it is a Pronic number or not. (Pronic number is the number which is the product of two consecutive integers) Examples:
12=3x4
20=4x5
42=6x7

import java.io.*;
public class Pronic
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number ");
int n=Integer.parseInt(br.readLine());
boolean flag=true;
for(int i=1;i<=n/2;i++)
{
if(i*(i+1)==n)
{
flag=true;
break;
}
}
if(flag)
System.out.println(n+" is a Pronic number");
else
System.out.println(n+" is a not a Pronic number");
}
}

Question 6

Write a program in Java to accept a string in lower case and change the first letter of every word to upper case. Display the new string.
Sample input: we are in cyber world.
Sample output: We Are In Cyber World [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 string");
String a=br.readLine();
a=a.toLowerCase();
a=a+“ ”;
String w=“”,s,ns=“”;
for(int i=0;i{
char ch=a.charAt(i);
if(ch!=‘ ’)
w+=ch;
else
{
s=w.substring(0,1);
s=s.toUpperCase()+w.substring(1);
ns+=s+“ ”;
w=“”;
}
}
a=ns.trim();
System.out.println(a);
}
}

Question 7

Design a class to overload a function volume() as follows:
(i) double volume(double R) – with radius(R) as an argument, returns the volume of sphere using the formula: V=4/3 x 22/7 x R3
(ii) double volume(double H, double R) – with height(H) and radius(R) as the arguments, returns the volume of a cylinder using the formula: V=22/7 x R2 x H
(iii) double volume(double L, double B, double H) – with length(L), breadth(B) and height(H) as the arguments, returns the volume of a cuboid using the formula: V=L x B x H [15]

import java.io.*;
public class Overload
{
public static double volume(double R)
{
double V=4/3.0 * 22/7.0 * R*R*R;
return V;
}
public static double volume(double H, double R)
{
double V= 22/7.0 * R*R*H;
return V;
}
{
double V= L*B*H;
return V;
}
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter radius of sphere");
double R=Double.parseDouble(br.readLine());
System.out.println("Enter number height and radius of cylinder");
double H=Double.parseDouble(br.readLine());
double R=Double.parseDouble(br.readLine());
System.out.println("Enter length, breadth and height of cuboid");
double L=Double.parseDouble(br.readLine());
double B=Double.parseDouble(br.readLine());
double H=Double.parseDouble(br.readLine());
double V1=volume(R);
double V2=volume(H,R);
V3=volume(L,B,H);
System.out.println("Volume of sphere: "+V1);
System.out.println("Volume of cylider: "+V2);
System.out.println("Volume of cuboid: "+V3);
}
}

Question 8

Write a menu driven program to display the pattern as per user’s choice.
Pattern 1 Pattern 2
ABCDE B
ABCD LL
ABC UUU
AB EEEE
A
For an incorrect option, an appropriate error message should be displayed. [15]

import java.io.*;
public class MenuDriven
{
public static void pattern1()
{
String s=“ABCDE”;
int l= s.length();
for(int i=l-1;i>=0 i--)
{
for(int j=0;j<=i;j++)
System.out.print("s.charAt(i)");
System.out.println("");
}
}
public static void pattern2()
{
String s=“BLUE”;
int l= s.length();
for(int i=0;i<l;i++)
{
for(int j=0;j<=i;j++)
System.out.print("s.charAt(i)");
System.out.println("");
}
}
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int ch;
do
{
System.out.println("Menu");
System.out.println("1 Pattern1");
System.out.println("2 Pattern2");
System.out.println("0 Exit");
System.out.print("Enter your choice: ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1: pattern1();
break;
case 2: pattern2();
break;
case 0: break;
default : System.out.println("Wrong choice. Try again!");
}
}
while(ch!=0);
}
}

Question 9

Write a program to accept name and total marks of N number of students in two single subscript array name[] and totalmarks[].
Calculate and print:
(i) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students)/N]
(ii) Deviation of each student’s total marks with the average.
[deviation= total marks of a student – average] [15]

import java.io.*;
public class Array
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter number of students: ");
int N=Integer.parseInt(br.readLine());
String name[]=new String[N];
double totalmarks[]=new double[N];
double deviation[]=new double[N};
double sum=0.0,average;
for(int i=0;i{
System.out.println("Enter name and total marks: ");
name[i]= br.readLine();
totalmarks[i]=Double.parseDouble(br.readLine());
}
for(int i=0;isum+= totalmarks[i];
average=sum/N;
for(int i=0;ideviation[i]= totalmarks[i]-average;
System.out.println("Average of total marks: "+average);
System.out.println("Name\tTotal Marks\tAverage\tDeviation");
for(int i=0;iSystem.out.println(name[i]+"\t"+totalmarks[i]+; "\t"+average+"\t"+ deviation[i]);
}
}