NEW
Font size
S
M
L
XL
WorksheetsC# for YP
Total questions: 30
Worksheet time: 15mins
Name
Class
Date
1.
What is the correct way to declare a nullable integer in C#?
a)
int? number = null;
b)
int number = null;
c)
nullable int number = null;
d)
Int32? number = void;
e)
None of these
2.
Which of the following is NOT a valid access modifier in C#?
a)
public
b)
private
c)
internal
d)
package
e)
None of these
3.
What will be the output of the following code?
string str = null;Console.WriteLine(str?.Length ?? 0);
a)
NullReferenceException
b)
0
c)
null
d)
Compilation error
e)
None of these
4.
Which keyword is used to prevent a class from being inherited?
a)
static
b)
sealed
c)
final
d)
abstract
e)
None of these
5.
What is the difference between const and readonly in C#?
a)
No difference, they are synonyms
b)
const is compile-time constant, readonly is runtime constant
c)
readonly is compile-time constant, const is runtime constant
d)
const can be modified, readonly cannot
e)
None of these
6.
Which collection type is best for frequently adding and removing items from both ends?
a)
List<T>
b)
LinkedList<T>
c)
Queue<T>
d)
Stack<T>
e)
None of these
7.
What does the virtual keyword indicate in C#?
a)
The method cannot be overridden
b)
The method can be overridden in derived classes
c)
The method is abstract
d)
The method is static
e)
None of these
8.
Which of the following is the correct syntax for exception handling in C#?
a)
try-catch-finally
b)
try-except-finally
c)
begin-rescue-ensure
d)
attempt-handle-cleanup
e)
None of these
9.
What is the output of the following code?
int x = 5;int y = ++x + x++;Console.WriteLine(y);
a)
11
b)
12
c)
13
d)
10
e)
None of these
10.
Which interface should a class implement to be used in a foreach loop?
a)
IEnumerable
b)
ICollection
c)
IList
d)
IComparable
e)
None of these
11.
What is the correct way to create a property with only a getter in C#?
a)
public int Value { get; }
b)
public int Value { get => _value; }
c)
public int Value => _value;
d)
All of the above
e)
None of these
12.
Which of the following statements about string in C# is correct?
a)
Strings are mutable
b)
Strings are reference types
c)
Strings are value types
d)
Strings can be null by default
e)
None of these
13.
What does the params keyword allow you to do?
a)
Pass a variable number of arguments to a method
b)
Create optional parameters
c)
Pass parameters by reference
d)
Create constant parameters
e)
None of these
14.
Which operator is used for null-coalescing in C#?
a)
?.
b)
??
c)
?:
d)
??=
e)
None of these
15.
What is the default value of a bool variable in C#?
a)
null
b)
true
c)
false
d)
undefined
e)
None of these
16.
Which of the following is NOT a valid way to initialize an array in C#?
a)
int[] arr = new int[5];
b)
int[] arr = {1, 2, 3, 4, 5};
c)
int[] arr = new int[]{1, 2, 3, 4, 5};
d)
int[] arr = new int[5]{1, 2, 3, 4, 5, 6};
e)
None of these
17.
What is the purpose of the using statement in C#?
a)
To import namespaces only
b)
To ensure proper disposal of resources
c)
To create aliases for types
d)
Both A and B
e)
None of these
18.
Which method should be overridden to provide custom string representation of an object?
a)
ToString()
b)
GetString()
c)
AsString()
d)
ToText()
e)
None of these
19.
What is the correct syntax for method overloading in C#?
a)
Methods must have different names
b)
Methods must have same name but different parameter lists
c)
Methods must have different return types
d)
Methods must use the overload keyword
e)
None of these
20.
Which keyword is used to call the base class constructor from a derived class constructor?
a)
super
b)
parent
c)
base
d)
this
e)
None of these
21.
What will happen if you try to access an array element beyond its bounds?
a)
It returns null
b)
It returns the default value
c)
It throws IndexOutOfRangeException
d)
It creates a new element
e)
None of these
22.
Which of the following is true about static members?
a)
They belong to the instance of the class
b)
They belong to the class itself, not to any instance
c)
They can be accessed only through objects
d)
They are automatically initialized to null
e)
None of these
23.
What is the correct way to check if a string is null or empty?
a)
str == null || str == ""
b)
string.IsNullOrEmpty(str)
c)
str?.Length == 0
d)
Both A and B
e)
None of these
24.
Which loop is guaranteed to execute at least once?
a)
for loop
b)
while loop
c)
do-while loop
d)
foreach loop
e)
None of these
25.
What does the ref keyword do when used with method parameters?
a)
Passes the parameter by value
b)
Passes the parameter by reference
c)
Creates a copy of the parameter
d)
Makes the parameter optional
e)
None of these
26.
Which of the following is the correct way to implement an indexer in C#?
a)
public int this[int index] { get; set; }
b)
public int indexer[int index] { get; set; }
c)
public int GetIndex(int index) { get; set; }
d)
public indexer int[int index] { get; set; }
e)
None of these
27.
What is the purpose of the abstract keyword?
a)
To create a class that cannot be instantiated
b)
To create methods that must be implemented by derived classes
c)
To define incomplete classes or methods
d)
All of the above
e)
None of these
28.
Which collection automatically maintains elements in sorted order?
a)
List<T>
b)
Dictionary<K,V>
c)
SortedSet<T>
d)
HashSet<T>
e)
None of these
29.
What is the difference between == and Equals() method in C#?
a)
No difference
b)
"==" compares references, Equals() compares values
c)
"==" can be overloaded, Equals() cannot
d)
It depends on the implementation
e)
None of these
30.
Which keyword is used to create an anonymous method in C#?
a)
anonymous
b)
lambda
c)
delegate
d)
function
e)
None of these
Reset
