Home Subjects C# Programming Basics & Syntax

C# Programming
Basics & Syntax

C# and .NET for campus placement

100 Q 4 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 1–10 of 100
Topics in C# Programming
Which of the following is a correct way to declare a property with auto-implementation in C#?
A public int Age { get; set; }
B public int Age() { get; set; }
C public Age int { get; set; }
D public int get Age; set;
Correct Answer:  A. public int Age { get; set; }
EXPLANATION

Auto-implemented properties in C# use the syntax: public Type PropertyName { get; set; } without explicitly defining the backing field.

Test
Consider this code: int[] arr = new int[5]; foreach(var item in arr) { Console.Write(item + " "); } What is the output?
A Nothing (empty output)
B 0 0 0 0 0
C Throws an IndexOutOfRangeException
D Null Null Null Null Null
Correct Answer:  B. 0 0 0 0 0
EXPLANATION

Arrays in C# are automatically initialized with default values. For int arrays, the default value is 0. Therefore, the output will be five zeros separated by spaces.

Test
Q.3 Medium Basics & Syntax
What is the difference between 'is' and '==' operators when comparing object references in C#?
A 'is' checks reference equality, '==' can be overloaded for value comparison
B '==' checks reference equality, 'is' checks type compatibility
C Both are identical for reference types
D 'is' only works with nullable types
Correct Answer:  A. 'is' checks reference equality, '==' can be overloaded for value comparison
EXPLANATION

The 'is' operator checks reference equality by default and is used for type checking, while '==' can be overloaded by classes to provide custom equality logic.

Test
In C# 2024, which feature allows you to use pattern matching to deconstruct objects and filter simultaneously in a single expression?
A List patterns with logical operators
B Record types with positional parameters
C Nullable reference types
D Anonymous methods
Correct Answer:  A. List patterns with logical operators
EXPLANATION

C# 12 (2024) introduced advanced list patterns that allow filtering and deconstructing collections in pattern matching expressions, improving code readability and efficiency.

Test
Q.5 Medium Basics & Syntax
What will be the behavior of the following code snippet?
string str = null;
string result = str?.ToUpper() ?? "DEFAULT";
Console.WriteLine(result);
A Prints 'DEFAULT'
B Prints an empty string
C Throws NullReferenceException
D Prints 'null'
Correct Answer:  A. Prints 'DEFAULT'
EXPLANATION

The null-coalescing operator (?.) with null-coalescing assignment (??) ensures that when str is null, the expression evaluates to null, and then ?? provides the default value 'DEFAULT'.

Test
Q.6 Medium Basics & Syntax
Which of the following statements about ref and out parameters in C# is correct?
A ref parameter must be initialized before passing, out parameter need not be
B out parameter must be initialized before passing, ref parameter need not be
C Both ref and out require initialization before method call
D Neither ref nor out requires initialization
Correct Answer:  A. ref parameter must be initialized before passing, out parameter need not be
EXPLANATION

ref parameters must be initialized before being passed to a method, while out parameters don't require initialization beforehand. The method must assign a value to out parameters before returning.

Test
Which collection type in C# is best suited for key-value pair storage with unique keys?
A List
B Dictionary
C HashSet
D Queue
Correct Answer:  B. Dictionary
EXPLANATION

Dictionary<K, V> stores key-value pairs and ensures unique keys. List stores ordered values, HashSet stores unique values only, Queue is FIFO.

Test
What is the output of: string s = "Hello"; Console.WriteLine(s.GetHashCode() == s.GetHashCode());
A true
B false
C NullReferenceException
D Compiler Error
Correct Answer:  A. true
EXPLANATION

GetHashCode() on the same string returns the same hash code value, so comparison is true. Strings are immutable in C#.

Test
In C# 2024, what is the latest feature for null-safety introduced in recent versions?
A Nullable reference types
B required keyword for properties
C init-only properties
D All of the above
Correct Answer:  D. All of the above
EXPLANATION

C# has progressively introduced nullable reference types, required keyword (C# 11), and init-only properties (C# 9) for better null-safety and immutability.

Test
Q.10 Medium Basics & Syntax
Which of the following correctly demonstrates the ternary operator in C#?
A int x = (age >= 18) ? "adult" : "minor";
B int x = (age >= 18) ? 1 : 0;
C bool x = (age >= 18) ? true; false;
D string x = (age >= 18) ? 18 : 0;
Correct Answer:  B. int x = (age >= 18) ? 1 : 0;
EXPLANATION

Option B is correct as both branches return int (1 or 0). Option A has type mismatch (string in int). Option C has wrong syntax. Option D has type mismatch.

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