2 marks
1. What is the difference between == and .Equals() in C#?
Answer:
== checks reference equality for objects and value equality for
primitive types.
.Equals() checks for value equality and can be overridden for
custom behavior.
2. List different types of operators in C#.
Answer:
Arithmetic (+, -, *, /, %)
Assignment (=, +=, -=, etc.)
Comparison (==, !=, <, >, etc.)
Logical (&&, ||, !)
Bitwise (&, |, ^, <<, >>)
Ternary (condition ? true : false)
Null-coalescing (??)
3. Explain ternary operator with example.
Answer:
Syntax: condition ? value_if_true : value_if_false
Example:
int x = 10, y = 20;
int max = (x > y) ? x : y; // max = 20
4. What is the purpose of switch statement in C#?
Answer:
To execute different code blocks based on the value of a single expression.
5. What happens if you forget a break statement in switch-case?
Answer:
It results in fall-through, where subsequent cases also execute.
6. Can you use if statements inside a switch block?
Answer:
Yes, nested if statements can be used within a switch-case.
7. What does the % operator do?
Answer:
It returns the remainder of a division operation.
8. When do you use && instead of &?
Answer:
Use && when you want short-circuiting, so the second condition is not
evaluated if the first is false.
9. How do you write an if-else statement in C#?
Answer:
if (condition)
// code if true
else
// code if false
10. What is short-circuit evaluation?
Answer:
In expressions like a && b, if a is false, b is not evaluated because the
overall result will be false regardless.
Programming Questions with Answers
11. Program to check if a number is even or odd.
int num = 7;
if (num % 2 == 0)
Console.WriteLine("Even");
else
Console.WriteLine("Odd");
12. Check if a number is divisible by 3 and 5.
int num = 15;
if (num % 3 == 0 && num % 5 == 0)
Console.WriteLine("Divisible by both 3 and 5");
else
Console.WriteLine("Not divisible");
13. Use ternary operator to find the maximum of two numbers.
int a = 10, b = 20;
int max = (a > b) ? a : b;
Console.WriteLine("Max: " + max);
14. Grading system using if-else if.
int marks = 85;
if (marks >= 90)
Console.WriteLine("Grade A");
else if (marks >= 75)
Console.WriteLine("Grade B");
else if (marks >= 60)
Console.WriteLine("Grade C");
else
Console.WriteLine("Fail");
15. Switch-case to print the day of the week.
int day = 3;
switch(day)
case 1: Console.WriteLine("Sunday"); break;
case 2: Console.WriteLine("Monday"); break;
case 3: Console.WriteLine("Tuesday"); break;
default: Console.WriteLine("Invalid Day"); break;
16. Check if a number is positive, negative, or zero.
int num = -4;
if (num > 0)
Console.WriteLine("Positive");
else if (num < 0)
Console.WriteLine("Negative");
else
Console.WriteLine("Zero");
17. Find the largest among three numbers.
int a = 10, b = 15, c = 8;
if (a > b && a > c)
Console.WriteLine("A is largest");
else if (b > c)
Console.WriteLine("B is largest");
else
Console.WriteLine("C is largest");
18. Check leap year.
int year = 2024;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
Console.WriteLine("Leap Year");
else
Console.WriteLine("Not a Leap Year");
19. Simple calculator using switch.
int a = 10, b = 5;
char op = '+';
switch(op)
case '+': Console.WriteLine(a + b); break;
case '-': Console.WriteLine(a - b); break;
case '*': Console.WriteLine(a * b); break;
case '/': Console.WriteLine(a / b); break;
default: Console.WriteLine("Invalid operator"); break;
20. Check if a number is prime.
int num = 7, count = 0;
for(int i = 1; i <= num; i++)
if(num % i == 0) count++;
Console.WriteLine(count == 2 ? "Prime" : "Not Prime");
Output Prediction Questions with Answers
21.
int x = 5, y = 10;
Console.WriteLine(x > y ? "X" : "Y");
Output: Y
22.
int x = 5;
x *= 2 + 3;
Console.WriteLine(x);
Output: 25
23.
int a = 4;
if (a % 2 == 0)
Console.WriteLine("Even");
else
Console.WriteLine("Odd");
Output: Even
24.
int a = 10, b = 20, c = 30;
Console.WriteLine(a + b * c);
Output: 610
Explanation: Multiplication has higher precedence → 20 * 30 = 600, 600
+ 10 = 610
25.
int x = 3;
Console.WriteLine(x++);
Console.WriteLine(++x);
Output:
CopyEdit
26.
bool flag = false;
if (!flag)
Console.WriteLine("False branch");
else
Console.WriteLine("True branch");
Output: False branch
7 marks
1.You are developing a user onboarding application that requires age-
based user categorization. The classification is as follows: users under 13
are labeled as "Child", those between 13 and 19 as "Teenager", 20 to 59
as "Adult", and 60 or older as "Senior". How would you implement this
logic using conditional statements in C#?
Answer:
int age = Convert.ToInt32(Console.ReadLine());
if (age < 13)
Console.WriteLine("Child");
else if (age <= 19)
Console.WriteLine("Teenager");
else if (age <= 59)
Console.WriteLine("Adult");
else
Console.WriteLine("Senior");
2.You're building a basic calculator that supports addition, subtraction,
multiplication, and division. The program should take two numbers and an
operator from the user and perform the calculation accordingly. How can
you implement this using a switch statement in C#?
Answer:
double a = Convert.ToDouble(Console.ReadLine());
double b = Convert.ToDouble(Console.ReadLine());
char op = Convert.ToChar(Console.ReadLine());
switch (op)
case '+': Console.WriteLine(a + b); break;
case '-': Console.WriteLine(a - b); break;
case '*': Console.WriteLine(a * b); break;
case '/':
if (b != 0)
Console.WriteLine(a / b);
else
Console.WriteLine("Error: Division by zero.");
break;
default:
Console.WriteLine("Invalid operator");
break;
3. Design a simple login system where a user is prompted to enter a
username and password. The program should verify if the entered
credentials match predefined values. How can you ensure secure
conditional checking using logical operators?
Answer:
string correctUser = "admin";
string correctPass = "1234";
Console.Write("Enter username: ");
string user = Console.ReadLine();
Console.Write("Enter password: ");
string pass = Console.ReadLine();
if (user == correctUser && pass == correctPass)
Console.WriteLine("Login successful");
else
Console.WriteLine("Invalid credentials");
4. You’re building a number checker that verifies if a number is divisible
by both 3 and 5. The result should display appropriate messages based on
whether the number is divisible by 3, 5, both, or neither. How would you
use conditional statements to implement this?
Answer:
int num = Convert.ToInt32(Console.ReadLine());
if (num % 3 == 0 && num % 5 == 0)
Console.WriteLine("Divisible by both 3 and 5");
else if (num % 3 == 0)
Console.WriteLine("Divisible by 3 only");
else if (num % 5 == 0)
Console.WriteLine("Divisible by 5 only");
else
Console.WriteLine("Not divisible by 3 or 5");
The % operator checks for remainder. Combining it with && and else if
handles each case efficiently. You can further optimize by using a switch
with boolean flags for flexibility.
5. You are developing a grading system where marks determine the
grade: A for 90+, B for 75-89, C for 60-74, and F for less than 60. How can
you implement this in C# using conditional logic?
Answer:
int marks = Convert.ToInt32(Console.ReadLine());
if (marks >= 90)
Console.WriteLine("Grade A");
else if (marks >= 75)
Console.WriteLine("Grade B");
else if (marks >= 60)
Console.WriteLine("Grade C");
else
Console.WriteLine("Grade F");
6. You're developing a utility tool that determines whether a given integer
is even or odd. The tool must give appropriate output based on the
number entered. How would you use C# conditional logic to build this?
Answer:
int number = Convert.ToInt32(Console.ReadLine());
if (number % 2 == 0)
Console.WriteLine("The number is Even.");
else
Console.WriteLine("The number is Odd.");
7. Design a C# program to classify temperatures into categories:
"Freezing" if below 0°C, "Cold" for 0–10°C, "Warm" for 11–25°C, and "Hot"
above 25°C. How would you implement this using conditional statements?
Answer:
int temp = Convert.ToInt32(Console.ReadLine());
if (temp < 0)
Console.WriteLine("Freezing");
else if (temp <= 10)
Console.WriteLine("Cold");
else if (temp <= 25)
Console.WriteLine("Warm");
else
Console.WriteLine("Hot");
8. Write a C# program that checks whether a given year is a leap year or
not. The rules are: it must be divisible by 4, but years divisible by 100 are
not leap years unless also divisible by 400. How would you use nested
conditions for this?
Answer:
int year = Convert.ToInt32(Console.ReadLine());
if (year % 4 == 0)
if (year % 100 == 0)
{
if (year % 400 == 0)
Console.WriteLine("Leap Year");
else
Console.WriteLine("Not a Leap Year");
else
Console.WriteLine("Leap Year");
else
Console.WriteLine("Not a Leap Year");
9. You are tasked with writing a program to check if a user is eligible to
vote. The minimum age for voting is 18. Based on user input, display
whether the person is eligible. How would you implement this in C#?
Answer:
int age = Convert.ToInt32(Console.ReadLine());
if (age >= 18)
Console.WriteLine("Eligible to vote.");
else
Console.WriteLine("Not eligible to vote.");
10. Create a program in C# to check whether a given number is positive,
negative, or zero. The user will input a number, and the program should
display the appropriate message. How can this be implemented?
Answer:
int num = Convert.ToInt32(Console.ReadLine());
if (num > 0)
Console.WriteLine("Positive number");
else if (num < 0)
Console.WriteLine("Negative number");
else
Console.WriteLine("Zero");