All Exams  >   Software Development  >   Basics of Java  >   All Questions

All questions of Strings for Software Development Exam

1 Crore+ students have signed up on EduRev. Have you? Download the App

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?

Sonal Yadav answered
The 'split()' method splits a string into an array of substrings based on the specified delimiter. In this case, the delimiter is a space, so the string is split into two words.

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

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?

Answer:

The correct answer is option 'C', the method used to determine the length of a string in Java is length(). Here is an explanation of why this is the correct answer:

1. Introduction:
In Java, the length of a string can be determined using the length() method. This method is a member of the String class and returns the number of characters present in the string.

2. Syntax:
The syntax to use the length() method is as follows:
```java
int length = str.length();
```
where str is the string for which we want to determine the length, and length is the variable that stores the length of the string.

3. Example:
Let's consider an example to understand how the length() method works:
```java
String str = "Hello, World!";
int length = str.length();
System.out.println("The length of the string is: " + length);
```
Output:
```
The length of the string is: 13
```
In the above example, the length() method is called on the string "Hello, World!" and the returned value (13) is stored in the variable length. The value is then printed to the console.

4. Explanation:
The length() method in Java calculates the length of a string by counting the number of characters present in the string. It does not include any whitespace or special characters in the count, only the actual characters in the string are considered.

5. Comparison with other options:
Let's compare option 'C' (length()) with the other given options:
- Option 'A' (count()): There is no built-in method called count() in Java for determining the length of a string.
- Option 'B' (size()): The size() method is used to determine the number of elements in a collection or container, such as an ArrayList. It is not applicable to strings.
- Option 'D' (getSize()): There is no built-in method called getSize() in Java for determining the length of a string.

Therefore, the correct option for determining the length of a string in Java is length().

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.

What will be the output of the following code?
String str = "Java";
str.toUpperCase();
System.out.println(str);
  • a)
    JAVA
  • b)
    java
  • c)
    Compilation error
  • d)
    Runtime error
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The 'toUpperCase()' method returns a new string with all characters converted to uppercase. However, the original string 'str' remains unchanged. Therefore, the output will be "java".

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.

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.

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("\\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.

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

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

Basics of Java

60 videos|37 docs|12 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev