Which of the following is the correct syntax to declare a variable in C#?
Answer: A
C# uses the syntax 'type variableName = value;' to declare and initialize variables. The equals sign (=) is the assignment operator.
Q.2Easy
Which keyword is used to create a class in C#?
Answer: A
C# is case-sensitive. The lowercase keyword 'class' is used to define a class. 'Class' and 'CLASS' are not valid keywords.
Q.3Easy
Which namespace is required to use Console.WriteLine() in C#?
Answer: B
Console class is part of the System namespace. You need to include 'using System;' at the beginning of your code to use Console.WriteLine().
Q.4Easy
Which of the following is NOT a valid C# variable name?
Answer: B
Variable names in C# can contain letters, digits, and underscores. They cannot contain special characters like '$'. The variable name must start with a letter or underscore.
Q.5Easy
What will be the output of the following code?
string s = "Hello";
s = s + " World";
Console.WriteLine(s);
Answer: B
String concatenation using '+' operator joins the two strings. s becomes 'Hello World' after the concatenation.
Advertisement
Q.6Easy
Which of the following is a value type in C#?
Answer: B
Enum is a value type in C#. String, Interface, and Delegate are reference types.
Q.7Easy
What is the default access modifier for a class member in C#?
Answer: B
The default access modifier for class members in C# is private, meaning they are accessible only within the same class.
Q.8Easy
Which of the following correctly declares a variable that can hold null?
Answer: B
Only nullable types (using ?) can hold null values for value types. int? is a nullable integer that can hold null.
Q.9Easy
What will be the output of: Console.WriteLine(310);
Answer: B
Integer division in C# truncates the decimal part. 310 = 3 (not 3.33) because both operands are integers.
Q.10Easy
Which of the following is a reference type in C#?
Answer: C
Classes are reference types. byte, decimal are value types, and struct is also a value type.
Q.11Easy
What is the correct syntax for a multi-line comment in C#?
Answer: C
/* */ is used for multi-line comments in C#. // is for single-line comments.
Q.12Easy
Which keyword is used to create a constant variable in C# that cannot be modified after initialization?
Answer: B
The 'const' keyword creates a constant that must be initialized at declaration and cannot be changed. 'readonly' allows initialization in constructor.
Q.13Easy
What will be the output of: int x = 10; int y = 20; int z = x > y ? x : y; Console.WriteLine(z);
Answer: B
The ternary operator checks if x > y. Since 10 is not greater than 20, it returns y which is 20.
Q.14Easy
What is the output of: decimal d = 10.5m; int x = (int)d; Console.WriteLine(x);
Answer: B
Explicit casting from decimal to int truncates the decimal part, resulting in 10.
Q.15Easy
In C#, which keyword is used to create a constant variable that cannot be modified after initialization?
Answer: A
The 'const' keyword creates a compile-time constant in C#. 'readonly' creates a runtime constant, 'static' defines class-level members, and 'immutable' is not a C# keyword.
Q.16Easy
What is the default access modifier for a class member in C# if not specified?
Answer: B
The default access modifier for class members is 'private'. For top-level classes, it's 'internal' by default.
Q.17Easy
Which namespace must be included to use LINQ in C#?
Answer: B
LINQ (Language Integrated Query) is available in the System.Linq namespace introduced in .NET 3.5.
Q.18Easy
In C#, what is the correct syntax to declare a nullable integer?
Answer: A
The '?' suffix is used to declare nullable value types in C#. int? means an integer that can also hold null values.
Q.19Easy
What will be printed when the following code executes?
string str = "Hello";
str = str + " World";
Console.WriteLine(str.Length);
Answer: B
"Hello" has 5 characters. After concatenation with " World" (6 characters including space), the total length is 11.
Q.20Easy
In C#, which keyword is used to declare a variable that cannot be changed after initialization?
Answer: A
The 'const' keyword declares a constant whose value cannot be modified. Both 'const' and 'readonly' prevent changes, but 'const' is compile-time constant while 'readonly' is runtime constant.