21 March 2013

C# -- General Questions and Answers Part-I



C# -- General Questions and Answers:

Part-I

1.  What is C#?
                    C# (pronounced "C sharp") is a simple, modern, object-oriented, and type-safe programming language. It will immediately be familiar to C and C++ programmers. C# combines the high productivity of Rapid Application Development (RAD) languages.

2.   What are the types of comment in C#?
            There are 3 types of comments in C#.  Single line (//)     Multi (/* */)     Page/XML Comments (///).

3.  What are the namespaces used in C#.NET?
          Namespace is a logical grouping of class.
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

4.  What are the characteristics of C#?
     There are several characteristics of C# are:
v  Simple   
v  Type safe   
v  Flexible    
v  Object oriented    
v  Compatible    
v  Consistent     
v  Interoperable 
v  Modern

5. What are the modifiers in C#?
      Modifiers are:
v  Abstract
v  Sealed
v  Virtual
v  Const
v  Event
v  Extern
v  Override
v  Read only
v  Static
v  New
6.   What are the types of access modifiers in C#?
Access modifiers in C# are: 
v  Public
v  Protect
v  Private
v  Internal
v  internal protect
7.  What is boxing and unboxing?
                 Boxing: Convert Value Type to Reference Type.
Unboxing:  Convert Reference Type to Value Type.
8.  What are the types of arrays in C#?
              Types of Arrays:
v  Single-Dimensional
v  Multidimensional
v  Jagged arrays.

9. Define Constructors?
                A constructor is a member function with the same name as its class. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the class.

10.  Define destructors?
                A destructor is called for a class object when that object passes out of scope or is explicitly deleted.
           A destructors as the name implies is used to destroy the objects that have been created by a constructors

11. What is ENUM?
                Enum are used to define constants.

12. What is the use of enumerated data type?
                An enumerated data type is another user defined type which provides a way for attaching names to numbers thereby increasing comprehensibility of the code. The Enum keyword automatically enumerates a list of words by assigning them values 0,1,2, and so on.

13. What is Jagged Arrays?         
    A jagged array is an array whose elements are arrays.
    The elements of a jagged array can be of different dimensions and sizes.
    A jagged array is sometimes called an array–of–arrays.

14. What is the use of using statement in C#?
                The using statement is used to obtain a resource, execute a statement, and then dispose of that resource.

15. What is serialization?
                Serialization is the process of converting an object into a stream of bytes.
De-serialization is the opposite process of creating an object from a stream of bytes.
Serialization / De-serialization is mostly used to transport objects.

16. What is a base class?
                A class declaration may specify a base class by following the class name with a colon and the name of the base class. Omitting a base class specification is the same as deriving from type object.

17. Can “this” be used within a static method?
                No ‘This’ cannot be used in a static method. As only static variables/methods can be used in a static method.

18. What are the different types of statements supported in C#?
               

    C# supports several different kinds of statements are
v  Block statements
v  Declaration statements
v  Expression statements
v  Selection statements
v  Iteration statements
v  Jump statements
v  Try catch statements
v  Checked and unchecked
v  Lock statement

19. What is method?
                A method is a member that implements a computation or action that can be performed by an object or class. Static methods are accessed through the class. Instance methods are accessed through instances of the class.

20. What is field?
                A field is a variable that is associated with a class or with an instance of a class.

21. What is event?
                An event is a member that enables a class or object to provide notifications. An event is declared like a field except that the declaration includes an event keyword and the type must be a delegate type.

22. What are literals and their types?
                    Literals are value constants assigned to variables in a program. C# supports several types of literals are
v  Integer literals
v  Real literals
v  Boolean literals
v  Single character literals
v  String literals
v  Backslash character literals

23. What are the features of c#?
v  C# is a simple and powerful programming language for writing enterprise edition applications.
v  This is a hybrid of C++ and VB. It retains many C++ features in the area statements, expressions, and operators and incorporated the productivity of VB.
v  C# helps the developers to easily build the web services that can be used across the Internet through any language, on any platform.
v  C# helps the developers accomplishing with fewer lines of code that will lead to the fewer errors in the code.
v  C# introduces the considerable improvement and innovations in areas such as type safety, versioning, events and garbage collections.

24.          What are the types of errors?
v  Syntax error
v  Logic error
v  Runtime error

25. What are the two data types available in C#?
v  Value type
v  Reference type

26. What is a code group?
                A code group is a set of assemblies that share a security context.

27. What are sealed classes in C#?
                The sealed modifier is used to prevent derivation from a class. A compile-time error occurs if a sealed class is specified as the base class of another class.

28. Define namespace?
                The namespace are known as containers which will be used to organize the hierarchical set of .Net classes.

29. What are the different types of variables in C#?
         Different types of variables used in C# are:
v  static variables
v  instance variable
v  value parameters
v  reference parameters
v  array elements
v  output parameters
v  local variables

