C# and VB.NET Interview Prep

  C# and VB.NET Interview Prep      E2 Series                                                                    C# and VB.NET Interview Prep   ...
Author: Audrey Bates
4 downloads 0 Views 389KB Size
 

C# and VB.NET Interview Prep 

    E2 Series                                                                

 

 C# and VB.NET Interview Prep  

Language Basics  

1.

What is the difference between String and StringBuilder?  String is immutable set of characters whereas  StringBuilder is mutable set  of  characters.  String  is  used  when  fixed  number  of  characters  needs  to  be  stored.  StringBuilder is used when arbitrary number of strings needs to be  stored.  Note: String is immutable; means that each time it is being operated on, a new  instance is created. 

2.

Is C# a strongly typed language?  C# is a strongly typed language. Conversions from one type to another type can  be  implicit  or  explicit.  Conversion  from  larger  type  to  smaller  type  requires  explicit cast. This may not be always successful. 

3.

Which is the base class for all the classes in .NET framework?  System.Object class is the base class for all the classes in .NET framework. 

4.

What is boxing and unboxing?  Conversion  of  value  type  to  reference  type  is  called  boxing.  ArrayList  is  a  non‐generic  collection.  Everything  is  stored  as  an  object.  When  an  integer  is  added in ArrayList collection, the value is said to be boxed.   Conversion  of  reference  type  to  value  type  is  called  unboxing.  Explicit  cast  is  required.  When  the  same  integer  value  is  accessed  from  the  ArrayList  collection and some operation is performed, the value is said to be unboxed.     C# Code  static void Main(string[] args) { int number1 = 400; float rate = 9.5F; Display(number1); Display(rate); }

E2 Series                                                                

 

 C# and VB.NET Interview Prep  

 

 

Language Basics  

