All Exams  >   EmSAT Achieve  >   C++ for EmSAT Achieve  >   All Questions

All questions of Strings for EmSAT Achieve Exam

What is the output of the following code snippet?
string s = "Hello World";
cout << s.length() << endl;
  • a)
    11
  • b)
    10
  • c)
    12
  • d)
    9
Correct answer is option 'A'. Can you explain this answer?

Reem Al Saadi answered
Explanation:

String Length:
- The variable `s` is assigned the string "Hello World".
- The `length()` function in C++ returns the number of characters in the string.
- In the string "Hello World", there are 11 characters including the space.
Therefore, the output of the code snippet will be 11.

Which of the following is not a valid method to concatenate two strings in C++?
  • a)
    Using the '+' operator
  • b)
    Using the 'concat()' function
  • c)
    Using the 'append()' function
  • d)
    Using the 'strcpy()' function
Correct answer is option 'B'. Can you explain this answer?

Hani Al Wahidi answered
Explanation:

The correct answer is option B, which states that using the `concat()` function is not a valid method to concatenate two strings in C.

Concatenating strings in C:
In C, there are multiple ways to concatenate two strings. Concatenation refers to combining two strings into a single string. Here are some commonly used methods:

1. Using the `+` operator:
The `+` operator is used for arithmetic operations in C, but it can also be used to concatenate two strings. However, it does not modify the original strings and creates a new string with the combined result.

2. Using the `strcat()` function:
The `strcat()` function is a standard library function in C, defined in the `string.h` header. It is specifically designed to concatenate two strings. It modifies the original string by appending the second string to the end of the first string.

3. Using the `strncat()` function:
Similar to `strcat()`, the `strncat()` function is used to concatenate two strings. However, it allows specifying the maximum number of characters to be appended from the second string.

4. Using the `strcpy()` and `strcat()` functions:
The `strcpy()` function is used to copy one string into another, and the `strcat()` function is used to concatenate two strings. By combining these functions, we can achieve string concatenation.

Invalid method: concat()
The `concat()` function is not a standard library function in C. It does not exist in the C standard library, so it cannot be used to concatenate strings. It might be a custom function defined by a programmer, but it is not a part of the standard C language.

In summary:
To concatenate two strings in C, the valid methods are using the `+` operator, `strcat()`, `strncat()`, or a combination of `strcpy()` and `strcat()`. The `concat()` function is not a valid method in C for string concatenation.

What is the output of the following code snippet?
string str = "Hello";
cout << str[2];
  • a)
    H
  • b)
    e
  • c)
    l
  • d)
    o
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The output will be 'l' because string indexing starts from 0, so str[2] refers to the third character in the string.

What will be the output of the following code?
```cpp
string s = "abc";
cout << s.find_first_of("def");
```
  • a)
    -1
  • b)
    0
  • c)
    1
  • d)
    2
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The output of the code is -1 because 's.find_first_of("def")' searches for the first occurrence of any character in "def" within the string and returns the index. Since none of those characters are found, it returns -1.

What will be the output of the following code?
```cpp
string s = "Hello World";
cout << s.replace(6, 5, "Everyone").length();
```
  • a)
    11
  • b)
    12
  • c)
    13
  • d)
    14
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The output of the code is 13 because 's.replace(6, 5, "Everyone").length()' replaces a substring starting at index 6 with "Everyone" and then returns the length of the resulting string.

What is the output of the following code snippet?
```cpp
string s = "Hello World";
int index = s.find("World");
cout << index << endl;
```
  • a)
    0
  • b)
    6
  • c)
    11
  • d)
    -1
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The output of the code snippet is 6 because 's.find("World")' returns the starting index of the first occurrence of "World" in the string, which is 6 in this case.

Chapter doubts & questions for Strings - C++ 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 - C++ 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.

C++ for EmSAT Achieve

71 videos|45 docs|15 tests

Top Courses EmSAT Achieve