1. Define variable and constant.
A variable can be defined as a meaningful name that is given to a data storage location in the computer memory that contains a value. Every variable associated with a data type determines what type of value can be stored in the variable, for example an integer, such as 100, a decimal, such as 30.05, or a character, such as ‘A’.
You can declare variables by using the following syntax:
<Data_type> <variable_name> ;
A constant is similar to a variable except that the value, which you assign to a constant, cannot be changed, as in case of a variable. Constants must be initialized at the same time they are declared. You can declare constants by using the following syntax:
const int interestRate = 10;
2. What is a data type? How many types of data types are there in .NET ?
A data type is a data storage format that can contain a specific type or range of values. Whenever you declare variables, each variable must be assigned a specific data type. Some common data types include integers, floating point, characters, and strings. The following are the two types of data types available in .NET:
- Value type – Refers to the data type that contains the data. In other words, the exact value or the data is directly stored in this data type. It means that when you assign a value type variable to another variable, then it copies the value rather than copying the reference of that variable. When you create a value type variable, a single space in memory is allocated to store the value (stack memory). Primitive data types, such as int, float, and char are examples of value type variables.
- Reference type – Refers to a data type that can access data by reference. Reference is a value or an address that accesses a particular data by address, which is stored elsewhere in memory (heap memory). You can say that reference is the physical address of data, where the data is stored in memory or in the storage device. Some built-in reference types variables in .Net are string, array, and object.
3. Mention the two major categories that distinctly classify the variables of C# programs.
Variables that are defined in a C# program belong to two major categories: value type and reference type. The variables that are based on value type contain a value that is either allocated on a stack or allocated in-line in a structure. The variables that are based on reference types store the memory address of a variable, which in turn stores the value and are allocated on the heap. The variables that are based on value types have their own copy of data and therefore operations done on one variable do not affect other variables. The reference-type variables reflect the changes made in the referring variables.
Predict the output of the following code segment:
int x = 42;
int y = 12;
o = x;
w = y * (int)o;
/* The output of the code is 504. */
4. Which statement is used to replace multiple if-else statements in code.
In Visual Basic, the Select-Case statement is used to replace multiple If – Else statements and in C#, theswitch-case statement is used to replace multiple if-else statements.
5. What is the syntax to declare a namespace in .NET?
In .NET, the namespace keyword is used to declare a namespace in the code.
The syntax for declaring a namespace in C# is:
The syntax for declaring a namespace in VB is:
6. What is the difference between constants and read-only variables that are used in programs?
Constants perform the same tasks as read-only variables with some differences. The differences between constants and read-only are
- Constants are dealt with at compile-time.
- Constants supports value-type variables.
- Constants should be used when it is very unlikely that the value will ever change.
- Read-only variables are evaluated at runtime.
- Read-only variables can hold reference type variables.
- Read-only variables should be used when run-time calculation is required.