Professional Documents
Culture Documents
ICSE Question Answer 2016
ICSE Question Answer 2016
(d) Name the type of error (syntax, runtime or logical error) in each case
given below: [2]
(i) Math.sqrt (36-45)
(ii) int a;b;c;
Ans. (i) Runtime error
Math.sqrt(36-45) = Math.sqrt(-9) which cannot be computed as square root of a
negative number is not defined. Therefor, a runtime error will be thrown.
(ii) Syntax error
Multiple variables can be defined in one of the following ways
int a, b, c;
int a; int b; int c;
(a) State the difference between == operator and equals() method. [2]
Ans. == compares if the two objects being compared refer to the instance while
equals() method which can be overridden by classes generally compares if the
contents of the objects are equals.
(b) What are the types of casting shown by the following examples: [2]
(i) char c = (char)120;
(ii) int x = ‘t’;
Ans. (i) Explicit casting
(ii) Implicit casting
Question 3.
(a) Give the output of the following string functions : [2]
(i) “MISSISSIPPI”.indexOf(‘S’) + “MISSISSIPPI”.lastIndexOf(‘I’)
(ii) “CABLE”.compareTo(“CADET”)
Ans.
(i) “MISSISSIPPI”.indexOf(‘S’) + “MISSISSIPPI”.lastIndexOf(‘I’)
= 2 + 10
= 12
(ii) String’s compareTo method compares ASCII values of characters starting
from the left.
“CABLE”.compareTo(“CADET”)
The first and seconds characters are the same in both the strings. So, we
compare the third characters.
Compare B and D = ASCII value of B – ASCII value of D
= 66 – 68
= -2
1 if(x%2 == 0)
2 System.out.print("EVEN");
3 else
4 System.out.print("ODD");
Ans.
1 System.out.print(x % 2 == 0 ? "EVEN" : "ODD");
(f) Convert the following while loop to the corresponding for loop : [2]
1 int m = 5, n = 10;
2 while (n>=1)
3{
4 System.out.println(m*n);
5 n–-;
6}
Ans.
1 for(int m=5, n=10; n >=1; n--) {
2 System.out.println(m*n);
3}
(g) Write one difference between primitive data types and composite data
types. [2]
Ans. A primitive data type is not composed of other data types. Ex: int, float,
double while a composite data type is composed of other data types. Ex: class
(h) Analyze the given program segment and answer the following
questions : [2]
(i) Write the output of the program segment
(ii) How many times does the body of the loop gets executed?
1 for(int m=5; m<=20; m+=5)
2{
3 if(m%3 == 0)
4 break;
5 else
6 if(m%5 == 0)
7 System.out.println(m);
8 continue;
9}
Ans.
For loop initialization: m = 5
Loop condition check: m <= 20 = 5 <=20 = true
Loop execution for first time
m%3 == 0
= 5 % 3 == 0
= 2 == 0
= false
else is executed
m%5 == 0
= 5 % 5 == 0
= 0 == 0
= true
5 is printed
Loop increment statement is executed: m+=5 = m = m + 5 = 5 + 5 = 10
Loop condition check: m <= 20 = 10 <=20 = true
Loop body is executed second time and 10 is printed
Loop increment statement is executed: m+=5 = m = m + 5 = 10 + 5 = 15
Loop condition check: m <= 20 = 15 <=20 = true
Loop body is executed third time
m%3 == 0
= 15 % 3 == 0
= 0 == 0
= true
break statement is executed and loop terminates
(i) 5
10
(ii) 3 times
Question 4.
Define a class named BookFair with the following description: [15]
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 DISC
Less than or equal to Rs 1000 2% o
More than Rs 1000 and less than or equal to Rs 3000 10%
More than Rs 3000 15%
(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.
Ans.
1 import java.util.Scanner;
2
3 public class BookFair {
4 private String Bname;
5 private double price;
6
7 public BookFair() {
8 Bname = null;
9 price = 0.0;
10 }
11
12 public void Input() {
13 Scanner scanner = new Scanner(System.in);
14 System.out.print("Enter book name: ");
15 Bname = scanner.nextLine();
16 System.out.print("Enter price: ");
17 price = scanner.nextDouble();
18 }
19
20 public void calculate() {
21 double discountPercentage = 0;
22 if (price <= 1000) {
23 discountPercentage = 2;
24 } else if (price > 1000 && price <= 3000) {
25 discountPercentage = 10;
26 } else if (price > 3000) {
27 discountPercentage = 15;
28 }
29 price = price - (price * discountPercentage / 100);
30 }
31
32 public void display() {
33 System.out.println("Name: " + Bname);
34 System.out.println("Price after discount: " + price);
35 }
36
37 public static void main(String[] args) {
38 BookFair bookFair = new BookFair();
39 bookFair.Input();
40 bookFair.calculate();
41 bookFair.display();
42 }
43 }
Sample output
1 Enter book name: How to Java
2 Enter price: 2500
Question 5.
Using the switch statement, write a menu driven program for the following: [15]
1
23
456
7 8 9 10
11 12 13 14 15
I
IC
ICS
ICSE
Ans.
1 import java.util.Scanner;
2
3 public class Menu {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.println("1. Floyd's triangle");
7 System.out.println("2. ICSE Pattern");
8 System.out.print("Enter your choice: ");
9 int choice = scanner.nextInt();
10 switch (choice) {
11 case 1:
12 System.out.print("Enter n (number of lines): ");
13 int n = scanner.nextInt();
14 int currentNumber = 1;
19 }
20 System.out.println();
21 }
22 break;
23 case 2:
24 System.out.print("Enter word: ");
25 String word = scanner.next();
26 for (int i = 0; i < word.length(); i++) {
27 for (int j = 0; j <= i; j++) {
28 System.out.print(word.charAt(j) + " ");
29 }
30 System.out.println();
31 }
32 break;
33 default:
34 System.out.println("Invalid choice");
35 break;
36 }
37 }
38 }
Sample output 1
1 1. Floyd's triangle
2 2. ICSE Pattern
Sample output 2
1 1. Floyd's triangle
2 2. ICSE Pattern
3 Enter your choice: 2
4 Enter word: ICSE
5I
6I C
7I C S
8I C S E
Sample output 3
1 1. Floyd's triangle
2 2. ICSE Pattern
3 Enter your choice: 3
4 Invalid choice
Question 6.
Special words are those words which starts and ends with the same letter. [15]
Examples:
EXISTENCE
COMIC
WINDOW
Palindrome words are those words which read the same from left to right and
vice-versa.
Example:
MALAYALAM
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 print whether the word is a
palindrome or only special word.
Ans.
1 import java.util.Scanner;
2
3 public class Words {
4
5 public static void main(String[] args) {
6 Scanner scanner = new Scanner(System.in);
7 System.out.print("Enter word: ");
8 String word = scanner.next();
9
10 // Check if word is palindrome
11 String reverse = "";
12 for (int i = word.length() - 1; i >= 0; i--) {
13 reverse = reverse + word.charAt(i);
14 }
15 if (word.equals(reverse)) {
16 System.out.println("Palindrome");
17 }
18
19 // Check if word is a special word
20 char firstLetter = word.charAt(0);
21 char lastLetter = word.charAt(word.length() - 1);
22 if (firstLetter == lastLetter) {
23 System.out.println("Special word");
24 }
25 }
26 }
Sample output 1
1 Enter word: MALAYALAM
2 Palindrome
3 Special word
Sample output 2
1 Enter word: COMIC
2 Special word
Question 7:
Design a class to overload a function SumSeries() as follows: [15]
(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:
s = (x/1) – (x/2) + (x/3) – (x/4) + (x/5) … to n terms
(ii) void SumSeries() – To find and display the sum of the following series:
s = 1 + (1 X 2) + (1 X 2 X 3) + … + (1 X 2 X 3 X 4 X … 20)
Ans.
1 public class Series {
2
3 public void SumSeries(int n, double x) {
4 double sum = 0;
5 for (int i = 1; i <= n; i++) {
6 if (i % 2 == 1) {
7 sum = sum + (x / i);
8 } else {
9 sum = sum - (x / i);
10 }
11 }
12 System.out.println("Sum = " + sum);
13 }
14
15 public void SumSeries() {
16 int sum = 0;
17 for (int i = 1; i <= 20; i++) {
18 int product = 1;
19 for (int j = 1; j <= i; j++) {
20 product = product * j;
21 }
22 sum = sum + product;
23 }
24 System.out.println("Sum = " + sum);
25 }
26 }
Question 8:
Write a program to accept a number and check and display whether it is a Niven
number of not. [15]
(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.
Ans.
1 import java.util.Scanner;
2
3 public class NivenNumber {
4 public static void main(String[] args) {
5 Scanner scanner = new Scanner(System.in);
6 System.out.print("Enter a number: ");
7 int number = scanner.nextInt();
8
9 int sumOfDigits = 0;
10 int copyOfNum = number;
11 while (number > 0) {
12 int remainder = number % 10;
13 number = number / 10;
14 sumOfDigits = sumOfDigits + remainder;
15 }
16
17 if (copyOfNum % sumOfDigits == 0) {
18 System.out.println("Niven number");
19 } else {
20 System.out.println("Not a niven number");
21 }
22
23 }
24 }
Sample output 1
1 Enter a number: 126
2 Niven number
Sample output 2
1 Enter a number: 34
2 Not a niven number
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!” [15]
Seven wonders – CHICHEN ITZA, CHRIST THE RDEEEMER, TAJMAHAL,
GREAT WALL OF CHINA, MACHU PICCHU, PETRA, COLOSSEUM
Locations – MEXICO, BRAZIL, INDIA, CHINA, PERU, JORDAN, ITALY
Example – Country Name: INDIA Output: INDIA – TAJMAHAL
Country Name: USA Output: Sorry Not Found!
Ans.
1 import java.util.Scanner;
2
3 public class SevenWonders {
4 public static void main(String[] args) {
String[] sevenWonders = { "CHICHEN ITZA", "CHRIST THE
5
RDEEEMER", "TAJMAHAL", "GREAT WALL OF CHINA",
6 "MACHU PICCHU", "PETRA", "COLOSSEUM" };
String[] locations =
7
{ "MEXICO", "BRAZIL", "INDIA", "CHINA", "PERU", "JORDAN", "ITALY" };
8
9 Scanner scanner = new Scanner(System.in);
10 System.out.print("Enter country: ");
11 String country = scanner.next();
12
13 // Search country in the array using linear search
14 int index = -1;
15 for (int i = 0; i < locations.length; i++) {
16 if (locations[i].equals(country)) {
17 index = i;
18 }
19 }
20
21 if (index != -1) {
Sample output 1
1 Enter country: INDIA
2 INDIA - TAJMAHAL
Sample output 2
1 Enter country: USA
2 Sorry Not Found!