Home Subjects C# Programming Basics & Syntax

C# Programming
Basics & Syntax

C# and .NET for campus placement

56 Q 4 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 1–10 of 56
Topics in C# Programming
Q.1 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
Q.2 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.3 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
Q.4 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
Q.5 Medium Basics & Syntax
Which statement about method overloading in C# is correct?
A Methods can have the same name if they differ in return type only
B Methods can have the same name if they differ in parameter types or count
C Method overloading is not supported in C#
D Methods must have different access modifiers to be overloaded
Correct Answer:  B. Methods can have the same name if they differ in parameter types or count
EXPLANATION

Method overloading requires different parameter types, count, or order. Return type alone is not enough. C# fully supports overloading.

Test
Q.6 Medium Basics & Syntax
What is the output of: char ch = 'A'; Console.WriteLine((int)ch);
A 65
B A
C 1
D 97
Correct Answer:  A. 65
EXPLANATION

char 'A' has ASCII value 65. Casting to int gives the numeric ASCII value.

Test
Q.7 Medium Basics & Syntax
Which of the following is true about the 'var' keyword in C#?
A var can hold any type of data without type checking
B var is explicitly typed at compile-time based on initialization
C var is dynamically typed like in Python
D var can be used for global variables only
Correct Answer:  B. var is explicitly typed at compile-time based on initialization
EXPLANATION

var is implicitly typed. The type is inferred and set at compile-time based on the right-hand side assignment, providing type safety.

Test
Q.8 Medium Basics & Syntax
Which keyword is used to create a constant variable in C# that cannot be changed?
A const
B readonly
C static
D fixed
Correct Answer:  A. const
EXPLANATION

const creates a compile-time constant. readonly allows assignment only in constructor or at declaration. static makes it class-level, and fixed is for pointers.

Test
Q.9 Medium Basics & Syntax
What is the output of: int x = 10; int y = x++; Console.WriteLine(x + " " + y);
A 10 10
B 11 10
C 11 11
D 10 11
Correct Answer:  B. 11 10
EXPLANATION

Post-increment (x++) returns the current value first, then increments. So y gets 10, then x becomes 11.

Test
Q.10 Medium Basics & Syntax
What is the main purpose of the 'finally' block in C#?
A To catch exceptions
B To execute code whether an exception occurs or not
C To throw custom exceptions
D To define exception handlers
Correct Answer:  B. To execute code whether an exception occurs or not
EXPLANATION

The 'finally' block executes regardless of whether an exception is thrown or caught. It is used for cleanup operations like closing file streams or database connections.

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