Welcome to ExamTopics
ExamTopics Logo
- Expert Verified, Online, Free.

Unlimited Access

Get Unlimited Contributor Access to the all ExamTopics Exams!
Take advantage of PDF Files for 1000+ Exams along with community discussions and pass IT Certification Exams Easily.

Exam 70-483 topic 3 question 3 discussion

Actual exam question from Microsoft's 70-483
Question #: 3
Topic #: 3
[All 70-483 Questions]

You have the following C# code.
StringBuilder sb = new StringBuilder(reallyLongString);
The reallyLongString variable is a string in which a very long string is stored.
You need to identify whether a string stored in an object named StringToFind is within the StringBuilder sb object.
Which code should you use?

  • A. sb.Equals(stringToFind);
  • B. sb.ToString().IndexOf(stringToFind);
  • C. sb.ToString().CompareTo(stringToFind);
  • D. sb.ToString().Substring(stringToFind.Length);
Show Suggested Answer Hide Answer
Suggested Answer: A 🗳️
References:
https://docs.microsoft.com/en-us/dotnet/api/system.text.stringbuilder.equals?view=netframework-
4.7.2#System_Text_StringBuilder_Equals_System_Text_StringBuilder_

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
L1am
Highly Voted 4 years, 5 months ago
Equals checks for equality, but the question asks whether StringToFind is within the StringBuilder sb object. So it can contain but not be equal to the sb object. I think the answer can be B, because IndexOf returns index position of the first occurrence of the specified string in the searched instance. If there is no occurrences IndexOf return -1. So f the stringToFind is within sb the return value is >=0 , else -1
upvoted 33 times
...
XXXX
Highly Voted 4 years, 2 months ago
answer B
upvoted 24 times
...
noussa
Most Recent 3 years, 3 months ago
B is the correct answer
upvoted 2 times
M62
3 years, 2 months ago
Agree with noussa it is B
upvoted 1 times
...
...
zzMichielzz
3 years, 6 months ago
B is the Answer. Tested this. StringBuilder sb = new StringBuilder(); sb.Append("This is a very long sentence"); var stringToFind = "long"; var a = sb.Equals(stringToFind); => a = false var b = sb.ToString().IndexOf(stringToFind); => b = 15 var c = sb.ToString().CompareTo(stringToFind); => c = 1 var d = sb.ToString().Substring(stringToFind.Length); => d = "is a very long sentence" StringBuilder sb2 = new StringBuilder(); sb2.Append("ABC"); stringToFind = "BC"; var c1 = sb2.ToString().CompareTo(stringToFind); => c = -1 stringToFind = "ABC"; var c2 = sb2.ToString().CompareTo(stringToFind); => c = 0 stringToFind = "ABA"; var c3 = sb2.ToString().CompareTo(stringToFind); => c = 1 stringToFind = "AAA"; var c4 = sb2.ToString().CompareTo(stringToFind); => c = 1
upvoted 3 times
zzMichielzz
3 years, 6 months ago
The last part is to prove that the outcome of 'CompareTo' has to do with the order. I forgot the last one: stringToFind = "AB"; var c5 = sb2.ToString().CompareTo(stringToFind); // c = 1 Both c1 en c5 are in 'ABC' but c1 has -1 en c5 is 1. c1 = after ABC, and c5 = before ABC.
upvoted 3 times
zzMichielzz
3 years, 6 months ago
so why B is working. Because it really gives a positive value if the string is inside the longstring (>0) otherwise it returns a (-1) and this is perfect to make a condition around this. if (sb.ToString().IndexOf(stringToFind) >= 0) { true part } else { false part }
upvoted 1 times
...
...
...
KT1978
3 years, 6 months ago
A is not the right answer for this. The stringToFind is not equal to the reallyLongString. The answer is B. Although it says stored as an object. A string is an object of type String whose value is text. sb.Equals(stringToFind) return false sb.ToString().IndexOf(stringToFind) returns the position of the string in the really long string
upvoted 2 times
...
Bart_v_M
3 years, 7 months ago
This question does not contain the right answer. A correct answer would be sb.Contains(stringToFind). Then the output is: True.
upvoted 1 times
Bart_v_M
3 years, 7 months ago
Sorry, I was not entirely correct before. Although sb.Contains(stringToFind) is a correct solution that would in my view be the best possible answer to this question, I found out that the following code works as well: string reallyLongString = "redgreenblueblackwhitepurpleyellow"; StringBuilder sb = new StringBuilder(reallyLongString); string stringToFind = "purple"; // gives an output of 1, which means that “purple” is stored in reallyLongString. Console.WriteLine(sb.ToString().Equals(stringToFind)); Now, if the output is 1 stringToFind is stored in sb. If the output is -1, it is not stored in sb. And if the output is 0, stringToFind equals sb. So, my conclusion is that C is the only correct answer to the question.
upvoted 1 times
Bart_v_M
3 years, 7 months ago
Sorry again, I made a mistake in the last line of my code above. This should be: Console.WriteLine(sb.ToString().CompareTo(stringToFind)); The CompareTo method works fine!
upvoted 1 times
Nix2
3 years, 7 months ago
string reallyLongString = "redgreenblueblackwhitepurpleyellow StringBuilder sb = new StringBuilder(reallyLongString); string stringToFind = "orange"; // Also gives an output of 1, even though "orange" is not stored in reallyLongString Console.WriteLine(sb.ToString().IndexOf(stringToFind)); I still think the answer is B.
upvoted 1 times
Nix2
3 years, 7 months ago
Oops, pasted my final, working code. I meant: Console.WriteLine(sb.ToString().CompareTo(stringToFind));
upvoted 1 times
Nix2
3 years, 7 months ago
Doh. Looks like CompareTo has a totally different purpose. Not sure if links are allowed, so just Google it :) Answer is B.
upvoted 1 times
...
...
...
...
...
...
zumm
3 years, 8 months ago
None of the answers are correct. String reallyLongString = "redbluegreenblackpink"; StringBuilder sb = new StringBuilder(reallyLongString); Console.WriteLine("StringBuilder Contents"); Console.WriteLine(sb.ToString()); Object stringToFind = "green"; Console.WriteLine("stringToFind"); Console.WriteLine(stringToFind); //Contains Console.WriteLine("Contains way"); Console.WriteLine(sb.ToString().Contains(stringToFind.ToString())); //IndexOf Console.WriteLine("IndexOf way"); Console.WriteLine(sb.ToString().IndexOf(stringToFind.ToString())>0); //Output StringBuilder Contents redbluegreenblackpink stringToFind green Contains way True IndexOf way True //Equals is used for comparing if two StringBuilders are the same.
upvoted 2 times
thiemamd
3 years, 8 months ago
I have tested 'B'. It works for me. String reallyLongString = "redbluegreenblackpink"; StringBuilder sb = new StringBuilder(reallyLongString); string stringTofind = "blue"; Console.WriteLine(sb.Equals(stringTofind)); Console.WriteLine(sb.ToString().IndexOf(stringTofind)); //It prints //False //3
upvoted 3 times
...
DiegoB
3 years, 8 months ago
it said the string is store in an object, so must be object " storetofind = "green"; ", in this case indexOf and Substring doesnt work
upvoted 2 times
DiegoB
3 years, 8 months ago
my bad, your right
upvoted 1 times
...
...
...
dawcio12
3 years, 9 months ago
Correct answer is A) because "StringToFind is a string stored in an object".
upvoted 1 times
...
lh2607
3 years, 9 months ago
B is the answer.
upvoted 2 times
...
Jannie
3 years, 10 months ago
Answer is B
upvoted 2 times
...
Xzibit23
3 years, 10 months ago
A method within the stringbuilder class, called contains would be perfect for this task, however I dont see it in the answer options, we could go with IndexOf() , this one shows the point of apperance of some unicode text in a stringbuilder. If the text is within the stringbuilder we will get the position returned, I agree on B as well
upvoted 3 times
...
VladWork
4 years ago
Answer is A: you compare 2 objects: object named StringToFind I think they missed a line of code where StringToFind is a StringBuilder
upvoted 1 times
...
TonyBezerra
4 years, 1 month ago
I just tested and the option A didn't work, it returned false, while the option B returned 0. So, the right answer is B.
upvoted 6 times
...
MrSmock
4 years, 1 month ago
The Equal method in C# is for comparing two instances of the same type. We are comparing StringBuilder and a String object so answer A will not work. I'll say answer B is correct.
upvoted 4 times
...
Vodkaalk
4 years, 6 months ago
C is the answer
upvoted 3 times
sscooter1010
4 years, 2 months ago
I don't see how. The requirement is to check to see if you can find a string within the reallyLongString. CompareTo will compare against the entire string. I vote for B, which would search to the location of the string within reallyLongString.
upvoted 11 times
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...