Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

ARRAYS & STRINGS

ARRAYS
1. What is an array in Java?
An array is a finite and ordered collection of homogeneous data elements. It is
finite because it contains a limited number of elements. It is ordered because all
the elements are stored one by one in a contiguous location of computer memory
(heap) in a linear fashion. It is homogeneous because all elements of an array are
of the same data type only. We can store either primitive types or object
references into it.
2. Can you declare an Array without Array size?
No, you cannot declare Array without Array size. You will get compile time error.
3. Where does Array stored in JVM memory ?
Array is an object in java. So, Array is stored in heap memory in JVM.
4. Given a primitive Array in java, where does in JVM it is stored?
An Array will always be an object on heap memory, even if the Array is declared
to hold primitive elements.
5. What is the difference between ArrayStoreException and
ArrayOutOfBoundsException ?
ArrayStoreException is thrown if you are trying to add incompatible data type.
For example, if you try to add an integer object to String Array, then
ArrayStoreException is thrown.
ArrayOutOfBoundsException is thrown when an attempt is made to access the
Array with illegal index. For example, illegal index means if the index is either
negative or greater than or equal to the size of the Array.
6. What are the advantages of Array ?
a) We can sort multiple elements of Array at the same time
b) Using index, we can access the element of the Array in O(1) time.

+91 9000045750 packetprep.com


7. What are the disadvantages of Array?
a) To create an Array, contiguous memory is required. It is possible in JVM
that the memory is available to accommodate Array but memory available
is not contiguous
b) The Array is static data structure. It is of fixed size. We cannot increase or
decrease the size of the Array after creation.
8. What will happen if you do not initialize an Array?
Array will take default value depending upon the data type.
9. How do you declare a two dimensional Array in java?

The above statement will create a 4 x 4 matrix.


10. Can you use Generics with an array?
No, you cannot use Generic feature with an array, that’s why sometime List is a
better choice over an array in Java, which is also recommended by Joshua Bloch
in his class Java book, Effective Java, a must-read for writing good code in Java.
11. What is a two-dimensional array in Java?
An array of the array in Java. You can declare them like

which is a matrix of 3x3.


12. What is the default value of Array?
Any new Array is always initialized with a default value as follows
 For byte, short, int, long – default value is zero (0).
 For float, double – default value is 0.0.
 For Boolean – default value is false.
 For object – default value is null.
13. Is there any difference between int[] a and int a[]?
No difference both are the legal statement.

+91 9000045750 packetprep.com


STRINGS
14. What are different ways to create String Object?
We can create String object using new operator like any normal java class or we
can use double quotes to create a String object. There are several constructors
available in String class to get String from char array, byte array, StringBuffer
and StringBuilder.

15. Difference between String buffer and StringBuilder


StringBuffer is synchronized i.e. thread safe. It means two threads can’t call the
methods of StringBuffer simultaneously. StringBuffer is less efficient than
StringBuilder
StringBuilder is non-synchronized i.e. not thread safe. It means two threads can
call the methods of StringBuilder simultaneously. StringBuilder is more
efficient than StringBuffer.

16. Why String is immutable or final in Java


There are several benefits of String because it’s immutable and final.
 String Pool is possible because String is immutable in java.
 It increases security because any hacker can’t change its value and it’s used
for storing sensitive information such as database username, password etc.
 Since String is immutable, it’s safe to use in multi-threading and we don’t
need any synchronization.
 Strings are used in java class loader and immutability provides
17. What is string constant pool?
String objects are most used data objects in Java.

Hence, Java has a special arrangement to store the string objects. String
Constant Pool is one such arrangement. String Constant Pool is the memory

+91 9000045750 packetprep.com


space in the heap memory specially allocated to store the string objects created
using string literals. In String Constant Pool, there will be no two string objects
having the same content.

Whenever you create a string object using string literal, JVM first checks the
content of the object to be created. If there exist an object in the string constant
pool with the same content, then it returns the reference of that object. It doesn’t
create a new object. If the content is different from the existing objects then
only it creates new object

18. How do you create mutable string objects?


Using StringBuffer and StringBuilder classes. These classes provide mutable
string objects.
19. What is string intern?
String object in the string constant pool is called as String Intern. You can create
an exact copy of heap memory string object in string constant pool. This process
of creating an exact copy of heap memory string object in the string constant pool
is called interning. intern() method is used for interning.
20. Is string thread-safe in Java?
Yes, strings are thread safe. They are immutable and all immutable instances in
java are thread-safe, by default.
21. Why String is popular HashMap key in Java?
In Java, A key which has be to used in Map – shall be immutable and should
honor the contract between equals() and hashCode() method. String class satisfies
both conditions.
Also, String class provides many useful methods to compare, sort, tokenize or
lower-upper cases. These methods can be used while performing CRUD
operations on Map. It makes it a very useful class to use in Map rather than
creating your own class.

+91 9000045750 packetprep.com


22. Why Char array is preferred over String for storing password?
String is immutable in Java and stored in String pool. Once it’s created it stays
in the pool until unless garbage collected, so even though we are done with
password it’s available in memory for longer duration and there is no way to
avoid it. It’s a security risk because anyone having access to memory dump can
find the password as clear text.
If we use a char array to store password, we can set it to blank once we are done
with it. So we can control for how long it’s available in memory that avoids the
security threat with String.
23. How to convert String to char and vice versa?
This is a tricky question because String is a sequence of characters, so we can’t
convert it to a single character. We can use use charAt method to get the character
at given index or we can use toCharArray() method to convert String to character
array.

+91 9000045750 packetprep.com

You might also like