computer - Library classes -quick revision
computer - Library classes -quick revision
Revision notes
Syllabus :
1. Library Classes
2. Encapsulation and Inheritance
3. Arrays
4. String Handling
Ch-Library Classes
Points to remember:
1. Primitive data types are- byte, short, int, long, float, double, char, Boolean.
2. Composite/ Reference/ User Defined/ Non-primitive are- Classes, Arrays, Strings
3. Wrapper are classes available in- java.lang package.
b. String s = “1001”;
int x = Integer.valueOf(s);
double y = Double.valueOf(s);
System.out.println(“x=” +x);
System.out.println(“y =” +y);
Ans. x= 1001
y= 1001.0
c. System.out.println(“The King said\” Begin at the beginning!\” to me.”);
Ans. The King said “Begin at the beginning!” to me.
d. System.out.println(Character.isUpperCase(‘R’);
Ans. true
e. System.out.println(Character.toUpperCase(‘j’);
Ans. J
a. Integer.parseInt(“-321”)
Ans : -321
9(i). State the data type and value of res after the following is executed:
char ch = ‘t’;
res = Character.toUpperCase(ch);
Ans. The data type of res will be char, and its value following execution will be ‘T’.
9(ii). State the data type and value of y after the following is executed:
char x = ‘7’;
y = Character.isLetter(x);
Ans. The data type of y will be boolean, and its value following execution will be false.
15. Autoboxing / Boxing– is the automatic conversion of the primitive types into their corresponding
object wrapper classes.
Ex. Integer a = 20;
16. Auto-unboxing- It is the reverse of Autoboxing. It is the automatic conversion of wrapper class object
into its corresponding primitive type.
Ex. Integer a = 20;
int b = a;
17. Difference between in Primitive and non-primitive data type
Primitive Non-primitive/Composite/ User defined/ Referenced
1. represents a single value. 1. is a collection that holds zero or more primitive
values or objects.
2. When data is copied to a variable, the 2. When data is copied to a variable , only a referenced
variable gets its own copy of data, stored to the data is stored in the variable.
separately in memory.
3. In method call, primitive data is passed by 3. In method call, composite data type is passed by
value. reference.
*************************************************** *****************************************
Question 2
Question 3
Question 4
Question 5
Wrapper class uses first letter in upper case.
Question 6
Question 7
Question 8
Question 2
Question 3
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
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:
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)
Float.parseFloat()
Question 2
Double.toString()
Question 3
Integer.valueOf()
It is used to convert string data into the Integer wrapper object.
Question 4
Character.isDigit()
Question 5
Character.isWhitespace()
Output
false
Explanation
Question 2
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
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
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
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
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
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
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'.