Sunday, December 28, 2008
Sunday, November 30, 2008
CSharp Question
1. What is the name of the entry point function for all C# programs?
The main method is the name of the entry point function for all C# programs.
2. The C# data type int is a synonym for what CLR data type?
Data type int is a synonym for Int32 data type in the C#. (System.Int32)
3. In C# all primitive data types and user defined data types inherit from what super object?
They inherit from object.
4. How is the C# string class use of == different from all other classes?
All other classes will compare their identity comparison which will only return true if both references point to the same object. However, in string class, == is an overloading operator that compares the characters in the string. (not the reference values of them). Thus, if str1 = “abc” and str2=”abc”, str1 == str2 will return true even though str1 points to different object than str2.
5. What encoding does C# use for characters?
It uses 16-bit encoding. (Unicode)
6. What is the difference between the C# "ref" and "out" keywords when applied to method parameters?
The difference between the C# ref and out keywords when applied to method parameters:
- ref keyword is used to pass variable that have been initialized (have a value to it) by reference to a method, and the method will be able to modify the original variable in the caller.
- Out keyword is used to pass variable that have not been initialized ( does not have any value) by reference to a method, and the method will assign value to it.
In code:
Int a = 5;
Int b;
// If we want to pass a and b by reference
Method_Ref( ref a); // must use ref keyword because a have value
Method_Out( out b); // must use out keyword because b does not have value
7. What are the C# "checked" and "unchecked" keywords used for?
The checked keyword is used to control if the overflow-checking throws an exception. And, the unchecked keyword is used to control if the overflow-checking throws truncates bits.
8. What is the difference between a C# "using" directive and a C/C++ "#include" directive?
“using” directive helps the compiler locate a class that is used in this application. This class is predefined class that you can reuse. using directive identifies predefined classes or namespace that a C# application should be able to use. On the other hand, “#include” directive tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears. Thus, using directive only helps compiler to locate a predefined classes or namespace, while #include directive is like adding code to the program.
9. What is the difference between a C# struct and a C# class in terms of reference types and value types?
A struct in C# represents value type object. And, a class in C# is a reference type object.
10. Besides "public" and "private", what other two access modifiers can a C# class take?
Two other access modifiers are: “protected” and “internal”.
11. Why does C# use class destructors far less often that C++?
They have garbage collector (GC) mechanism.
12. In C#, are static methods accessed through a class name, an object name, or both?
It can only be accessed through class name only.
13. How do you make a C# class abstract (when you want to inherit from it but never implement it directly)?
Use the abstract modifier keyword in base class.
For example: public abstract void Draw(); // this method will not provide implementation.
14. How do you prevent a C# class from being used as a base class (inherited from)?
We need to use sealed modifier keyword.
15. C# does not support multiple inheritance. What C# mechanism allows you to have a semblance of multiple inheritance functionality?
C# mechanism that allows us to have a semblance of multiple inheritance is interfaces mechanism.
16. What are the two kinds of C# properties?
They are get and set.
17. Syntactically, what is the difference between calling a method and a property?
The difference is:
- calling a method: we need to use parentheses after method name.
ie: Console.WriteLine();
- calling a property: we don’t need to use parentheses after property variable.
ie: myClass.Name;
18. What is the approximate C# equivalent to a C++ function pointer?
The approximate c# equivalent to a C++ function pointer is delegate.
19. What C# keyword do you use to implement a variable length argument list?
C# keyword to implement a variable length argument list is params
20. In C# if you must use pointers, how do you do it?
We need to use unsafe keyword
Design and implement a C# class for a binary tree
download code here
binary tree class:
using System;
using System.Collections.Generic;
using System.Text;
namespace BinaryTree
{
class TreeNode
{
private TreeNode leftNode; // link to left child
private int data; // data stored in node
private TreeNode rightNode; // link to right child
// initialize data and make this a leaf node
public TreeNode(int nodeData)
{
data = nodeData;
leftNode = rightNode = null; // node has no children
} // end constructor
// LeftNode property
public TreeNode LeftNode
{
get
{
return leftNode;
} // end get
set
{
leftNode = value;
} // end set
} // end property LeftNode
// Data property
public int Data
{
get
{
return data;
} // end get
set
{
data = value;
} // end set
} // end property Data
// RightNode property
public TreeNode RightNode
{
get
{
return rightNode;
} // end get
set
{
rightNode = value;
} // end set
} // end property RightNode
// insert TreeNode into Tree that contains nodes;
// ignore duplicate values
public void Insert(int insertValue)
{
if (insertValue < data) // insert in left subtree
{
// insert new TreeNode
if (leftNode == null)
leftNode = new TreeNode(insertValue);
else // continue traversing left subtree
leftNode.Insert(insertValue);
} // end if
else if (insertValue > data) // insert in right subtree
{
// insert new TreeNode
if (rightNode == null)
rightNode = new TreeNode(insertValue);
else // continue traversing right subtree
rightNode.Insert(insertValue);
} // end else if
} // end method Insert
} // end class TreeNode
// class Tree declaration
public class Tree
{
private TreeNode root;
// construct an empty Tree of integers
public Tree()
{
root = null;
} // end constructor
// Insert a new node in the binary search tree.
// If the root node is null, create the root node here.
// Otherwise, call the insert method of class TreeNode.
public void InsertNode(int insertValue)
{
if (root == null)
root = new TreeNode(insertValue);
else
root.Insert(insertValue);
} // end method InsertNode
// begin preorder traversal
public void PreorderTraversal()
{
PreorderHelper(root);
} // end method PreorderTraversal
// recursive method to perform preorder traversal
private void PreorderHelper(TreeNode node)
{
if (node == null)
return;
// output node data
Console.Write(node.Data + " ");
// traverse left subtree
PreorderHelper(node.LeftNode);
// traverse right subtree
PreorderHelper(node.RightNode);
} // end method PreorderHelper
// begin inorder traversal
public void InorderTraversal()
{
InorderHelper(root);
} // end method InorderTraversal
// recursive method to perform inorder traversal
private void InorderHelper(TreeNode node)
{
if (node == null)
return;
// traverse left subtree
InorderHelper(node.LeftNode);
// output node data
Console.Write(node.Data + " ");
// traverse right subtree
InorderHelper(node.RightNode);
} // end method InorderHelper
// begin postorder traversal
public void PostorderTraversal()
{
PostorderHelper(root);
} // end method PostorderTraversal
// recursive method to perform postorder traversal
private void PostorderHelper(TreeNode node)
{
if (node == null)
return;
// traverse left subtree
PostorderHelper(node.LeftNode);
// traverse right subtree
PostorderHelper(node.RightNode);
// output node data
Console.Write(node.Data + " ");
} // end method PostorderHelper
// delete node function
// this will check the root first.
// then try to search the delete value in the tree
// if the node is found, delete the node based on three consideration
// 1 Deleting a leaf
// 2 Deleting a node with one child
// 3 Deleting a node with two children
public void DeleteNode(int deleteValue)
{
if (root == null)
return;
// check the root data
// delete root node if its data equals to delete value
else if (root.Data == deleteValue)
{
if (root.LeftNode == null)
root = root.RightNode;
else if (root.RightNode == null)
root = root.LeftNode;
else if (root.LeftNode != null && root.RightNode != null)
{
int data = getPredecessor(root.LeftNode);
delRecNode(root.LeftNode, data);
root.Data = data;
}
}
else
{
delRecNode(root, deleteValue);
}
}
// recursive function to delete node based on value
private bool delRecNode(TreeNode node, int value)
{
if (node == null)
return false; // do nothing
if (value < node.Data)
{
if(delRecNode(node.LeftNode, value))
{
// There are several cases to be considered in deletion:
// case 1 no children
if (node.LeftNode.LeftNode == null && node.LeftNode.RightNode == null)
node.LeftNode = null;
// case 2 node has 1 child
else if (node.LeftNode.LeftNode == null)
node.LeftNode = node.LeftNode.RightNode;
else if (node.LeftNode.RightNode == null)
node.LeftNode = node.LeftNode.LeftNode;
// case 3 node has 2 children
else if (node.LeftNode.LeftNode != null && node.LeftNode.RightNode != null)
{
int data = getPredecessor(node.LeftNode.LeftNode);
// delete precessor
delRecNode(node.LeftNode.LeftNode, data);
node.LeftNode.Data = data;
}
}
return false;
}
else if (value > node.Data)
{
if(delRecNode(node.RightNode, value))
{
// There are several cases to be considered in deletion:
// case 1 no children
if (node.RightNode.LeftNode == null && node.RightNode.RightNode == null)
node.RightNode = null;
// case 2 node has 1 child
else if (node.RightNode.LeftNode == null)
node.RightNode = node.RightNode.RightNode;
else if (node.RightNode.RightNode == null)
node.RightNode = node.RightNode.LeftNode;
// case 3 node has 2 children
else if (node.RightNode.LeftNode != null && node.RightNode.RightNode != null)
{
int data = getPredecessor(node.RightNode.LeftNode);
// delete precessor
delRecNode(node.RightNode.LeftNode, data);
node.RightNode.Data = data;
}
}
return false;
}
else if (value == node.Data)
{
return true;
}
return false;
}
// get the value of predecessor
private int getPredecessor(TreeNode node)
{
if (node.RightNode != null)
{
return getPredecessor(node.RightNode);
}
else
{
return node.Data;
}
}
}
}