What will be the output of the following code snippet?
int x = 5;
int y = ++x;
Console.WriteLine(x + " " + y);
Answer: B
++x is pre-increment. It increments x first (x becomes 6), then assigns the value to y. So both x and y are 6.
Q.2Medium
Which of the following data types in C# is a value type?
Answer: B
int is a value type (primitive type). string, object, and arrays are reference types in C#.
Q.3Medium
What is the correct way to declare a constant variable in C#?
Answer: A
The 'const' keyword is used to declare constants in C#. 'final' is Java syntax. While 'readonly' can be used, 'const' is the proper keyword for compile-time constants.
Q.4Medium
What is the difference between '==' and 'Equals()' when comparing strings in C#?
Answer: A
For strings, '==' compares references (memory addresses) while Equals() compares the actual string values. However, C# has optimized string comparison, so both often behave similarly for string literals.
Q.5Medium
Which of the following will correctly convert a string to an integer in C#?
Answer: B
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.
Advertisement
Q.6Medium
Which of the following correctly declares a nullable integer in C#?
Answer: A
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.
Q.7Medium
What is the output of the following code?
bool result = (5 > 3) && (2 < 1);
Console.WriteLine(result);
Answer: B
(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.
Q.8Medium
Which keyword is used to prevent a class from being inherited in C#?
Answer: B
'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.
Q.9Medium
What will happen when you try to access an array index that is out of bounds in C#?
Answer: C
Accessing an array element beyond its bounds throws an IndexOutOfRangeException at runtime. This is a safety feature to prevent accessing invalid memory.
Q.10Medium
Which keyword is used to create an immutable class member in C#?
Answer: B
readonly keyword prevents reassignment of a field after initialization. const is for compile-time constants, static for class-level, sealed for inheritance prevention.
Q.11Medium
What is the difference between 'var' and explicit type declaration in C#?
Answer: B
var is implicitly typed, meaning the compiler infers the type at compile time, but it's still strongly typed. It's not dynamic.
Q.12Medium
What will happen if you try to access an array element beyond its bounds?
Answer: B
Accessing array elements beyond bounds throws IndexOutOfRangeException at runtime.
Q.13Medium
What is the output of: int x = 5; Console.WriteLine(x++);
Answer: A
x++ is post-increment, so it returns the current value (5) before incrementing. The next Console.WriteLine would print 6.
Q.14Medium
Which of the following statements about abstract classes is correct?
Answer: C
Abstract classes can contain both abstract and non-abstract (concrete) methods and properties. They cannot be instantiated directly.
Q.15Medium
Which access modifier allows access from derived classes only?
Answer: C
protected members are accessible from the same class and derived classes, but not from outside the class hierarchy.
Q.16Medium
What is the output of: Console.WriteLine(5 == 5.0);
Answer: A
C# allows comparison between int and double. 5 and 5.0 are numerically equal, so == returns True.
Q.17Medium
Which of the following correctly declares a jagged array?
Answer: B
Jagged arrays use [][] syntax. [,] is for multidimensional arrays. int[][] allows arrays of varying lengths.
Q.18Medium
What is the purpose of the 'using' statement in C#?
Answer: D
using serves two purposes: importing namespaces and automatically disposing resources through IDisposable pattern.
Q.19Medium
What will be the output of: int x = 10; int y = x; y = 20; Console.WriteLine(x);
Answer: A
int is a value type. When y = x, a copy of x's value is assigned to y. Changing y doesn't affect x.
Q.20Medium
What will be the output of: sbyte x = -128; sbyte y = x - 1; Console.WriteLine(y);
Answer: B
sbyte range is -128 to 127. When overflow occurs without checked context, it wraps around to 127.