0% found this document useful (0 votes)
2 views

Java ASSIGNMENT 3 Answer Key - Set 3 (Exam Format)

The document provides an overview of key Java concepts, including interfaces, arrays, and strings. It explains that interfaces serve as blueprints for classes, enabling multiple inheritance by allowing classes to implement multiple interfaces. Additionally, it discusses array declaration and initialization methods, element access and modification using indices, and the distinction between mutable and immutable strings.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java ASSIGNMENT 3 Answer Key - Set 3 (Exam Format)

The document provides an overview of key Java concepts, including interfaces, arrays, and strings. It explains that interfaces serve as blueprints for classes, enabling multiple inheritance by allowing classes to implement multiple interfaces. Additionally, it discusses array declaration and initialization methods, element access and modification using indices, and the distinction between mutable and immutable strings.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java ASSIGNMENT 3 Answer Key (Based on Provided Notes - Exam Format)

1. Describe what an interface is in java. Explain how interfaces in java support


multiple inheritance.
●​ Interface Definition:
○​ According to the notes, an interface in Java is described as a blueprint of a
class [cite: 19].
○​ It contains static constants (implicitly public, static, and final) and abstract
methods (implicitly public and abstract, meaning they have no method body)
[cite: 19, 20].
○​ An interface is a mechanism used to achieve abstraction [cite: 19].
○​ Like abstract classes, interfaces cannot be instantiated directly [cite: 19].
●​ Support for Multiple Inheritance:
○​ The notes state that interfaces are used to achieve multiple inheritance in
Java [cite: 19].
○​ While Java does not support multiple inheritance through classes (inheriting
from more than one class directly) due to potential ambiguity issues (like the
"diamond problem"), a class can implement multiple interfaces [cite: 15].
○​ By implementing multiple interfaces, a class agrees to provide
implementations for all the abstract methods defined in those interfaces,
effectively inheriting multiple "contracts" or "types", thus achieving a form of
multiple inheritance.
2. Describe the process of implementing an interface in a class.

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.)

3. Describe an array in Java. Discuss different ways to declare and initialize


arrays in java.
●​ Array Description:
○​ While a formal definition isn't provided in the snippets, an array in Java is
implicitly shown as a collection object that holds a fixed number of values of a
single, specific data type. The elements are stored contiguously in memory.
●​ Declaring and Initializing Arrays:
○​ The notes demonstrate the following ways [cite: 9]:
1.​ Declaration and Instantiation: Declare the array reference and create
the array object using new, specifying the size. Elements get default
values (e.g., 0 for int).​
// Declaration and Instantiation separately​
int a[]; // Declaration​
a = new int[5]; // Instantiation (size 5)​

// Combined Declaration and Instantiation​
int b[] = new int[5];​

2.​ Declaration, Instantiation, and Initialization (Array Literal): Declare,


create, and initialize the array with specific values in one step using curly
braces {}. The size is determined by the number of elements provided.​
int c[] = {33, 3, 4, 5}; // Declares, instantiates, and initializes array c​

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.

You might also like