Software Development Exam  >  Software Development Notes  >  Basics of Java  >  Assignment: Strings in Java

Assignment: Strings in Java | Basics of Java - Software Development PDF Download

Multiple Choice Questions (MCQs)

Q.1. What is the correct way to initialize a String variable in Java?
(a) String name = "John";
(b) String name;
(c) String name = new String("John");
(d) String name = String("John");

Ans. (a)

Q.2. Which of the following methods is used to obtain the length of a String in Java?
(a) length()
(b) size()
(c) count()
(d) getSize()

Ans. (a)

Q.3. Which of the following is an example of an immutable String in Java?
(a) StringBuffer
(b) StringBuilder
(c) String
(d) Array

Ans. (c)

Q.4. Which of the following methods is used to concatenate two Strings in Java?
(a) append()
(b) concat()
(c) join()
(d) merge()

Ans. (b)

Q.5. What will be the output of the following code snippet?

String str1 = "Hello";

String str2 = "World";

String result = str1.concat(str2);
System.out.println(result);

(a) HelloWorld
(b) Hello World
(c) Hello
(d) World

Ans. (a)

High Order Thinking Questions (HOTS)

Q.1. Explain the concept of immutability in Strings in Java. Why are Strings immutable?

In Java, immutability refers to the inability to change the value of an object once it is created. Strings are immutable in Java to ensure their security and integrity. Immutability allows strings to be safely shared across multiple threads without the risk of data corruption.

Q.2. Compare and contrast StringBuilder and StringBuffer classes in Java.

StringBuilder and StringBuffer are both mutable alternatives to the immutable String class. StringBuilder is not synchronized and is therefore faster but not thread-safe. StringBuffer, on the other hand, is synchronized and thread-safe but slower.

Q.3. Write a Java program to reverse a given String without using any built-in methods or libraries.

Here's an example of reversing a string without using built-in methods:
String input = "Hello World";

String reversed = "";

for (int i = input.length() - 1; i >= 0; i--) {

    reversed += input.charAt(i);

}

System.out.println(reversed);

Q.4. How can you check if two String objects contain the same sequence of characters in Java?

To check if two String objects contain the same sequence of characters, you can use the equals() method or the equalsIgnoreCase() method in Java.

Q.5. What is the purpose of the split() method in the String class? Provide an example of splitting a string based on a delimiter.

The split() method in the String class is used to split a string into an array of substrings based on a specified delimiter. Here's an example:

String input = "apple,banana,orange";

String[] fruits = input.split(",");


for (String fruit : fruits) {

    System.out.println(fruit);

}

Fill in the Blanks

Q.1. A _______ in Java is a sequence of characters.

Ans. String

Q.2. The length of a String can be obtained using the _______ method.

Ans. length()

Q.3. _______ and _______ are mutable alternatives to the immutable String class.

Ans. StringBuilder, StringBuffer

Q.4. The _______ method is used to concatenate two String objects in Java.

Ans. concat()

Q.5. The split() method splits a String into an _______ based on a specified delimiter.

Ans. array

True or False

Q.1. Strings in Java are mutable.

Ans. False

Q.2. The StringBuilder class is synchronized and thread-safe.

Ans. False

Q.3. The charAt() method returns the character at a specific index in a String.

Ans. True

Q.4. The replace() method can be used to remove characters from a number string in Java.

Ans. True

Q.5. The StringBuffer class is more efficient than the StringBuilder class.

Ans. False

Hands-On Questions

Q.1. Write a Java program that takes a String input from the user and prints the reverse of that String.

import java.util.Scanner;


public class ReverseString {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a string: ");

        String input = scanner.nextLine();

        String reversed = "";


        for (int i = input.length() - 1; i >= 0; i--) {

            reversed += input.charAt(i);

        }


        System.out.println("Reversed string: " + reversed);

    }

}

Q.2. Write a Java program that counts the number of occurrences of a specific character in a given String.

public class CharacterCount {

    public static void main(String[] args) {

        String input = "Hello World";

        char target = 'o';

        int count = 0;


        for (int i = 0; i < input.length(); i++) {

            if (input.charAt(i) == target) {

                count++;

            }

        }


        System.out.println("Occurrences of '" + target + "': " + count);

    }

}

Q.3. Write a Java program that removes all the spaces from a given String.

public class RemoveSpaces {

    public static void main(String[] args) {

        String input = "  Hello  World  ";

        String withoutSpaces = input.replaceAll("\\s", "");


        System.out.println("Without spaces: " + withoutSpaces);

    }

}

Q.4. Write a Java program that splits a CSV (Comma-Separated Values) String into an array of values.

public class SplitCSV {

    public static void main(String[] args) {

        String input = "apple,banana,orange";

        String[] fruits = input.split(",");


        for (String fruit : fruits) {

            System.out.println(fruit);

        }

    }

}

Q.5. Write a Java program that removes all non-digit characters from a given number String.

public class RemoveNonDigits {

    public static void main(String[] args) {

        String input = "a1b2c3";

        String digitsOnly = input.replaceAll("\\D", "");


        System.out.println("Digits only: " + digitsOnly);

    }

}

The document Assignment: Strings in Java | Basics of Java - Software Development is a part of the Software Development Course Basics of Java.
All you need of Software Development at this link: Software Development
60 videos|37 docs|12 tests

Top Courses for Software Development

60 videos|37 docs|12 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

ppt

,

Previous Year Questions with Solutions

,

Important questions

,

Sample Paper

,

Assignment: Strings in Java | Basics of Java - Software Development

,

Semester Notes

,

Assignment: Strings in Java | Basics of Java - Software Development

,

Extra Questions

,

Free

,

Viva Questions

,

practice quizzes

,

Summary

,

shortcuts and tricks

,

mock tests for examination

,

MCQs

,

Exam

,

pdf

,

past year papers

,

Objective type Questions

,

study material

,

video lectures

,

Assignment: Strings in Java | Basics of Java - Software Development

;