Java Programming CA2 2-Mark - Answers
Java Programming CA2 2-Mark - Answers
An array is a data structure that stores a collection of elements, typically of the same type,
arranged in contiguous memory locations. Each element in an array is identified by an index or a key.
The purpose of arrays is to efficiently store and access multiple values of the same data type under a
single name. Arrays allow for easy iteration over elements, as well as random access to elements using
their indices.
2) Construct a java code to get the input for 2D array and print the same
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int columns = scanner.nextInt();
scanner.close();
}
}
3) Describe about the string immutability in java
In Java, strings are immutable, meaning their values cannot be changed after creation.
Operations like concatenation or substring creation generate new strings rather than modifying the
original. This characteristic ensures thread safety, simplifies memory management, and facilitates
efficient string manipulation through immutable objects.
Character arrays (char[]) store a sequence of characters in contiguous memory, offering mutability
and direct access by index. Strings, immutable sequences of characters, offer built-in methods for
manipulation and comparison but consume more memory due to their object-oriented nature, ensuring
thread safety and simplifying string handling.
`FileInputStream` reads raw bytes from a file, ideal for binary data. `FileReader` reads characters
using the system's default encoding, optimized for text files. While `FileInputStream` offers flexibility
and efficiency for binary data, `FileReader` simplifies text file reading with automatic character
decoding, but may lack flexibility for other data types.
6) Elaborate the procedure to read data from a file using BufferedReader in Java?
To read data from a file using BufferedReader in Java: Create a FileReader object to open the file,
then pass it to BufferedReader constructor. Use readLine() method to read lines iteratively. Close the
BufferedReader and FileReader objects when done to release resources.
The LocalDate class in Java represents a date without a time component, providing methods for
manipulating dates such as addition and subtraction. It's used to handle date-based operations, like
calculating intervals, formatting dates, and working with date-specific logic, offering simplicity and
clarity in date-related programming tasks.
8) Build a java syntax to obtain the current date and time
import java.time.LocalDateTime;
The purpose of the StringTokenizer class in Java is to break a string into tokens based on a
specified delimiter. It simplifies parsing and processing text by providing methods to iterate over the
tokens. StringTokenizer is commonly used for simple string parsing tasks in Java applications.
In contrast, a 2D array is an array of arrays, organized into rows and columns. It represents a grid-like
structure where each element is identified by two indices: one for the row and one for the column. This
allows for storing data in a tabular format.
While both are used to store multiple values, the key difference lies in their organization and access
patterns. A 1D array is suitable for representing a single sequence of items, while a 2D array is used for
tabular data or matrices where elements are arranged in rows and columns.
The purpose of the `charAt()` method in Java's String class is to retrieve the character at a
specified index within the string. It allows accessing individual characters of a string by their position,
facilitating various string manipulation tasks such as searching, comparison, and extraction of specific
characters.
14) Compare and contrast == and equals() when comparing strings in Java.
In Java, `==` compares object references, checking if two variables refer to the same memory
location. `equals()` is a method used to compare the actual contents of objects, particularly useful for
comparing strings. While `==` checks for reference equality, `equals()` checks for value equality,
comparing the characters of two strings.
The purpose of FileOutputStream in Java is to write data to a file as a stream of bytes. It allows
for creating, writing, and appending data to files byte by byte or in chunks. FileOutputStream is
commonly used for tasks such as saving files, logging data, and serializing objects to disk in various Java
applications.
16) Describe the steps to check whether a file exists
17) Explain the significance of the format method in the DateTimeFormatter class.
The format method in DateTimeFormatter class formats a date-time object into a string
representation based on the specified pattern. It provides flexibility to represent dates and times in
different formats according to application requirements.
18) Build a java syntax to parse a date string into a LocalDate object in Java?