C# Programming - Strings - Discussion
Discussion Forum : Strings - General Questions (Q.No. 16)
16.
Which of the following statements are correct?
- String is a value type.
- String literals can contain any character literal including escape sequences.
- The equality operators are defined to compare the values of string objects as well as references.
- Attempting to access a character that is outside the bounds of the string results in an IndexOutOfRangeException.
- The contents of a string object can be changed after the object is created.
Discussion:
2 comments Page 1 of 1.
Falgun_P said:
9 years ago
In the first option we can clearly see that the String is not a value type. It\'s a reference type. -->> So, first is incorrect.
Second option: String literals can contain any character literal including escape sequences. That means.
A regular string literal consists of zero or more characters enclosed in double quotes for Example: "Hello". And escape sequences (such as \t for the tab character). For example string c = "hello \t world"; O/P: // hello world.
So string literals can contain any character literal including escape sequences. --->> So, Second option is correct.
Third option is about Equality, while comparing a value types, the equality operator (==) returns "TRUE" if the values of its operands are equal. Else returns "False". And for the reference type (Only for String reference type), the equality operator (==) work exactly as the value type. (For your knowledge, other than string reference type the Equality operator return true if its two operands refer to the same object).
So, the third option is also incorrect because in string reference type its compare the value not reference. --->> So, Third option is incorrect.
Forth option clearly say itself when you try to access something (character) which really does not exist So obviously its throw an exception and which is an IndexOutOfRangeException. --->> So, Forth option is correct.
Fifth option, the content of the string can't be change after the object is created because strings are immutable.
For example: string a = "hello";
A = a + " " + "there"; O/P : "hello there".
During the compilation the compiler creates a new string object to store the new sequence of characters. So, fifth option is also incorrect.
Second option: String literals can contain any character literal including escape sequences. That means.
A regular string literal consists of zero or more characters enclosed in double quotes for Example: "Hello". And escape sequences (such as \t for the tab character). For example string c = "hello \t world"; O/P: // hello world.
So string literals can contain any character literal including escape sequences. --->> So, Second option is correct.
Third option is about Equality, while comparing a value types, the equality operator (==) returns "TRUE" if the values of its operands are equal. Else returns "False". And for the reference type (Only for String reference type), the equality operator (==) work exactly as the value type. (For your knowledge, other than string reference type the Equality operator return true if its two operands refer to the same object).
So, the third option is also incorrect because in string reference type its compare the value not reference. --->> So, Third option is incorrect.
Forth option clearly say itself when you try to access something (character) which really does not exist So obviously its throw an exception and which is an IndexOutOfRangeException. --->> So, Forth option is correct.
Fifth option, the content of the string can't be change after the object is created because strings are immutable.
For example: string a = "hello";
A = a + " " + "there"; O/P : "hello there".
During the compilation the compiler creates a new string object to store the new sequence of characters. So, fifth option is also incorrect.
Bhavani said:
10 years ago
Please explain question?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers