0% found this document useful (0 votes)
80 views18 pages

ICSE Computer Applications X Answers

Chapter 6 covers library classes and composite data types in Java, including the differences between primitive and composite data types. It explains wrapper classes, autoboxing, unboxing, and provides examples of methods for converting between data types. The chapter also includes mental drills, short answer questions, and programming exercises related to character handling and number manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views18 pages

ICSE Computer Applications X Answers

Chapter 6 covers library classes and composite data types in Java, including the differences between primitive and composite data types. It explains wrapper classes, autoboxing, unboxing, and provides examples of methods for converting between data types. The chapter also includes mental drills, short answer questions, and programming exercises related to character handling and number manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 6

Library Classes

Mental Drill

Tick () the correct option.


1. Which of the following is a composite data type?
a. int b. String
c. char d. float
Ans. b. String
2. Which of the following is not a wrapper class?
a. Byte b. Int
c. Long d. Float
Ans. b. Int
3. Which of the following is valid method of initialising?
a. Integer a=new (5); b. Integer a=Integer(5);
c. Integer a=new Integer(5); d. Integer a=new (“5”);
Ans. d. Integer a=new (“5”);
4. If s=“123”, which among the following will convert it to an integer?
a. int a=Integer(s); b. int a=(int)s;
c. int a=parseInt(s); d. int a=[Link](a);
Ans. d. int a=[Link](a);
5. Which among the following is valid character initialisation?
a. Character c=new Character(‘c’); b. Character c=new Character(“c”);
c. Both a and b d. None of these
Ans. a. Character c=new Character(‘c’);
6. Which among the following methods return a true or a false?
a. toLowerCase() b. toUpperCase()
c. parseInt() d. isUpperCase()
Ans. d. isUpperCase()
7. What package is a part of the wrapper class which is imported by default into all Java programs?
a. [Link] b. [Link]
c. [Link] d. None of these
Ans. b. [Link]

Computer Applications – X (ICSE Course) Answers 200


8. Which class is inherited by the Character and Boolean wrapper classes?
a. Object b. Number
c. Both a and b d. None of these
Ans. a. Object
9. What is, converting a primitive value into an object of the corresponding wrapper class called?
a. Autoboxing b. Unboxing
c. Type Casting d. All of these
Ans. a. Autoboxing
10. Which among the following function is used to check whether a given character is a tab space
or not?
a. isTab() b. isTabSpace()
c. isSpace() d. isWhitespace()
Ans. d. isWhitespace()

SECTION A
A. Answer the following questions.
1. What is composite data type?
Ans. In computer science, a composite data type or compound data type is any data type which can
be constructed in a program using the programming language’s primitive data types and other
composite types.
2. State the difference between primitive and composite data type.
Ans.
Primitive data type Composite data type
Primitives are the fundamental data types Composite data types are user defined types
that are represented by keywords namely and may have any name defined by the user.
byte, short, int, long, char and boolean.

Variables of primitive data type stores values Variables of composite data type stores
of corresponding data type. reference to an object.
It can store only one type of value. It can store data of multiple data type.

The size is predetermined. The size is not predetermined.

201 Library Classes


3. Explain with an example, how objects are passed as a reference.
Ans. Example of how objects are passed as reference:
class PassByReference
{
static void change(Number r)
{
r.a=r.a+5;
r.b=r.b+5;
}
static void call()
{
Number n=new Number(5,6);
[Link](n.a+“\t”+n.b);
change(n);
[Link](n.a+“\t”+n.b);
}
}
will result in the following output when the call( ) function is executed:
5 6
10 1
4. What is a wrapper class? Name the wrapper classes used in java.
Ans. A Wrapper class is a class whose object wraps or contains a primitive data types. Wrapper classes
used in Java- Byte, Short, Integer, Long, Character, Boolean, Float and Double.
5. Draw the wrapper class hierarchy.
Ans.
Object

Number Character Boolean

Byte Short Integer Long Float Double

