All Exams  >   EmSAT Achieve  >   Java for EmSAT Achieve  >   All Questions

All questions of Strings for EmSAT Achieve Exam

What is the output of the following code snippet?
String str1 = "Java";
String str2 = new String("Java");
System.out.println(str1 == str2);
  • a)
    true
  • b)
    false
  • c)
    Compile-time error
  • d)
    Runtime error
Correct answer is option 'B'. Can you explain this answer?

The code snippet is incomplete and does not provide a value for the variable "str1". Without knowing the value of "str1", it is not possible to determine the output of the code.

What will be the output of the following code?
String str = "Hello, World";
String result = str.substring(7, 12);
System.out.println(result);
  • a)
    World
  • b)
    Hello
  • c)
    ,
  • d)
    Compilation error
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The 'substring()' method returns a new string that is a substring of the original string. In this case, the substring from index 7 to 11 (excluding the character at index 12) is "World".

What is the output of the following code snippet?
String str = "Hello World";
String[] words = str.split(" ");
System.out.println(words.length);
  • a)
    2
  • b)
    1
  • c)
    11
  • d)
    Compile-time error
Correct answer is option 'A'. Can you explain this answer?

Understanding the Code Snippet
The given code snippet is as follows:
java
String str = "Hello World";
String[] words = str.split(" ");
System.out.println(words.length);

Code Breakdown
- **String Initialization**:
- `String str = "Hello World";` initializes a string variable `str` with the value "Hello World".
- **Splitting the String**:
- `str.split(" ");` uses the `split` method to divide the string into an array of substrings based on the specified delimiter, which in this case is a space (" ").
- **Resulting Array**:
- The method `split(" ")` will create an array with two elements: "Hello" and "World".

Calculating the Output
- **Length of the Array**:
- `words.length` retrieves the number of elements in the array `words`. Since the string "Hello World" was split into two parts, `words.length` will return `2`.

Final Output
- The code then prints this length, resulting in the output `2`.

Conclusion
Given the explanation above, the correct answer to the question is option 'A', as the output of the code snippet is indeed `2`.
This illustrates how the `split` method effectively separates a string into an array based on the specified delimiter, providing a straightforward way to count the number of words in a string.

Which of the following statements is true about mutable and immutable strings in Java?
  • a)
    Strings are always mutable in Java.
  • b)
    Immutable strings cannot be changed once created.
  • c)
    Mutable strings cannot be used in Java.
  • d)
    Immutable strings are more memory-efficient than mutable strings.
Correct answer is option 'B'. Can you explain this answer?

Hadi Al Nahyan answered
Immutable Strings in Java
Immutable strings in Java are strings that cannot be changed once they are created. This means that any operation that appears to modify an immutable string actually creates a new string with the desired modifications. The original string remains unchanged.

Advantages of Immutable Strings
There are several advantages to using immutable strings in Java:

1. Thread Safety: Immutable strings are inherently thread-safe because they cannot be modified. Multiple threads can access and use the same immutable string without worrying about concurrent modifications.

2. Security: Immutable strings are often used to store sensitive information such as passwords or encryption keys. Since they cannot be changed, there is no risk of accidentally modifying or exposing the sensitive data.

3. String Pool: Java maintains a pool of immutable strings, known as the string pool. When a new string is created, Java checks if an identical string already exists in the pool. If it does, the new string references the existing one, reducing memory usage. This string interning helps improve performance and memory efficiency.

4. Caching: Immutable strings can be safely cached, as their values cannot change. This caching can lead to performance improvements, especially when dealing with frequently used strings.

5. Hashing: Immutable strings are suitable for use as keys in hash-based data structures like HashMaps. Since the hash code of a string is based on its content, and immutable strings cannot be modified, the hash code remains consistent.

Conclusion
Immutable strings in Java provide several advantages, including thread safety, security, efficient memory usage through string interning, caching capabilities, and suitability for use as keys in hash-based data structures. Understanding the concept of immutability is crucial for efficient and reliable Java programming.

What is the output of the following code snippet?
String str = "Hello";
String newStr = str.toUpperCase();
System.out.println(newStr);
  • a)
    hello
  • b)
    HELLO
  • c)
    Hello
  • d)
    Compile-time error
