CodeBlog.xyz

C# – 50 simple questions

September 3, 2023 | by Meir Achildiev

C# questions
  • What is C#?
    • C# is a type-safe, managed and object oriented language, which is compiled by .NET framework for generating intermediate language (IL).
  • Write a simple C# program to print “Hello, World!”
using System;
class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
    }
}
  • How do you declare a variable in C#?
    • You can declare a variable by specifying its type followed by its name. For example, int myNumber; declares an integer variable named “myNumber”.
  • How do you assign a value to a variable in C#?
    • You can assign a value to a variable using the “=” operator. For example, myNumber = 10; assigns the value 10 to the “myNumber” variable.
  • What are the different types of variables in C#?
    • C# has various types of variables including integer (int), double, char, bool, string, and arrays.
  • What is a method in C#?
    • A method is a group of statements that perform a specific task. Methods in C# provide reusability of code and simplicity.
  • Write a simple method in C#.
void DisplayMessage()
{
    Console.WriteLine("Hello from DisplayMessage method!");
}
  • What is an object in C#?
    • An object is an instance of a class. It is created using the “new” keyword.
  • How do you create an object in C#?
    • You create an object by using the “new” keyword followed by the class name and parentheses. For example, Dog myDog = new Dog();.
  • What is a constructor in C#?
    • A constructor is a special method in a class that is automatically called when an object of that class is created. It usually initializes the data members of the new object.
  • Write a constructor for the Dog class.
public class Dog
{
    public string name;
    public int age;

    public Dog(string name, int age)
    {
        this.name = name;
        this.age = age;
    }
    
    public void Bark()
    {
        Console.WriteLine("Woof!");
    }
}
  • What are access modifiers in C#?
    • Access modifiers in C# are keywords that set the accessibility of classes, methods, and other members. Access modifiers include public, private, protected, internal, and protected internal.
  • What is inheritance in C#?
    • Inheritance is a fundamental characteristic of object-oriented programming in C#. It allows you to create a new class that reuses, extends, and modifies the behavior that is defined in another class.
  • Write a simple example of inheritance in C#.
public class Animal
{
    public void Eat()
    {
        Console.WriteLine("Eating...");
    }
}

public class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Woof!");
    }
}
  • What is an interface in C#?
    • An interface in C# is a blueprint of a class. It is like an abstract class because it can contain only abstract methods, properties, indexers or events. It can’t contain concrete methods.
  • Write a simple example of an interface in C#.
public interface IFlyable
{
    void Fly();
}

public class Bird : IFlyable
{
    public void Fly()
    {
        Console.WriteLine("Flying...");
    }
}
  • What is polymorphism in C#?
    • Polymorphism is one of the fundamental characteristics of object-oriented programming. It allows you to treat derived class members as their base class type.
  • What is encapsulation in C#?
    • Encapsulation is one of the fundamental characteristics of object-oriented programming. It refers to the bundling of data, and the methods that operate on that data into a single unit, i.e., class.
  • How to implement encapsulation in C#?
    • Encapsulation is implemented by using access specifiers. An example of encapsulation is creating a class and the class members are made private.
  • What is an exception in C#?
    • An exception is a problem that arises during the execution of a program. In C#, an exception is a runtime error that is thrown by the .NET Framework when a program encounters a problem.
  • How do you handle exceptions in C#?
    • You handle exceptions in C# using a try-catch block. The code that might throw an exception is put inside a try block, and the code to handle the exception is put inside a catch block.
  • Write an example of exception handling in C#.
try
{
    int[] myArray = new int[5];
    myArray[6] = 30;
}
catch (IndexOutOfRangeException ex)
{
    Console.WriteLine("An exception occurred: " + ex);
}
  • What is the “finally” block in C#?
    • The “finally” block is used to execute a given set of statements, whether an exception is thrown or not thrown. For example, if you open a file, it must be closed whether an exception is raised or not.
  • What are arrays in C#?
    • Arrays in C# are a type of data structure that can hold a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is better to think of it as a collection of variables of the same type.
  • How do you declare an array in C#?
    • You can declare an array by specifying the type of its elements, followed by square brackets, and then the name of the array. For example, int[] myArray;.
  • How do you initialize an array in C#?
    • You can initialize an array when it’s declared by providing values for its elements inside curly braces. For example, int[] myArray = {1, 2, 3, 4, 5};.
  • What are multidimensional arrays in C#?
    • Multidimensional arrays are arrays with more than one dimension. For example, you might have a 2D array representing a grid or a matrix.
  • How do you declare a multidimensional array in C#?
    • You can declare a multidimensional array by adding commas within the square brackets. For example, int[,] myMatrix;.
  • How do you initialize a multidimensional array in C#?
    • You can initialize a multidimensional array like this: int[,] myMatrix = {{1, 2}, {3, 4}, {5, 6}};.
  • What are loops in C#?
    • Loops in C# are used to perform a task multiple times until a specific condition is met. C# has several types of loops, including for, while, do-while, and foreach.
  • Write a simple for loop in C#.
for(int i = 0; i < 10; i++)
{
    Console.WriteLine(i);
}
  • Write a simple while loop in C#.
int i = 0;
while(i < 10)
{
    Console.WriteLine(i);
    i++;
}
  • What is a “do-while” loop in C#?
    • A “do-while” loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
  • Write a simple “do-while” loop in C#.
int i = 0;
do
{
    Console.WriteLine(i);
    i++;
} while(i < 10);
  • What is a “foreach” loop in C#?
    • The “foreach” loop is used to iterate over a collection (like an array or a list).
  • Write a simple “foreach” loop in C#.
int[] numbers = {1, 2, 3, 4, 5};
foreach(int number in numbers)
{
    Console.WriteLine(number);
}
  • What are conditionals in C#?
    • Conditionals in C# are used to perform different actions based on different conditions. Common conditional statements include “if”, “else”, and “switch”.
  • Write a simple “if” statement in C#.
int number = 10;
if(number > 5)
{
    Console.WriteLine("The number is greater than 5");
}
  • What is an “else” statement in C#?
    • The “else” statement is used to specify a block of code to be executed if the condition in the “if” statement is false.
  • Write a simple “if-else” statement in C#.
int number = 3;
if(number > 5)
{
    Console.WriteLine("The number is greater than 5");
}
else
{
    Console.WriteLine("The number is not greater than 5");
}
  • What is a “switch” statement in C#?
    • The “switch” statement is used to select one of many code blocks to be executed.
  • Write a simple “switch” statement in C#.
int dayOfWeek = 3;
switch (dayOfWeek)
{
    case 1:
        Console.WriteLine("Monday");
        break;
    case 2:
        Console.WriteLine("Tuesday");
        break;
    case 3:
        Console.WriteLine("Wednesday");
        break;
    //...and so on
    default:
        Console.WriteLine("Invalid day");
        break;
}
  • What are “nullables” in C#?
    • Nullable types in C# are data types which can have a value or have no value at all (null). For example, you could have a nullable integer, int? myNumber;.
  • How do you create a nullable type in C#?
    • You can create a nullable type by using the ? operator. For example, int? myNumber;.
  • What is the “??” operator in C#?
    • The “??”, or null-coalescing operator, is used to define a default value for nullable types. For example, int myNumber = nullableNumber ?? defaultNumber;.
  • What is the “?” operator in C#?
    • The “?” or ternary operator is a shorthand way of writing an if-else statement. It’s called the ternary operator because it takes three operands.
  • Write a simple example of the “?” operator in C#.
int number = 10;
string message = number > 5 ? "The number is greater than 5" : "The number is not greater than 5";
Console.WriteLine(message);

RELATED POSTS

View all

view all