C# Questions I Flashcards
Name different types errors which can occur during the execution of a program?
There are three types of errors which can occur during the execution of a program.
Syntax Errors
Runtime Errors
Logical errors
When a syntax error occurs?
A syntax error occurs when the program violates one or more grammatical rules of the programming language. These errors are detected at compile time, i.e., when the translator (compiler or interpreter) attempts to translate the program.
When a runtime error occurs?
A runtime error occurs when the computer is directed to perform an illegal operation by the program such as dividing a number by zero.
Runtime errors are the only errors which are displayed immediately during the execution of a program. When these errors occur, the computer stops the execution of the programming and can display a diagnostic message that will help in locating the error.
When a logical error occurs?
The logical error happens when a program implements a wrong logic. The translator (compiler or interpreter) does not report any error message for a logical error. These errors are the most difficult to locate.
What is Machine code?
Machine code is a language, which can be processed directly by a microprocessor without any need of the previous transformation.
Programmers never write programs directly in machine code.
What is modeling language?
An artificial language that can be used to express information or knowledge or systems in an arrangement which is defined by a reliable number of rules. These rules are also used for interpretation of the meaning of components in the structure.
What does a break statement do in the switch statement?
The switch statement is a selection control statement that is used to handle multiple choices and transfer control to the case statements within its body. The following code snippet shows an example of the use of the switch statement in C#:
switch(choice) { case 1: console.WriteLine(First); break; case 2: console.WriteLine(Second); break; default: console.WriteLine(Wrong choice); break; }
In switch statements, the break statement is used at the end of a case statement. The break statement is mandatory in C# and it avoids the fall through of one case statement to another.
What is the main difference between sub-procedure and function?
The sub-procedure is a block of multiple visual basic statements within Sub and End Sub statements. It is used to perform certain tasks, such as changing properties of objects, receiving or processing data, and displaying an output. You can define a sub-procedure anywhere in a program, such as in modules, structures, and classes.
We can also provide arguments in a sub-procedure; however, it does not return a new value.The function is also a set of statements within the Function and End Function statements.It is similar to sub - procedure and performs the same task.The main difference between a function and a sub - procedure is that sub - procedures do not return a value while functions do.
Differentiate between Boxing and Unboxing.
When a value type is converted to an object type, the process is known as boxing;
whereas, when an object type is converted to a value type, the process is known as unboxing.
Boxing and unboxing enable value types to be treated as objects.
Boxing a value type packages it inside an instance of the Object reference type. This allows the value type to be stored on the garbage collected heap. Unboxing extracts the value type from the object. In this example, the integer variable iis boxed and assigned to object obj.
What is the difference between a struct and a class in C#?
Class and Struct both are the user defined data type but have some major difference:
Struct
* The struct is value type in C# and it inherits from System.Value Type.
* Struct is usually used for smaller amounts of data.
* Struct can’t be inherited to other type.
* A structure can’t be abstract.
* No need to create object by new keyword.
* Do not have permission to create any default constructor.
Class
- The class is reference type in C# and it inherits from the System.Object Type.
- Classes are usually used for large amounts of data.
- Classes can be inherited to other class.
- A class can be abstract type.
- We can’t use an object of a class with using new keyword.
- We can create a default constructor.
Struct
- The struct is value type in C# and it inherits from System.Value Type.
- Struct is usually used for smaller amounts of data.
- Struct can’t be inherited to other type.
- A structure can’t be abstract.
- No need to create object by new keyword.
- Do not have permission to create any default constructor.
Class
- The class is reference type in C# and it inherits from the System.Object Type.
- Classes are usually used for large amounts of data.
- Classes can be inherited to other class.
- A class can be abstract type.
- We can’t use an object of a class with using new keyword.
- We can create a default constructor.
What is an Abstract Class?
Nie można tworzyć instancji klasy abstrakcyjnej: Nie można utworzyć obiektu z klasy abstrakcyjnej. Służy ona do dziedziczenia przez inne klasy.
Może zawierać metody abstrakcyjne: Metoda abstrakcyjna to metoda, która nie ma implementacji (definicji) w klasie abstrakcyjnej, i musi być zaimplementowana w klasie dziedziczącej.
Może zawierać metody z implementacją: Klasa abstrakcyjna może także zawierać metody z pełną implementacją, które nie muszą być zmieniane przez klasy pochodne.
Może zawierać właściwości, pola, konstruktory: Klasy abstrakcyjne mogą mieć właściwości, pola, a także konstruktory, które mogą być używane przez klasy pochodne.
What is an Interface?
An interface contains only the signatures of methods, properties, events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.
An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body.
As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments.
The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class.
Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.
When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.
When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.
What is enum in C#?
An enum is a value type with a set of related named constants often referred to as an enumerator list.
The enum keyword is used to declare an enumeration. It is a primitive data type, which is user defined.
An enum type can be an integer (float, int, byte, double etc.). But if you used beside int it has to be cast.
An enum is used to create numeric constants in .NET framework. All the members of enum are of enum type. There must be a numeric value for each enum type.
The default underlying type of the enumeration element is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.
What is the difference between “continue” and “break” statements in C#?
Using break statement, you can ‘jump out of a loop’ whereas by using continue statement, you can ‘jump over one iteration’ and then resume your loop execution.
What is the difference between constant and readonly in c#?
Constants perform the same tasks as read-only variables with some differences. The differences between constants and read-only are
Constants:
1.Constants are dealt with at compile-time.
2.Constants supports value-type variables.
3.Constants should be used when it is very unlikely that the value will ever change.
4. Implicitly static, meaning it belongs to the type, not an instance of the type. You cannot mark a const as static explicitly.
CONST IS COMPILE AND STATIC
Read-only:
1. Read-only variables * are evaluated at runtime**.
2. Read-only variables can hold reference type variables.
3. Read-only variables should be used when run-time calculation is required.
4. Can be assigned either at the time of declaration or in the constructor of the class.
5. Can be used as instance-level or static. If marked static, it applies to the class, otherwise, it’s tied to an instance.
READONLY IS RUNTIME AND INSTANCE LEVEL
What are 3 types of parameters?
In C Sharp (C#) we can have three types of parameters in a function. The parameters can be
in parameter (which is not returned back to the caller of the function),
out (return muliple values) parameter and
ref parameter. Both ref and out are treated differently at run time and they are treated the same at compiler properties are not variables, therefore it cannot be passed as an out or ref parameter
Ref
THE PARAMETER HAVE TO HAVE A VALUE BEFORE THE FUNCTION CALL!
THE PARAMETER DONT HAVE TO BE CHANGED IN FUNCTION
WHEN WE WANT TO PASS A PARAMETER TO A FUNCTION AND THEN CHANGE IT