6. Show with an example, how wrapper class object can be created and initialised?
Ans. Example:
Byte b=new Byte(byte) 10):
7. Explain autoboxing and unboxing in java.
Ans. Converting a primitive value into an object of the corresponding wrapper class is called
autoboxing. Converting an object of a wrapper type to its corresponding primitive value is
called unboxing.

Computer Applications – X (ICSE Course) Answers 202


8. Explain the usage of <Wrapper Class>.parse…( ) method.
Ans. This function is used to convert a string to corresponding primitive data type. This function is
present in all wrapper classes except Character and Boolean wrapper classes. The following
example shows how this function can be used.
byte b=[Link](“12”);
short s=[Link](“12”);
int i=[Link](“123”);
long l=[Link](“98412”);
float f=[Link](“12.3f”);
double d=[Link](“12.46”);
9. What is the difference between isUpperCase() and toUpperCase() method?
Ans. The isUpperCase() method is used to check whether a given character is in uppercase or not.
The toUpperCase() method is used to convert a given character to uppercase.
10. What is the use of isWhitespace() method in Java?
Ans. A whitespace in Java is used as a space, tab, or a new line, and this method determines whether
the given char(ch) is a whitespace or not.
11. Why is a class known as a composite data type?
Ans. A composite data type is one which is composed with various primitive data type. A class defined
with various primitive data types such as int, double etc; so it is known as a composite data type;
and it is used to create objects which hold similar types of values and behaviours (functions).
12. Name the package that contain the wrapper classes.
Ans. [Link]

B. Short answer type questions.


1. Answer as directed:
a. Assign a integer 123 to an int variable a.
b. Convert the variable a to a String and store it in the variable s.
Ans. a. int a=123;
b. String s=[Link](a);
2. Answer as directed:
a. Assign an string “123” to a String variable a.
b. Convert the variable a to an integer and store it in the variable s.
Ans. a. String a=”123”;
b. int s=[Link](a);
3. For the program given below mark the parts ___1___ and __2__ as autoboxing or unboxing.
class Example
{
static void main()

203 Library Classes


{
Integer i;
int sum=0;
for(i=1;i<=10;i++) //___1___
{
sum=sum+i; //__2__
}
[Link](sum);
}
}
Ans. __1__:Autoboxing
__2__:Unboxing
4. Give the output of the following program when the main( ) method is executed:
class Overload
{
static void wrapper(int a)
{
[Link](“int type=”+a);
}
static void wrapper(Integer a)
{
[Link](“Integer type=”+a);
}
static void main()
{
int b=13;
Integer c=new Integer(b);
wrapper(c);
wrapper(b);
}
}
Ans. Output:
Integer type=13
int type=13
5. Write one word answer for the following:
(i) A method that converts a string to a primitive integer data type
(ii) The default initial value of a boolean variable data type
Ans. (i) [Link]()
(ii) false
6. Write the output of the following:
(i) [Link] ([Link](‘R’));
(ii) [Link]([Link](‘j’));

Computer Applications – X (ICSE Course) Answers 204


Ans. (i) true
(ii) J
7. State the method that:
(i) Converts a string to a primitive float data type
(ii) Determines if the specified character is an uppercase character
Ans. (i) [Link]()
(ii) [Link]()
8. State the data type and the value of y after the following is executed:
char x =‘7’;
y = [Link](x);
Ans. y is of boolean type its value is false.
9. Write the return type of the following library functions:
(i) isLetterOrDigit(char)
(ii) parseInt()
Ans. (i) boolean
(ii) int
10. State the data type and value of res after the following is executed:
char ch=‘t’;
res=[Link](ch);
Ans. res is of char type is value is ‘T’.
11. Give the output of the following code:
String A=“26”, B=“100”;
String D=A+B+“200”;
int x=[Link](A);
int y=[Link](B);
int d=x+y;
[Link](“Result 1=”+D);
[Link](“Result 2=”+d);
Ans. Output:
Result 1= 26100200
Result 2=126
12. Write a difference between the functions isUpperCase() and toUpperCase().
Ans. Difference:
isUpperCase() toUpperCase()
Checks whether a given character is in uppercase Converts a given character to uppercase.
or not.
Return type is boolean. Return type is char data type.

