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.
(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.
Q1. (a) Which of the following are valid comments?
(i) /* comment */
(ii) /* comment
(iii) // comment
(iv) */ comment */
[2 marks]
A1. (a) (i) and (iii) are valid comments. The style of (i) is used for multi-line comments as well. The style of (iii) is used for single line comments only.
In (ii), the ending */ is missing.
In (iv) the start needs to be /* and not */
Q1. (b) What is meant by a package? Name any two Java Application Programming Interface packages. [2 marks]
A1. (b) A package is a name given to a collection of classes. It is a mechanism for organizing classes and preventing name conflicts between classes. Java packages are stored as compressed JAR files; this also allows for faster downloading of class files as a group.
Examples of Java API packages are: java.lang, java.util, java.io, etc.
Q1. (c) Name the primitive data type in Java that is:
(i) A 64-bit integer and is used when you need a range of values wider than those provided by int.
(ii) A single 16-bit Unicode character whose default value is ‘\u0000′.
[2 marks]
A1. (c)
(i) long
(ii) char
Q1. (d) State one difference between the floating point literals float and double. [2 marks]
A1. (d) The float data type is 4 bytes (32 bits) in size, and the double data type is 8 bytes (64 bits) in size.
Icse-2014-A5A more detailed answer, though not required, is:
Q1. (e) Find the errors in the given program segment and rewrite the statements correctly to assign values to an integer array. [2 marks]
int a = new int(5);
for(int i=0;i<=5;i++) a[i]=i;
A1. (e)
int[] a = new int[5];
for(int i=0;i<5;i++) a[i]=i;
Statement 1 has two errors: the [] is required as “int[] a” or “int a[]”. Also, the new int[5] uses square brackets.
Statement 2 has one error. The condition check should not include the = sign, since the element index should go from 0 to 4 only.
Q2. (a) Operators with higher precedence are evaluated before operators with relatively lower precedence. Arrange the operators given below in order of higher precedence to lower precedence.
(i) &&
(ii) %
(iii) >=
(iv) ++
[2 marks]
A2. (a)
++, %, >=, &&
Q2. (b) Identify the statements listed below as assignment, increment, method invocation or object creation statements.
(i) System.out.println(“Java”);
(ii) costPrice = 457.50;
(iii) Car hybrid = new Car();
(iv) petrolPrice++;
[2 marks]
A2. (b)
(i) Method invocation
(ii) Assignment
(iii) Object creation
(iv) Increment
Q2. (c) Give two differences between the switch statement and the if-else statement. [2 marks]
A2. (c) The differences are:
SWITCH IF .. ELSE
Conditions are checked as case statements (int, char, etc.) Conditions must evaluate to a boolean value only.
If no break statement is used, subsequent condition blocks may also get executed. This is the fall-through behaviour of switch…case statements. One condition block gets executed at a maximum.
Syntactically, the default clause is used for the final catch-all condition, if needed. The else clause (without if) if used for the final catch-all condition, if needed.
Q2. (d) What is an infinite loop? Write an infinite loop statement. [2 marks]
A2. (d) An infinite loop is a loop that executes forever, and has no reachable exit condition. It iterates through the loop body repeatedly. Inside the loop body, normally, an exit condition is present.
Example 1:
while(true) // infinite loop
{
System.out.println("Printing forever!");
}
Example 2:
for(;;)
{
System.out.println("Printing forever!");
}
Q2. (e) What is a constructor? When is it invoked? [2 marks]
A2. (e) A constructor is the code that is executed when an object of a class is being created. It is normally used to initialize the state of the instance variables of the object being created. A constructor is written like a method with no return value, and has the same name as the name of the class. A constructor is invoked whenever an object is created using the new reserved word.
Example:
Car c = new Car("Indica");
class Car
{
String model; // instance variable
Car(String model) // constructor
{
this.model = model;
}
}
Q3. (a) List the variables from those given below that are composite data types.
(i) static int x;
(ii) arr[i] = 10;
(iii) obj.display();
(iv) boolean b ;
(v) private char chr;
(vi) String str;
[2 marks]
A3. (a) Anything that is not a primitive data type is a composite data type. Hence the variables arr, obj, and str are composite data types.
Note: Remember, String and arrays of primitives are also composite, since arrays are objects.
Q3. (b) State the output of the following program segment:
String str1 = "great"; String str2 = "minds";
System.out.println(strl.substring(0,2).concat(str2.substring(l)));
System.out.println(("WH"+(strl.substring(2).toUpperCase())));
[2 marks]
A3. (b)
(i) grinds
(ii) WHEAT
Q3. (c) What are the final values stored in variables x and y below?
double a = -6.35;
double b = 14.74;
double x = Math.abs(Math.ceil(a));
double y = Math.rint(Math.max(a,b));
[2 marks]
A3. (c)
The variable x will contain 6.0. This is because ceiling makes -6.35 as -6.0. Then, abs() converts this into +6.0.
The variable y will contain 15.0. This is because the maximum of a and b is 14.74. And then rint() converts this into 15.0.
Q3. (d) Rewrite the following program segment using if-else statements instead of the ternary operator.
String grade=(mark>=90)?"A":(mark>=80)?"B":"C";
[2 marks]
A3. (d)
if(mark >= 90)
grade = "A";
else if(mark >= 80)
grade = "B";
else
grade = "C";
Q3. (e) Give the output of the following method:
public static void main(String[] args)
{
int a = 5;
a++;
System.out.println(a);
a-=(a--) - (--a);
System.out.println(a);
}
[2 marks]
A3. (e)
6
4
Explanation: The first output is simple, since a has got incremented earlier from 5 to 6.
The second output needs some explanation. Remember that a-=something is the same as a=a-something. What this means is that the existing value of a, before the statement starts, which is 6 will be used. But, it will come into play after the RHS has completely been evaluated.
The RHS is (a–) – (–a) = (6–) – (–a) = 6 – (–a) [but a is now 5, due to a--] = 6 – (–5) = 6 – 4 = 2.
To this, we apply a-=2 = a = a-2 (using the ORIGINAL value of a, which is 6) = 6 – 2 = 4.
Q3. (f) What is the data type returned by the library functions?
(i) compareTo()
(ii) equals()
[2 marks]
A3. (f)
(i) int
(ii) boolean
Q3. (g) State the value of characteristic and mantissa when the following code is executed:
String s = "4.3756";
int n = s.indexOf(".");
int characteristic=Integer.parseInt(s.substring(0,n));
int mantissa=Integer.valueOf(s.substring(n+1));
[2 marks]
A3. (g)
The value of characteristic is 4.
The value of mantissa is 3756.
Q3. (h) Study the method and answer the given questions:
public void sampleMethod()
{ for(int i=0;i<3;i++)
{ for(int j=0;j<2;j++)
{int number = (int)(Math.random() * 10);
System.out.println(number); }}}
(i) How many times does the loop execute?
(ii) What is the range of possible values stored in the variable number?
[2 marks]
A3. (h)
(i) The inner loop executes 6 times.
(ii) The range of possible values stored in the variable number is integers from 0 till 9, both included.
Q3. (i) Consider the following class:
public class myClass {
public static int x=3, y=4;
public int a=2, b=3; }
(i) Name the variables for which each object of the class will have its own distinct copy.
(ii) Name the variables that are common to all objects of the class.
[2 marks]
A3. (i)
(i) a, b
(ii) x, y
Q3. (j) What is the output when the following code segments are executed?
(i)
String s = "1001";
int x = Integer.valueOf(s);
double y = Double.valueOf(s);
System.out.println("x="+x);
System.out.println("y="+y);
(ii)
System.out.println("The king said \"Begin at the beginning!\" to me.");
[2 marks]
A3. (j)
(i)
x=1001
y=1001.0
(ii)
The king said "Begin at the beginning!" to me.
Q4. Define a class named movieMagic with the following description:
Instance variables/data members:
int year – to store the year of release of a movie
String title – to store the title of the movie
float rating – to store the popularity rating of the movie (minimum rating=0.0 and maximum rating=5.0)
Member methods:
(i) movieMagic() – Default constructor to initialize numeric data members to 0 and String data members to “”.
(ii) void accept() – To input and store year, title and rating
(iii) void display() – To display the title of the movie and a message based on the rating as per the table below.
Rating Message to be displayed
0.0 to 2.0 Flop
2.1 to 3.4 Semi-hit
3.5 to 4.5 Hit
4.6 to 5.0 Super Hit
Write a main method to create an object of the class and call the above methods.
[15 marks]
A4.
import java.util.*;
class movieMagic
{
// Instance variables
int year;
String title;
float rating;
// Constructor
public movieMagic()
{
this.year = 0;
this.title = "";
this.rating = 0.0F;
}
// Other member methods
public void accept()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the year of the movie : ");
this.year = Integer.parseInt(sc.nextLine());
System.out.print("Enter the title of the movie : ");
this.title = sc.nextLine();
System.out.print("Enter the rating of the movie: ");
this.rating = Float.parseFloat(sc.nextLine());
// check and ensure minimum value is 0.0
if(this.rating < 0.0F)
{
this.rating = 0.0F;
}
// check and ensure maximum value is 5.0
if(this.rating > 5.0F)
{
this.rating = 5.0F;
}
}
// display
public void display()
{
String message = "";
if(rating <= 2.0F)
message = "Flop";
else if(rating <= 3.4F)
message = "Semi-hit";
else if(rating <= 4.5F)
message = "Hit";
else
message = "Super Hit";
System.out.println(this.title + " [" + this.year + "] is a " + message);
}
public static void main(String[] args)
{
movieMagic movie = new movieMagic();
movie.accept();
movie.display();
}
}
Question 5.
A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original two-digit number.
Example: Consider the number 59.
Sum of digits = 5 + 9 = 14
Product of its digits = 5 x 9 = 45
Sum of the sum of digits and product of digits= 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, output the message “Special 2-digit number” otherwise, output the message “Not a Special 2-digit number”.
import java.util.Scanner;
public class SpecialNumber
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number: ");
int number = scanner.nextInt();
if(number>0 && number<100)
{
int rightDigit = number % 10;
int leftDigit = number / 10;
int sumOfDigits = rightDigit + leftDigit;
int productOfDigits = rightDigit * leftDigit;
int sumOfProductAndSum = sumOfDigits + productOfDigits;
if (sumOfProductAndSum == number)
{
System.out.println("Special 2-digit number");
}
else
{
System.out.println("Not a Special 2-digit number");
}
}
else
{
System.out.println("Not a 2-digit number");
}
}
}
Question 6.
Write a program to assign a full path and file name as given below. Using library functions, extract and output the file path, file name and file extension separately as shown.
Input C:\Users\admin\Pictures\flower.jpg
Output Path: C:\Users\admin\Pictures\
File name: flower
Extension: jpg
import java.util.Scanner;
public class Q6 {
public static void main(String[] args) {
Scanner ss = new Scanner(System.in);
System.out.print("Enter file name and path: ");
String input = ss.nextLine();
int indexOfLastBackslash = input.lastIndexOf('\\');
int indexOfDot = input.lastIndexOf('.');
String outputPath = input.substring(0, indexOfLastBackslash + 1);
String fileName = input.substring(indexOfLastBackslash + 1, indexOfDot);
String extension = input.substring(indexOfDot + 1);
System.out.println("Path: " + outputPath);
System.out.println("File Name: " + fileName);
System.out.println("Extension: " + extension);
}
}
Question 7.
Design a class to overload a function area() as follows:
(i) double area(double a. double b, double e) with three double arguments, returns the
area of a scalene triangle using the formula:
area=sqrt(s(s-a)(s-b)(s-c))
where
s=(1+b+c)/2
(ii) double area(int a, int b, int height) with three integer arguments, returns the area of a trapezium using the formula:
area=1/2h(a+b)
(iii) double area(double diagonal1, double diagonal2) with two double arguments, returns
the area of a rhombus using the formula:
area=1/2(d1*d2)
import java.io.*;
public class ol
{
public static double area(double a, double b, double c) {
double s = (a + b + c) / 2;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
return area;
}
public static double area(int a, int b, int height)
{
double area = 0.5 * height * (a + b);
return area;
}
public static double area(double diagonal1, double diagonal2)
{
double area = 0.5 * (diagonal1 * diagonal2);
return area;
}
public static void main(String ss[])throws IOException
{
double a1=2,b1=3,c1=4;
int a=5,b=6,h=7;
double d1=10,d2=12;
area(a1,b1,c1);
area(a,b,h);
area(d1,d2);
}
}
Question 8.
Using the switch statement. write a menu driven program to calculate the maturity amount of a Bank Deposit.
The user is given the following options:
(i) Term Deposit
(ii) Recurring Deposit
For option (i) accept principal(P), rare of ¡interest(r) and time period in years(n). Calculate and output the maturity amount(A) receivable using the formula
A=p[1+(r/100)]^n
For option (ii) accept Monthly Installment (P), rate of interest(r) and time period in months (n). Calculate and output the maturity amount(A) receivable using the formula
A=p*n+p*((n*(n-1))/2)*(r/100)*(1/12)
For an incorrect option, an appropriate error message should be displayed.
import java.util.Scanner;
public class Inst {
public static void main(String[] args) {
Scanner ss = new Scanner(System.in);
System.out.println("Menu");
System.out.println("1. Term Deposit");
System.out.println("2. Recurring Deposit");
System.out.print("Enter your choice: ");
int choice = ss.nextInt();
switch (choice) {
case 1:
System.out.print("Enter principal: ");
int P1 = ss.nextInt();
System.out.print("Enter rate of interest: ");
int r1 = ss.nextInt();
System.out.print("Enter period in years: ");
int n1 = ss.nextInt();
double maturityAmount1 = P1 * Math.pow(1 + r1 / 100.0, n1);
System.out.println("Maturity Amount is " + maturityAmount1);
break;
case 2:
System.out.print("Enter monthly installment: ");
int P2 = ss.nextInt();
System.out.print("Enter rate of interest: ");
int r2 = ss.nextInt();
System.out.print("Enter period in months: ");
int n2 = ss.nextInt();
double maturityAmount2 = P2 * n2 + P2 * (n2 * (n2 + 1) / 2.0) * (r2 / 100.0) * (1.0 / 12);
System.out.println("Maturity Amount is " + maturityAmount2);
break;
default:
System.out.println("Invalid choice");
}
}
}
Question 9.
Write a program to accept the year of graduation from school as an integer value from the user. Using the Binary Search technique on the sorted array of integers given below, output the message ‘Record exists’ if the value input is located in the array. If not, output the message Record does not exist”.
(1982, 1987, 1993. 1996, 1999, 2003, 2006, 2007, 2009, 2010)
import java.util.Scanner;
public class Search {
public static void main(String[] args) {
Scanner ss = new Scanner(System.in);
System.out.print("Enter year of graduation: ");
int graduationYear = ss.nextInt();
int[] years = {1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010};
boolean found = false;
int left = 0;
int right = years.length - 1;
while (left <= right)
{
int middle = (left + right) / 2;
if (years[middle] == graduationYear)
{
found = true;
break;
}
else if (years[middle] < graduationYear)
{
left = middle + 1;
}
else
{
right = middle - 1;
}
}
if (found)
{
System.out.println("Record exists");
}
else
{
System.out.println("Record does not exist");
}
}
}