Home Subjects C# Programming

C# Programming

C# and .NET for campus placement

154 Q 4 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 111–120 of 154
Topics in C# Programming
Q.111 Medium Basics & Syntax
Which collection type in C# maintains insertion order and allows duplicate elements?
A HashSet
B Dictionary
C List
D SortedSet
Correct Answer:  C. List
EXPLANATION

List<T> maintains insertion order and allows duplicates. HashSet<T> doesn't allow duplicates. Dictionary<K,V> maintains key-value pairs. SortedSet<T> keeps elements sorted without duplicates.

Test
Q.112 Medium Basics & Syntax
What will be the output of: string s1 = "Hello"; string s2 = "Hello"; Console.WriteLine(s1 == s2);
A true
B false
C null
D Error
Correct Answer:  A. true
EXPLANATION

In C#, the == operator for strings compares their values, not references. Since both s1 and s2 contain "Hello", the result is true. This is different from object references comparison.

Test
Q.113 Medium Basics & Syntax
In C#, what does the 'params' keyword allow you to do?
A Pass parameters by reference
B Accept a variable number of arguments of the same type
C Define optional parameters
D Declare parameters with default values
Correct Answer:  B. Accept a variable number of arguments of the same type
EXPLANATION

'params' allows a method to accept a variable number of arguments of the same type as an array. For example: void Method(params int[] numbers) can accept 0 or more integers.

Test
Q.114 Medium Basics & Syntax
Which of the following is a nullable type in C#?
A int?
B Nullable
C Both A and B are equivalent
D Neither A nor B can be nullable
Correct Answer:  C. Both A and B are equivalent
EXPLANATION

Both int? and Nullable<int> represent the same nullable integer type. int? is the shorthand syntax for Nullable<int>. They allow the variable to hold a null value in addition to integer values.

Test
Q.115 Medium Basics & Syntax
Which method is used to convert a string to an integer in C#?
A string.ToInteger()
B int.Parse() or int.TryParse()
C Convert.ToInt32()
D Both B and C are correct
Correct Answer:  D. Both B and C are correct
EXPLANATION

Both int.Parse() and Convert.ToInt32() can convert strings to integers. int.TryParse() is safer as it returns a boolean indicating success/failure. All are valid approaches.

Test
Q.116 Medium Basics & Syntax
In C#, what is the difference between 'struct' and 'class'?
A struct is a reference type, class is a value type
B struct is a value type, class is a reference type
C There is no difference; they are synonymous
D struct supports inheritance, class does not
Correct Answer:  B. struct is a value type, class is a reference type
EXPLANATION

struct is a value type stored on the stack, while class is a reference type stored on the heap. struct does not support inheritance from other structs/classes (except interfaces), while classes do.

Test
Q.117 Medium Basics & Syntax
Which of the following correctly demonstrates string interpolation in C# 6.0 and above?
A string name = "John"; Console.WriteLine("Hello " + name);
B string name = "John"; Console.WriteLine(string.Format("Hello {0}", name));
C string name = "John"; Console.WriteLine($"Hello {name}");
D string name = "John"; Console.WriteLine(@"Hello {name}");
Correct Answer:  C. string name = "John"; Console.WriteLine($"Hello {name}");
EXPLANATION

String interpolation uses the \( prefix with curly braces to embed expressions. Option C shows the correct syntax: \)"Hello {name}". The @ symbol creates verbatim strings, not interpolated strings.

Test
Q.118 Medium Basics & Syntax
What will be the output of the following code? int a = 5; int b = ++a; Console.WriteLine(a + " " + b);
A 5 5
B 6 6
C 6 5
D 5 6
Correct Answer:  B. 6 6
EXPLANATION

++a is pre-increment, which increments 'a' to 6 first, then assigns it to 'b'. So both a and b become 6. Output is '6 6'.

Test
Q.119 Medium Basics & Syntax
Which of the following is a correct implementation of property with auto-backing field in C#?
A public string Name { get; set; }
B public string Name { get set; }
C public string Name => _name;
D public string Name { get; private set; }_name;
Correct Answer:  A. public string Name { get; set; }
EXPLANATION

Auto-properties with get; set; syntax automatically create backing fields. Option C is an expression-bodied property (read-only).

Test
Q.120 Medium Basics & Syntax
What is the output of: var x = 5; Console.WriteLine(x.GetType().Name);
A Object
B Int32
C int
D var
Correct Answer:  B. Int32
EXPLANATION

The 'var' keyword declares a variable where the type is inferred at compile-time. Here, x is inferred as int, and GetType().Name returns 'Int32'.

Test
IGET
IGET AI
Online · Exam prep assistant
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips