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 (40 Marks)
Attempt all questions.
Question 1
(a) What are the default values of the primitive data type int and float?
Default value of int = 0.
Default value of float = 0.0F.
(b) Name any two OOP’s principles.
Inheritance
Polymorphism
(c) What are identifiers?
Identifiers are the names given to variables, functions, classes in a program.
(d) Identify the literals listed below:
(i) 0.5
int literal
(ii) ‘A’
char literal
(iii) false
boolean literal
(iv) “a”
String literal
(e) Name the wrapper classes of char type and boolean type.
Wrapper class for char type is Character.
Wrapper class for boolean type is Boolean.
Question 2
(a) Evaluate the value of n if value of p = 5, q = 19
int n = (q – p) > (p – q)? (q – p) : ( p – q);
n = 14
(b) Arrange the following primitive data types in an ascending order of their size:
(i) char
(ii) byte
(iii) double
(iv) int
byte, char, int, double
(c) What is the value stored in variables res given below?
double res = Math.pow(“345”.indexOf(‘5’), 3);
8.0
(d) Name the two types of constructors.
Parameterized constructor.
Non-parameterized constructor.
(e) What are the values of a and b after the following function is executed, if the values passed are 30 and 50:
void paws(int a, int b){
a = a + b;
b = a – b;
a = a – b;
System.out.println(a + “, ” + b);
}
50, 30
Question 3
(a) State the data type and value of y after the following is executed:
char x = ‘7’;
y = Character.isLetter(x);
Value of y = false
Data type of y = boolean
(b) What is the function of catch block in exception handling? Where does it appear in a program?
The catch block is used to receive the exception object thrown from the try block, whenever an exception occurs. It occurs after the try block.
(c) State the output when the following program segment is executed:
String a = “Smartphone”, b = “Graphic Art”;
String h = a.substring(2, 5);
String k = b.substring(8).toUpperCase();
System.out.println(h);
System.out.println(k.equalsIgnoreCase(h));
art
true
(d) The access specifier that gives the most accessibility is ________ and the least accessibility is ________.
public, private
(e) (i) Name the mathematical function which is used to find sine of an angle given in radians.
Math.sin()
(ii) Name a string function which removes the blank spaces provided in the prefix and suffix of a string.
trim()
(f) (i) What will this code print?
int arr[] = new int[5];
System.out.println(arr);
(i) 0 (ii) value stored in arr[0] (iii) 0000 (iv) garbage value
Garbage value
(ii) Name the keyword which is used to resolve the conflict between method parameter and instance variables/fields.
this keyword
(g) State the package that contains the class:
(i) BufferedReader
java.io package
(ii) Scanner
java.util package
(h) Write the output of the following program code:
char ch;
int x = 97;
do{
ch = (char)x;
System.out.print(ch + ” “);
if(x % 10 == 0)
break;
++x;
}while(x <= 100);
a b c d
(i) Write the Java expression for:
(a * a + b * b) / (2 * a * b)
(j) If int y = 10 then find int z = (++y * (y++ + 5));
z = 11 * (11 + 5)
z = 11 * 16
z = 176
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
Define a class called ParkingLot with the following description:
Instance variables/data members:
int vno – to store the vehicle number
int hours – to store the number of hours the vehicle is parked in the parking lot
double bill – to store the bill amount
Member functions:
void input() – to input and store the vno and hours.
void calculate() – to compute the parking charge at the rate of ₹ 3 for the first hour or part thereof and ₹ 1.50 for each additional hour or part thereof.
void display() – to display the detail
Write a main() method to create an object of the class and call the above methods.
import java.util.Scanner;
class ParkingLot{
int vno;
int hours;
double bill;
void input(){
Scanner in = new Scanner(System.in);
System.out.print("Vehicle number: ");
vno = Integer.parseInt(in.nextLine());
System.out.print("Number of hours: ");
hours = Integer.parseInt(in.nextLine());
}
public void calculate(){
if(hours == 1)
bill = 3.0;
else
bill = 3.0 + (hours - 1) * 1.50;
}
public void display(){
System.out.println("Vehicle No. " + vno);
System.out.println("Number of hours: " + hours);
System.out.println("Total bill: Rs. " + bill);
}
public static void main(String args[]){
ParkingLot obj = new ParkingLot();
obj.input();
obj.calculate();
obj.display();
}
}
Question 5
Write two separate programs to generate the following patterns using iteration (loop) statements:
(a)
*
* #
* # *
* # * #
* # * # *
class Pattern1{
public static void main(String args[]){
for(int i = 1; i <= 5; i++){
char ch = '*';
for(int j = 1; j <= i; j++){
System.out.print(ch);
if(ch == '*')
ch = '#';
else
ch = '*';
}
System.out.println();
}
}
}
(b)
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
class Pattern2{
public static void main(String args[]){
for(int i = 1; i <= 5; i++){
for(int j = 5; j >= i; j--)
System.out.print(j);
System.out.println();
}
}
}
Question 6
Write a program to input and store roll numbers, names and marks in 3 subjects of n number of students in five single dimensional array and display the remark based on average marks as given below:
(The maximum marks in the subject are 100)
Average marks = Total Marks / 3
Average Marks | Remark |
85 – 100 | EXCELLENT |
75 – 84 | DISTINCTION |
60 – 74 | FIRST CLASS |
40 – 59 | PASS |
Less than 40 | POOR |
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());
int roll[] = new int[n];
String name[] = new String[n];
int s1[] = new int[n];
int s2[] = new int[n];
int s3[] = new int[n];
for(int i = 0; i < n; i++){
System.out.print("Name: ");
name[i] = in.nextLine();
System.out.print("Roll: ");
roll[i] = Integer.parseInt(in.nextLine());
System.out.print("Marks in subject1: ");
s1[i] = Integer.parseInt(in.nextLine());
System.out.print("Marks in subject2: ");
s2[i] = Integer.parseInt(in.nextLine());
System.out.print("Marks in sucject3: ");
s3[i] = Integer.parseInt(in.nextLine());
double avg = (s1[i] + s2[i] + s3[i]) / 3.0;
if(avg >= 85)
System.out.println("Remark: EXCELLENT");
else if(avg >= 75)
System.out.println("Remark: DISTINCTION");
else if(avg >= 60)
System.out.println("Remark: FIRST CLASS");
else if(avg >= 40)
System.out.println("Remark: PASS");
else
System.out.println("Remark: POOR");
}
}
}
Question 7
Design a class to overload a function joyString() as follows:
(i) void joyString(String s, char ch1, char ch2) with one string argument and two character arguments that replaces the character argument ch1 with the character argument ch2 in the given string s and prints the new string.
Example:
Input value of s = “TECHNALAGY”
ch1 = ‘A’
ch2 = ‘O’
Output: “TECHNOLOGY”
(ii) void joyString(String s) with one string argument that prints the position of the first space and the last space of the given string s.
Example:
Input value of s = “Cloud computing means Internet based computing”
Output:
First index: 5
Last index: 36
(iii) void joyString(String s1, String s2) with two string arguments that combines the two strings with a space between them and prints the resultant string.
Example:
Input value of s1 = “COMMON WEALTH”
Input value of s2 = “GAMES”
Output: COMMON WEALTH GAMES
(use library functions)
class Overload{
public static void joyString(String s, char ch1, char ch2){
s = s.replace(ch1, ch2);
System.out.println(s);
}
public static void joyString(String s){
int first = s.indexOf(' ');
int last = s.lastIndexOf(' ');
System.out.println("First index: " + first);
System.out.println("Last index: " + last);
}
public static void joyString(String s1, String s2){
String s3 = s1 + " " + s2;
System.out.println(s3);
}
}
Question 8
Write a program to input twenty names in an array. Arrange these names in descending order of alphabets, using the bubble sort technique.
import java.util.Scanner;
class Arrange{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
String names[] = new String[20];
System.out.println("Enter 20 names:");
for(int i = 0; i < names.length; i++)
names[i] = in.nextLine();
for(int i = 0; i < names.length; i++){
for(int j = 0; j < names.length - 1 - i; j++){
if(names[j].compareToIgnoreCase(names[j + 1]) < 0){
String temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}
}
System.out.println("Names in descending order:");
for(int i = 0; i < names.length; i++)
System.out.println(names[i]);
}
}
Question 9
Using the switch statement, write a menu driven program to:
(i) To find and display all the factors of a number input by the user (including 1 and excluding number itself).
Example:
Sample Input: n = 15
Sample Output: 1, 3, 5
(ii) To find and display the factorial of a number input by the user (the factorial of a non-negative integer n, denoted by n!, is the product of all integers less than or equal to n.
Example:
Sample Input: n = 5
Sample Output: 5! = 1 × 2 × 3 × 4 × 5 = 120.
For an incorrect choice, an appropriate error message should be displayed.
import java.util.Scanner;
class Menu{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("1. Display factors");
System.out.println("2. Display factorial");
System.out.print("Enter your choice: ");
int choice = Integer.parseInt(in.nextLine());
switch(choice){
case 1:
System.out.print("N = ");
int n = Integer.parseInt(in.nextLine());
for(int i = 1; i <= n; i++){
if(n % i == 0)
System.out.println(i);
}
break;
case 2:
long f = 1L;
System.out.print("N = ");
n = Integer.parseInt(in.nextLine());
for(int i = 1; i <= n; i++){
f *= i;
}
System.out.println("Factorial: " + f);
break;
default:
System.out.println("Invalid choice!");
}
}
}