Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Java Arrays and Strings

Uploaded by

sundarmatsa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Arrays and Strings

Uploaded by

sundarmatsa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

ARRAYS

1. What is an array in Java?


Answer: An array in java is an object which contains elements of similar datatype. The elements of an
array are stored in contiguous memory location. Array is index-based, the first element is stored in 0th
index and second element is stored in 1st index and so on.

2. How do you declare and initialize an array in Java?

Answer:

Declaration: int[] arr; or int arr[];

Initialization: arr = new int[10]; or int[] arr = {1, 2, 3, 4};

3. How do you access elements of an array in Java?


Answer: By using index, arr[0] to access the first element, arr[1] to access the second element and so
on.
4. What is Anonymous Array in java?
Answer: if you are creating an Array without any name is called anonymous array. It is an array just for
creating and using instantly.
5. What are the advantages and disadvantages of an array?
Answer: Advantages: Code Optimization, Random access, easy to use
Disadvantages: Size Limit, Insertion and deletion operations are costly. (O(n) time complexity)
6. What is single dimensional array?
Answer: A single dimensional array in Java is an array that holds a sequence of elements, all of the
same type, accessed through an index.
7. What is two-Dimensional array?
Answer: A Two-Dimensional array is a simplest form of the multi-dimensional array. A two-
Dimensional array is an array of single-Dimensional array.
8. What is multi-dimensional array?
Answer: A multi-dimensional array is an array of arrays. Data in multi-dimensional arrays are stored in
tabular form i.e., rows and columns.
9. What are the differences between 1D array and 2D array?
Answer:
1D Array:
- 1D Array is a sequence of elements all of same type.
- It represents multiple data items in a list form
2D array:
- 2D array is an array of arrays.
- It represents multiple data items in a tabular form.
10. How do you find the length of an array in Java?
Answer: By using length property.
11. How do you iterate over an array in Java?
Answer: using for loop and enhanced for loop.

12. How do you copy an array in Java?

Answer: Using Arrays.copyOf(Object src, int srcPos, Object dest, int destPos, int length)
13. How do you sort an element in java?
Answer: By Using ‘Arrays.sort()’ method.
14. What is an ArrayIndexOutOfBound Exception?
Answer: The virtual machine throws an ArrayIndexOutOfBound Exception if the length of an array in
negative, equal to array size or greater than array size while traversing an array.
15. How do you handle the ArrayIndexOutOfBoundsException in java?
Answer: By using a ‘try-catch’ block
Example:
try {
int element = arr[index];
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Index out of bounds: " + e.getMessage());
}
16. What is jagged array in java?
Answer: If we are creating an odd number of columns in a 2D Array, is called as jagged array.
Strings
1. What is String in java?
Answer: In generally String is a sequence of characters. In java, String is an object that represents
the sequence of characters. The java.lang.String class is used to create a string object.
2. How to create a string object?
Answer: There are two ways to create a string object:
- String literals.
- By new keyword.
3. Why java uses the concept of String literals?
Answer: To make java more memory efficient.
4. What is immutable String in java?
Answer: Immutable means unmodifiable or unchangeable. Immutable string means once String
is created, its data or state cannot be changed but a new string object is created.
5. Why string objects are immutable?
Answer: Because of Security, Synchronization, Concurrency, Cache, Class loader.
6. What is String constant pool?
Answer: String constant pool is a special memory area which is used to store string literals.
7. Why String class is final in java?
Answer: because no one can override the methods of the String class.
8. How do you compare java Strings?
Answer: There are three ways to compare Strings in java.
- By using equals() method: compares the original content of the string.
- By == operator: compares the reference of the string not values.
- By using compareTo() method: compares the string lexicographically and returns the integer
value. If s1 == s2 - returns 0, if s1>s2 - returns positive value, if s1<s2 - returns negative
value.
9. What is substring in java?
Answer: substring is a subset of another String.
10. What is StringBuffer class?
Answer: StringBuffer class is a thread-safe (Synchronized) which is used to create mutable strings.
11. What is String Builder class?
Answer: StringBuilder class is not thread-safe(non-Synchronized) which is used to create mutable
Strings in java.
12. What are the differences between String & StringBuffer?
Answer:
- Strings are immutable whereas stringBuffer or StringBuilder is mutable.
- String is slow and consumes more memory when we concatenate too many strings.
Whereas StringBuffer is fast and consumes less memory.
- String uses String constant pool whereas StringBuilder uses heap memory
13. What are the differences between StringBuffer & StringBuilder?
Answer:
StringBuffer:
- StringBuffer is Synchronized i.e., thread safe.
- StringBuffer is less efficient than stingBuilder.
StringBuilder:
- StringBuffer is non-Synchronized i.e., not thread safe.
- StringBuilder is more efficient than stringBuffer.

You might also like