C# Interview Questions and Answers For Beginners
C# Interview Questions and Answers For Beginners
C# Interview Questions :
Ans: Datatype refers to the type of data that can be stored in a variable. It also specifies how memory would be
allocated to a variable and the operations that can be performed on that variable.
Ans: Value types that can accept a normal value or a null value are referred to as Nullable types
Ans: The ?? operator is called the null coalescing operator. It is used to define a default value for a nullable
value type.
Ans: The HasValue property is used to check whether a variable having value or not.
Object Var
The object was introduced with C# 1.0 Var was introduced with C# 3.0
Useful when you want to store multiple types of values Useful when you don't know the type of
to a single variable. assigned value.
It cannot be passed as a method
It can be passed as a method argument.
argument.
Q6: What is the ref keyword in C#?
Ans: The ref keyword causes an argument to be passed by reference, not by value.
Ans: The params keyword enables a method parameter to receive n number of arguments. These n number of
arguments are changed by the compiler into elements in a temporary array.
Ans: An operator is a symbol that tells the compiler what operations can be performed o an operand.
Ans: A ternary operator works on a conditional expression that returns a Boolean value. It is a shorthand form of
if-else.
Ans: Typecasting is a mechanism to covert one type of value to another value. It is possible only when both the
data types are compatible with each other.
Ans: 1. Explicit conversion: Conversion of larger data type to smaller data type. It might result in loss of data so
it is also an unsafe type of casting.
2. Implicit conversion: Conversion of a smaller data type to a larger data type and conversion of derived classes
to base class.
Ans: The out is a keyword in C# which is used for passing the arguments to methods as a reference type. It is
generally used when a method returns multiple values.
Q15: Can you use out and ref for overloading as the different signature of method?
Ans: No, we cannot do that. Even if both ref and out are treated differently at runtime they treated the same at
compile time. Hence it cannot be overloaded with the same type of arguments.
Ans: Any argument can be passed by parameter name instead of the parameter position.
Ans: Safe code: The code which runs under the management of CLR is called safe code
Unsafe code: The code which does not run under the management of CLR is called unsafe code.
Ans: Boxing: Implicit conversion of a value type(int, char) to a reference type is known as boxing. Eg:
Unboxing: Explicit conversion of the same reference type(which is created back in boxing process) back to value
type is known as unboxing.
Ans: Implicit conversion of derived classes to a base class is called Upcasting and explicit conversion of the base
class to a derived class is called Downcasting.
Ans: Decision-making statements help you to make a decision based on certain conditions. Different types of
decision-making statements are: if statements, if-else statements, if-else-if statements, and switch statements.
Q22: Which one is better/fast, switch or if-else-if statements and why?
Ans: Switch statements are father than if-else-if statements because in if-else if statements each and every
condition has to check but in case of the switch statement compiler does not need to check earlier cases.
Ans: The goto statement transfers program control to a labeled statement. The statement must exist in the scope
of the goto statement.
Ans: The return statement terminates the execution of the method in which it appears and returns control to the
calling method.
Ans: The jump statements transfer the program control from one point in the program to another point of the
program.
Ans: This statement throws an exception which indicates that an error has occurred during the program
execution.
Q27: What do you mean by an array and what are the different types of an array in C#?
Ans: An array is a collection of the same type of elements that are accessible by a numerical index. Different
types of array are- one dimensional, two dimensional, and jagged arrays.
Ans: A multi-dimensional array is an array with more than one level or dimension. For example, a 2D array,3D
array, etc. Eg:
int[,] arr2D = new int[6,8];// declaration of 2D array arr2D[0,0] = 1;
Ans: A jagged array is an array whose elements are array itself of different dimensions and sizes.
Ans: An object is a representative of the class and is responsible for memory allocation of its member functions.
An object is a real-world entity having attributes and behaviors.
Ans: A constructor is a special type of function which has the same name as its class. The constructor is invoked
whenever an object of a class is created.
Ans: To define multiple constructors for a class, you need to overload the constructors. It means you need to
define parameterized constructors that accept parameters from outside but a static constructor is called by the
CLR and CLR cannot pass parameters to the parameterized constructor.
Ans: The destructor is a special type of member function which has the same name as its class name preceded
by tilde[~] sign. It is used to release unmanaged resources allocated by the object. It cannot be called explicitly.
Ans: The using statement obtains the specified resources, uses it and then automatically calls the dispose
method to clean up the specified resources when the execution of the statement is completed.
Ans: An access modifier is used to specify the accessibility of the class member.
Ans: No, destructors cannot have access modifiers as they are directly called by compiler.
Ans: An enum is a value type that stores a list of named constants which are known as enumerators.
Class Structure
The class can have default and parameterized constructor. The structure can have only default.
The class can have a destructor. The structure cannot have destructor.
The class is a reference type. The structure is a value type.
Ans: Static class members are declared using the static keyword. These can be only called with the class name.
Q42: What is the base class in .NET framework from which all the classes are derived?
Ans: System.Object
Ans: A static class is a special class that is loaded into memory automatically by the CLR at the time of code
execution.
Ans: A static class is useful when you want to provide common utilities like: configuration settings, driver
functions, etc
Ans: Sealed classes are special types of class that is being restricted to be inherited. It is used to prevent
inheritance
No, Multiple catch blocks can't be executed. Once the proper catch code executed, the control is transferred to
the final block, and then the code that follows the final block gets executed.
Ans: A value type holds a data value within its own memory space. Example
int a = 30;
Reference type stores the address of the object where the value is being stored. It is a pointer to another
memory location.
string b = "Hello";
Q48. What is an object pool in .NET?
Ans: An object pool is a container having objects ready to be used. It tracks the object that is currently in use, the
total number of objects in the pool.
Ans: A partial method is a special method within a partial class or struct. One part of a struct has the only partial
method declaration means signature and another part of the same partial class may have an implementation.
Ans: Methods can be overloaded using different data types for a parameter, different order of parameters, and the
different number of parameters.
Ans: Serialization is the process of converting an object into a stream of bytes. That helps in storing that object
into memory or a fill int he form of XML, JSON, etc. Its main purpose is to save the state of an object so that it can
be recreated when it requires.
Ans: Reflection is used to examine objects and their types. The system. Reflection namespace contains classes
that allow you to obtain information and about assemblies, modules, and types.
Ans: Exceptions are unexpected or unseen errors that occur during the execution of a program. It can be caused
due to the improper use of user inputs, system errors, etc
Ans: System.Exception class is provided by the .NET framework to handle any type of exception that occurs. The
exception class is the base class for all other exception classes.
Ans: Exception handling is a method to capture run-time errors and handle them correctly. It is done by using Try-
catch blocks and throw keyword.
2. Binary serialization
3. SOAP serialization
Ans: A delegate is a reference type that holds the reference to a method class method. Any method which has
the same signature as a delegate can be assigned to delegate.
Ans: If the derived class doesn't want to use methods in the base class, the derived class can implement its own
version of the same method with the same signature. For example, in the classes given below, DriveType() is
implemented in the derived class with the same signature. This is called Method Hiding.
Callback Mechanism
Asynchronous Processing
Abstract and Encapsulate method
Multicasting
Ans: A property acts as a wrapper around a field. It is used to assign and read the value from that field by using
set and get accessors. The property can be created for a public, private, protected and internal field.
Q64: What is the difference between “as” and “is” operators in C#?
Ans: An indexer enables a class or struct instances to be indexed in the same way as an array. It is defined using
this keyword.
Ans: Encapsulation is implemented by using access specifiers. An access specifier defines the scope and
visibility of a class member.
Ans: A collection is a set of related objects. It is a class, so you must declare a new collection before you can add
elements to that collection.
Ans: Generic is a class that allows the user to define classes and methods with the placeholder. Generics were
added to version 2.0 of the C# language. The basic idea behind using Generic is to allow type (Integer, String, etc
and user-defined types) to be a parameter to methods, classes, and interfaces.
Ans: Anonymous types allow us to create a new type without defining them. This is a way of defining read-only
properties into a single object without having to define type explicitly.
Ans: A collection of threads, termed as a Thread Pool in C#. Such threads are for performing tasks without
disturbing the execution of the primary thread. After a thread belonging to a thread pool completes execution, it
returns to the thread pool.
Visit : csspoint101.com