205 Library Classes


SECTION B
Write programs for the following:
1. Input a character and check whether it is an alphabet or not, if it is check whether it is in
uppercase or in lower case.
Example:
INPUT: A
Output: Uppercase
Input: 2
Output: Not an alphabet
Ans.
import [Link].*;
class Sol1
{
static void main()
{
Scanner sc=new Scanner([Link]);
char c;
[Link](“Enter a character:”);
c=[Link]().charAt(0);
if ([Link](c))
{
if ([Link](c))
[Link](“Uppercase”);
else
[Link](“Lowercase”);
}
else
[Link](“Not an alphabet”);
}
}
2. Create a function int sum(integer a) which accepts an Integer object as parameter and return
the sum of its digits. In the main() input an integer and find the sum of its digits using the
above method.
Ans. import [Link].*;
class Sol2
{
static int sum(Integer a)
{
int d,s=0;
while(a>0)

Computer Applications – X (ICSE Course) Answers 206


{
d=a%10;
s+=d;
a/=10;
}
return s;
}

static void main()


{
Scanner sc=new Scanner([Link]);
Integer n,s;
[Link](“Enter a Number:”);
n=[Link]();
s=sum(n);
[Link](“Sum:”+s);
}
}
3. Create a function Boolean isPalindrome(Integer a) which checks whether the Integer object ‘a’
is a palindrome number or not. In the main input into an int type variable and check whether
it is a palindrome number or not. Palindrome number is such a number which is same as its
reverse,
example 121, 1331 etc.
Ans. import [Link].*;
class Sol3
{
static Boolean isPalindrome(Integer a)
{
int i,c=0;
for(i=1;i<=a;i++)
{
if(a%i==0)
c++;
}
if(c==2)
return true;
else
return false;
}

static void main()


{
Scanner sc=new Scanner([Link]);

207 Library Classes


Integer n;
boolean f;
[Link](“Enter a Number:”);
n=[Link]();
f=isPalindrome(n);
if(f)
[Link](“Palindrome”);
else
[Link](“Not Palindrome”);
}
}
4. Input 10 characters and find the frequency of uppercase and lowercase characters separately.
For example:
Input: A, b, c, d, E, F, G, H, I, j
Ouput: Uppercase Frequency: 6
Lowecase Frequency: 4
Ans.
import [Link].*;
class Sol4
{
static void main()
{
Scanner sc=new Scanner([Link]);
int i,u=0,l=0;
char c;
[Link](“Enter 10 characters:”);
for(i=1;i<=10;i++)
{
c=[Link]().charAt(0);
if([Link](c))
u++;
else if([Link](c))
l++;
}
[Link](“Uppercase Frequency:”+u);
[Link](“Lowercase Frequency:”+l);
}
}
5. Input 10 characters and concatenate only those characters which are either a letter or a digit.
For example:
Input: A, #, 1, d, E, F, %, H, 5, j
Ouput: A1dEFH5

Computer Applications – X (ICSE Course) Answers 208


Ans.
import [Link].*;
class Sol5
{
static void main()
{
Scanner sc=new Scanner([Link]);
int i;
char c;
String s=“ ”;
[Link](“Enter 10 characters:”);
for(i=1;i<=10;i++)
{
c=[Link]().charAt(0);
if([Link](c))
s+=c;
}
[Link](“Result:”+s);
}
}
6. Input an integer and form a new number by removing all odd digits from it.
For example,
Input: 123456
Output: 246
Ans.
import [Link].*;
class Sol6
{
static void main()
{
Scanner sc=new Scanner([Link]);
int n,d,i,nn;
String s=””;
[Link](“Enter a number:”);
n=[Link]();
for(i=n;i>0;i/=10)
{
d=i%10;
if(d%2==0)
s=d+s;
}
nn=[Link](s);
[Link](“Result:”+nn);
}
}

