2016
2016
Section A
Question 1
Answer
Encapsulation is a mechanism that binds together code and the data it manipulates. It keeps
them both safe from the outside world, preventing any unauthorised access or misuse. Only
member methods, which are wrapped inside the class, can access the data and other methods.
Answer
Keywords are reserved words that have a special meaning to the Java compiler. Example:
class, public, int, etc.
Answer
1. java.io
2. java.util
(d) Name the type of error ( syntax, runtime or logical error) in each case given below:
Answer
1. Runtime Error
2. Syntax Error
1. p = x.length
2. q = x[2] + x[5] * x[1]
Answer
1. 6
2. q = x[2] + x[5] * x[1]
q = 7 + 10 * 3
q = 7 + 30
q = 37
Question 2
Answer
equals() ==
It is used to check if the contents of two strings It is used to check if two variables refer to the same
are same or not object in memory
Example: Example:
String s1 = new String("hello"); String s1 = new String("hello");
String s2 = new String("hello"); String s2 = new String("hello");
boolean res = s1.equals(s2); boolean res = s1 == s2;
System.out.println(res); System.out.println(res);
The output of this code snippet is true as contents The output of this code snippet is false as s1 and s2
of s1 and s2 are the same. point to different String objects.
(b) What are the types of casting shown by the following examples:
Answer
1. Explicit Cast.
2. Implicit Cast.
(c) Differentiate between formal parameter and actual parameter.
Answer
They represent the values received by the called They represent the values passed to the called
function. function.
Answer
Answer
1. public
2. private
Question 3
1. "MISSISSIPPI".indexOf('S') + "MISSISSIPPI".lastIndexOf('I')
2. "CABLE".compareTo("CADET")
Answer
1. 2 + 10 = 12
⇒ 66 - 68
2. ASCII Code of B - ASCII Code of D
⇒ -2
1. Math.ceil(4.2)
2. Math.abs(-4)
Answer
1. 5.0
2. 4
Answer
T=A2+B2+C2T=A2+B2+C2
Answer
if (x%2 == 0)
System.out.print("EVEN");
else
System.out.print("ODD");
Answer
int m = 5, n = 10;
while (n>=1)
{
System.out.println(m*n);
n--;
}
Answer
int m = 5;
for (int n = 10; n >= 1; n--) {
System.out.println(m*n);
}
(g) Write one difference between primitive data types and composite data types.
Answer
Primitive Data Types are built-in data types defined by Java language specification whereas
Composite Data Types are defined by the programmer.
(h) Analyze the given program segment and answer the following questions:
1. Write the output of the program segment.
2. How many times does the body of the loop gets executed ?
Output
5
10
Loop executes 3 times.
m Output Remarks
5 5 1st Iteration
5
10 2nd Iteration
10
5
15 3rd Iteration — As m % 3 becomes true, break statement exists the loop.
10
Answer
⇒ a = 7 + (7 + 9 + 8 + 8)
⇒ a = 7 + 32
⇒ a = 39
1. isLetterOrDigit(char)
2. replace(char, char)
Answer
1. boolean
2. String
Section B
Question 4
Member methods:
(i) BookFair() — Default constructor to initialize data members
(ii) void Input() — To input and store the name and the price of the book.
(iii) void calculate() — To calculate the price after discount. Discount is calculated based on
the following criteria.
Price Discount
More than ₹1000 and less than or equal to ₹3000 10% of price
(iv) void display() — To display the name and price of the book after discount.
Write a main method to create an object of the class and call the above member methods.
Answer
import java.util.Scanner;
public BookFair() {
bname = "";
price = 0.0;
}
public void input() {
Scanner in = new Scanner(System.in);
System.out.print("Enter name of the book: ");
bname = in.nextLine();
System.out.print("Enter price of the book: ");
price = in.nextDouble();
}
price -= disc;
}
Output
Question 5
Using the switch statement, write a menu driven program for the following:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Answer
import java.util.Scanner;
public class KboatPattern
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Floyd's triangle");
System.out.println("Type 2 for an ICSE pattern");
switch (ch) {
case 1:
int a = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(a++ + "\t");
}
System.out.println();
}
break;
case 2:
String s = "ICSE";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j <= i; j++) {
System.out.print(s.charAt(j) + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
Output
Question 6
Special words are those words which start and end with the same letter.
Example: EXISTENCE, COMIC, WINDOW
Palindrome words are those words which read the same from left to right and vice-
versa.
Example: MALYALAM, MADAM, LEVEL, ROTATOR, CIVIC
All palindromes are special words but all special words are not palindromes.
Write a program to accept a word. Check and display whether the word is a palindrome or
only a special word or none of them.
Answer
import java.util.Scanner;
if (isPalin) {
System.out.println("Palindrome");
}
else {
System.out.println("Special");
}
}
else {
System.out.println("Neither Special nor
Palindrome");
}
}
}
Output
Question 7
(i) void sumSeries(int n, double x): with one integer argument and one double argument to
find and display the sum of the series given below:
void sumSeries() {
long sum = 0, term = 1;
for (int i = 1; i <= 20; i++) {
term *= i;
sum += term;
}
System.out.println("Sum=" + sum);
}
}
Output
Question 8
Write a program to accept a number and check and display whether it is a Niven number or
not.
(Niven number is that number which is divisible by its sum of digits.).
Example:
Consider the number 126. Sum of its digits is 1 + 2 + 6 = 9 and 126 is divisible by 9.
Answer
import java.util.Scanner;
int digitSum = 0;
while (num != 0) {
int digit = num % 10;
num /= 10;
digitSum += digit;
}
/*
* digitSum != 0 check prevents
* division by zero error for the
* case when users gives the number
* 0 as input
*/
if (digitSum != 0 && orgNum % digitSum == 0)
System.out.println(orgNum + " is a Niven number");
else
System.out.println(orgNum + " is not a Niven
number");
}
}
Output
Question 9
Write a program to initialize the seven Wonders of the World along with their locations in
two different arrays. Search for a name of the country input by the user. If found, display the
name of the country along with its Wonder, otherwise display "Sorry not found!".
Seven Wonders:
CHICHEN ITZA, CHRIST THE REDEEMER, TAJ MAHAL, GREAT WALL OF CHINA,
MACHU PICCHU, PETRA, COLOSSEUM
Locations:
MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Examples:
Country name: INDIA
Output: TAJ MAHAL
Answer
import java.util.Scanner;
if (i == locations.length)
System.out.println("Sorry Not Found!");
}
}
Output