Chapter 01 To 06
Chapter 01 To 06
Chapter 01
1. What is visual studio?
Visual Studio 2013 is a tool-rich programming environment containing the functionality that you need to create large
or small C# projects running on Windows 7, Windows 8, and Windows 8.1.
Visual Studio is an IDE (Integrated Development Environment) for creating Application on top of .NET Framework.
In visual studio 2013, You can create application in other languages like Visual Basic .NET, C++, F#.
2. What is a Console application?
A console application is an application that runs in a command prompt window without a graphical user interface
(GUI).
3. What is Solution Explorer in visual studio?
Solution explorer shows projects in the current solution and also files, references, resources associated with every
project.
You can easily access items like code files using solution explorer.
4. What is properties folder in a project?
Properties folder contains a file AssemblyInfo.cs
AssemblyInfo.cs contains attributes related to output assembly produced by the project such as the name of the
author, the date the program was written, and so on.
5. What is references in a project?
References contains list of libraries (shipped with .NET or developed by others) that are used in the project.
6. What are assemblies?
In the Microsoft .NET Framework, these libraries are called assemblies.
Developers use assemblies to package useful functionality that they have written so that they can distribute it to other
developers who might want to use these features in their own applications.
7. What is App.config (Application configuration) file?
→ App.config file contains settings that control the behavior of the application at run-time.
→ Changes to App.config do not require to recompile the application.
8. What is output window?
Output shows build related messages.
If you write any message in Debug stream are shown in output window.
9. What is a namespace?
A namespace groups together related types.
A namespace can contain class, struct, interface, delegate and even namespace.
Advantages:
→ A namespace groups together logically related types.
→ Namespace removes name-conflicts, e.g. two classes can have same name if there are in different namespace.
10. What is using directive?
A using directive imports types in a namespace in the current code file.
If we import a namespace by the use of using directive in the current code, we can write short names for the types of
the imported namespace instead of writing fully qualified name.
Example:
The fully qualified name of Console class of System namespace is System.Console
But if we add using System directive, we can write only Console
Chapter 02
1. What is a statement?
A statement is a command to perform action.
Every statement in C# ends with semi-colon (;)
We write statements inside a method.
2. What is syntax?
The code writing rules of a particular programming language is syntax.
Each programming language (including C#) follow rules for the formation and construction of statements – these rules
are syntax.
3. What is Semantics?
In a programming language what the statements actually do is known as semantics.
4. Why C #is called free-format language?
C# ignores extra whitespaces, new line.
All extra spaces are condensed to one space.
you are free to lay out your statements in any style you choose.
That is why C# is called free-format language.
5. What are identifiers?
Identifiers are the names that you use to identify the elements in your programs, such as namespaces, classes,
methods, and variables.
6. What are rules for identifiers?
An identifier cannot:
→ Start with number but can contain number after first one
→ Contain spaces
→ Contain special characters (_ character is valid)
→ be a reserved word
Valid identifiers: student1, first_name
Invalid identifiers: parRate$, first name, first-name
7. What is a keyword?
The word that has special meaning in a programming language is called keyword
C# has 77 reserved or keyword
Examples of keywords are class, namespace, private, void etc.
8. What is a variable?
A variable is a storage location where we can store a value.
Value in a variable can be change as program executes.
9. How do declare a variable?
To declare a variable, we must define what type of data it will store.
Syntax
typename varaiblename;
Example: int a; bool isPassed; double rate;
10. Why C #is called strongly-typed (type-safe) language?
In C# to declare a variable we must define what type of data it will store.
Once type is set, it will hold only that type of data only.
And that is why c# is strongly-typed language.
11. What is unassigned variable?
A variable that is declared but never assigned a value to it is called unassigned variable.
12. What is definite assignment rule?
C# does not allow to use an unassigned variable.
To operate on a variable, we must store value to it.
This is called definite assignment rule.
13. What are the arithmetic operators in C#?
+ for addition
- for subtraction
* for multiplication
/ for division
% for remainder or modulus
14. What operator can be used on numbers and strings?
Plus (+) Operator
15. What is concatenation?
Creating a new string by combining two string is called concatenation.
+ Operator is used to concatenate strings.
16. What is operator precedence?
Operator precedence dictates which operator in an expression is evaluated first.
17. What is operator associativity?
Operator associativity dictates in which direction (left to right or right to left) operators in an expression are evaluated.
18. What is pre-increment (prefix increment) and post-increment (postfix increment)?
When we place ++ before a variable, the variable is pre incremented. Value is incremented and then expression is
evaluated.
Example
Int a, b=1;
a = ++b;
a will have 2 and b will have 2
When we place ++ after a variable, the variable is post incremented. Expression is evaluated and then Value is
incremented.
Example
int a, b=1;
a = b++;
a will have 1 but b will have 2
19. What is implicitly typed variable?
The variable that’s type is inferred from the value assigned to it is implicitly typed variable.
We use var keyword to declare implicitly typed variables.
var rate = 7.5;
Chapter 03
1. What is a method?
A method is a set of statements identified by a name.
The statements are executed each time the method is called
A method can accept data and also return data
2. What things are included in method definition?
In simplest form a method's definition must contain -
→ Return type
→ Method name
→ Parameter list (may not present if no data to be passed to methods)
→ Method body enclosed in a pair of curly braces ({})
3. Write the basic syntax of a method definition in C#?
Syntax
returnType methodName (parameter1, parameter2, ….)
{
// method body statements go here
}
Example
int Add (int a, int b)
{
//code
}
4. What is return type of a method?
Return type specifies what type data method returns when called.
5. What is a void method?
A void method returns no data when called.
6. What is parameter list in method?
Parameter list defines type and order of data to pass to method when you want to call it in your code.
7. What is return statement in a method?
Return statement finishes the methods and return control to the caller.
Return statement also returns value to the caller.
The method that has a return type must have a return statement.
Void methods do not require return statement.
8. Why do you use return statement in a void statement?
Sometimes we need to exit immediately from the method conditionally, it that case we can use return statement in a
void method.
Void methods use the keyword return immediately followed by a semicolon.
9. What is method calling?
Method calling is used to execute the code inside a method.
Statements inside a method never run unless it is called.
10. What is scope of a variable?
Scope of variable means the boundary where a variable becomes available and usable.
A variable exists as long as the scope is being executed.
11. What is a local variable?
The variables that are declared in methods are local variables.
12. What is the difference between Console.Write and Console.WriteLine method?
Console.Write method prints the supplied value to the console window.
Console.WriteLine method prints the supplied value to the console window and adds a new line.
Chapter 04
1. What is a decision (selection) statement?
A decision statement allows conditional branching in program.
It allows execute one statement block out of two alternative based on condition
The common decision statement in C# if…else…
A if…else… statement looks like
If (condition)
{
//codes if true
}
else
{
//code if false
}
2. What is a Boolean variable?
The variable that can hold either true or false
3. What is Boolean expression?
An expression that results either true or false is a Boolean expression.
4. What is bool data type?
C# has a data type bool (short of Boolean) that can be either true or false
Example
bool ok = false;
bool isContinued = true;
bool b = 5 >6;
5. What is a Boolean operator?
The operators that perform calculation on Boolean values and result of the calculation is either true or false.
6. What are Boolean operators’ /Condition logic operators? How they are expressed in C#?
There three Boolean operators:
→ Logical AND- && in c#
→ Logical OR - || in c#
→ NOT - ! in c#
7. How equality and inequality operators expressed in c#?
Operator C# Expression Example
Equality == A == B
Inequality != A != B
8. What is short-circuiting in Boolean operators in C#?
The && and || exhibit short-circuit behavior.
If the first part of the expression determines the result of the whole expression, the right part is not evaluated. The
feature is known as short-circuiting.
For example, say you have, (a > b) && (++c>5)
→ If a>b is false, then the whole expression is false irrespective of what is the result of ++c>5
→ In this case c# will never evaluate ++c>5
9. Write the basic syntax of if..else.. statement.
Syntax
if (condition)
statement-1;
else
statement-2;
10. What is cascading if statement?
We can chain together multiple if statements which are tested one after another until one block is executed.
Basic example
if (condition-1)
{
//code
}
else if (condition-2)
{
//code
}
else if (condition-3)
{
//code
}
else
{
//code
}
11. When can you use switch statement instead of cascading if statement?
In a cascading if statement each condition compares with a unique value, in that case we can use switch statement.
12. Write basic syntax of switch statement?
switch (control-variable)
{
case value-1:
//statement
break;
case value-2:
//statement
break;
case value-3:
//statement
break;
default:
//statement
break;
}
Chapter 05
1. What is a compound assignment operator?
C# has shortcuts to combine any arithmetic operations with assignment operators.
These are known as compound assignment operators.
For example
a += 5 is equivalent to a = a+5
2. What is an iteration statement?
An iteration statement allows to run one or more statements repeatedly as long as a condition remains true.
3. Write down the syntax of a while loop in c#?
while (booleanexpression)
{
//statements
}
4. What is a sentinel variable?
The variable that controls how many times a loop will perform is called sentinel variable.
5. Write down the syntax of a for loop in C#?
for(intililization;Boolean expression; update control variable)
{
//statements
}
Example
for(int i=1;i<=10;i++)
{
Console.WriteLine(i);
}
6. Write the syntax of a do loop in C#?
do
{
//statements
} while (Boolean expression);
7. What is difference between the while and the do loop?
In while loop condition is checked before loop body is executed whereas in do loop condition is checked after the
loop body is executed
In while loop, the loop body might never execute but in do loop the loop body is guaranteed to execute once.
8. What is the purpose of the break statement in a loop?
The break statement causes to exit the loop immediately.
9. Why is the continue statement used in a loop?
The continue statement cause to leave the current iteration and go for the next iteration.
Chapter 06
1. What are exceptions?
An exception is an unexpected situation that arises when a program is running and program cannot continue.
When an error occurs, c# runtime throws an exception and the exception object contains the information about the
error.
2. What is exception handling?
Exception handling is a built-in mechanism in .NET framework to detect and handle run-time errors.
3. What is a catch handler?
A catch handler handles a specific type of error.
One or more catch handler is used with a try block.
When an exception is thrown in the try block, execution jumps to the first matching catch handler.
4. What is an unhandled exception?
The exception that is not trapped inside the program using appropriate catch handler is called unhandled exception.
If a try block throws an exception and there is no corresponding catch handler the program terminates with an
unhandled exception.
5. Write the syntax of error handling code structure in c#?
try
{
// Statements that can cause exception
}
catch(ExceptionType1 x)
{
// Statements to handle exception
}
catch(ExceptionType2 y)
{
// Statements to handle exception
}
…
finally
{
// Statement to clean up
}
6. Why is the finally block used in exception handling?
The finally block is guaranteed to run if an error occurs in try block or not.
To ensure that a statement is always run, whether or not an exception has been thrown, is to write that statement
inside a finally block.
Chapter 07
1. What is a class?
A Class is the design template for a certain kind of objects.
A Class dictates how an object of a certain type is created, what data it will hold, and what things it can do.
Actually a Class is the design specification of a kind of objects.
2. What is an object?
An object is an instance of a class.
A Class only provides design. But the objects actually do the tasks and hold data at run-time.
3. What is encapsulation?
Encapsulation means information hiding.
An object hides data and implementation details from outside and controls access to them – this feature is known as
encapsulation.
Encapsulation refers to an object's ability to hide data and behavior that are not necessary to its user.
4. What are the purposes of encapsulation?
Encapsulation actually has two purposes:
→ To combine methods and data within a class
→ To control the accessibility of the methods and data
5. What is constructor?
A constructor is a special method that runs automatically when we create a new object of a class.
A constructor has the same name as the class, and it can take parameters, but it cannot return a value (not even
void).
Every class must have a constructor. If you don’t write one, the compiler automatically generates a default
constructor.
Constructors can be overloaded i.e. a class can have multiple constructors.
6. 6. What is accessibility in a class?
Accessibility means controlling access to members in a class.
To control accessibility there are various modifiers – public, protected, internal and private.
7. What is the meaning of: public and private?
A method or field is public if it is accessible from both within and outside of the class.
A method or field is private if it is accessible only from within the class.
8. What is a static member of a class?
Static members are class members not object members.
Static members are shared members and only one copy of it is created.
9. What const field?
A const is a static field but its value never changes.
You can declare a field as const only when the field is a numeric type.
You must initialize const field.
10. 1What is anonymous class?
An anonymous class is a class that does not have a name.
You create an anonymous class simply by using the new keyword and a pair of braces defining the fields and values
that you want the class to contain.
When you define an anonymous class, the compiler generates its own name for the class.
Chapter 08
1. What are value types?
Most of primitive types such as int, double (excluding string) are value types.
They are fixed sized values.
They are created on the stack.
2. What is a reference type?
A reference type is created on the heap.
All objects are reference types.
Reference variables are stored in two places; the object is created on heap. In the stack the address of the place
where the object is created is stored.
3. What is a nullable type?
A nullable type is value type but can be assigned null to it.
A nullable type has two extar properties:
→ HasValue: used to check if the variable is assigned
→ Value: returns the value stored.
To declare a nullable type, we use ? sign after the type.
Example?
int? n = null;
if(!n.HasValue) n = 100;
4. What is “ref” keyword?
When you pass a value type with the “ref” keyword to a method, the actual reference is passed
In this case, value changed by the method is visible outside of the method
You cannot pass unassigned variable as ref variable.
5. What is “out” keyword?
The “out” keyword acts like “ref” keyword.
The difference is that it can accept unassigned variable with “out” keyword.
And the method accepting out variable must assign value to the variable.
6. What do you mean by boxing and unboxing?
Converting a value type to a reference type is called boxing.
Converting a reference type to a value type is unboxing?
Example:
int n = 10;
object o = n; //boxing
int m = (int)o; //unboxing
7. What is “is” operator?
The “is” operator is used to check that an object is of certain type.
What is “as” operator”?
The “as” is used with reference type.
The “as” operator is used to cast a reference type.