0% found this document useful (0 votes)
10 views

03.01.2025 Array and string

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

03.01.2025 Array and string

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

C#.

Net -Arrays and Strings MCQ


1. How do you declare an array of integers in C#?
a) int[] myArray;
b) array<int> myArray;
c) int myArray[];
d) int array[] myArray;
2. What will be the default value of elements in a newly created int array in C#?
a) 0
b) 1
c) null
d) -1
3. How can you determine the length of an array in C#?
a) array.length
b) array.size
c) array.Length
d) array.count
4. Which of these is a correct way to initialize an array in C#?
a) int[] array = new int[5]{1, 2, 3, 4, 5};
b) int[] array = new int[]{1, 2, 3, 4, 5};
c) int array[] = {1, 2, 3, 4, 5};
d) int[] array = new int[5](1, 2, 3, 4, 5);
5. How do you access the third element in an array named 'data' in C#?
a) data[2];
b) data[3];
c) data(2);
d) data(3);
6. What is the output of the following C# code?
int[] nums = {1, 2, 3};
Console.WriteLine(nums[1]);
a) 1
b) 2
c) 3
d) Error
7. How do you create a two-dimensional array in C#?
a) int[,] multiArray = new int[2,3];
b) int[][] multiArray = new int[2][3];
c) int[2][3] multiArray = new int[][];
d) int[,] multiArray = { {1, 2}, {3, 4, 5} };
8. What does the following code do in C#?
Array.Sort(myArray);
a) Reverses the array
b) Sorts the array in descending order
c) Sorts the array in ascending order
d) Shuffles the array randomly
9. Which method is used to reverse the elements of an array in C#?
a) Array.Reverse()
b) Array.Flip()
c) Array.Invert()
d) Reverse.Array()
10. In C#, what exception is thrown if you access an array index that is out of bounds?
a) ArrayIndexOutOfBoundsException
b) IndexOutOfRangeException
c) ArgumentOutOfRangeException
d) InvalidOperationException
11. What is the correct way to declare a jagged array in C#?
a) int[][] jaggedArray;
b) int[,] jaggedArray;
c) int[] jaggedArray[];
d) int[][,] jaggedArray;
12. How do you copy an array in C#?
a) Array.Copy(sourceArray, destinationArray, length);
b) sourceArray.Copy(destinationArray, length);
c) destinationArray = sourceArray.Copy(length);
d) destinationArray = sourceArray;
13. Which keyword is used to iterate over an array in C#?
a) foreach
b) for
c) in
d) iterate
14. What is the default value for elements in a boolean array in C#?
a) true
b) false
c) null
d) 0
15. How do you initialize an array of strings with 3 elements in C#?
a) string[] names = new string[3]{"John", "Jane", "Doe"};
b) string[] names = new string[]{"John", "Jane", "Doe"};
c) string[] names = {"John", "Jane", "Doe"};
d) All of the above
16. How do you check if an array is empty in C#?
a) array.IsEmpty()
b) array.Length == 0
c) array.Count() == 0
d) array == null
17. Which method is used to find a specific element in an array in C#?
a) Array.Find()
b) Array.Search()
c) Array.Get()
d) Array.Lookup()
18. What is the result of using the Clone() method on an array in C#?
a) It creates a deep copy of the array.
b) It creates a shallow copy of the array.
c) It sorts the array.
d) It reverses the array.
19. How do you declare an array with initial size but without initializing the elements in
C#?
a) int[] nums = new int[5];
b) int[] nums = new int[]{5};
c) int[] nums = new int[] {0, 0, 0, 0, 0};
d) int[] nums = {5};
20. Which of the following is not a valid array declaration in C#?
a) int[] arr = new int[5];
b) var arr = new int[5];
c) int[] arr = new int[]{1, 2, 3, 4, 5};
d) int arr[] = new int[5];
21. How do you concatenate strings in C#?
a) Using the '+' operator
b) Using the '&' operator
c) Using the concat() method
d) Using the append() method
22. What method is used to compare two strings for equality in C#?
a) equals()
b) isEqual()
c) CompareTo()
d) Compare()
23. Which method in the String class returns the length of a string in C#?
a) Length
b) Count
c) Size
d) GetLength
24. How can you convert a string to uppercase in C#?
a) toUpperCase()
b) ToUpper()
c) Uppercase()
d) MakeUpper()
25. What is the output of the following code in C#?
a) Hello,
b) World!
c) ello, World!
d) , World!
26. What is the character used to denote a verbatim string in C#?
a) @
b) #
c) $
d) %
27. How do you split a string on specific characters in C#?
a) Using the Split() method
b) Using the Divide() method
c) Using the Break() method
d) Using the Cut() method

28. What does the Trim() method do in C#?


a) Removes all whitespace
b) Removes whitespace from the start and end of a string
c) Trims a string to a specified length
d) Removes specified characters from a string
29. How do you determine if a string starts with a specific substring in C#?
a) StartsWith()
b) BeginsWith()
c) HasPrefix()
d) LeadingString()
30. What is the result of string concatenation using the following code in C#?
a) HelloWorld
b) Hello World
c) Hello+World
d) Hello_World
31. How do you find the position of a substring within a string in C#?
a) Index()
b) FindIndex()
c) Locate()
d) IndexOf()
32. Which method is used to replace a specified substring with another substring in a
string in C#?
a) Change()
b) Switch()
c) Replace()
d) Modify()
33. What is the output of the following code in C#?
a) S
b) h
c) a
d) C
34. How do you convert a string to a number in C#?
a) int.Parse()
b) Convert.ToInt32()
c) StringToNumber()
d) Both a and b
35. What does the IsNullOrEmpty method do in C#?
a) Checks if a string is empty or consists of only whitespace characters
b) Checks if a string is null or has a length of zero
c) Checks if a string is null or consists of only whitespace characters
d) Checks if a string is null or empty array
36. How do you format a string in C# using placeholders?
a) Using the Format() method
b) Using string interpolation
c) Using the Concat() method
d) Both a and b

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;

You might also like