209 Library Classes


7. Input an integer and remove all even digits and display it. Now check whether the new number
is a perfect number or not.
Ans.
import [Link].*;
class Sol7
{
static void main()
{
Scanner sc=new Scanner([Link]);
int n,d,i,nn,sum=0;
String s=””;
[Link](“Enter a number:”);
n=[Link]();
for(i=n;i>0;i/=10)
{
d=i%10;
if(d%2!=0)
s=d+s;
}
nn=[Link](s);
for(i=1;i<nn;i++)
{
if(nn%i==0)
sum+=i;
}
if(sum==nn)
[Link](“Perfect Number”);
else
[Link](“Not a Perfect Number”);
}
}
8. Input 10 characters and form a new string by concatenating all the characters by converting all
uppercase to lowercase and vice-versa.
For example:
Input: A, b, 1, d, E, F, G, H, @, #
Ouput: aB1Defgh@#
Ans.
import [Link].*;
class Sol8
{
static void main()
{
Scanner sc=new Scanner([Link]);

Computer Applications – X (ICSE Course) Answers 210


int i;
char c;
String ns=””;
[Link](“Enter 10 characters:”);
for(i=1;i<=10;i++)
{
c=[Link]().charAt(0);
if([Link](c))
c=[Link](c);
else if([Link](c))
c=[Link](c);
ns=ns+c;
}
[Link](“Result:”+ns);
}
}
9. Design a class to overload a function sum() as follows:
(a) void sum(int n) – with one int argument that finds the sum of the digits in n.
(b) void sum(Integer n) – with one Integer argument that finds the sum of the digits in n.
Also create the main() method to call the above methods.
Ans.
class Overload
{
void sum(int n)
{
int d,s=0;
while(n>0)
{
d=n%10;
s+=d;
n/=10;
}
[Link](“Sum=”+s);
}
void sum(Integer n)
{
Integer d,s=0;
while(n>0)
{
d=n%10;
s+=d;
n/=10;
}
[Link](“Sum=”+s);

211 Library Classes


}
void main()
{
int a=1234;
Integer b=23498;
sum(a);
sum(b);
}
}
10. Create a class called Number having the following members:
Instance Variables:
a and b of int type
Member Functions:
• Parameterised constructor to initailise a and b.
• void swap() that interchanges the value of a and b.
• void display() to display the data members.
In the main() method input 2 interchange their values using the methods of the above class.
Ans.
import [Link].*;
class Number
{
int a,b;
Number(int x,int y)
{
a=x;
b=y;
}
void swap()
{
int t=a;
a=b;
b=t;
}
void display()
{
[Link](a+” “+b);
}

static void main()


{
Scanner sc=new Scanner([Link]);
int x,y;
[Link](“Enter 2 numbers:”);

Computer Applications – X (ICSE Course) Answers 212


x=[Link]();
y=[Link]();
Number ob=new Number(x,y);
[Link]();
[Link]();
}
}
11. Create a class called StringNumber having the following members:
Instance Variables:
s of String type
n of int type
Member functions:
• StringNumber(String t) – to initialize s with t that contains a number (example “123”) and
n with 0.
• void assign() – assign the number stored in s to n after converting it to an integer.
• void largest() – find the largest digit in n and display it.
Also create the main to show the implementation.
Ans.
import [Link].*;
class StringNumber
{
String s;
int n;
StringNumber(String t)
{
s=t;
n=0;
}
void assign()
{
n=[Link](s);
}
void largest()
{
int i,d,l=0;
for(i=n;i>0;i/=10)
{
d=i%10;
if(l==0)
l=d;
if(d>l)
l=d;
}
[Link](“Largest Digit:”+l);

213 Library Classes


}

static void main()


{
Scanner sc=new Scanner([Link]);
String s;
[Link](“Enter a number:”);
s=[Link]();
StringNumber ob=new StringNumber(s);
[Link]();
[Link]();
}
}
12. Create a function Boolean isBouncy(Integer n) to check whether it is a Bouncy Number or not.
Increasing Number : Working from left-to-right if no digit is exceeded by the digit to its left it
is called an increasing number; for example, 22344.
Decreasing Number : Similarly if no digit is exceeded by the digit to its right it is called a
decreasing number; for example, 774410.
Bouncy Number : We shall call a positive integer that is neither increasing nor decreasing a
“bouncy” number; for example, 155349. Clearly there cannot be any bouncy numbers below
100.
Ans.
class Sol12
{
Boolean isBouncy(Integer n)
{
int d,i,fa=0,fd=0,sd;
for(i=n;i>9;i/=10)
{
d=i%10;
sd=(i/10)%10;
if(sd>d)
fa=1;
if(sd<d)
fd=1;
}
if(fa==1 && fd==1)
return true;
else
return false;
}
}

