Skip to main content

Java Exception Handling MCQ Test

  Loading…

Implementing Arrays and String Functions in Java

 

1️⃣ Working with Arrays in Java

An array in Java is a collection of elements of the same type, stored in contiguous memory locations.

🔹 Declaring and Initializing an Array:

public class ArrayExample {
public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; // Array declaration and initialization System.out.println("First element: " + numbers[0]); // Accessing array elements // Using a loop to print all elements System.out.println("Array Elements:"); for (int num : numbers) { System.out.println(num); } } }

Output:

First element: 10
Array Elements: 10 20 30 40 50

🔹 Taking Array Input from User and Finding Sum:

import java.util.Scanner;
public class ArrayInput { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter number of elements: "); int n = sc.nextInt(); // Taking input for array size int[] arr = new int[n]; // Declaring array int sum = 0; // Taking input in array System.out.println("Enter " + n + " numbers:"); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); sum += arr[i]; // Calculating sum } System.out.println("Sum of elements: " + sum); sc.close(); } }

Example Output:

Enter number of elements: 3
Enter 3 numbers: 5 10 15 Sum of elements: 30

2️⃣ String Functions in Java

A string in Java is a sequence of characters, handled using the String class.

🔹 Basic String Operations:

public class StringExample {
public static void main(String[] args) { String str = "Hello, Java!"; System.out.println("Original String: " + str); System.out.println("Length: " + str.length()); System.out.println("Character at index 1: " + str.charAt(1)); System.out.println("Substring (0 to 5): " + str.substring(0, 5)); System.out.println("Uppercase: " + str.toUpperCase()); System.out.println("Lowercase: " + str.toLowerCase()); System.out.println("Replaced 'Java' with 'World': " + str.replace("Java", "World")); } }

Output:

Original String: Hello, Java!
Length: 12 Character at index 1: e Substring (0 to 5): Hello Uppercase: HELLO, JAVA! Lowercase: hello, java! Replaced 'Java' with 'World': Hello, World!

🔹 Concatenating and Comparing Strings:

public class StringOperations {
public static void main(String[] args) { String s1 = "Hello"; String s2 = "World"; // Concatenation String s3 = s1 + " " + s2; System.out.println("Concatenated String: " + s3); // Comparing strings String str1 = "Java"; String str2 = "java"; System.out.println("Are str1 and str2 equal? " + str1.equals(str2)); System.out.println("Ignoring case, are str1 and str2 equal? " + str1.equalsIgnoreCase(str2)); } }

Output:

Concatenated String: Hello World
Are str1 and str2 equal? false Ignoring case, are str1 and str2 equal? true

🔹 Checking if a String is Palindrome:

import java.util.Scanner;
public class PalindromeCheck { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a string: "); String str = sc.nextLine(); String reversed = new StringBuilder(str).reverse().toString(); if (str.equalsIgnoreCase(reversed)) { System.out.println("The string is a palindrome."); } else { System.out.println("The string is not a palindrome."); } sc.close(); } }

Example Output:

Enter a string: madam
The string is a palindrome.

Conclusion

Arrays help store multiple values in a single variable.
Strings provide many useful functions like concatenation, substring, replacement, and comparison.
✅ Java provides built-in methods to handle both arrays and strings efficiently.

🚀 Next Steps:

  • Learn multidimensional arrays.
  • Work with StringBuilder for faster string manipulations.
  • Explore sorting and searching techniques on arrays.

Comments

Popular posts from this blog

Iterators and Collections

In Java, iterators are objects that allow for sequential access to the elements of a collection. The Java Collections Framework provides the Iterator interface, which defines methods for iterating over collections such as lists, sets, and maps. Here's an explanation of iterators and their relationship with collections, along with examples: Iterator Interface: The Iterator interface provides methods to iterate over the elements of a collection sequentially: - boolean hasNext(): Returns true if there are more elements to iterate over. - E next(): Returns the next element in the iteration. - void remove():  Removes the last element returned by `next()` from the underlying collection (optional operation). Collections and Iterators: 1. Collection Interface:    - Collections represent groups of objects, such as lists, sets, and maps.    - They provide methods for adding, removing, and accessing elements. 2. Iterator Usage:    - Collections implement the Iter...

The Collection Interface.

  The Collection Interface.