0% found this document useful (0 votes)
23 views2 pages

Java String Function MCQs Explained

Uploaded by

ekta patel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views2 pages

Java String Function MCQs Explained

Uploaded by

ekta patel
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java String Function MCQs

1. What will be the output of the following code?

String str = "Java";


[Link]([Link]());
A) 3
B) 4
C) 5
D) 0
Answer: B) 4

2. Which method is used to compare two strings for equality (case-sensitive)?


A) equals()
B) ==
C) compareTo()
D) equalsIgnoreCase()
Answer: A) equals()

3. What is the result of this code?

String s1 = "Hello";
String s2 = "HELLO";
[Link]([Link](s2));
A) true
B) false
C) compilation error
D) runtime exception
Answer: A) true

4. Which method returns the character at a specified index?


A) charAt()
B) getChar()
C) indexOf()
D) substring()
Answer: A) charAt()

5. What is the output?

String s = "Programming";
[Link]([Link](3, 7));
A) gram
B) ogra
C) gramm
D) grami
Answer: A) gram

6. Which method is used to remove leading and trailing spaces?


A) trim()
B) strip()
C) stripSpaces()
D) removeSpaces()
Answer: A) trim()

7. What does indexOf('a') return if the character is not found?


A) 0
B) -1
C) 1
D) Exception
Answer: B) -1

8. What will be the output?

String s = "Java";
[Link]([Link]());
A) java
B) JAVA
C) Java
D) JAVa
Answer: B) JAVA

9. Which of the following creates a new String object?


A) String s = "Hello";
B) String s = new String("Hello");
C) Both A and B
D) None of these
Answer: C) Both A and B

10. What does this code print?

String s = "Java Programming";


[Link]([Link]('a', 'o'));
A) Jovo Progromming
B) Jovo Progrumming
C) Java Programming
D) Error
Answer: A) Jovo Progromming

Common questions

Powered by AI

Creating a String object with the 'new' keyword explicitly instructs Java to allocate new memory for the string object, thus not using the string pool. This results in a distinct object even if an identical string exists in the pool. In contrast, using a string literal checks the pool for existing strings, enhancing memory efficiency by reusing the existing string reference. This distinction is important for understanding memory usage and identity concerns in Java .

The charAt() method is crucial for accessing specific characters in a string by their index. It allows developers to iterate over a string and perform operations on individual characters. For instance, given the string "Hello", calling charAt(1) will return 'e', which is the character at index 1. This method is commonly used in loops for tasks like counting specific characters or checking character cases .

The substring() method returns a new string that is a subsequence of the given string, starting from a specified begin index, and optionally extends to an end index. For example, with the string "Programming", calling substring(3,7) produces "gram". This method is useful for extracting parts of a string needed for parsing, data manipulation, and transformation tasks without altering the original string .

Not understanding string immutability can lead to inefficient code, as each modification creates a new string object and discards the old one. This excessive object creation results in high memory usage and increased garbage collection, negatively impacting performance, especially within loops, where concatenation operations are frequent. Developers often mitigate this by using StringBuilder for mutable sequences .

When using the toUpperCase() method, developers should be mindful of locale-specific characters that may not convert as expected across different languages. This is crucial in applications dealing with internationalization. For instance, converting Turkish 'i' to uppercase yields different results based on locale. Additionally, since strings in Java are immutable, toUpperCase() returns a new string, which means repeated calls can lead to increased memory usage .

The equalsIgnoreCase() method is preferred when comparing strings where case distinctions are not important, such as validating user input where capitalization does not matter. For example, comparing usernames or processing text inputs. Conversely, the equals() method should be used when case distinctions are critical, such as in password validation or cryptographic operations where 'Password' and 'password' should not be treated as equal .

The equals() method is used to compare the content of two strings for equality, which means it checks if the actual characters in both strings are the same. In contrast, '==' checks for reference equality, meaning it tests whether both strings point to the same memory location. Using equals() is crucial for content comparison, especially when strings might be created with new String() and thus have different references .

The indexOf() method is essential for locating the position of a character or a substring within a string. It returns the index of the first occurrence, which helps in tasks such as searching and validation within strings. If the specified character or substring is not found, it returns -1. For instance, calling indexOf('x') on "abc" returns -1, indicating 'x' is not present .

The replace() method is useful in scenarios requiring dynamic text modification, such as formatting outputs, adapting templates, or encoding data. For example, in multilingual support, replacing placeholders with localized strings is imperative. In text processing, replace() quickly sanitizes input by swapping or removing harmful characters (e.g., replacing < and > with safe HTML entities).

The trim() method is widely used for removing leading and trailing whitespace from strings, which is crucial for data cleaning and ensuring consistent data input. This is particularly useful in user input validation and preparation of strings for storage or comparison. However, a limitation is that trim() does not affect whitespace inside the string, sometimes requiring additional processing to remove all unnecessary spaces .

You might also like