2012
2012
Section A
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 forma
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.
Math.sqrt(2 * a * s + u * u)
(e) Name the type of error (syntax, runtime or logical error) in each case given below:
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;
}
}
(b) Complete the code below to create an object of Scanner class:
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?
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.
(b) State the values of n and ch.
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.
(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
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:
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
0
8
Explanation
(i) Rewrite the following program segment using while instead of for statement.
int f = 1, i;
for(i = 1; i <= 5; 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:
class MyClass{
static int x = 7;
int y = 2;
public static void main(String args[]){
MyClass obj = new MyClass();
System.out.println(x);
obj.sampleMethod(5);
int a = 6;
System.out.println(a);
}
void sampleMethod(int n){
System.out.println(n);
System.out.println(y);
}
}
Answer
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.
3. void display() — To display the details in the following format:
Answer
import java.util.Scanner;
void input() {
Scanner in = new Scanner(System.in);
System.out.print("Enter book title: ");
title = in.nextLine();
System.out.print("Enter author: ");
author = in.nextLine();
System.out.print("Enter accession number: ");
accNum = in.nextInt();
}
void compute() {
Scanner in = new Scanner(System.in);
System.out.print("Enter number of days late: ");
int days = in.nextInt();
int fine = days * 2;
System.out.println("Fine = Rs." + fine);
}
void display() {
System.out.println("Accession Number\tTitle\tAuthor");
System.out.println(accNum + "\t\t" + title + "\t" +
author);
}
Output
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 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;
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;
}
}
Output
Question 7
Design a class to overload a function polygon() as follows:
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:
Answer
Output
Question 8
Answer
import java.util.Scanner;
switch (ch) {
case 1:
int a = 0, b = 1;
System.out.print(a + " " + b);
for (int i = 3; i <= 10; i++) {
int term = a + b;
System.out.print(" " + term);
a = b;
b = term;
}
break;
case 2:
System.out.print("Enter number: ");
int num = in.nextInt();
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
System.out.println("Sum of Digits " + " = " +
sum);
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;
public class KboatStdCodes
{
public static void main(String args[]) {
final int SIZE = 10;
Scanner in = new Scanner(System.in);
String cities[] = new String[SIZE];
String stdCodes[] = new String[SIZE];
System.out.println("Enter " + SIZE +
" cities and their STD codes:");
int idx;
for (idx = 0; idx < SIZE; idx++) {
if (city.compareToIgnoreCase(cities[idx]) == 0) {
break;
}
}
Output