C# Programming Questions and Answers
Which of these methods is an alternative to getChars() that stores the characters in an array of bytes?
getBytes() stores the character in an array of bytes. It uses default character to byte conversions.
What will be the output of the given code snippet?
staticvoid main(String args[])
{
char chars[]={'x', 'y', 'z'};
String s =newString(chars);
Console.WriteLine(s);
}
String(chars) is a constructor of class string, it initializes string s with the values stored in character array chars, therefore s contains “xyz”.
Output :xyz
Which of these methods can be used to convert all characters in a String into a character array?
Answer: Option (C)
Choose the effective stringBuilder method which helps in producing output for the given code?
staticvoid Main(string[] args)
{
StringBuilder s =new StringBuilder("object");
s./*______*/("Oriented Language");
Console.WriteLine(s);
Console.ReadLine();
}
Output : objectOriented Language
static void Main(string[] args)
{
StringBuilder s = new StringBuilder(“object”);
s.Append(“Oriented Language”);
Console.WriteLine(s);
Console.ReadLine();
}
Output : objectOriented Language
Which of these methods of the class String is used to obtain length of String object?
Method length() of string class is used to get the length of the object as string.Length and hence invokes the length() method.
How to print \\ on the screen?
Console.WriteLine(“\\\\”);
Output : \\
What will be the output for the given code snippet?
staticvoid Main(string[] args)
{
string s =" i love you";
Console.WriteLine(s.IndexOf('l')+" "+ s.lastIndexOf('o')+" "+ s.IndexOf('e'));
Console.ReadLine();
}
indexof(‘l’) and lastIndexof(‘o’) are pre defined functions which are used to get the index of first and last occurrence of
the character pointed by l and c respectively in the given array.
Output : 3, 9, 6
How is a string typically processed?
Answer: Option (A)