Liibrary Class Notes
Liibrary Class Notes
Question 1
Question 2
Question 3
Question 4
Question 5
Question 6
1
CLASS X
Question 7
Question 8
Question 1
Question 2
Question 3
Question 4
Question 5
2
CLASS X
Question 1
A package contains:
1. tags
2. classes
3. data
4. arrays
Question 2
1. block
2. object
3. wrapper class
4. none
Question 3
1. autoboxing
2. explicit conversion
3. shifting
4. none
3
CLASS X
Question 4
Question 5
Question 1
What is a package?
Question 2
4
CLASS X
The asterisk(*) sign indicates that all the classes in the imported package can be used in the program.
Question 3
Wrapper classes wrap the value of a primitive type in an object. Wrapper classes are present in java.lang package. The different
wrapper classes provided by Java are Boolean, Byte, Integer, Float, Character, Short, Long and Double.
Question 4
Differentiate between:
isUpperCase() toUpperCase()
It is used to check if the character given as its It is used to convert the character given as its
argument is in upper case or not. argument to upper case
parseInt() toString()
5
CLASS X
Primitive Data Types are built-in data types defined by Composite Data Types are defined by the
Java language specification programmer
Examples of Primitive Data Types are byte, short, int, Examples of Composite Data Types are
long, float, double, char, boolean Class and Array
Question 5(i)
int res = 'A';
What is the value of res?
Value of res is 65.
Question 5(ii)
java.lang
Question 5(iii)
Write the prototype of a function check which takes an integer as an argument and returns a character.
char check(int n)
6
CLASS X
Question 1
Float.parseFloat()
Question 2
Double.toString()
Question 3
Integer.valueOf()
Question 4
Character.isDigit()
Question 5
Character.isWhitespace()
Question 1
char ch = '*';
boolean b = Character.isLetter(ch);
System.out.println(b);
Output
false
Explanation
Question 2
char c = 'A';
int n = (int) c + 32;
System.out.println((char)n);
Output
Explanation
int n = (int) c + 32 ⇒ 65 + 32 ⇒ 97
So, variable n get the value of 97. 97 is the ASCII code of small a so casting n to char, prints a to the console.
Question 3
String s= "7";
int t =Integer.parseInt(s);
t=t+1000;
System.out.println(t);
8
CLASS X
Output
1007
Explanation
Integer.parseInt() converts "7" into an int value i.e. the decimal number 7. t+1000 adds the number 7 to 1000 giving 1007 as the output.
Question 4
char c = 'B';
int i = 4;
System.out.println(c+i);
System.out.println((int)c+i);
Output
70
70
Explanation
In the expression c + i, c is of type char and i is of type int. As int is the higher type so char gets promoted to int. Thus, ASCII code of 'B' which is
66 is added to 4 giving the output as 70. This is an example of implicit type conversion.
In the next expression (int)c + i, c which is of char type is explicitly casted to int. Again, ASCII code of 'B' which is 66 is added to 4 giving the
output as 70. This is an example of explicit type conversion.
Question 5
char ch = 'y';
char chr = Character.toUpperCase(ch);
int p = (int) chr;
System.out.println(chr + "\t" + p);
Output
9
CLASS X
Y 89
Explanation
Character.toUpperCase() method converts small y to capital Y so chr gets the value of 'Y'. Casting chr to int gives the ASCII code of 'Y' which is 89.
Question 6
int n = 97;
char ch = Character.toUpperCase((char)n);
System.out.println(ch + " Great Victory");
Output
A Great Victory
Explanation
97 is the ASCII code of small a so Character.toUpperCase((char)n) returns capital A which is stored in ch.
Question 7
char ch = 'x'; int n = 5;
n = n + (int)ch;
char c = (char)n;
System.out.println((char)((int)c-26));
Output
Explanation
As ASCII code of 'x' is 120, so the expession n + (int)ch ⇒ 5 + 120 ⇒ 125. After that, the expression (char)((int)c-26) ⇒ (char)(125 - 26) ⇒
(char)99 ⇒ 'c' as ASCII code of 'c' is 99. So, c is the final output.
10
CLASS X
Question 8
char ch = 'A';
char chr = Character.toLowerCase(ch);
int n = (int)chr-32;
System.out.println((char)n + "\t" + chr);
Output
A a
Explanation
Character.toLowerCase() converts 'A' to 'a'. ASCII code of 'a' is 97. n becomes 65 — (int)chr-32 ⇒ 97 - 32 ⇒ 65. 65 is the ASCII code of 'A'.
Question 1
Write a program in Java to input a character. Find and display the next 10 th character in the ASCII table.
import java.util.Scanner;
Output
11
CLASS X
Question 2
12
CLASS X
{
public void displayFiveChars() {
Scanner in = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = in.next().charAt(0);
Output
13
CLASS X
Question 3
Write a program in Java to generate all the alternate letters in the range of letters from A to Z.
public class KboatAlternateLetters
{
public void displayAlternateLetters() {
14
CLASS X
Output
Question 4
15
CLASS X
Write a program to input a set of 20 letters. Convert each letter into upper case. Find and display the number of vowels and number of consonants
present in the set of given letters.
import java.util.Scanner;
Output
16
CLASS X
17
CLASS X
Question 5
Write a program in Java to accept an integer number N such that 0<N<27. Display the corresponding letter of the alphabet (i.e. the letter at
position N).
[Hint: If N =1 then display A]
import java.util.Scanner;
Output
18
CLASS X
19
CLASS X
Question 6
Write a program to input two characters from the keyboard. Find the difference (d) between their ASCII codes. Display the following messages:
If d=0 : both the characters are same.
If d<0 : first character is smaller.
If d>0 : second character is smaller.
Sample Input :
D
P
Sample Output :
d= (68-80) = -12
First character is smaller
import java.util.Scanner;
Output
20
CLASS X
21
CLASS X
Question 7
Write a program to input a set of any 10 integer numbers. Find the sum and product of the numbers. Join the sum and product to form a single
number. Display the concatenated number.
[Hint: let sum=245 and product = 1346 then the number after joining sum and product will be 2451346]
import java.util.Scanner;
22
CLASS X
sum += n;
prod *= n;
}
Output
Question 8
23
CLASS X
Write a menu driven program to generate the upper case letters from Z to A and lower case letters from 'a' to 'z' as per the user's choice.
Enter '1' to display upper case letters from Z to A and Enter '2' to display lower case letters from a to z.
import java.util.Scanner;
switch (ch) {
case 1:
for (int i = 90; i > 64; i--) {
char c = (char)i;
System.out.print(c);
System.out.print(" ");
count++;
case 2:
for (int i = 97; i < 123; i++) {
char c = (char)i;
System.out.print(c);
System.out.print(" ");
count++;
24
CLASS X
default:
System.out.println("Incorrect Choice");
}
}
}
Output
25
CLASS X
Question 9
Write a program to input a letter. Find its ASCII code. Reverse the ASCII code and display the equivalent character.
Sample Input: Y
Sample Output: ASCII Code = 89
Reverse the code = 98
Equivalent character: b
import java.util.Scanner;
int a = (int)l;
System.out.println("ASCII Code = " + a);
26
CLASS X
int r = 0;
while (a > 0) {
int digit = a % 10;
r = r * 10 + digit;
a /= 10;
}
Output
Question 10
27
CLASS X
switch (ch) {
case 1:
for (int i = 65; i <= 69; i++)
System.out.println((char)i);
break;
case 2:
for (int i = 118; i <= 122; i++)
System.out.println((char)i);
break;
default:
break;
}
}
}
Output
28
CLASS X
29
CLASS X
Question 11
(i)
30
CLASS X
A
ab
ABC
abcd
ABCDE
public class KboatPattern
{
public void displayPattern() {
for (int i = 65; i < 70; i++) {
for (int j = 65; j <= i; j++) {
if (i % 2 == 0)
System.out.print((char)(j+32));
else
System.out.print((char)j);
}
System.out.println();
}
}
}
Output
31
CLASS X
(ii)
ZYXWU
ZYXW
ZYX
32
CLASS X
ZY
Z
public class KboatPattern
{
public void displayPattern() {
for (int i = 86; i <= 90; i++) {
for (int j = 90; j >= i; j--) {
System.out.print((char)j);
}
System.out.println();
}
}
}
Output
33
CLASS X
(iii)
ABCDE
ABC
A
34
CLASS X
Output
35
CLASS X
(iv)
PRTV
PRT
PR
P
36
CLASS X
Output
37
CLASS X
(v)
A*B*C*D*E*
A*B*C*D*
A*B*C*
38
CLASS X
A*B*
A*
public class KboatPattern
{
public void displayPattern() {
for(int i = 69; i >= 65; i--) {
for (int j = 65; j <= i; j++) {
System.out.print((char)j + "*");
}
System.out.println();
}
}
}
Output
39
CLASS X
(vi)
aaaaa
bbbbb
AAAAA
BBBBB
40
CLASS X
Output
41
CLASS X
42
CLASS X
43