Friday 28 June 2013

Basics of C Programming


1. Is is true that all C# types derive from common base class?


Yes and No. All types can be treated as if they are derived from System.Object, but in order to treat an instance of a value type (e.g int, float) as Object derived, the instance must be converted to reference type using the process called “boxing”.



2.What are the fundamental differences between Value Types and Reference Types? 



Primitive types (e.g int, float, double) and structs are value types. Reference type includes classes, interfaces, strings and arrays.The basic idea is an instance of value type represents actual data stored on the stack, where as an instance of a reference type represents a pointer or reference to the data stored on the heap. 

3.Does C# support multiple inheritence? 

No. But C# supports multiple inheritence of interfaces, not classes. 

4.Does C# have destructors?

No. But instead C# supports a method called Finalize which is almost to similar to destructors in C++. 

5. What is a static constructor?

A constructor for a class rather than instances of a class. The static constructor is called first when the type is loaded. Its also called type constructor. 

6.What is a virtual funtion?

We can declare a function as virtual in a base type so that derived types can provide their own implementation for the function. We have to use the “virtual” keyword to make a function virtual. 

7.How do you force a derived type to provide implementation of a specific method in base type?

We need to use the “abstract” modifier on the method. Also we need to make the class “abstract”. We can’t provide any implementations for this method. 

8.What is the good/useful feature of System.Exception?

The useful feature is “StackTrace” property.  This provides a call stack which records where the exception was thrown. 

9.How “struct”s is differed from a “class”s?

  • Structs are value types.
  • Boxing and unboxing are used to convert between struct and object.
  • The meaning of “this” is different for struct.
  • A struct is not permitted to declare parameterless instance constructor.
A struct is not permitted to declare destructors (or) finaliazers.

10.What are the access modifiers available in C#? How does access modifier affect visibility of types?

private, public, protected, internal, protected internal

private - access is limited to the containing type
public  - access is not restricted
protected –access is restricted to the containing class or types
                    derived from the containing class
internal – access is limited to the assembly

11.What is jagged arrays in C#? Give me an example. 


A jagged array is an array whose elements are arrays.  It is also called “array-of-arrays”.

The following is a single-dimentional array that has three elements, each of which is a single-dimentional array of integers:
Int32[][] jagints = new Int32[][3]

12.What are the differences between “out” and “ref” in C#?

The “ref” causes a method to refer to the same variable that was passed into the method. Any changes to the variable will be reflected in that variable when the control passes back to the calling method.

An argument passed to a “ref” parameter must be initialized. But if an method parameter is marked as “out”, whose argument doesn’t have to be explicitely initialized before passed to an “out” parameter.

13.What is the nested type in C#? What is interesting about nested types?

A type declared within a class or struct is called nested type.

Ex:  class A
        {
            class B   //nested type
             {
              }
 
        }

The nested type can access “private” and “protected” members of its containing type, but the containing type can’t access its nested types “private” or “protected” members. 

14.What is a private constructor and what is the use of it? 


A private constructor is a special instance constructor. It commonly used in classes that contain static members only. In such a case other classes are not allowed  to create instances of this class. 

15.What are indexers? How to declare an indexer?

Indexers allow you to index a class or struct instance in the same way as an array.  Indexers are similar to properties except that their accessors take parameters.
     
Ex:    public int this [int index]
         {
                get { }
                set { }
         }

16.How exceptions are handled in C#?  
  

Exceptions are handled through “try/catch/finally” blocks.

When the exception occurs, the runtime searches for the nearest “catch” clause that can handle the exception.  The system will always executes the code in “finally” block regardless of whether an exception is thrown.

17.What is the output of the following program?

public interface Employee {
  int GetSalary();
  void GiveRaise(int amount);
}
public struct Clerk : Employee {
  private int salary;
  public Clerk(int salary) {
    this.salary = salary;
  }
  public int GetSalary() {
    return salary;
  }
  public void GiveRaise(int amount) {
    salary += amount;
  }
}

class Test {
  static void Main(string[] args) {
    Clerk c = new Clerk(1000);
    ((Employee)c).GiveRaise(50);
    System.Console.WriteLine(c.GetSalary());
 
  }
}


The answer is 1000.  Since the struct c cast to a reference, it involves boxing.  So a copy of c is made, and the GiveRaise method is called on that copy, not on the orginal object. 

18.What is “new” modifier and What is “new” operator?

“new” modifier is used to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.

“new” operator is used to create objects and invoke constructors.

19.
        What is the output of the following program?
      class Base
      {
        public Base() {
          Console.WriteLine("Base Const. Called");
        }
      }
      class Derived : Base
      {
            public static int i = b();
            public static int b() {
                  Console.WriteLine("Derived Static Call");
                  return 6;
            }
            public Derived() {
            Console.WriteLine("Derived Const. Called");
            }
     
      }
      class EntryPoint  {
         static void Main(string[] args)  {
            Base d = new Derived();
         }  


Derived Static Call
Base Const. Called
Derived Const. Called

It means that if there are variable initializers present, the runtime will call that before starting class initialization.

20. Is this program valid? If yes, what is the output of the following program?

class Test
{
   static int a = b + 1;
   static int b = a + 1;
   static void Main() {
      Console.WriteLine("a = {0}, b = {1}", a, b);
   }
} 

 
Despite the circular references of a and b, the program is valid. 
Its output is a=1, b= 2, because the static fields are initialized to 0 before their initializers are executed.


No comments:

Post a Comment