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 121–130 of 154
Topics in C# Programming
Q.121 Medium Basics & Syntax
Which statement about anonymous functions in C# is TRUE?
A Anonymous functions must have a return type
B Lambda expressions are a type of anonymous function
C Anonymous functions cannot access outer scope variables
D All anonymous functions require explicit parameter types
Correct Answer:  B. Lambda expressions are a type of anonymous function
EXPLANATION

Lambda expressions (introduced in C# 3.0) are a concise form of anonymous functions. They can infer return types and parameter types, and can access outer scope variables.

Test
Q.122 Medium Basics & Syntax
What is the correct way to create a generic method in C#?
A public void Method() { }
B public void Method() { }
C public void Method[T]() { }
D public T void Method() { }
Correct Answer:  A. public void Method() { }
EXPLANATION

Generic methods use angle brackets <T> after the method name. The syntax is: public void Method<T>() where T is the type parameter.

Test
Q.123 Medium Basics & Syntax
In C#, what is the difference between '==' and 'Equals()' when comparing strings?
A No difference, they are identical
B '==' compares references, Equals() compares values for strings
C 'Equals()' is slower than '=='
D '==' can compare null values safely
Correct Answer:  B. '==' compares references, Equals() compares values for strings
EXPLANATION

For strings in C#, '==' is overloaded to compare values, and Equals() also compares values. However, for other reference types, '==' compares references while Equals() compares values.

Test
Q.124 Medium Basics & Syntax
Which collection type in C# automatically prevents duplicate values?
A List
B HashSet
C Queue
D Stack
Correct Answer:  B. HashSet
EXPLANATION

HashSet<T> is an unordered collection that automatically prevents duplicate values. List<T> allows duplicates.

Test
Q.125 Medium Basics & Syntax
What is the output of: bool result = (5 > 3) && (10 < 8); Console.WriteLine(result);
A true
B false
C null
D error
Correct Answer:  B. false
EXPLANATION

5 > 3 is true, but 10 < 8 is false. The && (AND) operator returns false because one condition is false.

Test
Q.126 Medium Basics & Syntax
Which of the following correctly demonstrates the use of an out parameter in C#?
A void Calculate(out int result) { result = 10; }
B void Calculate(in int result) { result = 10; }
C void Calculate(ref int result) { result = 10; }
D void Calculate(int result) { result = 10; }
Correct Answer:  A. void Calculate(out int result) { result = 10; }
EXPLANATION

The 'out' keyword is used when a method needs to return a value through a parameter. The parameter must be assigned before the method exits.

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

++x is pre-increment, so x becomes 6, then y is assigned 6. Both x and y are 6.

Test
Q.128 Medium Basics & Syntax
Which of the following demonstrates proper use of string interpolation in C# 6.0+?
A $"Name is {name}"
B "Name is " + name
C string.Format("Name is {0}", name)
D "Name is \{name}"
Correct Answer:  A. $"Name is {name}"
EXPLANATION

String interpolation uses the $ prefix followed by curly braces containing expressions.

Test
Q.129 Medium Basics & Syntax
What happens when you try to access an array element beyond its length in C#?
A Returns null
B Returns default value of the type
C Throws IndexOutOfRangeException
D Returns the last element
Correct Answer:  C. Throws IndexOutOfRangeException
EXPLANATION

Accessing an array index beyond its bounds throws an IndexOutOfRangeException at runtime.

Test
Q.130 Medium Basics & Syntax
Which of the following statements about properties in C# is TRUE?
A Properties can only have get accessors
B Properties can have get and/or set accessors with different access levels
C Properties cannot be overridden
D Properties must have both get and set accessors
Correct Answer:  B. Properties can have get and/or set accessors with different access levels
EXPLANATION

Properties can have get and set accessors independently with different access modifiers like public get, private set.

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