Share
Explore

C# Test Questions Bank

megaphone

Detailed tutorial with full examples and descriptions of how to write a C# hello world program with classes and objects and method calls, in Visual Studio Code.

In this tutorial, you'll learn how to create a simple 'Hello World' program using class, objects, and method calls in C#. We'll be utilizing Visual Studio Code to write and run the code.
Please make sure you have Visual Studio Code installed and set up on your system.

Let's begin.
Firstly, open Visual Studio Code.
Now, let's create a new C# file.
Click on 'File' -> 'New File'. You can save it with any name you like, for instance, 'Program.cs'. Please make sure to use the '.cs' extension, which denotes a C# file.
Now, let's start coding.
In C#, every application must contain a Main method which serves as the point of entry for the program. Moreover, the class containing the Main method is the entry class. But before that, we need to specify a namespace.
For simplicity, we'll use 'HelloWorld' as the namespace.
namespace HelloWorld
{

}
Now, let's define our class. Inside the namespace, define a class 'Greetings'.
namespace HelloWorld
{
class Greetings
{
}
}
Within the class, declare a method 'SayHello' which will print 'Hello, World!'. We'll make it 'public' so that it can be accessed from outside the class and 'void' denoting that it doesn't return anything.
namespace HelloWorld
{
class Greetings
{
public void SayHello()
{
Console.WriteLine("Hello, World!");
}
}
}
To call this method, we'll need to create an instance [the Object] of the 'Greetings' class in the Main method.
In C#, the Main method serves as the entry point for the program.
namespace HelloWorld
{
class Greetings
{
public void SayHello()
{
Console.WriteLine("Hello, World!");
}
}
class Program
{
static void Main(string[] args)
{
Greetings greet = new Greetings();
greet.SayHello();
}
}
}
Now, let's run the program.
But before you can run this in Visual Studio Code, you need to create a new .NET console app project.
Here's how:
Open the terminal in Visual Studio Code. You can do this by clicking on 'Terminal' > 'New Terminal'.
Navigate to the folder where you want to create your project.
Create a .NET console app by typing:
dotnet new console --force

dotnet build
Now, navigate to the newly created folder which has the same name as your project.
Open up the folder in Visual Studio Code by typing 'code .' in the terminal.
Now you should see a 'Program.cs' file in the Explorer, where you can replace the existing code with your 'Hello World' program, then save it.
Now to run the code, type:
dotnet run
You should see 'Hello, World!' printed to the console.
That's it! You've written a simple C# program using a class, method, and object. The object 'greet' is an instance of the class 'Greetings', and we're using this object to call the method 'SayHello' within the class.
This program showcases fundamental programming constructs in C# and .NET.

To run the C# program in Visual Studio Code, follow these steps:

1. Open Visual Studio Code. 2. Go to 'File' -> 'Open File' and select your C# file (e.g., 'Program.cs'). 3. Open a new terminal in Visual Studio Code by going to 'Terminal' -> 'New Terminal'. 4. Navigate to the directory containing your C# file using the 'cd' command. For instance, if your file is in a folder named 'MyProgram' on your desktop, you would type `cd Desktop/MyProgram`. 5. Before you can run the program, you need to build it. Use the .NET CLI (Command Line Interface) to do this. You can build the program by typing `dotnet build` in the terminal. This will compile your code and check for any errors. 6. If the code compiles successfully, you will get a 'Build succeeded' message. You can now run the program by typing `dotnet run` in the terminal.
Please ensure you've installed .NET SDK (Software Development Kit) on your computer because the 'dotnet' command requires it. You can download it from Microsoft's official website. After installing the SDK, you should be able to use 'dotnet' commands.

