Professional Documents
Culture Documents
COMPUTER APPLICATIONS
(Theory)
(Two Hours)
Question 1
(a) Give one example each of a primitive data type and a composite data type.
[2]
Ans. Primitive Data Types – byte, short, int, long, float, double, char, boolean
Composite Data Type – Class, Array, Interface
(b) Give one point of difference between unary and binary operators. [2]
Ans. A unary operator requires a single operand whereas a binary operator
requires two operands.
Examples of Unary Operators – Increment ( ++ ) and Decrement ( — )
Operators
Examples of Binary Operators – +, -, *, /, %
(c) Differentiate between call by value or pass by value and call by reference
or pass by reference. [2]
Ans. In call by value, a copy of the data item is passed to the method which is
called whereas in call by reference, a reference to the original data item is
passed. No copy is made. Primitive types are passed by value whereas
reference types are passed by reference.
(d) Write a Java expression for (under root of 2as+u 2) [2]
Ans. Math.sqrt ( 2 * a * s + Math.pow ( u, 2 ) )
( or )
Math.sqrt ( 2 * a * s + u * u )
(e) Name the types of error (syntax, runtime or logical error) in each case
given below:
(i) Division by a variable that contains a value of zero.
(ii) Multiplication operator used when the operation should be division.
(iii) Missing semicolon. [2]
Ans.
(i) Runtime Error
(ii) Logical Error
(iii) Syntax Error
Question 2
(a)Create a class with one integer instance variable. Initialize the variable
using:
(i) default constructor
(ii) parameterized constructor. [2]
Ans.
Question 3
(a) What are the values of x and y when the following statements are
executed? [2]
Ans. x = false
y = 63
1 char c = 'A':
2 int n = c + 1;
(c) What will be the result stored in x after evaluating the following
expression? [2]
1 int x=4;
2 x += (x++) + (++x) + x;
Ans.
2.0
3.0
Explanation : Math.min(Math.floor(x), y) = Math.min ( 2.0, 2.5 ) = 2.0
Math.max(Math.ceil(x), y)) = Math.max ( 3.0, 2.5 ) = 3.0
1 String s = "Examination";
2 int n = s.length();
3 System.out.println(s.startsWith(s.substring(5, n)));
4 System.out.println(s.charAt(2) == s.charAt(6));
Ans.
false
true
Explanation : n = 11
s.startsWith(s.substring(5, n)) = s.startsWith ( “nation” ) = false
( s.charAt(2) == s.charAt(6) ) = ( ‘a’== ‘a’ ) = true
(g) State the data type and values of a and b after the following segment is
executed.[2]
Ans.
0
8
(i) Rewrite the following program segment using while instead of for
statement [2]
1 int f = 1, i;
3 f *= i;
4 System.out.println(f);
5}
Ans.
2 while (i <= 5) {
3 f *= i;
4 System.out.println(f);
5 i++;
6}
(j) In the program given below, state the name and the value of the
(i) method argument or argument variable
(ii) class variable
(iii) local variable
(iv) instance variable [2]
1 class myClass {
2
3 static int x = 7;
4 int y = 2;
5
6 public static void main(String args[]) {
7 myClass obj = new myClass();
8 System.out.println(x);
9 obj.sampleMethod(5);
10 int a = 6;
11 System.out.println(a);
12 }
13
14 void sampleMethod(int n) {
15 System.out.println(n);
16 System.out.println(y);
17 }
18 }
Question 4
Ans.
1 public class Library {
2
3 int acc_num;
4 String title;
5 String author;
6
7 public void input() throws IOException {
BufferedReader br
8
= new BufferedReader(newInputStreamReader(System.in));
9 System.out.print("Enter accession number: ");
10 acc_num = Integer.parseInt(br.readLine());
11 System.out.print("Enter title: ");
12 title = br.readLine();
13 System.out.print("Enter author: ");
14 author = br.readLine();
15 }
16
17 public void compute() throws IOException {
BufferedReader br
18
= new BufferedReader(newInputStreamReader(System.in));
19 System.out.print("Enter number of days late: ");
20 int daysLate = Integer.parseInt(br.readLine());;
25 public void display() {
26 System.out.println("Accession Number\tTitle\tAuthor");
27 System.out.println(acc_num + "\t" + title + "\t" + author);
28 }
29
30 public static void main(String[] args) throws IOException {
31 Library library = new Library();
32 library.input();
33 library.compute();
34 library.display();
35 }
36 }
Question 5
Given below is a hypothetical table showing rates of Income Tax for male
citizens below the age of 65 years:
Is greater than 5,00,000 and less than or equal to [ (TI - 5,00,000 ) *20% ] +
8,00,000 34,000
Write a program to input the age, gender (male or female) and Taxable
Income of a person.If the age is more than 65 years or the gender is female,
display “wrong category*.
If the age is less than or equal to 65 years and the gender is male, compute
and display the Income Tax payable as per the table given above. [15]
Ans.
1 import java.util.Scanner;
2
3 public class IncomeTax {
4
5 public static void main(String[] args) {
16 double tax;
if (income <= 160000) { tax =0; } else if (income
17 > 160000 && income <= 500000) { tax = (income - 160000)
* 10 / 100; }else if (income >= 500000 && income <= 800000) {
18 tax = (income - 500000) * 20 / 100 + 34000;
19 } else {
21 }
Question 6
Write a program to accept a string. Convert the string to uppercase. Count and
output the number of double letter sequences that exist in the string.
Sample Input: “SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE
Sample Output: 4 [ 15]
Ans.
1 import java.util.Scanner;
2
3 public class StringOperations {
4
5 public static void main(String[] args) {
15 }
16 System.out.println(count);
17 }
18 }
Question 7
Example:
(i) Input value of n=2, ch=’O’
Output:
OO
OO
(ii) Input value of x=2, y=5
Output:
@@@@@
@@@@@
(iii) Output:
*
**
*** [15]
Ans.
1 public class Overloading {
2
3 public void polygon(int n, char ch) {
6 System.out.print(ch);
7 }
8 System.out.println();
9 }
10 }
11
12 public void polygon(int x, int y) {
21 public void polygon() {
24 System.out.print("*");
25 }
26 System.out.println();
27 }
28 }
29 }
Question 8
Ans.
1 import java.util.Scanner;
2
3 public class Menu {
4
5 public static void main(String[] args) {
7 System.out.println("Menu");
8 System.out.println("1. Fibonacci Sequence");
9 System.out.println("2. Sum of Digits");
10 System.out.print("Enter choice: ");
11 int choice = scanner.nextInt();
12 switch (choice) {
13 case 1:
14 int a = 0;
15 int b = 1;
16 System.out.print("0 1 ");
for (int i = 3; i <= 10; i++) { int c = a + b;
System.out.print(c + " "); a = b; b = c;
17 } break; case2: System.out.print("Enter a number:
"); int num = scanner.nextInt(); intsum
= 0; while (num > 0) {
18 int rem = num % 10;
19 sum = sum + rem;
20 num = num / 10;
21 }
Question 9
Write a program to accept the names of 10 cities in a single dimension string
array and their STD (Subscribers Trunk Dialing) codes in another single
dimension integer array. Search for a name of a city input by the user in the
list. If found, display “Search Successful” and print the name of the city along
with its STD code, or else display the message “Search Unsuccessful, No such
city in the list’. [15]
Ans.
view source
print ?
1 import java.util.Scanner;
2
3 public class Cities {
4
5 public static void main(String[] args) {
17 boolean searchSuccessful = false;
19 if (cities[i].equals(target)) {
20 System.out.println("Search successful");
21 System.out.println("City : " + cities[i]);
COMPUTER APPLCATIONS
(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 the 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 bracket [ ]
SECTION – A (40 Marks)
Question 1.
(a) What is the difference between an object and a class? [2]
Ans. 1. A class is a blueprint or a prototype of a real world object. It contains
instance variables and methods whereas an object is an instance of a class.
2. A class exists in the memory of a computer while an object does not.
3. There will be only one copy of a class whereas multiple objects can be
instantiated from the same class.
(b) What does the token ‘keyword’ refer to in the context of Java? Give an
example for keyword. [2]
Ans. Keywords are reserved words which convey special meanings to the
compiler and cannot be used as identifiers. Example of keywords : class,
public, void, int
(c) State the difference between entry controlled loop and exit controlled loop.
[2]
Ans. In an entry controlled loop, the loop condition is checked before
executing the body of the loop. While loop and for loop are the entry
controlled loops in Java.
In exit controlled loops, the loop condition is checked after executing the body
of the loop. do-while loop is the exit controlled loop in Java.
(d) What are the two ways of invoking functions? [2]
Ans. If the function is static, it can be invoked by using the class name. If the
function is non-static, an object of that class should be created and the
function should be invoked using that object.
(e) What is difference between / and % operator? [2]
Ans. / is the division operator whereas % is the modulo (remainder) operator.
a / b gives the result obtained on diving a by b whereas a % b gives the
remainder obtained on diving a by b.
Question 2.
(a) State the total size in bytes, of the arrays a [4] of char data type and p [4]
of float data type. [2]
Ans. char is two bytes. So a[4] will be 2*4=8 bytes.
float is 4 bytes. So p[4] will be 4*4=16 bytes.
(b) (i) Name the package that contains Scanner class.
(ii) Which unit of the class gets called, when the object of the class is created?
[2]
Ans. (i) java.util
(ii) Constructor
(c) Give the output of the following: [2]
String n = “Computer Knowledge”;
String m = “ Computer Applications ”;
System.out.println(n.substring (0,8). concat (m.substring(9)));
System.out.println(n.endsWith(“e”));
Ans. n.substring(0,8) gives “Computer”. m.substring(9) gives “Applications”.
These two on concatenation gives “ComputerApplications”. n ends with “e”.
So, it gives true.
The output is:
ComputerApplications
true
(d) Write the output of the following: [2]
(i) System.out.println (Character.isUpperCase(‘R’));
(ii) System.out.println(Character.toUpperCase(‘j’));
Ans. (i) true
(ii) J
(e) What is the role of keyword void in declaring functions? [2]
Ans. void indicates that the function doesn’t return any value.
Question 3
(a) Analyse the following program segment and determine how many times
the loop will be executed and what will be the output of the program
segment ? [2]
int p = 200;
while(true)
{
if (p<100)
break;
p=p-20;
}
System.out.println(p);
Ans.p values changes as follows : 200, 180, 160, 140, 120, 100, 80. So, the
loop executes six times and value of p is 80.
(b) What will be the output of the following code? [2]
(i)
int k = 5, j = 9;
k += k++ – ++j + k;
System.out.println("k= " +k);
System.out.println("j= " +j);
Ans.
k=6
j = 10
Explanation:
k += k++ – ++j + k
k = k + k++ – ++j + k
k = 5 + 5 – 10 + 6
k=6
j = 10 as it has been incremented in the ++j operation.
(ii) [2]
double b = -15.6;
double a = Math.rint (Math.abs (b));
System.out.println("a= " +a);
Ans.
a =16.0
int age;
public Age() {
age = -1;
}
or
z = ( 5 * Math.pow ( x, 3 ) + 2 * y ) / ( x + y )
(f) Write a statement each to perform the following task on a string: [2]
(i) Find and display the position of the last space in a string s.
(ii) Convert a number stored in a string variable x to double data type
Ans. (i) System.out.println(s.lastIndexOf(” “);
(ii) Double.parseDouble(x)
(g) Name the keyword that: [2]
(i) informs that an error has occurred in an input/output operation.
(ii) distinguishes between instance variable and class variables.
Ans. (i) throw
(ii) static
(h) What are library classes ? Give an example. [2]
Ans. Library classes are the predefined classes which are a part of java API.
Ex: String, Scanner
(i) Write one difference between Linear Search and Binary Search . [2]
Ans. Linear search can be used with both sorted and unsorted arrays. Binary
search can be used only with sorted arrays.
SECTION – B (60 Marks)
Attempt any four questions from this Section.
The answers in this Section should consist of the Programs in either Blue J
environment or any program environment with Java as the base.
Each program should be written using Variable descriptions/Mnemonic Codes
such that the logic of the program is clearly depicted.
Flow-Charts and Algorithms are not required.
Question 4.
Ans.
import java.util.Scanner;
int bno;
int phno;
String name;
int days;
int charge;
Write a program to input and sort the weight of ten people. Sort and display
them in descending order using the selection sort technique. [15]
Ans.
import java.util.Scanner;
Write a program to input a number and print whether the number is a special
number or not. (A number is said to be a special number, if the sum of the
factorial of the digits of the number is same as the original number). [15]
Ans.
import java.util.Scanner;
}
Question 7
Ans.
import java.util.Scanner;
}
Question 8.
Design a class to overload a function compare ( ) as follows: [15]
(a) void compare (int, int) – to compare two integer values and print the
greater of the two integers.
(b) void compare (char, char) – to compare the numeric value of two
character with higher numeric value
(c) void compare (String, String) – to compare the length of the two strings
and print the longer of the two.
Ans.
public class Overloading {
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 the 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 bracket [ ]
SECTION – A (40 Marks)
Question 1.
(a) Define the term Byte Code. [2]
Ans. The Java compiler compiles the source programs into an intermediate
code called the Java Byte Code which is interpreted by the Java Virtual
Machine (JVM)
(b) What do you understand by type conversion? [2]
Ans. Type conversion or casting is the conversion of the data type of a literal
from one type to another. There are tow types of types of casting – implicit
casting and explicit casting.
(c) Name two jump statements and their use. [2]
Ans. break and continue are the two jump statements in Java. break is used to
force early termination of a loop. continue is used to move to the next
iteration of the loop while skipping the remaining code in the current iteration.
(d) What is Exception ? Name two Exception Handling Blocks. [2]
Ans. An Exception is an error which occurs during the execution of a program.
The exception handling blocks are try, catch and finally.
(e) Write two advantages of using functions in a program. [2]
Ans. i) Function make code reusable.
ii) Functions improve modularity and facilitate easy debugging.
Ads by websave. More Info | Hide These Ads
Question 2.
(a) State the purpose and return data type of the following String functions:
[2]
(i) indexOf ( )
(ii) compareTo ( )
Ans. i) indexOf() returns the index of the character or String passed as the
parameter in the string on which is invoked.
Return type is int.
ii) compareTo() lexicographically compares the String passed as an argument
to the String on which it is invoked.
Return type is int.
(b) What is the result stored in x, after evaluating the following expression [2]
int x = 5;
x = x++ * 2 + 3 * –x;
Ans. x = 5 * 2 + 3 * -6
x = 10 – 18
x = -8
(c) Differentiate between static and non-static data members. [2]
Ans. i) static variables belong to the class and all object share a single
instance of the static variables. Non static varaiables belong to the objects.
Each object has a copy of these members.
ii) static functions can access only static data members. Non static function
can access both static and non static data members.
(d) Write the difference between length and length() functions. [2]
Ans. length is a property of an array which gives the size of the array. length()
is a function of the String class which returns the size of the String.
(e) Differentiate between private and protected visibility modifiers. [2]
Ans. private members are accessible only in the class in which they have been
defined. protected members are accessible in the class in which they have
been defined as well in the sub classes of that class.
Question 3
(a) What do you understand by data abstraction? Explain with an example.[2]
Ans. Abstraction refers to representing the essential features of a system
without considering all the details. Example: When we drive a car, we
concentrate on how to drive it without bothering ourselves on how the engine
works and other things.
(b) What will be the output of the following code?
(i) [2]
int m=2;
int n=15;
for(int i = 1; i<5; i++)
m++; –n;
System.out.println("m=" +m);
System.out.println("n="+n);
Ans.
m=1
n=14
Note that there is no semicolon at the end of the loop. So, it is an empty loop
and does’t affect the values of m and n.
(ii) [2]
char x = 'A' ; int m;
m=(x=='a') ? 'A' : ‘a’;
System.out.println("m="+m);
Ans.
m=a
(c) Analyse the following program segment and determine how many times
tne
loop will be executed and what will be the output of the program segment. [2]
int k=1, i=2;
while (++i<6)
k*=i;
System.out.println(k);
Ans. Following are the iterations of the loop
3 < 6 ---- k = 1 * 3 = 3
4 < 6 ---- k = 3 * 4 = 15
5 < 6 ---- k = 12 * 5 = 60
6 < 6 ---- false
60
(d) Give the prototype of a function check which receives a character ch and
an integer n and returns true or false. [2]
Ans.
public boolean function(char ch, int n)
(e) State two features of a constructor. [2]
Ans. i) A constructor has the same name as that of a class.
ii) A constructor does not have any return type.
iii) A constructor is automatically called during object creation.
(f) Write a statement each to perform the following task on a string:
(i) Extract the second last character of a word stored in the variable wd. [2]
Ans.
char ch = wd.charAt(wd.length()-2);
(ii) Check if the second character of a string str is in uppercase. [2]
Ans.
boolean result = Character.isUpperCase(str.charAt(1));
(g) What will the following functions return when executed? [2]
(j) Math.max(-17, -19)
(ii) Math.ceil(7.8)
Ans. i) -17
ii) 8.0
(h) (i) Why is an object called an instance of a class?
(ii) What is the use of the keyword import? [2]
Ans. i) An object is called an instance of a class because the objects contains a
copy of all the instance variables of the class.
ii) The import keyword is used to import classes from external packages.
SECTION – B (60 Marks)
Attempt any four questions from this Section.
The answers in this Section should consist of the Programs in either Blue J
environment or any program environment with Java as the base.
Each program should be written using Variable descriptions/Mnemonic Codes
such that the logic of the program is clearly depicted.
Flow-Charts and Algorithms are not required.
Question 4.
Write a program to perform binary search on a list of integers given below, to
search for an element input by the user. If it is found display the element
along with its position, otherwise display the message “Search element not
found”. [15]
5,7,9,11,15,20,30,45,89,97
Ans.
import java.util.Scanner;
Ans.
import java.util.Scanner;
String name;
int age;
int m1, m2, m3;
int maximum;
double average;
public Student() {
}
public Student(String n, int a, int marks1, int marks2, int marks3, int max,
double avg) {
name = n;
age = a;
m1 = marks1;
m2 = marks2;
m3 = marks3;
maximum = max;
average = avg;
}
}
Sample Output:
Enter name: Sai
Enter age: 20
Enter marks1: 95
Enter marks2: 100
Enter marks3: 99
Name: Sai
Age: 20
Marks1 95
Marks2 100
Marks3 99
Maximum: 100
Average: 98.0
Question 6
Shasha Travels Pvt. Ltd. gives the following discount to its customers: [15]
Ticket amount Discount
Above Rs 70000 – 18%
Rs 55001 to Rs 70000 – 16%
Rs 35001 to Rs 55000 – 12%
Rs 25001 to Rs 35000 – 10%
less than Rs 25001 – 2%
Write a program to input the name and ticket amount for the customer and
calculate the discount amount and net amount to be paid. Display the output
in the following format for each customer :
(Assume that there are 15 customers, first customer is given the serial no
(SlNo.) 1, next customer 2 … and so on.
Ans.
import java.util.Scanner;
Write a menu driven program to accept a number and check and display
whether it is a Prime Number or not OR an Automorphic Number or not. (Use
switch-case statement). [15]
Example:
INPUT OUTPUT
P[] Q [] R []
4 19 4
6 23 6
1 7 1
2 8 2
3 3
10 10
19
23
7
8
Ans.
import java.util.Scanner;
Sample Output:
ICSE Computer Applications 2009
(Two Hours)
Attempt all questions from Section A and any four four questions from Section
B. The intended marks for each question are given in brackets []
Section A
Question 1:
(a) Why is a class called a factory of objects? [2]
Ans. A class is known as a factory of objects because objects are instantiated
from classes. Each object gets a copy of the instance variables present in the
class. It is like a factory producing objects.
(b) State the difference between a boolean literal and a character literal. [2]
Ans. i) A boolean literal can store one of two values – true and false. A
character literal can store a single Unicode character.
ii) The memory required by a boolean literal depends on the implementation.
The memory required by a character literal is 2 bytes.
(c) What is the use and syntax of a ternary operator? [2]
Ans. The ternary operator is a decision making operator which can be used to
replace certain if else statement.
Syntax: condition ? expression1 : expression2
(d) Write one word answer for the following: [2]
i) A method that converts a string to a primitive integer data type
ii) The default initial value of a boolean variable data type
Ans. i) Integer.parseInt()
ii) false
(e) State one similarity and one difference between while and for loop. [2]
Ans. A while loop contains only a condition while a for loop contains
initialization, condition and iteration.
Ads by websave. More Info | Hide These Ads
Question 2:
(a) Write the function prototype for the function “sum” that takes an integer
variable (x) as its argument and returns a value of float data type. [2]
Ans.
public float sum(int x)
(b) What is the use of the keyword this? [2]
Ans. this refers to the object on which the method has been invoked. If an
instance variable and a local variable in a method have the same name, the
local variable hides the instance variable. The keyword this can be used to
access the instance variable as shown in the example below:
public class thisKeywordExample {
int a; // varaible 1
}
}
(c) Why is a class known as a composite data type? [2]
Ans. A class is composed of instance variables which are of different data
types. hence, a class can be viewed as a composite data type which is
composed of primitive and other composite data types.
(d) Name the keyword that: [2]
i) is used for allocating memory to an array
ii) causes the control to transfer back to the method call
Ans. i) new
ii) return
(e) Differentiate between pure and impure functions. [2]
Ans. i) A pure function does not change the state of the object whereas an
impure function changes the state of the object by modifying instance
variables.
ii) get functions are examples of pure functions and set functions are
examples if impure functions.
Question 3:
(d) When there are multiple definitions with the same function name, what
makes them different from each other? [2]
Ans. The function prototype make multiple definitions of a function different
from each other. Either the number, type or order or arguments should be
different for the functions having identical names.
Q3. (e) Given that int x[][] = { {2,4,6}, {3,5,7} };
What will be the value of x[1][0] and x[0][2] ? [2]
Ans. We can write the array as
As there is no break statement for case ‘b’, statements in case ‘c’ will also be
printed when opn = ‘b’.
ii) Output will be
Wrong Input
As ‘x’ doesn’t match with either ‘a’, ‘b’ or ‘c’, the default statement will be
executed.
iii) Output will be
Platform Independent
(g) Consider the following code and answer the questions that follow: [4]
class academic
{
int x, y;
void access()
{
int a, b;
academic student = new academic();
System.out.println("Object created");
}
}
i) What is the object name of class academic?
ii) Name the class variables used in the program?
iii) Write the local variables used in the program.
iv) Give the type of function used and its name.
Ans. i) student
ii) x and y
iii) a and b
iv) Type: Non Static
Name: access
Q3 (h) Convert the following segment into an equivalent do loop. [3]
int x,c;
for(x=10,c=20;c>10;c=c-2)
x++;
Ans.
int x, c;
x = 10;
c = 20;
do {
x++;
c = c - 2;
} while (c > 10);
Section B
Question 4
An electronics shop has announced the following seasonal discounts on the
purchase of certain items.
Purchase Amount in Rs. Discount on Laptop Discount on Desktop PC
Write a program based on the above criteria to input name, address, amount
of purchase and type of purchase (L for Laptop and D for Desktop) by a
customer. Compute and print the net amount to be paid by a customer along
with his name and address. [15]
Ans.
import java.util.Scanner;
}
Sample Output:
Enter name: Ram
Enter address: 12-5/6
Enter type of purchase: L
Enter amount of purchase: 200000
Name: Ram
Address: 12-5/6
Net Amount: 180000.0
Question 5:
Write a program to generate a triangle or an inverted triangle till n terms
based upon the user’s choice of triangle to be displayed. [15]
Example 1
Input: Type 1 for a triangle and
type 2 for an inverted triangle
1
Enter the number of terms
5
Output:
1
22
333
4444
55555
Example 2:
Input: Type 1 for a triangle and
type 2 for an inverted triangle
2
Enter the number of terms
6
Output:
666666
55555
4444
333
22
1
Ans.
import java.util.Scanner;
Question 6:
Write a program to input a sentence and print the number of characters found
in the longest word of the given sentence.
For example is S = “India is my country” then the output should be 7. [15]
Ans.
import java.util.Scanner;
Question 7:
i) void num_calc(int num, char ch) with one integer argument and one
character argument, computes the square of integer argument if choice ch is
‘s’ otherwise finds its cube.
ii) void num_calc(int a, int b, char ch) with two integer arguments and one
character argument. It computes the product of integer arguments if ch is ‘p’
else adds the integers.
iii) void num_calc(String s1, String s2) with two string arguments, which
prints whether the strings are equal or not.
Ans.
public class Overloading {
Question 8
Write a menu driven program to access a number from the user and check
whether it is a BUZZ number or to accept any two numbers and to print the
GCD of them. [15]
Ans.
import java.util.Scanner;
}
Sample Output 1:
1. Buzz Number
2. GCD
Enter your choice: 1
Enter a number: 49
49 is a buzz number
Sample Output 2:
1. Buzz Number
2. GCD
Enter your choice: 2
Enter two numbers: 49 77
GCD: 7
Question 9
The annual examination results of 50 students in a class is tabulated as
follows.
Roll no. Subject A Subject B Subject C
… … … …
Write a program to read the data, calculate and display the following:
Ans.
import java.util.Scanner;
If you have any doubts, ask them in the comments section at the bottom of
this page.
ICSE Paper – 2013
Class – X
Subject – Computer Applications
(Two Hours)
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 is meant by precedence of operators? [2]
Ans. Precedence of operators refers to the order in which the operators are
applied to the operands in an expression. For example, * has higher
precedence than +. So, the expression 8 + 2 * 5 will evaluate to 8 + 10 = 18
b) What is a literal? [2]
Ans. A literal is a constant data item. There are different literals like integer
literals, floating point literals and character literals.
c) State the Java concept that is implemented through:
i) a super class and a subclass.
ii) the act of representing essential features without including background
details. [2]
Ans. i) Inheritance
ii) Abstraction
d) Give a difference between constructor and method. [2]
Ans. i)A constructor has no return type which a method has a return type.
ii) The name of the constructor should be the same as that of the class while
the name of a method can be any valid identifier.
iii) A constructor is automatically called upon object creation while methods
are invoked explicitly.
e) What are the types of casting shown by the following examples? [2]
i) double x =15.2;
int y =(int) x;
ii) int x =12;
long y = x;
Ans. i) Explicit casting
ii) Implicit casting
Question 2:
a) Name any two wrapper classes. [2]
Ans. Byte, Short, Integer, Long, Float, Double, Boolean, Character
b) What is the difference between break and continue statements when they
occur in a loop. [2]
Ans. The break statement terminates the loop while the continue statements
current iteration of the loop to be skipped and continues with the next
iteration.
c) Write statements to show how finding the length of a character array and
char[] differs from finding the length of a String object str. [2]
Ans. The length of a character array is found by accessing the length attribute
of the array as shown below:
char[] array = new char[7];
int lengthOfCharArray = array.length;
The length of a String object is found by invoking the length() method which
returns the length as an int.
String str = “java”;
int lengthOfString = str.length();
d) Name the Java keyword that: [2]
(i) indicates a method has no return type.
(ii) stores the address of the currently calling object.
Ans. i) void
ii) this
e) What is an exception? [2]
Ans. An exception is an unforeseen situation that occurs during the execution
of a program. In simpler words, they are the errors that occur during the
execution of a program. The JRE throws an Exception object to indicate an
exception which contains the information related to that exception.
Question 3:
a) Write Java statement to create an object mp4 of class digital. [2]
Ans.
digital mp4 = new digital ();
b) State the values stored in variables str1 and str2 [2]
String s1 = "good";
String s2="world matters";
String str1 = s2.substring(5).replace('t','n');
String str2 = s1.concat(str1);
Ans. s2.substring(5) gives ” matters”. When ‘t’ is replaced with ‘n’, we get ”
manners”.
“good” when concatenated with “world matters” gives “goodworld matters”.
So, str1 = ” manners” and str2 = “goodworld matters”.
c) What does a class encapsulate? [2]
Ans. A class encapsulated the data (instance variables) and methods.
d) Rewrite the following program segment using the if..else statement. [2]
comm =(sale>15000)?sale*5/100:0;
Ans.
if ( sale > 15000 ) {
comm = sale * 5 / 100;
} else {
comm = 0;
}
e) How many times will the following loop execute? What value will be
returned? [2]
int x = 2, y = 50;
do{
++x;
y-=x++;
}while(x<=10);
return y;
Ans. In the first iteration, ++x will change x from 2 to 3. y-=x++ will increase
x to 4. And y becomes 50-3=47.
In the second iteration, ++x will change x from 4 to 5. y-=x++ will increase x
to 6. And y becomes 47-5=42.
In the third iteration, ++x will change x from 6 to 7. y-=x++ will increase x to
8. And y becomes 42-7=35.
In the fourth iteration, ++x will change x from 8 to 9. y-=x++ will increase x
to 10. And y becomes 35-9=24.
In the fifth iteration, ++x will change x from 10 to 11. y-=x++ will increase x
to 12. And y becomes 24-11=13.
Now the condition x<=10 fails.
So, the loop executes five times and the value of y that will be returned is 13.
f) What is the data type that the following library functions return? [2]
i) isWhitespace(char ch)
ii) Math.random()
Ans. i) boolean
ii) double
g) Write a Java expression for ut + ½ ft2 [2]
Ans. u * t + 0.5 * f * Math.pow ( t, 2)
h) If int n[] ={1, 2, 3, 5, 7, 9, 13, 16} what are the values of x and y? [2]
x=Math.pow(n[4],n[2]);
y=Math.sqrt(n[5]+[7]);
Ans. n[4] is 7 and n[2] is 3. So, Math.pow(7,3) is 343.0.
n[5] is 9 and n[7] is 16. So Math.sqrt(9+16) will give 5.0.
Note that pow() and sqrt() return double values and not int values.
i) What is the final value of ctr after the iteration process given below,
executes? [2]
int ctr=0;
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j+=2)
++ctr;
Ans. Outer loop runs five times. For each iteration of the outer loop, the inner
loop runs 3 times. So, the statement ++ctr executes 5*3=15 times and so the
value of ctr will be 15.
j) Name the methods of Scanner class that: [2]
i) is used to input an integer data from standard input stream.
ii) is used to input a string data from standard input stream.
Ans. i) scanner.nextInt()
ii) scanner.next()
SECTION B (60 Marks )
Attempt any four questions from this Section.
The answers in this section should consist of the program in Blue J
environment with Java as the base. Each program should be written using
Variable descriptions / Mnemonics Codes such that the logic of the program is
clearly depicted.
Flow-charts and Algorithms are not required.
Question 4:
int product_code;
String flavour;
String pack_type;
int pack_size;
int product_price;
public FruitJuice() {
product_code = 0;
flavour = "";
pack_type = "";
pack_size = 0;
product_price = 0;
}
Ans.
import java.util.Scanner;
Write a program that encodes a word into Piglatin. To translate word into
Piglatin word, convert the word into uppercase and then place the first vowel
of the original word as the start of the new word along with the remaining
alphabets. The alphabets present before the vowel being shifted towards the
end followed by “AY”.
Sample Input(1): London Sample Output(1): ONDONLAY
Sample Input(2): Olympics Sample Output(2): OLYMPICSAY [15]
Ans.
import java.util.Scanner;
public class Piglatin {
Ans.
public class Overload {
Ans.
class Initialize
x=0;
x=a;
(b) Complete the code given below to create an object of Scanner class: [2]
Ans. Scanner sc=new Scanner (System.in);
© What is an array? Write a statement to declare an integer array of 10 elements[2]
Ans. An array is a structure created in memory to store multiple similar type of data under
a single subscripted variable name.
int a[]=new int[10];
int y=(a<b) ? a : b;
Ans.
x=true
y=36
int n=c+1;
char ch=(char)n;
x+= (x++)+(++x)+x;
Ans.
x=4+4+6+6
=20
System.out.println(Math.min(Math.floor(x),y));
System.out.println(Math.max(Math.ceil(x),y));
Ans.
2.0
3.0
int n=s.length();
System.out.println(s.startsWith(s.substring(5,n)));
System.out.println(s.charAt(2) == s.charAt(6));
Ans.
false
true
b = (s1.equals(s2));
System.out.println(s.indexOf(‘m’));
System.out.println(s.lastIndexOf(‘m’));
Ans.
0
(i) Rewrite the following program segment using while instead of for statement[2]
int f=1,I;
f*=i; System.out.println(f);}
Ans.
int f=1, i=1;
while (i<=5)
f*=i
System.out.println(f);
i++;
static int x = 7;
int y = 2;
System.out.println(x);
Obj.sampleMethod(5);
int a= 6;
System.out.println(a);
}
System.out.println(n);
System.out.println(y);
Member methods:
Void input() : to input and store the accession number, title and author
Void compute(): to accept the number of days late, calculate and display the fine charged t
the rate of Rs. 2 per day
Ans
import java.io.*;
class Library {
int acc_num;
acc_num= Integer.parseInt(obj.readLine());
title= obj.readLine();
author = obj.readLine();
int n= Integer.parseInt(obj.readLine());
int fine=n*2;
System.out.println(“fine : “+fine);
void display() {
System.out.println(“Accession Number\t\tTitle\t\tAuthpr”);
System.out.println(“acc_num+”\t\t”+title+”\t\t”+author);
ob.input();
ob. compute();
ob.display();
Question 5.
Given below is a hypothetical table showing rates of income tax for male citizens below the
age of 65 yrs:
Write a program to input the age, gender(male or female) and taxable income(TI) of a
person.
If the age is more than 65 yrs or the gender is female, display “wrong category”.
If the age is less than or equal to 65 yrs and the gender is male, compute and display the
income tax payable as per the table given above. [15]
Ans.
import java.io.*;
class IncomeTax {
int age=Integer.parseInt(ob.readLine());
double tax=0.0;
if((age>65) || (gender.equals(“female”)))
System.out.println(“wrong category”);
if(TI<160000)
tax=0;
tax=(TI-160000)*(double)10/100*TI;
tax=(TI-500000)*((double)20/100*TI)+34000;
else
tax=(TI-800000)*((double)30/100*TI)+94000;
else
System.out.println(“wrong category”);
}//main ends
} //class ends
Question 6.
Write a program to accept a string. Convert it to uppercase. Count and output the number
of double letter words that exist in the string. [15]
s=s1.toUpperCase();
int l=s.length();
int c=0;
if(s.charAt(p) == s.charAt(p+1))
c++;
}//for ends
System.out.println( c );
}//method ends
}//class ends
Question 7.
Design a class to overload a function polygon() as follows: [15]
(i) void polygon(int n, char ch) : with one integer argument and one character type
argument that draws a filled square of side n using the character stored in ch.
(ii) void polygon(int x, int y) : with two integer arguments that draws a filled rectangle of
length x and breadth y, using the symbol ‘@’
(iii) void polygon() : with no argument that draws a filled triangle shown below:
**
***
Ans.
class Overload {
System.out.print(ch);
System.out.println();
}//method ends
System.out.print(“@”);
System.out.println();
}//method ends
void polygon() {
System.out.print(“*”);
System.out.println();
}//method ends
}//class ends
Question 8.
Using the switch statement, write a menu-driven program to:
(i) Generate and display the first 10 terms of the Fibonacci series 0,1,1,2,3,5,….
(ii) find the sum of the digits of an integer that is input. Eg 15390=18
Ans.
import java.io.*;
class Menudriven {
int ch=Integer.parseInt(ob.readLine());
switch( ch)
case 1:
c=a+b;
System.out.print(“ “+c);
a=b;
b=c;
}//for ends
break;
case 2:
System.out.println”\nenter an integer”);
while(n!=0)
rem= n%10;
n/=10;
sum+=rem;
}//while ends
break;
default:
}//switch ends
}//main ends
}//class ends
Question 9.
Write a program to accept the names of 10 cities in a single dimension string array and
their STD codes in another single dimension integer array. Search for a name of a city input
by the user in the list. If found, display “search successful” and print the name of the city
along with its STD code, or else display the message” search unsuccessful, no such city in
the list”. [15]
Ans.
import java.io.*;
class LinearSearch {
int c=0;
if(city[p] . equals(ele))
c=1;
break;
} //end if
}//end for
if(c==1)
System.out.print(“Search Successful”);
else
}//end method
}//end class