Correct answer is option 'B'. Can you explain this answer?

-

Explanation:
- String str = "Hello";: Initialize a String variable `str` with the value "Hello".
- String newStr = str.toUpperCase();: Call the `toUpperCase()` method on the `str` variable, which converts the characters in the string to uppercase and assigns the result to the `newStr` variable.
- System.out.println(newStr);: Print the value of `newStr` to the console.
Therefore, the output of the code snippet will be "HELLO" since the `toUpperCase()` method converts all characters in the string to uppercase.

What will be the output of the following code?
String str = "OpenAI";
String result = str.substring(0, 2) + str.substring(3);
System.out.println(result);
  • a)
    Open
  • b)
    OpenA
  • c)
    pAI
  • d)
    Compilation error
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The 'substring()' method returns a new string that is a substring of the original string. In this case, the substring from index 0 to 1 (excluding the character at index 2) is "Op", and the substring from index 3 to the end is "AI". The two substrings are concatenated to form the string "OpAI".

What is the output of the following code snippet?
String str = "Hello";
str += " World";
System.out.println(str);
  • a)
    HelloWorld
  • b)
    Hello World
  • c)
    WorldHello
  • d)
    Compile-time error
Correct answer is option 'B'. Can you explain this answer?

Ameer Al Aswad answered
It seems like the code snippet is incomplete. The variable "str" has not been assigned any value or operation. Without any further code, it is not possible to determine the output of the snippet.

Which of the following methods is used to determine the length of a string in Java?
  • a)
    count()
  • b)
    size()
  • c)
    length()
  • d)
    getSize()
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The 'length()' method is used to determine the length of a string in Java. For example, 'str.length()' will return the number of characters in the string 'str'.

What will be the output of the following code?
String str = "Hello,World";
String[] tokens = str.split(",");
System.out.println(tokens[0]);
  • a)
    Hello
  • b)
    World
  • c)
    Compilation error
  • d)
    Runtime error
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The 'split()' method divides the original string into substrings at the specified delimiter. In this case, the first token is "Hello", which is stored at index 0 of the 'tokens' array.

What will be the output of the following code?
String str = "Hello, World";
String[] tokens = str.split("\\s+");
System.out.println(tokens[0]);
  • a)
    Hello
  • b)
    World
  • c)
    Compilation error
  • d)
    Runtime error
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The 'split()' method splits the original string into substrings based on one or more whitespace characters. In this case, the first token is "Hello", which is stored at index 0 of the 'tokens' array.

Which of the following methods is used to concatenate two strings in Java?
  • a)
    concat()
  • b)
    merge()
  • c)
    add()
  • d)
    append()
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The 'concat()' method is used to concatenate two strings in Java. For example, 'str1.concat(str2)' will concatenate 'str2' to the end of 'str1'.

What will be the output of the following code?
String str = "Hello,World";
String[] tokens = str.split(",");
System.out.println(tokens.length);
  • a)
    1
  • b)
    2
  • c)
    Compilation error
  • d)
    Runtime error
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The 'split()' method is used to split a string into an array of substrings based on a delimiter. In this case, the delimiter is "," (comma), so the string will be split into two tokens: "Hello" and "World".

What will be the output of the following code?
String str = "Hello,World";
String[] tokens = str.split(",");
System.out.println(tokens[1]);
  • a)
    Hello
  • b)
    World
  • c)
    Compilation error
  • d)
    Runtime error
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The 'split()' method divides the original string into substrings at the specified delimiter. In this case, the second token is "World", which is stored at index 1 of the 'tokens' array.

Chapter doubts & questions for Strings - Java for EmSAT Achieve 2025 is part of EmSAT Achieve exam preparation. The chapters have been prepared according to the EmSAT Achieve exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for EmSAT Achieve 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Strings - Java for EmSAT Achieve in English & Hindi are available as part of EmSAT Achieve exam. Download more important topics, notes, lectures and mock test series for EmSAT Achieve Exam by signing up for free.

Java for EmSAT Achieve

60 videos|37 docs|12 tests

Top Courses EmSAT Achieve