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 141–150 of 154
Topics in C# Programming
Q.141 Medium Basics & Syntax
Which of the following statements about abstract classes is correct?
A Cannot contain abstract methods
B Can be instantiated directly
C Can contain non-abstract methods and properties
D All abstract class members must be abstract
Correct Answer:  C. Can contain non-abstract methods and properties
EXPLANATION

Abstract classes can contain both abstract and non-abstract (concrete) methods and properties. They cannot be instantiated directly.

Test
Q.142 Medium Basics & Syntax
What is the output of: int x = 5; Console.WriteLine(x++);
A 5
B 6
C Compilation error
D Cannot determine
Correct Answer:  A. 5
EXPLANATION

x++ is post-increment, so it returns the current value (5) before incrementing. The next Console.WriteLine would print 6.

Test
Q.143 Medium Basics & Syntax
What will happen if you try to access an array element beyond its bounds?
A Returns default value
B Throws IndexOutOfRangeException
C Returns null
D Compilation error
Correct Answer:  B. Throws IndexOutOfRangeException
EXPLANATION

Accessing array elements beyond bounds throws IndexOutOfRangeException at runtime.

Test
Q.144 Medium Basics & Syntax
What is the difference between 'var' and explicit type declaration in C#?
A var creates a dynamic variable
B var is implicitly typed but strongly typed at compile time
C var has no type checking
D var cannot be used in loops
Correct Answer:  B. var is implicitly typed but strongly typed at compile time
EXPLANATION

var is implicitly typed, meaning the compiler infers the type at compile time, but it's still strongly typed. It's not dynamic.

Test
Q.145 Medium Basics & Syntax
Which keyword is used to create an immutable class member in C#?
A static
B readonly
C const
D sealed
Correct Answer:  B. readonly
EXPLANATION

readonly keyword prevents reassignment of a field after initialization. const is for compile-time constants, static for class-level, sealed for inheritance prevention.

Test
Q.146 Medium Basics & Syntax
What will happen when you try to access an array index that is out of bounds in C#?
A Returns null
B Returns 0
C Throws IndexOutOfRangeException
D Returns undefined value
Correct Answer:  C. Throws IndexOutOfRangeException
EXPLANATION

Accessing an array element beyond its bounds throws an IndexOutOfRangeException at runtime. This is a safety feature to prevent accessing invalid memory.

Test
Q.147 Medium Basics & Syntax
Which keyword is used to prevent a class from being inherited in C#?
A final
B sealed
C abstract
D static
Correct Answer:  B. sealed
EXPLANATION

'sealed' keyword prevents a class from being inherited in C#. 'final' is Java syntax. 'abstract' requires a class to be inherited. 'static' makes members class-level, not related to inheritance.

Test
Q.148 Medium Basics & Syntax
What is the output of the following code?
bool result = (5 > 3) && (2 < 1);
Console.WriteLine(result);
A true
B false
C Error
D null
Correct Answer:  B. false
EXPLANATION

(5 > 3) is true, but (2 < 1) is false. The '&&' (AND) operator returns true only if both conditions are true. Since one is false, the result is false.

Test
Q.149 Medium Basics & Syntax
Which of the following correctly declares a nullable integer in C#?
A int? x = null;
B int x = null;
C nullable int x = null;
D int* x = null;
Correct Answer:  A. int? x = null;
EXPLANATION

In C#, 'int?' is used to declare a nullable integer. A regular 'int' cannot be assigned null. int* is a pointer (unsafe code), not a nullable type.

Test
Q.150 Medium Basics & Syntax
Which of the following will correctly convert a string to an integer in C#?
A int x = (int)"123";
B int x = int.Parse("123");
C int x = Convert.ToInt32("abc");
D int x = "123" as int;
Correct Answer:  B. int x = int.Parse("123");
EXPLANATION

int.Parse() converts a string to an integer. Direct casting doesn't work for strings. Convert.ToInt32() would throw an exception if the string is not a valid number. The 'as' operator doesn't work for strings to primitives.

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