Assess your understanding of C# data types, variables, and operators with this beginner-friendly quiz. Sharpen your knowledge of syntax rules, declaration methods, and operational logic essential to C# programming.
Which of the following is a valid variable name in C#?
Explanation: _myVariable is correct because variable names in C# can start with an underscore. int@Var is incorrect because the @ symbol is not allowed in this position for naming. 3count is invalid as variable names cannot start with a digit. class is a reserved keyword and cannot be used as a variable name.
What is the default value assigned to an uninitialized int variable declared as a field in a C# class?
Explanation: The default value for an int field in a class is 0, ensuring the variable starts with a defined numeric value. null is only applicable for reference types or nullable value types, not for int. undefined does not apply to C#, as variables are not 'undefined'. The number 1 is simply distractor and is not the default for int fields.
Which keyword allows a variable's data type to be inferred by the compiler in C#?
Explanation: var is correctly used in C# to instruct the compiler to infer the type based on the assigned value. auto and inf are invalid in C#, though auto is used in some other languages. dynamic is a distinct keyword that allows runtime type assignment, not compile-time inference.
In the statement 'float x = 3.14F;', what does the 'F' indicate in C#?
Explanation: The 'F' suffix specifies that the literal value should be treated as a float. By default, decimal numbers like 3.14 without any suffix are considered double. 'F' does not stand for function in this context, and it is a recognized suffix, making other options incorrect.
Which data type is most suitable for storing a single Unicode character in C#?
Explanation: char is the correct data type for storing a single Unicode character. string is used for sequences of characters, not just one. byte is for 8-bit unsigned integers, and bool stores boolean values (true or false).
What will be the value of the expression '(true u0026u0026 false)' in C#?
Explanation: The logical AND operator 'u0026u0026' returns true only if both operands are true; here, one is false, so the result is false. true is incorrect because both conditions aren't true. null and undefined are not valid boolean values for logic expressions in C#.
Which operator is used in C# to concatenate two strings?
Explanation: The plus (+) operator joins two strings together in C#. The ampersand (u0026) is not used for string concatenation in C#, though it is in some other languages. The period (.) is used for member access, and the asterisk (*) is for multiplication.
Given 'int a = 5;', which code correctly converts 'a' to a string in C#?
Explanation: a.ToString() is the standard way in C# to convert an int to its string representation. (string)a would only work for reference types or explicit casts, not primitive types like int. string(a) and to_string(a) are not valid C# syntax, although the latter is used in other languages.
What happens if a variable of type byte (range 0–255) is assigned the value 300 in C# without any special handling?
Explanation: Assigning a value outside the valid range causes the value to wrap around due to overflow in unchecked contexts. C# does not throw a compile-time error in this assignment. It raises an exception only in checked contexts. The value does not convert to null, as byte is a non-nullable value type.
Which operator checks for equality between two values in C#?
Explanation: The '==' operator checks for equality in C#. The single equals (=) is used for assignment, not comparison. '!=' checks for inequality, while '=u003E' is used in lambda expressions, not for equality comparison.