Saturday, November 13, 2010

Value Labs written test questions

These are the questions in written test paper of Value Labs. Test conducted for 3-4 yrs Exp. dot net professionals. There are only 10 questions in the Question Paper.

1) Write a Class Car and write two properties Name and Make Year properties  inside it. Create a car list and add 5 cars to it and print each car's name and make year.

  public class Car
    {
        private string s_Name="";
        private int i_Year=0;

        public string Name
        {
            get { return s_Name; }
            set{s_Name=value;}
        }
   
        public int ModelYear
        {
        get{return i_Year;}
        set{i_Year=value;}
        }
    }

private void button1_Click(object sender, EventArgs e)
        {
            List<Car> MyList=new List<Car>();

            for (int i = 1; i <= 5; i++)
            {
                Car Obj = new Car();
                Obj.Name = "MyCar" + i.ToString();
                Obj.ModelYear = Convert.ToInt32("200" + i.ToString());

                MyList.Add(Obj);
            }

            foreach (Car item in MyList)
            {
                MessageBox.Show("Name :" + " " + item.Name.ToString() + "  " + "Model :" + "  " + item.ModelYear.ToString());
            }
        }


2)  Write the ways of converting String parameter into integer?

Actually, it is not possible to convert a string( which contains only characters) into int. However, if string contains only numeric values then you can convert it into int by using 

(i) Convert.ToInt32("-105");

(ii) Int32.Parse("-105");
try
{
    int m = Int32.Parse("abc");
}
catch (FormatException e)
{
    Console.WriteLine(e.Message);
}
// Output: Input string was not in a correct format. 
(iii)
// TryParse returns true if the conversion succeeded
// and stores the result in the specified variable.
int j;
bool result = Int32.TryParse("-105", out j);
if (true == result)
    Console.WriteLine(j);
else
    Console.WriteLine("String could not be parsed.");
// Output: -105

3) Write Try, Catch and Finally such that all blocks should be executed.

      private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MessageBox.Show("Try block executed");
                throw new Exception();
            }
            catch
            {
                MessageBox.Show("Catch block executed");
            }
            finally
            {
                MessageBox.Show("Finally block executed");
            }
        }

 4) How do you remove duplicate values in an array? write code piece..

  private void button1_Click(object sender, EventArgs e)
        {
            int[] a = new int[] {1,1,2,2,3,3,4,4};

            System.Collections.ArrayList al = new System.Collections.ArrayList();

            for (int i = 0; i < a.Length- 1; i++)
            {
                if (!al.Contains(a[i]))
                {
                    al.Add(a[i]);
                }
            }
              
           //Converting ArrayList to an Array        
            a = (int[])al.ToArray(typeof(int));


            for (int j = 0; j < a.Length; j++)
            {
                MessageBox.Show(a[j].ToString());
            }
        }     
5) How can you write the same method in Parent Class in Child Class without using virtual keyword?

You can do that by just hiding(shadowing in vb.net) the parent class method in child class using new keyword.

public class MyClass
    {
        public int MyMethod()
        {
            MessageBox.Show("MyMethod() in Parent Class");
            return 1;
        }
    }
    public class MyDerived : MyClass
    {
       new public string MyMethod()
        {
            MessageBox.Show("MyMethod() in Derived Class");
            return "";
        }
    }

6) Search item in Hash Table and print the value?


     private void button1_Click(object sender, EventArgs e)
        {
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            ht.Add("key1", "Value1");
            ht.Add("key2", "Value2");
            ht.Add("key3", "Value3");

            //Searching item in HashTable
            if (ht.ContainsKey("key3"))
            {
                MessageBox.Show(ht["key3"].ToString());
            }
        }

7) Can two methods overloading  like this? If not justify your answer..
       public int AddMethod(int a, int b, int c)
        { return a + b + c; }

        public string AddMethod(int a1, int b1, int c1)
        { return (a + b + c).ToString(); }

 Methods must differ with their signatures not with their return types. The above code gives compile error.

8) How do you show page hits for a particular page without considering postbaks? write code...

In the App_Code folder, add a class file (eg: AppGlobal.cs) and make that class as static. Declare a static variable within it which gives the no of hits of a particular page.

In the file App_Code/AppGlobal.cs

public static class AppGlobal
{
public static int iNoOfPageHits = 0;
}

In the file Default.aspx, Page_Load event write the following..

protected void Page_Load(object sender, EventArgs e)

{
if (!Page.IsPostBack)
{
AppGlobal.iNoOfPageHits += 1;
}
Response.Write(AppGlobal.iNoOfPageHits.ToString());
}


9) What is the output of this code..

Char White = 'W';
Char Grey = 'G';
Char MyChar;
for(int count=0; count<;5*2-1; count++)
{
 MyChar=White;

for(int j=2; j<;count; count++)
{
if( count % j == 0)
{
 MyChar=Grey;
}
Console.WriteLine(MyChar.ToString());
break;
}
}

10) What is the difference between Response.Write and Response.Output.Write? Write a code piece...

Response.Output.Write gives you String.Format-style output and Response.Write doesn't. 
Response.Output.Write has 17 overloaded methods which includes formatting. Response.Write has just 4 overloaded methods and none of them gives us formatting.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.