Here are 10 test questions for a basic beginner level C# test:
Short Answer Questions:
What is a variable in C#?
What is a class in C#?
What is an object in C#?
What is the difference between a while loop and a do-while loop in C#?
What is the difference between a for loop and a foreach loop in C#?
Multiple Choice Questions:
Which of the following is not a valid data type in C#? a) int b) float c) double d) char e) string
Which of the following is not a valid access modifier in C#? a) public b) private c) protected d) internal e) final
Which of the following is not a valid way to create an object in C#? a) MyClass obj = new MyClass(); b) MyClass obj = MyClass(); c) var obj = new MyClass(); d) dynamic obj = new MyClass();
What is the output of the following code snippet? int x = 5; if (x > 3) { Console.WriteLine("x is greater than 3"); } else { Console.WriteLine("x is less than or equal to 3"); } a) x is greater than 3 b) x is less than or equal to 3 c) 5 is greater than 3 d) 5 is less than or equal to 3
What is the output of the following code snippet? int[] arr = { 1, 2, 3, 4, 5 }; for (int i = 0; i < arr.Length; i++) { Console.WriteLine(arr[i]); } a) 1 2 3 4 5 b) 5 4 3 2 1 c) 1 3 5 d) 2 4
Answers:
A variable in C# is a named storage location that holds a value of a specific data type.
A class in C# is a blueprint or template for creating objects that define its properties and methods.
An object in C# is an instance of a class that has its own set of values for the properties defined in the class.
A while loop checks the condition at the beginning of the loop, while a do-while loop checks the condition at the end of the loop. This means that a do-while loop will always execute at least once, even if the condition is false.
A for loop is used when you know the number of times you want to iterate, while a foreach loop is used when you want to iterate over all the elements in a collection.
e) string
e) final
b) MyClass obj = MyClass();
a) x is greater than 3
a) 1 2 3 4 5
Coding Question:
Write a C# program that takes two integers as input and outputs their sum.

using System;

class Program
{
static void Main(string[] args)
{
Console.Write("Enter the first number: ");
int num1 = int.Parse(Console.ReadLine());

Console.Write("Enter the second number: ");
int num2 = int.Parse(Console.ReadLine());

int sum = num1 + num2;

Console.WriteLine("The sum of {0} and {1} is {2}", num1, num2, sum);
}
}

Explanation: The program uses the Console.ReadLine() method to read input from the user and the int.Parse() method to convert the input to integers. It then calculates the sum of the two numbers and outputs the result using the Console.WriteLine() method.



10 test questions for a beginner-level C# test:
1. Short Answer: What is the difference between a variable and a constant in C#? A) A variable is a reserved memory location while a constant is a literal value. B) A variable is a literal value while a constant is a reserved memory location. C) A variable can be reassigned while a constant cannot. D) A variable cannot be reassigned while a constant can. Answer: C) A variable can be reassigned while a constant cannot.
2. Multiple Choice: Which of the following is NOT a valid way to declare a variable in C#? A) int x = 5; B) var x = 5; C) X = 5; D) x = 5; Answer: B) var x = 5; (var is not a keyword in C#, and this declaration would give a compile error)
3. Short Answer: What is the purpose of the “public” access modifier in C#? A) To make a member variable accessible only within the same class. B) To make a member variable accessible only within the same assembly. C) To make a member variable accessible from any class in the same project. D) To make a member variable accessible from any class in any project. Answer: D) To make a member variable accessible from any class in any project.
4. Multiple Choice: Which of the following control structures allows you to repeat a section of code multiple times? A) if statement B) switch statement C) loop (e.g. for, while, do-while) D) try-catch block Answer: C) loop (e.g. for, while, do-while)
5. Short Answer: What is the difference between a value type and a reference type in C#? A) Value types are stored in memory while reference types are stored on the stack. B) Value types are stored on the stack while reference types are stored in memory. C) Value types have a null value by default while reference types do not. D) Value types cannot be null while reference types can be null. Answer: D) Value types cannot be null while reference types can be null.
6. Multiple Choice: How would you declare an array of integers in C#? A) int[] myArray; B) Array myArray = new Array(10); C) int myArray[10]; D) int[] myArray = new int[10]; Answer: D) int[] myArray = new int[10];
7. Short Answer: What is the purpose of the “private” access modifier in C#? A) To make a member variable accessible only within the same class. B) To make a member variable accessible only within the same assembly. C) To make a member variable accessible from any class in the same project. D) To make a member variable accessible from any class in any project. Answer: A) To make a member variable accessible only within the same class.
8. Multiple Choice: Which of the following statements will print “Hello World!” to the console using System.Console.WriteLine(); in C#? A) Console.WriteLine("Hello World!"); B) System.Console.Write("Hello World!"); C) Console.Write("Hello World!"); D) System.Console.WriteLine("Hello World!"); Answer: D) System.Console.WriteLine("Hello World

