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

Passing and Returning Objects in Java Methods

Passing and Returning Objects in Java Methods In Java, objects can be passed as parameters to methods and returned from methods just like other primitive data types. This allows for flexibility and the manipulation of object state within methods. Let's explore how passing and returning objects work in Java. Passing Objects as Parameters When you pass an object as a parameter to a method, you are essentially passing a reference to that object. This means that changes made to the object inside the method will affect the original object outside the method.  Example: class Car {     String model;     Car(String model) {         this.model = model;     } } public class CarProcessor {     // Method to modify the Car object     static void modifyCar(Car car, String newModel) {         car.model = newModel;     }     public static void main(String[] args) {       ...

Understanding Constructors in Java: A Simple Guide with Examples and Analogies

  What is a Constructor in Java? In Java, a constructor is a special type of method that is used to initialize objects. When you create an object of a class, the constructor is called automatically. Its main job is to set the initial values of the object’s properties or perform any setup that the object needs before it can be used. Why Do We Need Constructors? You need constructors because: Initialization : Constructors are responsible for initializing an object when it is created. Automatic Execution : A constructor is automatically called when an object is created, so you don’t have to manually initialize every property. Simplifying Object Creation : It simplifies object creation by providing default values or custom initialization. Where Do Constructors Fit in Java? Constructors fit within a class. They are used whenever a new object of that class is created, and they allow the object to be initialized. Constructors must have the same name as the class, and they don't have a re...