csharpbeginner
C# Basics
Variables, loops and classes
6 questions
By EZ4Code Team
1. What is the standard output method in C#?
Console.WriteLine()
System.out.println()
print()
Response.Write()
Explanation: Console.WriteLine() outputs to the console with a newline; Console.Write() does not add a newline.
2. What is the keyword to declare an integer variable?
int
Integer
num
var int
Explanation: int is an alias for System.Int32, declaring a 32-bit integer; C# also has long/short/byte, etc.
3. What does the var keyword mean?
The compiler infers the variable type (still strongly typed)
A dynamic type that can change
Declares a mutable variable
Declares a global variable
Explanation: var lets the compiler infer the type from the initializer expression; it is still statically and strongly typed, and must be initialized at definition.
4. What is the keyword to define a class in C#?
class
Class
struct
type
Explanation: class defines a reference type; struct defines a value type; C# is case-sensitive and keywords are lowercase.
5. What is the syntax of a foreach loop?
foreach (var item in collection)
foreach (item in collection)
for (item in collection)
each (item of collection)
Explanation: foreach (var item in collection) iterates over an enumerable collection, taking one element at a time; collection must implement IEnumerable.
6. What is the syntax for string interpolation?
$"Hello {name}"
"Hello {name}"
"Hello " + name
string.Format("Hello {0}", name)
Explanation: The $ prefix enables string interpolation; {name} inserts the variable's value, equivalent to string.Format but more intuitive.