Computer Applications – X (ICSE Course) Answers 214


13. Design a program to accept a positive whole number and find the binary equivalent of the
number and count the number of 1’s in it and display whether it is a Evil number or not with
an appropriate message. Output the result in format given below:
Example 1
INPUT : 15
BINARY EQUIVALENT : 1111
NO. OF 1’s : 4
OUTPUT : EVIL NUMBER
Example 2
INPUT : 26
BINARY EQUIVALENT : 11010
NO. OF 1’s : 3
OUTPUT : NOT AN EVIL NUMBER
Ans.
import [Link].*;
class Sol13
{
static void main()
{
Scanner sc=new Scanner([Link]);
int n,d,i,c=0;
String s=“ ”;
[Link](“Enter a number:”);
n=[Link]();
for(i=n;i>0;i/=2)
{
d=i%2;
if(d==1)
c++;
s=d+s;
}
[Link](“Binary Equivalent:”+s);
[Link](“No. of 1’s:”+c);
if(c%2==0)
[Link](“Evil Number”);
else
[Link](“Not an Evil Number”);
}
}

215 Library Classes


14. Write a Program in Java to input a number and check whether it is a Fascinating Number or
not.
Fascinating Numbers : Some numbers of 3 digits or more exhibit a very interesting property.
The
property is such that, when the number is multiplied by 2 and 3, and both these products
are concatenated with the original number, all digits from 1 to 9 are present exactly once,
regardless of the number of zeroes.
Let’s understand the concept of Fascinating Number through the following example:
Consider the number 192,
192 x 1 = 192
192 x 2 = 384
192 x 3 = 576
Concatenating the results : 192384576
It could be observed that ‘192384576’ consists of all digits from 1 to 9 exactly once. Hence, it
could be concluded that 192 is a Fascinating Number.
Some examples of fascinating Numbers are : 192, 219, 273, 327, 1902, 1920, 2019, etc.
Ans.
import [Link].*;
class Sol14
{
static void main()
{
Scanner sc=new Scanner([Link]);
long n,d,i,t,nn,f=0,j,c;
String s=””;
[Link](“Enter a number:”);
n=[Link]();
for(i=1;i<=3;i++)
{
t=n*i;
s=s+t;
}
nn=[Link](s);
for(i=1;i<=9;i++)
{
c=0;
for(j=nn;j>0;j/=10)
{
d=j%10;
if(d==i)
c++;
}

Computer Applications – X (ICSE Course) Answers 216


if(c!=1)
f=1;
}
if(f==0)
[Link](“Fascinating number”);
else
[Link](“Not a Fascinating number”);
}
}
15. Write a program to accept a number and check and display whether it is a Niven number of
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.
Ans.
import [Link].*;
class Sol15
{
static void main()
{
Scanner sc=new Scanner([Link]);
int n,d,i,s=0;
[Link](“Enter a number:”);
n=[Link]();
for(i=n;i>0;i/=10)
{
d=i%10;
s=d+s;
}
if(n%s==0)
[Link](“Niven Number”);
else
[Link](“Not a Niven Number”);
}
}

217 Library Classes

You might also like