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

Strings MCQs for EmSAT Achieve Exam

It covers all Important Questions with answers on Strings for the EmSAT Achieve exam. The questions are based on important topics. Details about the questions:
  • Topic: Strings
  • Type of Questions: MCQs with solutions
  • Number of Questions: 30
  • You can attempt them on EduRev to score high in 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.

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 is the output of the following code snippet?
String str1 = "Hello";
String str2 = "Hello";
System.out.println(str1 == str2);
  • a)
    true
  • b)
    false
  • c)
    Compile-time error
  • d)
    Runtime error
Correct answer is option 'A'. Can you explain this answer?

Null;

String str2 = "";

System.out.println(str1 == null);

System.out.println(str2 == null);

System.out.println(str1.equals(null));

System.out.println(str2.equals(null));

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 will be the output of the following code?
String str = "Java Programming";
String result = str.replace("Java", "Python");
System.out.println(result);
  • a)
    Java Programming
  • b)
    Python Programming
  • c)
    Programming
  • d)
    Compilation error
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The 'replace()' method replaces all occurrences of a specified substring with another substring. In this case, "Java" is replaced with "Python", resulting in the string "Python Programming".

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'.

Chapter doubts & questions for Strings - Java for EmSAT Achieve 2026 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 2026 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