Core java interview Questions You'll Most Likely Be Asked (Second Edition)


What are some of the important methods on the


Download 1.52 Mb.
Pdf ko'rish
bet60/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   56   57   58   59   60   61   62   63   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

What are some of the important methods on the
Collection interface?
Answer:
The following are some of the important methods on the
Collection interface:
add: Allows adding an object to the collection
remove: Allows removing an object from the collection
contains: Allows checking if an object is present in the
collection
size: Returns the length of the collection
iterate: Allows iterating over the collection



156.
157.
CHAPTER
15
Enumerations, Autoboxing and
Wrapper Classes
What are the wrapper classes available in Java?
Answer:
Wrapper classes are classes that correspond to the Java
primitive types. They help to convert primitive types to objects
and vice versa. This is particularly useful when you want to use
Collections. Since a Collection does not accept primitive values,
you can use the corresponding wrapper type. Most of the
wrapper classes take their primitive type or String as an
argument. Java supports the following wrapper classes:
Primitive Type
Wrapper Type
byte
Byte
short
Short
int
Integer
long
Long
float
Float
double
Double
char
Character
boolean
Boolean
What will be the output of the following code?


158.
public class BooleanDemo {
public static void main(String a[]){
//create Boolean using boolean primitive type
boolean boo1 = true;
Boolean booObj1 = new Boolean (boo1); //line 1
System.out.println(“Wrapper class Boolean output:
“+booObj1);
Boolean booObj2 = new Boolean (“false”); //line 2
System.out.println(“Wrapper class Boolean output:
“+booObj2);
System.out.println(booObj1.booleanValue());
}
}
Answer:
Line 1 creates a boolean wrapper type boolObj1 corresponding
to the bool variable. Line 2 creates a boolean wrapper
boolObj2 using a String value. Java automatically converts
wrapper types to primitive types. In addition, there is a method
called booleanValue() on the Boolean wrapper class that
returns the primitive boolean value. So, this code will print the
following output:
Wrapper class Boolean output: true
Wrapper class Boolean output: false
true

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   56   57   58   59   60   61   62   63   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling