-
- After each test case has been completed, mark them complete : - ✅ Task completed
Instructions: Declare and initialize an int variable and a double variable, then print their sum.
// Declare a variable of type int and initialize it int myInteger = __;
// Declare a variable of type double and initialize it double myDouble = __;
// Print the sum of the two variables Console.WriteLine(__ + __);
Instructions: Declare and initialize a string and a char variable, then print their concatenated result.
// Declare and initialize a string variable string myString = __;
// Declare a char variable and initialize it char myChar = __;
// Print the concatenated result Console.WriteLine(__ + __);
Instructions: Convert an int to a string and parse a string to a double, then print the converted values.
// Convert an integer to a string int number = 123; string numberString = __;
// Parse a string to a double string piString = "3.14"; double pi = __;
// Print the converted values Console.WriteLine(); Console.WriteLine();
Instructions: Write an if-else statement to check if a number is positive or non-positive, and print the appropriate message.
int number = ; if () { Console.WriteLine("Number is positive."); } else { Console.WriteLine("Number is non-positive."); }
Instructions: Write a switch-case statement to print messages based on the value of a char variable representing a grade.
char grade = ; switch () { case 'A': Console.WriteLine("Excellent!"); break; case 'B': Console.WriteLine("Good!"); break; default: Console.WriteLine("Grade not recognized."); break; }
Instructions: Write a for loop to print a count from 0 to a specified number.
for (int i = 0; __; i++) { Console.WriteLine("Count: " + __); }
Instructions: Declare a method that returns the sum of two integers.
// Declare a method that returns the sum of two integers public static int Add(__, __) { return __; }
Instructions: Declare and invoke a method that prints a message.
// Declare and invoke a method that prints a message public static void PrintMessage() { Console.WriteLine(); }
// Invoke the method here PrintMessage(__);
Instructions: Declare overloaded methods to print a single value or two concatenated values, then invoke them.
// Declare overloaded methods public static void Display() { Console.WriteLine(); }
public static void Display(__, ) { Console.WriteLine( + " " + __); }
// Invoke the overloaded methods here Display(); Display(, __);
Instructions: Define a class with a field and a method, create an object of the class, and call the method.
// Define a class with a field and a method class Person { public string name;
public void Introduce()
{
Console.WriteLine("Hi, my name is " + __);
}
}
// Create an object of the class and call the method Person person = new Person(); person.__ = "Alice"; person.__();
Instructions: Define a base class and a derived class, create an object of the derived class, and call methods from both classes.
// Define a base class class Animal { public void Eat() { Console.WriteLine("Eating..."); } }
// Define a derived class class Dog : Animal { public void Bark() { Console.WriteLine("Barking..."); } }
// Create an object of the derived class and call methods Dog dog = new Dog(); dog.(); dog.();
Instructions: Define a base class with a virtual method, override the method in a derived class, and demonstrate polymorphism.
// Define a base class with a virtual method class Shape { public virtual void Draw() { Console.WriteLine("Drawing a shape..."); } }
// Define a derived class that overrides the method class Circle : Shape { public override void Draw() { Console.WriteLine("Drawing a circle..."); } }
// Demonstrate polymorphism Shape shape = new Circle(); shape.__();
Instructions: Declare and initialize an array of integers, then print all its elements.
// Declare and initialize an array of integers int[] numbers = { __, __, __, __ };
// Print all elements of the array for (int i = 0; i < ; i++) { Console.WriteLine(); }
Instructions: Declare and initialize a list of strings, add a new element to the list, and print all its elements.
// Declare and initialize a list of strings List fruits = new List() { __, __, __ };
// Add a new element to the list fruits.__("Mango");
// Print all elements of the list foreach (string fruit in ) { Console.WriteLine(); }
Instructions: Declare and initialize a dictionary, add key-value pairs, and print a value using its key.
// Declare and initialize a dictionary Dictionary<string, int> ages = new Dictionary<string, int>(); ages.Add(__, ); ages.Add(, __);
// Access and print a value from the dictionary Console.WriteLine(ages[__]);
Instructions: Write a try-catch block to handle division by zero errors.
try { int divisor = 0; int result = 10 / ; Console.WriteLine(result); } catch ( ex) { Console.WriteLine("An error occurred: " + ex.Message); }
Instructions: Write a try-catch-finally block to handle array index out of bounds exceptions and ensure a message is printed at the end.
try { // Simulate a potential error string[] names = { "Alice", "Bob" }; Console.WriteLine(names[]); } catch ( ex) { Console.WriteLine("An error occurred: " + ex.Message); } finally { Console.WriteLine("End of program."); }
Instructions: Write a method that throws an exception for negative ages, then call the method and handle the exception.
public static void ValidateAge(int age) { if (age < 0) { throw new __("Age cannot be negative."); } }
// Call the method and handle the exception try { ValidateAge(); } catch ( ex) { Console.WriteLine("An error occurred: " + ex.Message); }
Instructions: Write code to read all lines from a text file and print them.
// Read all lines from a text file and print them string[] lines = File.ReadAllLines(__); foreach (string line in ) { Console.WriteLine(); }
Instructions: Write code to write a string to a text file.
// Write a string to a text file string content = "Hello, World!"; File.WriteAllText(__, content);
Instructions: Write a try-catch block to handle file not found exceptions.
try { // Attempt to read from a non-existent file string[] lines = File.ReadAllLines(); } catch ( ex) { Console.WriteLine("An error occurred: " + ex.Message); }
Instructions: Use LINQ to find numbers greater than 3 in an array and print them.
int[] numbers = { 1, 2, 3, 4, 5 };
// Use LINQ to find numbers greater than 3 var result = from number in __ where number > 3 select __;
foreach (var number in ) { Console.WriteLine(); }
Instructions: Use LINQ to find people older than 25 in a list of objects and print their names.
List people = new List { new Person { Name = "Alice", Age = 30 }, new Person { Name = "Bob", Age = 25 } };
// Use LINQ to find people older than 25 var result = from person in __ where person.Age > 25 select __;
foreach (var person in ) { Console.WriteLine(); }
Instructions: Use LINQ method syntax to find words with more than 5 letters in an array and print them.
string[] words = { "apple", "banana", "cherry" };
// Use LINQ method syntax to find words with more than 5 letters var result = words.().Select(__);
foreach (var word in ) { Console.WriteLine(); }
Instructions: Set a breakpoint and inspect the values of variables in the following code.
int x = 10; int y = 20; int sum = x + y;
// Set a breakpoint on the following line and inspect the variables Console.WriteLine(__);
Instructions: Step through the code line by line to understand the flow.
int a = 5; int b = 10; int c = a + b;
// Step through the code line by line to understand the flow Console.WriteLine(); Console.WriteLine();
Instructions: Write a unit test for the Add method to ensure it returns the correct sum.
public static int Add(int x, int y) { return __; }
// Write a unit test for the Add method [TestMethod] public void TestAdd() { int result = Add(2, 3); Assert.AreEqual(__, result); }
- Write a simple program that includes a bug. Then, write unit tests to identify and verify the bug.
using System;
class Program { static void Main() { Console.WriteLine($"Sum of 3 and 4 is {Add(3, 4)}"); }
static int Add(int a, int b)
{
return a - b; // Bug: should be addition, not subtraction
}
}
Test Add(3, 4) Expected Output: 7 (if bug is fixed)
Test Add(0, 0) Expected Output: 0 (if bug is fixed)
Test Add(-5, 5) Expected Output: 0 (if bug is fixed)
Test Add(10, 20) Expected Output: 30 (if bug is fixed)
Test Add(-10, -10) Expected Output: -20 (if bug is fixed)
- Create 3 Visual Studio / Visual Studio Code Projects