03.01.2025 Array and string
03.01.2025 Array and string
37. What is the escape sequence used for a new line in C#?
a) \\n
b) \n
c) /n
d) //n
38. How can you check if a string contains a specific substring in C#?
a) Contains()
b) Has()
c) Includes()
d) Find()
39. What is the output of the following C# code?
int[] arr = { 1, 2, 3, 4, 5 };
arr[1] = 10;
arr[3] = arr[1] + arr[2];
Console.WriteLine(arr[3]);
a) 12
b) 13
c) 15
d) 10
40.Which of the following statements is true about arrays in C#?
a) The length of an array can be changed after it is declared.
b) The array elements must all be of the same data type.
c) Arrays are automatically initialized to their default values.
d) The array size must be known at runtime.
41.What will the following code output?
int[] arr = { 5, 10, 15 };
Array.Reverse(arr);
Console.WriteLine(string.Join(", ", arr));
a) 5, 10, 15
b) 15, 10, 5
c) 10, 15, 5
d) 15, 5, 10
42.What is the correct syntax to declare a jagged array in C#?
a) int[][] arr = new int[3][5];
b) int[3, 5] arr = new int[3, 5];
c) int[3][5] arr = new int[3][5];
d) int[] arr[] = new int[3][];
43.Which of the following methods can be used to search for an element in an array in
C#?
a) Array.Search()
b) Array.Find()
c) Array.FindIndex()
d) Array.IndexOf()
44.What will be the output of the following C# code?
string str = "Hello, World!";
string newStr = str.Substring(7, 5);
Console.WriteLine(newStr);
a) Hello
b) World
c) World!
d) lo, W
45.Which of the following string methods in C# can be used to remove leading and
trailing whitespace from a string?
a) Trim()
b) Remove()
c) Strip()
d) Clean()
46.What is the result of the following C# code?
string str = "CSharp";
str = str.Insert(3, "NET"); OUTPUT Is: CShNETarp
Console.WriteLine(str);
a) CSharpNET
b) CSharp
c) CNETSharp
d) CSNETharp
47.Which of the following methods will convert a string to an integer in C#?
a) Convert.ToInt32()
b) Int32.Parse()
c) String.ToInt()
d) Both a and b
48.Given the string "apple", what will be the result of calling string.Concat("fruit: ",
"apple".ToUpper())?
a) fruit: apple
b) fruit: APPLE
c) fruit: Apple
d) APPLEfruit:
49.What will the following code output?
string str = "apple, banana, cherry";
string[] words = str.Split(',');
Console.WriteLine(words[1].Trim());
a) banana,
b) banana
c) banana
d) apple
50.Which of the following expressions will result in an IndexOutOfRangeException?
a) int[] arr = new int[5]; arr[4] = 10;
b) int[] arr = new int[5]; arr[5] = 10;
c) string str = "hello"; str[2] = 'a';
d) string[] arr = new string[3]; arr[2] = "test";
C# Answer key
1.a) int[] myArray;
2.a) 0
3. c) array.Length
4.b) int[] array = new int[]{1, 2, 3, 4, 5};
5. a) data[2];
6. b) 2
7. a) int[,] multiArray = new int[2,3];
8. c) Sorts the array in ascending order
9. a) Array.Reverse()
10. b) IndexOutOfRangeException
11. a) int[][] jaggedArray;
12. a) Array.Copy(sourceArray, destinationArray, length);
13. a) foreach
14. b) false
15. d) All of the above
16. b) array.Length == 0
17. a) Array.Find()
18. b) It creates a shallow copy of the array.
19. a) int[] nums = new int[5];
20. d) int arr[] = new int[5];
21. a) Using the '+' operator
22. a) equals()
23. a) Length
24. b) ToUpper()
25. b) World!
26. a) @
27. a) Using the Split() method
28. b) Removes whitespace from the start and end of a string
29. a) StartsWith()
30. b) Hello World
31. d) IndexOf()
32. c) Replace()
33. b) h
34. d) Both a and b
35. b) Checks if a string is null or has a length of zero
36. d) Both a and b
37. b) \n
38. a) Contains()
39.b) 13
40.b) The array elements must all be of the same data type.
41.b) 15, 10, 5
42.d) int[] arr[] = new int[3][];
43.d) Array.IndexOf()
44.b) World
45.a) Trim()
46.a) CSharpNET
47.d) Both a and b
48.b) fruit: APPLE
49.c) banana
50.b) int[] arr = new int[5]; arr[5] = 10;