1. What is an array?
Array is set of values of similar type. Array elements share common name and array elements are stored in sequential memory locations.
2. What are the advantages and disadvantages of arrays?
Advantages:
· It is capable of storing many elements at a time
· It allows random accessing of elements i.e. any element of the array can be randomly accessed using indexes.
Disadvantages:
· Predetermining the size of the array is a must.
· There is a chance of memory wastage or shortage.
· To delete one element in the array, we need to traverse throughout the array.
· To delete one element in the array, we need to traverse throughout the array.
3. How do you declare arrays?
In Java, Single dimensional array can be declared as follows.
int a[ ] =new int[5];
Or int [] a= new int[5];
Or int a[] ={3,4,5,6,7};
4. What is a multidimensional array?
Multidimensional arrays can represent the data in the form of rows and columns i.e. by taking two or more indexes.
5. How do you declare double dimensional arrays?
In java, a double dimensional array can be declared as follows:
int a[][] = new int[5][5];
Or int [][]a = new int[5][5];
Or int []a[] = new int[5][5]
Or int a[][]={ {1,2,3}, {4,5,6}, {7,8,9}};
6. What is variable size array?
A variable size array allows having variable number of elements in each row of a double dimensional array.
7. What are Strings?
String is a set of characters. A java String is not a character array and it is not terminated by null character.
8. How a string can be initialized?
String can be initialized in many ways.
· String s1 = “Apoorva”;
· String s2 = new String(“Ramu”);
· String s3 = new String(new char[] {‘a’,’b’,’c’});
Here, the argument could be any character array. s3 will have “abc”.
· String s4 = new String(new byte[] {65,66,67});
Here, the argument could be any byte array. s4 will have “ABC”.
9. List down string methods.
s2 = s1.toLowerCase() | Converts the string to all lowercase |
s2 = s1.toUppercCase() | Converts the string to all Uppercase |
s2 = s1.replace( 'x', 'y') | Replaces all appearances of x with y |
s2 = s1.trim() | Removes white spaces at the beginning and end of the string |
s1.equals(s2) | Returns 'true' if s1 is equal to s2 |
s1. equalsIgnoreCase(s2) | Returns 'true' if s1 = s2, ignoring the case of characters |
s1.length() | Gives the length of s 1 |
s1.chartAt(n) | Gives nth character of s 1 |
s1.compareTo(s2) | Returns negative if s1 < s2, positive if s1 > s2, and zero if s1 is equal s2 |
s1.concat(s2) | Concatenates s1 and s2 |
s1.substring(n) | Gives substring starting from nth character |
s1.substring(n, m) | Gives substring starting from nth character up to mth (not including mth) |
String.valueOf(p) | Creates a string object of the parameter p (simple type or object) |
p.toString() | Creates a string representation of the object p |
s1.indexOf('x') | Gives the position of the first occurrence of 'x' in the string s1 |
s1.indexOf('x', n) | Gives the position of 'x' that occurs after nth position in the string s1 |
s1.lastIndexOf(‘x’) | Gives the position of the last occurrence of 'x' in the string s1 |
s1.startsWith(“s2”) | Returns true if s1 begins with s2 |
s1.endsWith(“s2”) | Returns true if s1 ends with s2 |
10. What is StringBuffer?
StringBuffer is a peer class of String class. While String creates strings of fixed length, StringBuffer creates strings of flexible length that can be modified in terms of both length and content.
11. What are the methods of StringBuffer?
append() | used for the concatenate the string in string buffer. This is better to use for dynamic string concatenation. |
insert() | used to insert any string or character at the specified position in the given string. |
reverse() | used to reverse the string present in string buffer. |
setCharAt() | used to set the specified character in buffered string at the specified position. |
charAt() | used to get the character at the specified position of the given string. |
substring() | used to get the sub string from the buffered string |
deleteCharAt() | used to delete the specific character at a given position from the buffered string |
length() | used to finding the length of the buffered string. |
delete() | used to delete multiple character at once from n position to m. |
capacity() | used to know the capacity of the object i.e. Number of characters + 16. |
12. What are the difference between String and StringBuffer
The few differences between String and StringBuffer are listed below.
· StringBuffer objects must ne instantiated using ‘new’. But, Strings can be instantiated without using ‘new’.
· StringBuffer objects are mutable. Where as, String objects are immutable.
· StringBuffer provides functions like setCharAt(), deleteCharAt() and etc.
· StringBuffer allocates extra 16 bits of memory
· StringBuffer can be treated as a dynamic string.
13. What is a Vector?
Vector class is contained in the java.util package. This class can be used to create a generic dynamic array known as vector that can hold objects of any type and any number.
14. What are the methods of Vector class?
addElement(item) | Adds the item specified to the list at the end |
elementAt(n) | Gives the name of the nth object |
slze() | Gives the number of objects present |
removeElement(item) | Removes the specified item from the list |
removeElementAt(n) | Removes the item stored in the nth position of the list |
removeAllElements( ) | Removes all the elements in the list |
copyInto(array) | Copies all items from list to array |
InsertElementAt (Item, n) | Inserts the item at nth position |
15. What are the differences between Vectors and Arrays.
ARRAYS | VECTORS |
Array is set of values of similar type. Array elements share common name and array elements are stored in sequential memory locations. | Vector class is contained in the java.util package. This class can be used to create a generic dynamic array known as vector that can hold objects of any type and any number. |
Predetermination of array size is a must | Predetermination of Vector size is not necessary |
Declaration is as follows: int a[] = new int [3]; | Declaration is as follows: Vector list = new Vector ( ); // without size Vector list = new Vector (3); // with size |
The set of values must of same type | The objects do not have to be homogeneous |
The size of the array is fixed. | It can store a list of objects that may vary in size |
Needs extra overhead to store objects | It can more conveniently store objects. |
Insertion and deletion are tiresome processes | Insertion and deletion are quite easy |
Here, we can store simple data types easily. | We cannot directly store simple data types in a vector. We have to convert them into objects. |
No direct methods to manipulate or insert or delete elements | As it is an object, several built-in methods are available to manipulate or insert or delete elements (like addElement(), removeElement(),…) |
16. What are wrapper classes?
Wrapper classes can be used to convert primitive data types like int, float, long, and double to objects. These classes are contained in the java.lang package. The following table lists simple data types and their corresponding wrapper classes.
Simple Type | Wrapper class |
boolean | Boolean |
char | Character |
double | Double |
float | Float |
int | Int |
long | Long |
byte | Byte |
short | Short |
17. What is Autoboxing and Unboxing.
The autoboxing and unboxing feature, introduced in J2SE 5.0, facilitates the process of handling primitive data types in collections. We can use this feature to convert primitive data types to wrapper class types and vice versa automatically.
18. What are Enumerated Data types.
An enum type is a type whose fields consist of a fixed set of constants. It is something like defining words with constant values.
0 comments:
Post a Comment