Variables & Data Types — The Architect's Exam
20 XPMedium
Ctrl+Enter RunCtrl+S Save
🎓 Blueprint Certification Exam
You've learned to label boxes, crunch numbers, engrave strings, and reshape materials. Now the city council demands proof that you truly understand C# data types before granting your Architect's license. Answer all 15 questions to earn your certification!
These questions cover real .NET interview topics — value vs reference types, stack vs heap, nullable types, precision pitfalls, and more. Take your time and think carefully.
📋 Instructions
🧠 Quiz Time
0 / 15 answered
1
In C#, which of the following is a value type?
A. string
B. object
C. int
D. dynamic
int is a value type (System.Int32) stored on the stack. string and object are reference types stored on the heap. dynamic is resolved at runtime but behaves as a reference type.
2
Where are value types typically stored in memory?
A. Heap only
B. Stack (when local variables)
C. Static memory segment only
D. CPU registers only
Local value-type variables are stored on the stack. When they are fields of a class (reference type), they live on the heap as part of that object. But as standalone local variables, the stack is their home.
3
What is the default value of an uninitialized int field in a C# class?
A. null
B. -1
C. 0
D. Compiler error
Numeric value-type fields default to 0. Only local variables must be explicitly initialized before use (compiler error otherwise). Class fields are auto-initialized to their default values.
4
What does int? mean in C#?
A. An integer that cannot be null
B. A nullable integer — it can hold an int value or null
C. A pointer to an integer
D. A read-only integer
int? is shorthand for Nullable<int>. It wraps the value type so it can also represent null, which is useful for database values and optional parameters.
5
Why should you use decimal instead of double for financial calculations?
A. decimal is faster than double
B. decimal uses less memory than double
C. decimal has higher precision (~28 digits) and avoids binary floating-point rounding errors
D. double cannot store negative numbers
decimal is a 128-bit type with ~28-29 significant digits and base-10 representation, making it ideal for money. double uses base-2 (IEEE 754) which can introduce tiny rounding errors like 0.1 + 0.2 ≠ 0.3.
6
What happens when you modify a string in C#?
A. The original string is modified in place
B. A new string object is created because strings are immutable
C. The string is converted to a char array, modified, then converted back
D. The CLR uses copy-on-write, so modification is deferred
Strings in C# are immutable. Every operation that appears to modify a string (Replace, ToUpper, concatenation, etc.) actually creates a brand-new string object. The original is unchanged and eligible for garbage collection.
7
What type does the compiler infer for: var x = 42;
A. dynamic
B. object
C. long
D. int
The var keyword asks the compiler to infer the type from the right-hand side. The literal 42 (without suffix) is an int by default. var does NOT make the variable dynamic — it is still statically typed as int at compile time.
8
Which method safely converts a string to an int without throwing an exception?
A. int.Parse()
B. Convert.ToInt32()
C. int.TryParse()
D. (int)stringValue
int.TryParse() returns a bool indicating success and outputs the result via an out parameter. int.Parse() and Convert.ToInt32() throw exceptions on invalid input. Direct casting (int) does not work on strings at all.
9
What is boxing in C#?
A. Converting a reference type to a value type
B. Wrapping a value type inside an object reference on the heap
C. Casting a string to an int
D. Allocating a struct on the stack
Boxing is the process of wrapping a value type (e.g., int) in a System.Object reference on the managed heap. Unboxing is the reverse — extracting the value type from the object. Boxing involves a heap allocation and should be avoided in performance-critical code.
10
What is the difference between const and readonly in C#?
A. They are identical — both are compile-time constants
B. const is evaluated at compile-time; readonly is evaluated at runtime and can be set in a constructor
C. readonly can only be used with strings; const works with all types
D. const values can be changed in the constructor; readonly cannot
const must be assigned a compile-time constant value and is baked into the IL. readonly fields can be assigned at declaration or in the constructor, evaluated at runtime. This makes readonly suitable for values that depend on runtime computation.
11
What is the output of: Console.WriteLine($"2 + 2 = {2 + 2}");
A. 2 + 2 = {2 + 2}
B. 2 + 2 = 4
C. Compiler error
D. 2 + 2 = 22
The $ prefix enables string interpolation. Expressions inside { } are evaluated at runtime. 2 + 2 evaluates to 4, so the result is "2 + 2 = 4". Without the $ prefix, the braces would be treated as literal characters.
12
What is the range of a char in C#?
A. 0 to 127 (ASCII only)
B. 0 to 255 (extended ASCII)
C. 0 to 65535 (Unicode UTF-16)
D. -128 to 127 (signed byte)
A char in C# is a 16-bit Unicode (UTF-16) character, ranging from U+0000 to U+FFFF (0 to 65,535). This covers the Basic Multilingual Plane. Characters outside this range (like some emoji) require surrogate pairs.
13
What happens with: int x = int.MaxValue; x = x + 1; in an unchecked context?
A. x becomes int.MaxValue + 1 (a long value)
B. An OverflowException is thrown
C. x wraps around to int.MinValue (-2147483648)
D. x becomes 0
In the default unchecked context, integer overflow wraps around silently. int.MaxValue (2147483647) + 1 overflows to int.MinValue (-2147483648). Use the checked keyword to get an OverflowException instead.
14
What does sizeof(int) return in C#?
A. 2
B. 4
C. 8
D. It depends on the platform (32-bit vs 64-bit)
int (System.Int32) is always 4 bytes (32 bits) in C#, regardless of whether the platform is 32-bit or 64-bit. Unlike C/C++, the sizes of primitive types in C# are fixed by the specification.
15
Which statement about string concatenation vs interpolation is TRUE?
A. Concatenation with + is always faster than interpolation
B. String interpolation ($"") is compiled into string.Format() or string.Concat() calls and is generally preferred for readability
C. Interpolation creates more garbage collection pressure than concatenation
D. Concatenation supports expressions inside {} but interpolation does not
String interpolation ($"") is syntactic sugar. The compiler transforms it into efficient string.Concat() or string.Format() calls. It is preferred for readability and maintainability. Modern C# (.NET 6+) can even optimize interpolation into Span-based writes for zero-allocation scenarios.