Fill in The Blanks: Package Is A Collection of Classes
Fill in The Blanks: Package Is A Collection of Classes
Question 1
Question 2
Question 3
Question 4
Question 5
Question 6
Question 7
Question 8
Question 2
Question 4
Question 5
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
Question 4
The parseInt() function is a member of:
Question 5
What is a package?
Question 2
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
Its return type is boolean Its return type is char
parseInt() toString()
It converts a string to an integer It converts an integer to a string
Its return type is int Its return type is String
Question 5(i)
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)
Write the purpose of the following functions
Question 1
Float.parseFloat()
Question 2
Double.toString()
Question 3
Integer.valueOf()
Question 4
Character.isDigit()
Question 5
Character.isWhitespace()
char ch = '*';
boolean b = Character.isLetter(ch);
System.out.println(b);
Output
false
Explanation
As Asterisk (*) is not a letter so Character.isLetter() method returns false.
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);
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
Y 89
Explanation
Question 6
int n = 97;
char ch = Character.toUpperCase((char)n);
System.out.println(ch + " Great Victory");
Output
A Great Victory
Explanation
Question 7
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.
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
Write a program in Java to input a character. Find and display the next 10th character in the
ASCII table.
import java.util.Scanner;
Output
Question 2
import java.util.Scanner;
Write a program in Java to generate all the alternate letters in the range of letters from A to Z.
Output
Question 4
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
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
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
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;
Output
Question 8
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++;
default:
System.out.println("Incorrect Choice");
}
}
}
Output
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);
int r = 0;
while (a > 0) {
int digit = a % 10;
r = r * 10 + digit;
a /= 10;
}
Output
Question 10
Write a menu driven program to display
(i) first five upper case letters
(ii) last five lower case letters as per the user's choice.
Enter '1' to display upper case letters and enter '2' to display lower case letters.
import java.util.Scanner;
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
Question 11
(i)
A
ab
ABC
abcd
ABCDE
Output
(ii)
ZYXWU
ZYXW
ZYX
ZY
Z
Output
(iii)
ABCDE
ABC
A
Output
(iv)
PRTV
PRT
PR
P
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 86; i >= 80; i = i - 2) {
for (int j = 80; j <= i; j = j + 2) {
System.out.print((char)j);
}
System.out.println();
}
}
}
Output
(v)
A*B*C*D*E*
A*B*C*D*
A*B*C*
A*B*
A*
Output
(vi)
aaaaa
bbbbb
AAAAA
BBBBB
Output