Computer Board Paper 2012 With Solutions
Computer Board Paper 2012 With Solutions
Question 1
(a) Give one example each of a primitive data type and a composite data type.
Answer
1. int
2. array
(b) Give one point of difference between unary and binary operators.
Answer
unary operators operate on a single operand whereas binary operators operate on two operands.
(c) Differentiate between call by value or pass by value and call by reference or pass by reference.
Answer
Values of actual parameters are copied to formal Reference of actual parameters is passed to formal
parameters. parameters.
Changes made to formal parameters are not reflected Changes made to formal parameters are reflected
back to actual parameters. back to actual parameters.
2as+u22as+u2
Answer
Math.sqrt(2 * a * s + u * u)
(e) Name the type of error (syntax, runtime or logical error) in each case given below:
3. Missing semicolon.
Answer
1. Runtime error
2. Logical error
3. syntax error
Question 2
(a) Create a class with one integer instance variable. Initialize the variable using:
1. default constructor
2. parameterized constructor
Answer
class Number {
int a;
public Number() {
a = 0;
public Number(int x) {
a = x;
Answer
Answer
An array is a structure to store a number of values of the same data type in contiguous memory
locations. The following statement declares an integer array of 10 elements:
1. Makes several passes through the array, selecting the next smallest item in the array each
time and placing it where it belongs in the array.
2. At each stage, compares the sought key value with the key value of the middle element of
the array.
Answer
1. Selection Sort
2. Binary Search
(e) Differentiate between public and private modifiers for members of a class.
Answer
public modifier makes the class members accessible both within and outside their class whereas
private modifier makes the class members accessible only within the class in which they are
declared.
Question 3
(a) What are the values of x and y when the following statements are executed?
Answer
Output
x = true
y = 36
Explanation
The ternary operator (a > b)? true : false returns true as its condition a > b is true so it returns its first
expression that is true.
The ternary operator (a < b)? a : b returns b as its condition a < b is false so it returns its second
expression that is b. Value of b is 36 so y also becomes 36.
char c = 'A';
int n = c + 1;
char ch = (char)n;
Answer
Value of n is 66 and ch is B.
int n = c + 1, here due to implicit conversion, 'A' will be converted to its ASCII value 65. 1 is added to
it making the value of n 66.
char ch = (char)n, here through explicit conversion 66 is converted to its corresponding character that
is 'B' so value of ch becomes B.
(c) What will be the result stored in x after evaluating the following expression?
int x = 4;
x += (x++) + (++x) + x;
Answer
⇒ x = x + ((x++) + (++x) + x)
x += (x++) + (++x) + x
⇒ x = 4 + (4 + 6 + 6)
⇒ x = 4 + 16
⇒ x = 20
System.out.println(Math.min(Math.floor(x), y));
System.out.println(Math.max(Math.ceil(x), y));
Answer
Output
2.0
3.0
Explanation
String s = "Examination";
int n = s.length();
System.out.println(s.startsWith(s.substring(5, n)));
System.out.println(s.charAt(2) == s.charAt(6));
Answer
Output
false
true
Explanation
Putting the value of n in s.substring(5, n), it becomes s.substring(5, 11). This gives "nation". As s does
not start with "nation" so s.startsWith(s.substring(5, n)) returns false.
Answer
1. Float.parseFloat()
2. Character.isUpperCase()
(g) State the data type and values of a and b after the following segment is executed:
a = (s1.compareTo(s2));
b = (s1.equals(s2));
Answer
Data type of a is int and value is 2. Data type of b is boolean and value is false.
String s = "malayalam";
System.out.println(s.indexOf('m'));
System.out.println(s.lastIndexOf('m'));
Answer
Output
Explanation
(i) Rewrite the following program segment using while instead of for statement.
int f = 1, i;
f *= i;
System.out.println(f);
Answer
int f = 1, i;
while (i <= 5) {
f *= i;
System.out.println(f);
i++;
(j) In the program given below, state the name and the value of the:
3. local variable
4. instance variable
class MyClass{
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);
Answer
2. Class variable is x
3. Local variable is a
4. Instance variable is y
Section B
Question 4
Member methods:
1. void input() — To input and store the accession number, title and author.
2. void compute() — To accept the number of days late, calculate and display the fine charged
at the rate of Rs. 2 per day.
Write a main method to create an object of the class and call the above member methods.
Answer
import java.util.Scanner;
void input() {
title = in.nextLine();
author = in.nextLine();
accNum = in.nextInt();
void compute() {
}
void display() {
System.out.println("Accession Number\tTitle\tAuthor");
obj.input();
obj.display();
obj.compute();
Output
Question 5
Given below is a hypothetical table showing rates of income tax for male citizens below the age of 65
years:
Taxable income (TI) in ₹ Income Tax in ₹
Is greater than Rs. 1,60,000 and less than or equal to Rs. 5,00,000. (TI - 1,60,000) x 10%
Is greater than Rs. 5,00,000 and less than or equal to Rs. 8,00,000 [(TI - 5,00,000) x 20%] + 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.
Answer
import java.util.Scanner;
double ti = in.nextDouble();
System.out.print("Wrong Category");
else {
tax = 0;
else if (ti <= 500000)
else
Output
Question 6
Write a program to accept a string. Convert the string into upper case letters. 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
Answer
import java.util.Scanner;
String s = in.nextLine();
int count = 0;
Output
Question 7
1. void polygon(int n, char ch) — with one integer and one character type argument to draw a
filled square of side n using the character stored in ch.
2. void polygon(int x, int y) — with two integer arguments that draws a filled rectangle of length
x and breadth y, using the symbol '@'.
3. void polygon() — with no argument that draws a filled triangle shown below:
Example:
2. Input value of x = 2, y = 5
Output:
@@@@@
@@@@@
3. Output:
*
**
***
Answer
System.out.print(ch);
System.out.println();
System.out.print('@');
System.out.println();
System.out.print('*');
System.out.println();
}
}
obj.polygon(2, 'o');
System.out.println();
obj.polygon(2, 5);
System.out.println();
obj.polygon();
Output
Question 8
Answer
import java.util.Scanner;
int ch = in.nextInt();
switch (ch) {
case 1:
int a = 0, b = 1;
int term = a + b;
a = b;
b = term;
break;
case 2:
while (num != 0) {
num /= 10;
break;
default:
System.out.println("Incorrect choice");
break;
Output
Question 9
Write a program to accept the names of 10 cities in a single dimensional string array and their STD
(Subscribers Trunk Dialling) codes in another single dimension integer array. Search for the 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".
Answer
import java.util.Scanner;
stdCodes[i] = in.nextLine();
int idx;
if (city.compareToIgnoreCase(cities[idx]) == 0) {
break;
System.out.println("Search Successful");
else {
System.out.println("Search Unsuccessful");
Output