ICSE Examination 2016 – 17 (Solution)
COMPUTER APPLICATION
Class – X
SECTION – A (40 MARKS)
Attempt all questions
Class – X
SECTION – A (40 MARKS)
Attempt all questions
Question 1:
(a) Define Encapsulation. [2]
Ans: - Encapsulation is the process of wrapping up of data and associated methods into a
single unit (class).
(b) What are keywords? Give an example. [2]
Ans: - Keywords are reserved words which convey special meaning to the compiler. They
cannot be used as identifiers. For example, class, void, etc.
(c) Name any tow library packages. [2]
Ans: - i) java.util
ii) java.io
(d) Name the type of error (Syntax, runtime or logical error) in each case given below:
(i) Math.sqrt(35 – 45);
(ii) int a;b;c; [2]
Ans: - i) Runtime error, because Math.sqrt(– 9) which cannot be computed as square root of
a negative number is not defined.
ii) Syntax error, because multiple variables can be defined in one of the following
ways:
int a, b, c;
int a; int b; int c;
(e) If int x[] = {4, 3, 7, 8, 9, 10}; [2]
what are the values of p and q?
(i) p = x.length
(ii) q = x[2] + x[5] * x[1]
Ans: - i) 6
ii) q = x[2] + x[5] * x[1]
= 7 + 10 * 3
= 7 + 30
=37
Question 2:
(a) State the difference between = = and equals() method. [2]
Ans:
== |
equals() |
i) It is a relational operator. |
i) It is a method of String class. |
ii) It checks for equality of primitive types of values. |
ii) It checks for equality of two String type of values. |
(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
(c) Differentiate between formal parameter and actual parameter. [2]
Ans: - Formal parameters are the parameters defined in the signature of a method. Actual
parameters are values given to the method at the time of method call.
(d) Write the function prototype of the following: [2]
A function PosChar which takes a String argument and a character argument and
returns an integer value.
Ans: - int PosChar(String s, char c)
(e) Name any two types of access specifiers. [2]
Ans: - public, private, protected and default (any two out of these 4).
Question 3:
(a) Give the output of the following String function: [2]
(i) “MISSISSIPPI”.indexOf('S') + “MISSISSIPPI”.lastIndexOf('I')
(ii) “CABLE”.compareTo(“CADET”)
Ans: - i) 2 + 10 = 12
ii) 66 – 68 = – 2
(b) Give the output of the following Math functions: [2]
(i) Math.ceil (4 . 2)
(ii) Math.abs (– 4)
Ans: - i) 5.0
ii) 4
(c) What is parameterized constructor? [2]
Ans: - A parameterized constructor is a constructor that accepts arguments which are used to
initialize the object being created.
(d) Write down Java expression for: [2]
OR,
double T = Math.sqrt((A*A) + (B*B) + (C*C));
(e) Rewrite the following using ternary operator: [2]
if(x%2 == 0)
System.out.print("EVEN");
else
System.out.print("ODD");
Ans: - System.out.println(x % 2 == 0 ? “EVEN” : “ODD”);
(f) Convert the following while loop to the corresponding for loop: [2]
int m = 5, n = 10;
while (n>=1)
{
System.out.println(m*n);
n–-;
}
Ans: - for(int m=5, n=10; n >=1; n--)
System.out.println(m*n);
(g) Write one difference between primitive data types and composite data types. [2]
Ans: - A primitive data types are predefined or inbuilt data types. For example, int, double,
etc.
Composite data types are defined by user and made-up of primitive data types values. For
example, class, array.
(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?
for(int m=5; m<=20; m+=5)
{
if(m%3 == 0)
break;
else
if(m%5 == 0)
System.out.println(m);
continue;
}
Ans: - i) 5
10
ii) 3 times
(i) Write the return type of the following library functions: [2]
(i) isLetterOrDigit(char)
(ii) replace(char, char)
Ans: - i) boolean
ii) String
SECTION – B (60 MARKS)
Attempt any four questions
Question 4:
Define a class named BookFair with the following description: [15]
Instance variables / Data members:
String Bname – stores the name of the book
double price – stores the price of the book
Member methods:
(i) BookFair() – default constructor to initialize data members
(
ii) void Input() – To input and store the name and price of the book
(iii) void calculate() – To calculate the price after discount. Discount is calculated
based on the following criteria.
Price |
Discount |
Less than or equal to ₹ 1000 |
2% of price |
More than ₹ 1000 and less than or equal to ₹ 3000 |
10% of price |
More than ₹ 3000 |
15% 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.
Ans: -
import java.util.*;
public class BookFair
{
String Bname;
double price;
BookFair()
{
Bname = null;
price = 0.0;
}
void Input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter book name: ");
Bname = sc.nextLine();
System.out.print("Enter price: ");
price = sc.nextDouble();
}
void calculate()
{
double discountPercentage = 0;
if (price <= 1000)
discountPercentage = 2;
else if (price <= 3000)
discountPercentage = 10;
else if (price > 3000)
discountPercentage = 15;
price = price - (price * discountPercentage / 100);
}
void display()
{
System.out.println("Name: " + Bname);
System.out.println("Price after discount: " + price);
}
public static void main()
{
BookFair ob = new BookFair();
ob.Input();
ob.calculate();
ob.display();
}
}
Question 5:
Using the switch statement, write a menu driven program for the following: [15]
(i) To print the Floyd‟s triangle [Given below]
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
(ii) To display the following pattern:
I
I C
I C S
I C S E
For an incorrect option, an appropriate error message should be displayed. [15]
Ans: -
import java.util.*;
public class Menu
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("1. Floyd's triangle");
System.out.println("2. ICSE Pattern");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
switch (ch)
{
case 1:
System.out.print("Enter n (number of lines): ");
int n = sc.nextInt();
int currentNumber = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(currentNumber + " ");
currentNumber++;
}
System.out.println();
}
break;
case 2:
System.out.print("Enter word: ");
String word = sc.next();
for (int i = 0; i < word.length(); i++)
{
for (int j = 0; j <= i; j++)
System.out.print(word.charAt(j) + " ");
System.out.println();
}
break;
default:
System.out.println("Invalid choice");
break;
}
}
}
Question 6:
Special words are those words which start and end with the same letter.
Examples:
EXISTENCE
COMIC
WINDOW
Palindrome words are those words which read the same from left to right and viceversa.
Examples:
MALAYALAM
MADAM
LEVEL
ROTATOR
CIVIC
All palindromes are special words, but all special words are not palindromes.
Write a program to accept a word to check and print whether the word is a palindrome
or only special word. [15]
Ans:
class Words
{
public static void main(String s)
{
String reverse = "";
for (int i = s.length() - 1; i >= 0; i--)
reverse = reverse + s.charAt(i);
if (s.equals(reverse))
System.out.println("Palindrome");
char firstLetter = s.charAt(0);
char lastLetter = s.charAt(s.length() - 1);
if (firstLetter == lastLetter)
System.out.println("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:
(ii) void SumSeries() – to find and display the sum of the following series:
s = 1 + (1×2) + (1×2×3) + …. + (1×2×3×4× …. 20)
Ans: -
class Series
{
void SumSeries(int n, double x)
{
double sum = 0;
for (int i = 1; i <= n; i++)
{
if (i % 2 == 1)
sum = sum + (x / i);
else
sum = sum - (x / i);
}
System.out.println("Sum = " + sum);
}
void SumSeries()
{
int sum = 0;
for (int i = 1; i <= 20; i++)
{
int product = 1;
for (int j = 1; j <= i; j++)
product = product * j;
sum = sum + product;
}
System.out.println("Sum = " + sum);
}
}
Question 8:
Write a program to accept a number and check and display whether it is a Niven
number or 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: -
class NivenNumber
{
public static void main(int n)
{
int sumOfDigits = 0;
int copyOfNum = n;
while (n > 0)
{
int remainder = n % 10;
n = n / 10;
sumOfDigits = sumOfDigits + remainder;
}
if (copyOfNum % sumOfDigits == 0)
System.out.println("Niven number");
else
System.out.println("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: -
import java.util.*;
public class SevenWonders
{
public static void main()
{
String[] sevenWonders = { "CHICHEN ITZA", "CHRIST THE
RDEEEMER", "TAJMAHAL", "GREAT WALL OF CHINA",
"MACHU PICCHU", "PETRA", "COLOSSEUM" };
String[] locations = { "MEXICO", "BRAZIL", "INDIA", "CHINA",
"PERU", "JORDAN", "ITALY" };
Scanner sc = new Scanner(System.in);
System.out.print("Enter country: ");
String country = sc.next();
int index = -1;
for (int i = 0; i < locations.length; i++)
{
if (locations[i].equals(country))
index = i;
}
if (index != -1)
System.out.println(locations[index] + " - " +
sevenWonders[index]);
else
System.out.println("Sorry Not Found!");
}
}
0 Comments:
Post a Comment