30. What is meant by method overloading?
                Method overloading permits multiple methods in the same class to have the same name as long as they have unique signatures. When compiling an invocation of an overloaded method, the compiler uses overload resolution to determine the specific method to invoke

31. What is parameter?
                Parameters are used to pass values or variable references to methods. The parameters of a method get their actual values from the arguments that are specified when the method is invoked. There are four kinds of parameters: value parameters, reference parameters, output parameters, and parameter arrays.

32. Is C# is object oriented?
                YEs, C# is an OO language in the tradition of Java and C++.

33. What are the special operators in C#?
      C# supports the following special operators.
v  is (relational operator)
v  as (relational operator)
v  typeof (type operator)
v  sizeof (size operator)
v  new (object creator)
v  .dot (member access operator)
v  checked (overflow checking)
v  unchecked (prevention of overflow checking)

34. What is meant by operators in c#?
                An operator is a member that defines the meaning of applying a particular expression operator to instances of a class. Three kinds of operators can be defined: unary operators, binary operators, and conversion operators. All operators must be declared as public and static.

35. What is a parameterized type?
                A parameterized type is a type that is parameterized over another value or type.

36. What is the use of abstract keyword?
                The modifier abstract is a keyword used with a class, to indicate that this class cannot itself have direct instances or objects, and it is intended to be only a 'base' class to other classes.

37.  What is the use of goto statement?
                The goto statement is also included in the C# language. This goto can be used to jump from inside a loop to outside. But jumping from outside to inside a loop is not allowed.

38. Does C# have a throws clause?
                No, unlike Java, C# does not require the developer to specify the exceptions that a method can throw.

39. Does C# support a variable number of arguments?
                Yes, using params keyword. The arguments are specified as a list of arguments of a specific type.

40. Can you override private virtual methods?
                No, private methods are not accessible outside the class.

41. What is a multi cast delegates?
                Each delegate object holds reference to a single method. However, it is possible for a delegate object to hold references of and invoke multiple methods. Such delegate objects are called multicast delegates or combinable delegates.

42. Which is an exclusive feature of C#?
                Xml documentation.

43. Is using of exceptions in C# recommended?
                Yes, exceptions are the recommended error handling mechanism in .NET Framework.

44. What does a break statement do in switch statements?
    The break statement terminates the loop in which it exists. It also changes the flow of the execution of a program.
    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.
45. What is smart navigation?
                The cursor position is maintained when the page gets refreshed due to the server side validation and the page gets refreshed.

46. What is an identifier?
                Identifiers are nothing but names given to various entities uniquely identified in a program.

47. What is the use of return statement?
    The return statement is associated with procedures (methods or functions). On executing the return statement, the system passes the control from the called procedure to the calling procedure. This return statement is used for two purposes:
v  to return immediately to the caller of the currently executed code
v  to return some value to the caller of the currently executed code.

48. What are the different ways a method can be overloaded?
                Different parameter data types, different number of parameters, different order of parameters.

49. Do events have return type?
                No, events do not have return type.

49. What are the different types of literals in C#?
v  Boolean literals: True and false are literals of the Boolean type that map to the true and false state, respectively.
v  Integer literals: Used to write values of types Int, uint, long, and ulong.
v  Real literals: Used to write values of types float, double, and decimal.
v  Character literals: Represents a single character and usually consists of a character in quotes, such as 'a'.
v  String literals: C# supports two types of string literals, regular string literal and verbatim string literals. A regular string literal consists of zero or more characters enclosed in double quotes, such as "116110". A verbatim string literal consists of an @ character followed by a double–quote character, such as ©"hello".
v  The Null literal: Represents the null–type.

50. Can you override private virtual methods?
                No. Private methods are not accessible outside the class.

51. What is nested class?
    A Nested classes are classes within classes.
    A nested class is any class whose declaration occurs within the body of another class or interface.

52. Can you have parameters for static constructors?
                No, static constructors cannot have parameters.

53. Is String is Value Type or Reference Type in C#?
                String is an object (Reference Type).

54. Does C# provide copy constructor?
                No, C# does not provide copy constructor.
55. Can a class have static constructor?
                Yes, a class can have static constructor. Static constructors are called automatically, immediately before any static fields are accessed, and are generally used to initialize static class members. It is called automatically before the first instance is created or any static members are referenced. Static constructors are called before instance constructors. An example is shown below.

56. What is the main use of delegates in C#?
                Delegates are mainly used to define call back methods.

57. Can events have access modifiers?
                Yes, you can have access modifiers in events. You can have events with the protected keyword, which will be accessible only to inherited classes. You can have private events only for objects in that class.

58. Why is the virtual keyword used in code?
                The Virtual keyword is used in code to define methods and the properties that can be overridden in derived classes.

59. How can we suppress a finalize method?
                GC.SuppressFinalize ()

60. Does C# support a variable number of arguments?
    Yes, using the params keyword.
    The arguments are specified as a list of arguments of a specific type, e.g., int. For ultimate flexibility, the type can be object.
    The standard example of a method which uses this approach is System.console.writeLine ().

