Java ASSIGNMENT 3 Answer Key - Set 3 (Exam Format)
Java ASSIGNMENT 3 Answer Key - Set 3 (Exam Format)
The specific process of how a class implements an interface (using the implements
keyword and providing method bodies for all abstract methods defined in the
interface) is not explicitly detailed in the retrieved snippets of the provided notes.
However, the notes do mention that an abstract class can provide the implementation
of an interface [cite: 20].
(General Java Info: A class uses the implements keyword followed by the interface
name(s). The class must then provide a concrete implementation for every abstract
method declared in the interface(s) it implements, or the class itself must be declared
abstract.)
4. Explain how to access and modify elements in an array. Discuss the role of
indices in array manipulation.
● Accessing and Modifying Elements:
○ Array elements are accessed and modified using their index (position) within
square brackets [].
○ Accessing: To retrieve the value at a specific position, use arrayName[index].
■ Example: System.out.println(a[i]); [cite: 9] (prints the element at index i)
○ Modifying: To change the value at a specific position, assign a new value
using arrayName[index] = newValue;.
■ Example: a[0] = 10; a[1] = 20; [cite: 9] (assigns values to the first two
elements)
● Role of Indices:
○ Indices are integer values used to identify the position of each element within
the array.
○ Java arrays are zero-based, meaning the index of the first element is 0, the
second is 1, and so on, up to length - 1 for the last element.
○ The index is crucial for iterating through the array (e.g., using a for loop from
0 to a.length - 1 [cite: 9]) and for performing operations on specific elements.
○ The length property of an array gives the total number of elements it can hold
[cite: 9].
5. Describe operation on String. Explain Mutable and Immutable String.
● String Operations (from notes):
○ Creation: Strings can be created as literals using double quotes (String s =
"welcome"; [cite: 23]) or using the new keyword.
○ Concatenation: The concat() method can be used to join strings (String s3 =
s1.concat(s2); [cite: 24]). The + operator can also typically be used for string
concatenation (though not explicitly shown in these snippets for strings).
○ Substring: A part of a string can be extracted using substring methods
(mentioned conceptually [cite: 25]).
○ Tokenizing: The StringTokenizer class (though older) provides methods like
hasMoreTokens() and nextToken() to break a string into parts based on
delimiters [cite: 26].
(Note: Many other common String operations like length(), charAt(), equals(),
toUpperCase(), etc., are not mentioned in the retrieved snippets.)
● Mutable and Immutable String:
○ Immutable String: The notes describe String literals and the String Constant
Pool [cite: 23]. When a String literal is created, the JVM checks this pool. If the
string already exists, a reference to the existing object is returned; otherwise,
a new String object is created and placed in the pool. This behavior strongly
implies that String objects in Java are immutable – their state (the sequence
of characters) cannot be changed after creation. Operations like concat()
create and return a new String object rather than modifying the original.
○ Mutable String: The concepts of mutable strings, typically handled by
classes like StringBuffer or StringBuilder which allow modification of the
character sequence after creation, are not described in the retrieved
snippets of the provided notes.