COMPUTER APPLICATIONS
(Theory)
(Two hours)
Answers to this paper must be written on the paper provided separately.
You will not be allowed to write during the first 15 minutes.
This time is to be spent in reading the question paper.
The time given at the head of this paper is the time allowed for writing the answers.
This paper is divided into two sections.
Attempt all questions from Section A and any four questions from Section B.
The intended marks for questions or parts of questions are given in brackets [ ].
SECTION A
Attempt all questions.
Question 1
(a) Define abstraction.
Abstraction is the process of hiding unnecessary details and highlighting only essential details.
(b) Differentiate between searching and sorting.
Searching is the process of checking whether an element is present in a given list or not. Sorting is the process of arranging the elements in a list in a certain order.
(c) Write a difference between the functions isUpperCase() and toUpperCase().
The isUpperCase() function checks whether the given character is in uppercase or not. The toUpperCase() function converts a given character to uppercase.
(d) How are private members of a class different from public members?
The private members can only be accessed from within that class, whereas public members can be accessed from within that class as well as from the outside of that class.
(e) Classify the following as primitive or non-primitive data types.
(i) char
primitive data type
(ii) arrays
non-primitive data type
(iii) int
primitive data type
(iv) classes
non-primitive data type
Question 2
(a) (i) int res = ‘A’;
What is the value of res?
res = 65
(ii) Name the package that contains wrapper classes.
java.util package
(b) State the difference between while and do while loop.
The while loop is an entry-controlled loop, whereas the do-while loop is an exit-controlled loop.
(c) System.out.print(“BEST “);
System.out.println(“OF LUCK”);
Choose the correct option for the output of the above statements:
(i) BEST OF LUCK
(ii) BEST
OF LUCK
(i) BEST OF LUCK
(d) Write the prototype of a function check which takes an integer as an argument and returns a character.
public static char check(int n)
(e) Write the return data type of the following function.
(i) endsWith()
boolean
(ii) log()
double
Question 3
(a) Write a Java expression for the following:
Math.sqrt(3 * x + x * x) / (a + b)
(b) What is the value of y after evaluating the expression given below?
y += ++y + y– + –y; when int y = 8
y = y + (++y + y– + –y)
y = 8 + (9 + 9 + 7)
y = 8 + 25
y = 33
(c) Give the output of the following:
(i) Math.floor(-4.7)
-5.0
(ii) Math.ceil(3.4) + Math.pow(2, 3)
12.0
(d) Write two characteristics of a constructor.
A constructor doesn’t have any return type (not even void).
Constructors are invoked during object creation.
(e) Write the output for the following:
System.out.println(“Incredible” + “\n” + “world”);
Incredible
world
(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");
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’)
ACHIAVAMANT
(ii) “DEDICATE”.compareTo(“DEVOTEE”)
-17
(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));
true
JAI
(i) Rewrite the following using ternary operator:
if(bill > 10000)
discount = bill * 10.0 / 100;
else
discount = bill * 5.0 / 100;
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);
20
SECTION B (60 Marks)
Attempt any four questions from this section.
The answers in this section should consist of the programs in either BlueJ environment or any program environment with Java as the base.
Each program should be written using variable descriptions/mnemonic codes so that the logic of the program is clearly depicted.
Flowcharts and algorithms are not required.
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 the customer wants to travel
long mobNo: To store the 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)
Type of Coaches | 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.
import java.util.Scanner;
class RailwayTicket{
String name;
String coach;
long mobNo;
int amt;
int totalAmt;
public void accept(){
Scanner in = new Scanner(System.in);
System.out.print("Name: ");
name = in.nextLine();
System.out.print("Coach: ");
coach = in.nextLine();
System.out.print("Mobile number: ");
mobNo = Long.parseLong(in.nextLine());
System.out.print("Amount: ");
amt = Integer.parseInt(in.nextLine());
}
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
totalAmt = amt;
}
public void display(){
System.out.println("Name: " + name);
System.out.println("Coach: " + coach);
System.out.println("Total amount: " + totalAmt);
System.out.println("Mobile Number: " + mobNo);
}
public static void main(String[] args) {
RailwayTicket obj = new RailwayTicket();
obj.accept();
obj.update();
obj.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 = 3 × 4
20 = 4 × 5
42 = 6 × 7
import java.util.Scanner;
class Pronic{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("N = ");
int n = Integer.parseInt(in.nextLine());
int p = 1;
while(p * (p + 1) < n)
p++;
if(p * (p + 1) == n)
System.out.println("Pronic number");
else
System.out.println("Not a Pronic number");
}
}
Question 6
Write a program in Java to accept a string in lowercase and change the first letter of every word to uppercase. Display the new string.
Sample input: we are in cyber world
Sample Output: We Are In Cyber World
import java.util.Scanner;
class Title{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter the string: ");
String s = in.nextLine().toLowerCase();
String t = "";
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if(i == 0 || s.charAt(i - 1) == ' ')
t += Character.toUpperCase(ch);
else
t += ch;
}
System.out.println(t);
}
}
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 × 22 / 7 × 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 × r2 × 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 × b × h
class Overload{
public static double volume(double r){
return 4.0 / 3 * 22.0 / 7 * r * r * r;
}
public static double volume(double h, double r){
return 22.0 / 7 * r * r * h;
}
public static double volume(double l, double b, double h){
return l * b * h;
}
}
Question 8
Write a menu-driven program to display the pattern as per user’s choice.
Pattern1
ABCDE
ABCD
ABC
AB
A
Pattern2
B
LL
UUU
EEEEE
For an incorrect choice, an appropriate error message should be displayed.
import java.util.Scanner;
class Pattern{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("1. ABCDE");
System.out.println("2. BLUE");
System.out.print("Enter your choice: ");
int choice = Integer.parseInt(in.nextLine());
switch(choice){
case 1:
for(char i = 'E'; i >= 'A'; i--){
for(char j = 'A'; j <= i; j++)
System.out.print(j);
System.out.println();
}
break;
case 2:
String s = "BLUE";
for(int i = 0; i < s.length(); i++){
for(int j = 0; j <= i; j++)
System.out.print(s.charAt(i));
System.out.println();
}
break;
default:
System.out.println("Invalid choice!");
}
}
}
Question 9
Write a program to accept name and total marks of N number of students in two 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]
import java.util.Scanner;
class Students{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Number of students: ");
int n = Integer.parseInt(in.nextLine());
String name[] = new String[n];
int totalMarks[] = new int[n];
double total = 0.0;
for(int i = 0; i < n; i++){
System.out.print("Name: ");
name[i] = in.nextLine();
System.out.print("Total marks: ");
totalMarks[i] = Integer.parseInt(in.nextLine());
total += totalMarks[i];
}
double avg = total / n;
System.out.println("Average marks: " + avg);
for(int i = 0; i < n; i++){
double d = totalMarks[i] - avg;
System.out.println("Deviation of " + name[i] + " = " + d);
}
}
}