Stage 2 - Variables, Operators, and Decisions
Q6. What is a variable in C#?
Quick interview answer:
A variable is a named place to store a value in memory.
For example, marks can store a student's marks and studentName can store the student's name.
string studentName = "Ravi";
int marks = 85;
Study in detail: Variables and data types - This article expands the topic with complete explanation, examples, and practice code.
Q7. Why does every variable need a data type?
Quick interview answer:
A data type tells C# what kind of value a variable can store.
For example, int stores whole numbers and string stores text. This helps C# catch mistakes before the program runs.
Study in detail: Variables and data types - This article expands the topic with complete explanation, examples, and practice code.
Q8. What is the difference between int, double, and decimal?
Quick interview answer:
int stores whole numbers like age or marks. double stores decimal numbers like percentage.
decimal is preferred for money because it gives better accuracy for financial values.
Study in detail: C# data types - This article expands the topic with complete explanation, examples, and practice code.
Q9. What is the difference between = and ==?
Quick interview answer:
= is used to assign a value to a variable. == is used to compare two values.
This is a common beginner mistake because both symbols look similar but do different jobs.
marks = 90; // assignment
marks == 90; // comparison
Study in detail: Operators - This article expands the topic with complete explanation, examples, and practice code.
Q10. What is control flow?
Quick interview answer:
Control flow means deciding which block of code should run based on a condition.
In C#, we commonly use if, else, switch, and loops to control the program flow.
if (marks >= 35)
{
Console.WriteLine("Pass");
}
Study in detail: Control flow - This article expands the topic with complete explanation, examples, and practice code.
Q11. When should we use switch instead of if/else?
Quick interview answer:
Use switch when one value has many fixed choices, like role, status, or grade.
Use if/else when conditions involve ranges, multiple comparisons, or complex business rules.
Study in detail: Control flow - This article expands the topic with complete explanation, examples, and practice code.