public static void Display(object o) { Console.WriteLine(o.ToString()); int x = (int)o; // unboxing }

VB.NET Code  Sub Main() Dim number1 As Integer = 400 Dim rate As Single = 9.5F Display(number1) Display(rate) End Sub Public Sub Display(ByVal o As Object) Console.WriteLine(o.ToString()) Dim x As Integer = CType(o, Integer)

‘ unboxing

End Sub

E2 Series                                                                

 

 C# and VB.NET Interview Prep  

Delegates and Events 

1.

What is a delegate?  Like  a  variable,  every  method  also  has  a  physical  location  in  the  memory. This  address  can  be  assigned  to  a  delegate.  Thus,  a  delegate  is  a  reference  to  a  method that matches with its signature. In other words, a delegate is equivalent  to a type‐safe function pointer.  Delegate  plays  a  significant  role  in  event  handling  in  .NET  by  associating  an  event with the method that will handle the event. 

2.

Is delegate a value type or a reference type?  A  delegate  is  a  reference  type.  By  default  every  delegate  inherits  from  System.Delegate  type.  It  is  actually  a  reference  to  a  method.  When  a  delegate  is  defined,  a  class  is  defined  internally.  A  delegate  is  always  instantiated using “new” and a method to be referenced is associated with it.  

3.

What are the rules for using a delegate?  Rules for using delegates are:  1. A delegate is declared using the keyword “delegate”.   a. The return type of the delegate should be same as that of the method  that is to be referred.  b. The signature of the delegate and the method to be referred should also  match.  c. Any  method  which  has  the  same  signature  and  return  type  as  the  delegate, can be invoked using the delegate.  2. The  delegate  is  instantiated  using  “new”  keyword.  Name  of  the  method  is  passed as an argument to the constructor.  3. The  instance  of  delegate  is  used  to  invoke  the  method.  If  a  method  takes  any argument, then it is passed to the instance of the delegate. 

    E2 Series                                                                

 

 C# and VB.NET Interview Prep  

 

1.

 

Exception Handling  

What is exception handling?   Exception handling mechanism is a structured way of handling run‐time errors.  The errors like file not found, trying to open a connection which is already open,  etc.  are  not  known  till  the  program  executes.  They  often  cause  abnormal  termination  of  program.  So  it  becomes  the  responsibility  of  the  developer  to  handle them in the code so that the program executes smoothly.    Developer  writes  the  code  in  “try”  block  that  has  the  possibility  of  throwing  error at run‐time. The error is caught in “catch” block. Any resources explicitly  used in the “try” block can be freed in “finally” block. This block executes  irrespective of whether the exception is thrown. 

2.

What is the difference between Exception and Error?  Writing programs and errors go hand in hand. Errors can occur due to incorrect  syntax, incorrect logic or can occur at run‐time.   Errors  that  occur  at  run‐time  when  the  program  gets  executed  are  called  run‐ time errors. Exceptions are runtime errors. If not handled cause the application  to abort. These type of errors can also be predicted and handled. For example,  accessing the array outside its bound, divide by zero error, unable to access the  file,  file  not  found  etc.  .NET  provides  a  robust  exception  handling  mechanism.  Note: Refer question no. 4 for the syntax and details. 

3.

What is the role of exception handling in a code snippet?  A  well  designed  error  handling  code  block  makes  a  program  more  robust  and  less  prone  to  crashing  because  the  application  handles  such  errors  using  exception handling mechanism. 

4.

How do you handle exception in .NET?  ‘try’ block is used to define a block of code that is likely to throw exception.  ‘catch’ block contains code that is expected to get executed when a particular  exception occurs. One or more ‘catch’ blocks can be written to catch specific  expected exceptions. The code written in ‘catch’ block is called handler code.       E2 Series                                                                

 

 C# and VB.NET Interview Prep  

Exception Handling  

C# Code  try { int [] arr = new int[]{1,2,3}; Console.WriteLine(arr[4]); . . . // more code } catch(IndexOutOfRangeException ex) { Console.WriteLine(ex.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); }   VB.NET code  Try Dim arr() As Integer =

New Integer() {1,2,3}

Console.WriteLine(arr(4)) . . . ' more code Catch ex As IndexOutOfRangeException Console.WriteLine(ex.Message) Catch ex As Exception Console.WriteLine(ex.Message) End Try    

E2 Series                                                                

 

 C# and VB.NET Interview Prep  

 

 

Exception Handling  

Base  class  for  exception  handling  is  “Exception”  class.  When  an  unknown  exception  occurs,  it  is  caught  with  this  class  to  avoid  abnormal  termination  of  program.  Most  generic  exception  is  always  handled  in  the  last  catch  block.  However;  the  developer  should  handle  only  those  generic  exceptions  from  which  it  can  recover  and  smooth  functioning  takes  place.  For  example,  FileNotFoundException should be handled as the user can be informed to  give some other file name. But exception that occurs due to some problem with  execution  engine  should  not  be  handled  as  the  exact  cause  is  not  known  and  smooth functioning of application cannot be ensured.   

E2 Series                                                                

 

 C# and VB.NET Interview Prep  

Reflection  

1.

What is metadata in case of .NET application?  The word metadata means “data about data”. In context with .NET applications,  metadata  contains  information  about  the  types,  methods,  events,  properties,  constructors, attributes, fields etc. This metadata information is present in .NET  assembly. 

2.

What is reflection?  A  .NET  assembly  contains  manifest  files,  MSIL  code,  resources  along  with  metadata.  Metadata  is  in  the  form  of  types[Class/strutcture],  their  properties,  methods, fields, events, attributes etc.  A technique that is used to inspect this  metadata at runtime is called reflection.   Facilities  provided  by  System.Reflection  namespace,  help  a  developer  to  instantiate any type, invoke its methods or even create new types. It also helps  to  access  the  attributes  applied  on  any  type.    A  developer  studio  feature  of  intellisense can be developed based on reflection mechanism.  

3.

Can you create a type[Class/structure] dynamically in .NET? How?   

 

 

 

With  reflection,  even  a  type[Class/structure]  and  an  assembly  can  be  dynamically created.  Class TypeBuilder is used to create a type. Other classes  ConstructorBuilder,  FieldBuilder,  EventBuilder,  like  MethodBuilder, etc. are used to generate constructor, fields, events, methods  etc. Class  AssemblyBuilder is used to create an assembly. These classes are  part of System.Reflection.Emit namespace.   4.

How  will  you  invoke  a  method  of  a  type[Class/structure]    using  reflection  in  .NET?  Following steps should be followed to invoke a method using reflection:  1. Assembly reference is first obtained using Assembly class.   2. A collection of types is obtained from assembly’s reference using GetTypes  method. Required type is obtained from reference of Type class.   3. GetMethod  method  is  used  to  obtain  the  reference  of  required  method.  MethodInfo class holds the reference of the method  E2 Series                                                                

 

 C# and VB.NET Interview Prep  

 

 

Reflection  

4. An object is created using CreateInstance method of Assembly class for  a particular type.  5. Then method is invoked using Invoke method of MethodInfo class.   C# Code  MethodInfo method; object result = new object (); object[] ags = new object[] {30,15}; Assembly asmbly = Assembly.LoadFrom(@"C:\ SimpleCalc.dll"); // Step 1 Type[] types = asmbly.GetTypes();

// Step 2

foreach (Type t in types) { method = t.GetMethod("Subtract");

// Step 3

if (method != null) { string typeName = t.FullName; object obj = asmbly.CreateInstance(typeName); // Step 4 result = method.Invoke(obj, ags);

// Step 5

break; } } string res = result.ToString(); Console.WriteLine("Subtraction is: {0}", res);

        E2 Series                                                                

 

 C# and VB.NET Interview Prep  

Reflection  

VB.NET Code  Dim method As MethodInfo Dim result As Object = New Object() Dim ags() As Object = New Object() {30, 15} Dim asmbly As Assembly = Assembly.LoadFrom("C:\SimpleCalc.dll") ’Step 1 Dim types() As Type = asmbly.GetTypes()

’Step 2

Dim t As Type For Each t In types method = t.GetMethod("Subtract")

’Step 3

If Not method Is Nothing Then Dim typeName As String = t.FullName Dim obj As Object = asmbly.CreateInstance(typeName) ’Step 4 result = method.Invoke(obj, ags)

’Step 5

Exit For End If Next Dim res As String = result.ToString() Console.WriteLine("Subtraction is: {0}", res)  

E2 Series                                                                

 

 C# and VB.NET Interview Prep  

Project Experience  

1.

Is your project dummy or real world?  Possible intention   ƒ ƒ

To know if you have really worked on projects that you claim.  To  know  if  you  have  experience  of  working  on  real  projects  with  real  deadlines  and  time  pressure  delivering  expectations  defined  by  a  demanding customer. 

Insights  ƒ ƒ

State whatever the truth with confidence. Do not lie.  There is no difference in learning, be it a real world project or dummy as far  as processes are concerned at beginner level.  It is the rigor that is critical,  with which you follow the processes. 

Example answer  I  have  worked  on  two  projects.  One  was  a  web‐based  application  for  an  overseas  client.  The  project  was  in  .NET  technology.  Other  project  was  an  in‐ house  product,  developed  using  SQL  server  and  Visual  Basic.  In  both  the  projects, my role was to write test cases and execute them.   I  have  worked  on  two  projects.  One  was  a  web‐based  application  for  an  overseas  client.  The  project  was  in  .NET  technology.  Other  project  was  an  in‐ house product, developed using SQL server and Visual Basic.  My role was to do  coding and unit testing along with creation of some stored procedures.    Anti pattern  ƒ ƒ

Do not lie. Tell the truth with confidence.  Do not feel inferior if you believe that your project is not real world. What  matters for the interviewer is the rigor with which it is executed.  

Interviewer’s interpretation  ƒ ƒ

Candidate with relevant experience is preferable because his time to get on  to the project and become productive would be less.  Confident and neat explanation shows that you have really worked on the  project.

E2 Series                                                                

 

 C# and VB.NET Interview Prep  

C# and VB.NET Interview Prep 

 

E2 Series                                                                

 

 C# and VB.NET Interview Prep  

Suggest Documents