Here are some beginner level test questions focusing on variables, classes and objects, flow of control, and coding in Visual Studio Code along with the answers:
Short Answer:
Question: What is a C# class?
Answer: A class in C# is a template for creating objects (a particular data structure), providing initial values for state (member variables) and implementations of behavior (member functions or methods).
Question: Explain the difference between public and private access modifiers in C#.
Answer: Public access modifier allows a class to expose its member variables and member functions to other functions and objects. Any public members can be accessed from outside the class. On the other hand, private access modifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members.
Question: What is an object in C#?
Answer: An object is an instance of a class. It's a running instance of a class created in memory which contains the data and methods defined by the class.
Question: Explain what a variable is in C#.
Answer: In C#, a variable is a name given to a storage area that is used to store values of various data types.
Question: Describe how to use if-else statements in C#.
Answer: If-else statements in C# are used to test the condition of a variable or expression. If the condition is true, the 'if' block of code is executed, otherwise, the 'else' block of code is executed.
MCQs:
Question: In Visual Studio Code, to run a C# program you would use ____ command. Options: A) csharp: run B) dotnet run C) csc run D) None of the above
Answer: B) dotnet run Explanation: 'dotnet run' is a command-line instruction used to run a .NET application from the source code.
Question: Which keyword is used to define a class in C#? Options: A) Class B) Def C) Int D) Str
Answer: A) Class Explanation: The keyword 'class' is used to define a class in C#.
Question: In C#, which keyword is used to declare a variable? Options: A) Declare B) Create C) Let D) none of the above
Answer: D) None of the above Explanation: In C#, we declare a variable by specifying the type followed by the variable name (e.g., int number).
Question: What is the correct way to instantiate an object in C#? Options: A) MyClass obj = MyClass(); B) MyClass obj(); C) MyClass obj = new MyClass(); D) new MyClass obj;
Answer: C) MyClass obj = new MyClass(); Explanation: In C#, the 'new' keyword is used to create an object from a class.
Question: What is the flow control statement used to iterate over a block of code multiple times until a specific condition is met in C#? Options: A) If-else B) Swim C) For D) Switch
Answer: C) For Explanation: A 'For' loop is used to repeatedly execute a block of code until a specific condition is met in C#.
What is C# and why is it a popular programming language? C# is a modern, object-oriented programming language developed by Microsoft.
It is popular because of its versatility, simplicity, and strong integration with the .NET framework, making it an ideal choice for developing various types of applications, including web applications, desktop applications, mobile apps, Microservices Applications learning to build SOA Service oriented architecture solutions.

Distributed Applications with EDGE Computing is emerging paradigm of Human Computer interaction.

The Experience Excuse: <JOB search related>
Job Search related:
What are the four fundamental principles of object-oriented programming?
Think about how you would organize your class component Hierachy to most efficienty ‘model’ (draw pictures with software) ENTITIES in your Business.
The purpose of the 4 Pillars of Object Orientation is to give us “heurstics” <thinking rules> to assist us in ‘painting the picture’ in software of how the Business Domain operates.
The four fundamental principles of object-oriented programming are
encapsulation: All code is locked up in a special box called an OBJECT: This is a Walled Garden.
inheritance: Create Hierachies to model real world entities in your Business Domain.
polymorphism: Greek word ‘poly’ means many ; ‘Morph’ means to change shape.
abstraction: relates to inheritance.
These principles help in organizing and structuring code, making it more reusable, maintainable, and scalable.
What is ASP.NET and how does it relate to C#? ASP.NET is a web application framework developed by Microsoft that makes it easy for developers to build dynamic web applications and service oriented architecture applications.
It is part of the .NET framework and can be used with C# to create powerful web applications with a rich user interface and robust server-side functionality.

What is the .NET framework and how does it support C# development? The .NET framework is a software development platform {=tools, code, documentation} developed by Microsoft that provides
a runtime environment
libraries
tools
for building, deploying, and running applications.
.NET supports multiple programming languages, including C#, and offers a wide range of features and functionalities that make it easier for developers to create high-quality applications.

What are the key steps in developing a C# ASP.NET web application?
Developing a C# ASP.NET web application involves:
setting up the development environment
creating a new project
designing the user interface
writing C# code for server-side functionality
debugging and testing the application
deploying and dispositioning the Application to product.
This process can be made easier with the help of tools like Visual Studio Code and the .NET SDK.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.