Questions on Library classes
- Categories Wrapper Classes, Java
1. Diff between primitive and composite data type
Ans.
Primitive Data Type | Composite data types. |
---|---|
Data types that are provided by Java are called primitive types | Data types created by programmers which make use of primitive type to create a variable as per the user’s requirement are called non-primitive or composite data types. |
The size is fixed. | The size depends on the number of member variables and their types. |
When a variable of primitive data type is defined only one memory location is allocated | When an object(variable) of class type is defined the memory allocated will vary. |
To create a variable of primitive type ‘new’ keyword is not used. | To create an object variable we make use of ‘new’ keyword. |
2. What is the difference between Integer and int?
Ans.
Integer | int |
Integer is a class defined in the java.lang package | int is a primitive data type defined in Java language itself |
Integer can be used as an argument for a method that requires an object | int can be used for all arithmetic calculations |
3. What are wrapper classes and in which library are they included?
Ans.
- A Wrapper class is a class whose object wraps or contains a primitive data types.
- They convert primitive data types into objects. The classes in java.util package handles only objects and hence wrapper classes help
Primitive Data type | Wrapper Class |
char | Character |
byte | Byte |
short | Short |
long | Integer |
float | Float |
double | Double |
boolean | Boolean |
4. What is Autoboxing and Unboxing in Java?
Ans.
Autoboxing: It converts eight primitive data types (short, int, long, float, double, char, byte and boolean) into its corresponding wrapper class object.
Unboxing : It converts wrapper class object into its corresponding primitive data type. It unboxing is reverse of autoboxing.
Example:
public class simply { void AutoBoxing_Unboxing(){ int num= 75; Integer n = num; System.out.println("After Autoboxing"); System.out.println("value of wrapper class Integer object n= "+n); num=n; System.out.println("After Unboxing"); System.out.println("value of primitive data type int num= "+num); } }
5. What is unboxing? Give an suitable example.
Ans. Unboxing: When we try to assign the object of a wrapper class to primitive type variable it automatically converts the object back to the primitive type, it is called unboxing and it also happens at the time of execution(runtime).
Example:
public class TestWrapper{ public static void main(String args[]){ Integer auto =new Integer(500); int k= auto; System.out.println ( " the value of auto is “+k); } }
6. State the difference between Integer.parseInt() and function Integer.valueOf () Give one example of each.
Ans.
Integer.parseInt() | Integer.valueOf() |
If we need the primitive int datatype then Integer.parseInt() method is to be used. | If Wrapper Integer object is needed then valueOf() method is to be used. |
Integer.parseInt() returns a primitive int. | Integer.valueOf() returns an Integer object |
Only a String can be passed as parameter to Integer.parseInt() | Both String and integer can be passed a parameter to Integer.valueOf() |
Example: Sring A = “432” int x= Integer.parseInt( A ); | Example: String A = “432” int x= Integer.valueOf( A); |
7. What is the difference between isUpperCase() and toUpperCase()?
Ans. isUpperCase( ): This character function returns true if its character argument is an uppercase/capital letter, otherwise it returns false. It returns a boolean result.
toUpperCase( ): This string function converts all the characters of a string in uppercase or capitals.
8. Difference between equals() and compareTo() method. Give one example of each.
Ans.
equals() | compareTo() |
---|---|
This function is used to check only the equality of two strings and return a boolean result in the form of true or false. | This is a sting function and used to compare two strings “lexicographically” and returns integer result in the form of positive (>0), negative(<0) or zero (=0) |
boolean res= “Simply”.equals(“Simply”) //return ‘true’ boolean res= “Simply”.equals(“SIMPLY”) //return ‘false’ | boolean res= “SIMPLY”.equals(“SIMPLY”) //it gives result zero |
9. Differentiate between Character.toLowerCase() and Character.isDigit() methods. With an example.
Ans.
Character.toLowerCase() | Character.isDigit() |
---|---|
It is used to convert a string into lower case form (Small letters). | It returns true if character argument is a digit, otherwise returns false. |
Example: char ch= ‘d’; char st= Character.toUpperCase( ch ); | Example: Char c=’b’; boolean st1= Character.isDigit(ch); |
10. State the purpose and return data type of the following String functions:
- indexOf ( )
- compareTo ( )
Ans.
- indexOf() returns the index of the character or String passed as the parameter in the string on which is invoked. Return type is int.
- compareTo() lexicographically compares the String passed as an argument to the String on which it is invoked. Return type is int.
11. Write the difference between length and length() functions.
Ans. length is a property of an array which gives the size of the array. length() is a function of the String class which returns the size of the String.