61. Which method will you call to start a thread?
                Start

62. What is Generic?
    Generic help us to create flexible strong type collection.
    Generic basically separate the logic from the data type in order maintain better reusability, better maintainability etc.

63. Which namespace enables multithreaded programming in XML?
                System. Threading

64. Can we declare a block as static in c#?
                No, because c# does not support static block, but it supports static method.

65. Can we declare a method as sealed?
                In C# a method can't be declared as sealed. However when we override a method in a derived class, we can declare the overridden method as sealed. By declaring it as sealed, we can avoid further overriding of this method.

66. What Command is used to implement properties in C#?
                Get & set access modifiers are used to implement properties in c#.
67. What is static member?
                The member defined as static which can be invoked directly from the class level, rather than from its instance.

68. What is Static Method?
                It is possible to declare a method as Static provided that they don't attempt to access any instance data or other instance methods.

69. What is a new modifier?
                The new modifier hides a member of the base class. C# supports only hide by signature.

70. What are the advantages of get and set properties in C#? 
v  The get property accessor is used to return the property value.
v  The set property accessor is used to assign a new value.

71.  Can multiple catch blocks be executed?
No, Multiple catch blocks can’t be executed. Once the proper catch code executed, the control is transferred to the finally block and then the code that follows the finally block gets executed

72. How can we sort the elements of the array in descending order?
Using Sort () methods followed by Reverse () method.

73. Write down the C# syntax to catch exception?
To catch an exception, we use try catch blocks. Catch block can have parameter of system. Exception type.
E.g.
1
2
3
4
5
6
7
try
{
GetAllData();
}
catch(Exception ex)
{
}

74.   What are circular references?
Circular reference is situation in which two or more resources are interdependent on each other causes the lock condition and make the resources unusable.

75.   What are generics in C#.NET?
Generics are used to make reusable code classes to decrease the code redundancy, increase type safety and performance. Using generics, we can create collection classes. To create generic collection, System.Collections.Generic namespace should be used instead of classes such as Arraylist in the System. Collections namespace. Generics promote the usage of parameterized types.

76.   What is an object pool in .NET?
An object pool is a container having objects ready to be used. It tracks the object that is currently in use, total number of objects in the pool. This reduces the overhead of creating and re-creating objects.

77. List down the commonly used types of exceptions in .Net?
v  ArgumentException
v  ArgumentNullException
v  ArgumentOutOfRangeException
v  ArithmeticException,
v  DivideByZeroException
v  OverflowException
v  IndexOutOfRangeException
v  InvalidCastException
v  InvalidOperationException
v  IOEndOfStreamException
v  NullReferenceException
v  OutOfMemoryException
v  StackOverflowException etc.

78. What are Custom Exceptions?
Sometimes there are some errors that need to be handled as per user requirements. Custom exceptions are used for them and are used defined exceptions.

 79. What are indexers in C# .NET?
Indexers are known as smart arrays in C#. It allows the instances of a class to be indexed in the same way as array.
E.g.

public int this[int index]    // Indexer declaration



80. Is C# code is managed or unmanaged code?
C# is managed code because Common language runtime can compile C# code to Intermediate language.

81. What are the ways to deploy an assembly?
An MSI installer, a CAB archive, and XCOPY command.

82. What’s a satellite assembly?
When you write a multilingual or multi-cultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.

83. What namespaces are necessary to create a localized application?
System. Globalization, System. Resources.

84. How do you generate documentation from the C# file commented properly with a command-line compiler?
Compile it with a /doc switch.

85. Is XML case-sensitive?
Yes, so and are different elements.

86. What debugging tools come with the .NET SDK?
CorDBG – command-line debugger, and DbgCLR – graphic debugger. Visual Studio .NET uses the DbgCLR. To use CorDbg, you must compile the original C# file using the /debug switch.

87. What does the this window show in the debugger?
It points to the object that’s pointed to by this reference. Object’s instance data is shown.

88. What does assert () do?
In debug compilation, assert takes in a Boolean condition as a parameter, and shows the error dialog if the condition is false. The program proceeds without any interruption if the condition is true.

89. Why are there five tracing levels in System.Diagnostics.TraceSwitcher?
The tracing dumps can be quite verbose and for some applications that are constantly running you run the risk of overloading the machine and the hard drive there. Five levels range from none to Verbose, allowing fine-tuning the tracing activities.

90. Where is the output of TextWriterTraceListener redirected?
To the Console or a text file depending on the parameter passed to the constructor.

91. What is a pre-requisite for connection pooling?
Multiple processes must agree that they will share the same connection, where every parameter is the same, including the security settings.

92.   How to use nullable types in .Net?
Value types can take either their normal values or a null value. Such types are called nullable types.



Int? someID = null;
If(someID.HasVAlue)
{
}


Thanks to GOOGLE.

No comments: