Library Classes
Library Classes
Question 1
Which of the following is a primitive data type?
1. int
2. float
3. char
4. All of these
Answer
All of these
Question 2
1. int
2. float
3. char
4. String
Answer
String
Question 3
1. int
2. boolean
3. char
4. String
Answer
boolean
Question 4
1. int
2. boolean
3. char
4. String
Answer
char
Question 5
1. -321
2. 321
3. 321.0
4. "321"
Answer
-321
Question 6
Name the method that can convert a string into its integer equivalent.
1. Integer.parseInteger()
2. Integer.getInt()
3. Integer.parseInt()
4. Integer.readInt()
Answer
Integer.parseInt()
Question 7
Answer
Question 8
1. java.io
2. java.util
3. java.awt
4. java.lang
Answer
java.lang
Question 1
Answer
Library classes are the pre-written classes which are a part of the Java system. For
example, the String and Scanner class.
Java environment has a huge library of library classes that contain pre-defined methods to
simplify the job of a programmer. These methods support input/output operations, String
handling and help in development of network and graphical user interface.
Question 2(i)
Answer
Primitive data types are pre-defined by the language and form the basic building blocks
of representing data. They store a single value of a specific declared type at a time. The
eight built-in primitive data types in Java are byte, short, int, long, float, double, char,
boolean.
Question 2(ii)
Answer
A composite data type is a data type which can be constructed in a program using the
programming language's primitive data types. It is a collection of primitive data types.
Examples of composite data types are String and Array.
Question 2(iii)
Answer
The data type defined by the user to perform some specific task is known as a user-
defined data type. For example, the classes created by the user are user defined data
types.
Question 3
Answer
In a class, one can assemble items of different data types to create a composite data type.
The class can also be considered as a new data type created by the user, that has its own
functionality. The classes allow these user-defined types to be used in programs. Hence, a
class is called a composite data type.
Question 4
Answer
A wrapper class allows us to convert a primitive data type into an object type. Each of
Java's eight primitive data types has a wrapper class dedicated to it. These are known as
wrapper classes because they wrap the primitive data type into an object of that class.
Question 5
Answer
The automatic conversion of primitive data type into an object of its equivalent wrapper
class is known as Autoboxing. For example, the below statement shows the conversion of
an int to an Integer.
Integer a = 20;
Here, the int value 20 is autoboxed into the wrapper class Integer variable myinteger.
Auto-unboxing is the reverse process of Autoboxing. It is the automatic conversion of a
wrapper class object into its corresponding primitive type. For example,
Question 6
Answer
The wrapper class Double has a method parseDouble() which is used to parse a numeric
string into a double value.
Syntax:
Double.parseDouble(String s);
Example:
String str = "437246.643";
double d = Double.parseDouble(str);
Question 7
Describe wrapper class methods available in Java to parse string values to their numeric
equivalents.
Answer
The wrapper class methods available in Java to parse string values to their numeric
equivalents are described below:
Question 8
Answer
We can check if a given character is a digit, a letter or a space by using the following
methods of Character class:
1. isDigit(char) — It returns true if the specified character is a digit; returns false
otherwise.
Syntax:
boolean isDigit(char ch)
2. isLetter(char) — It returns true if the specified character is a letter; returns false
otherwise.
Syntax:
boolean isLetter(char ch)
3. isLetterOrDigit(char) — It returns true if the specified character is a letter or a
digit; returns false otherwise.
Syntax:
boolean isLetterOrDigit(char ch)
4. isWhitespace(char) — It returns true if the specified character is whitespace;
returns false otherwise.
Syntax:
boolean isWhitespace(char ch)
Question 9(i)
Answer
isLowerCase() toLowerCase()
Question 9(ii)
Answer
isUpperCase( ) toUpperCase( )
Question 9(iii)
Answer
isDigit() isLetter()
isDigit() method returns true if the isLetter() method returns true if the
specified character is a digit; returns false specified character is a letter; returns false
otherwise. otherwise.
Question 9(iv)
Answer
parseFloat() parseDouble()
parseFloat() method returns a float value parseDouble() method returns a double value
represented by the specified string. represented by the specified string.
Question 10
Define a class (using the Scanner class) to generate a pattern of a word in the form of a
triangle or in the form of an inverted triangle, depending upon user's choice.
Sample Input:
switch (choice) {
case 1:
for(int i = 0; i < len; i++) {
for(int j = 0; j <= i; j++) {
System.out.print(word.charAt(j));
}
System.out.println();
}
break;
case 2:
for (int i = len - 1; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
char ch = word.charAt(j);
System.out.print(ch);
}
System.out.println();
}
break;
default:
System.out.println("Incorrect choice");
break;
}
}
}
Question 11
Member Methods:
void input() - to input and store the details of the customer
void compute() - to compute the rental charge
The automatic conversion of primitive data type into an object of its equivalent wrapper
class is known as Autoboxing.
The automatic conversion of an object of wrapper class into primitive data type is known
as Auto-unboxing.
The methods which return Wrapper class objects from primitive values are as follows:
1. Byte.valueOf()
2. Short.valueOf()
3. Integer.valueOf()
4. Long.valueOf()
5. Float.valueOf()
6. Double.valueOf()