C#程序员面试题20110212
伊克斯数码软件研发工程师招聘要求及能力测试C#
1.定义一个已排序数组int[] numarray = new int[]{1, 5, 10, 15, 18, 23, 45, 56},使用二分法算
法查找出45这个数。
2.在不生成临时string数组的前提下,将已有数组中的字符反向排列。(不要使用
Array.Reverse()方法)
注:以下题目均默认已正确引入命名空间
3.int[][] myArray3 = new int[3][] {new int[3]{5,6,2},new int[5]{6,9,7,8,3},new int[2]{3,2}}; 则,myArray3[2][2]的值是多少:
结果:
4.请给出以下程序的输出结果
namespace TestPaper
{
public class TestClass
{
public TestClass() {
_value = "Constructor Value";
}
public void OutputData() {
Console.WriteLine("_value = {0}", _value);
}
public string _value = "Default Initialize Value";
}
class Program
{
static void Main(string[] args)
{
TestClass test1 = new TestClass();
test1.OutputData();
TestClass test2 = new TestClass() { _value = "Initialize Value" };
test2.OutputData();
}
}
}
结果:
5.请指出以下程序错误
namespace TestPaper
{
class Program
{
static void Main(string[] args)
{
B b = new B();
b.fun();
}
}
class A
{
public int X{ private get;set; }
}
class B
{
protected A a = new A();
public void fun(){
a.X = 10;
Console.WriteLine(a.X);
}
}
}
结果:
6.请给出以下程序的输出结果
namespace TestPaper
{
class Program
{
static void Main(string[] args)
{
int x = 5, y = 8, z = 11;
Console.WriteLine(++x * --y + z++);
}
}
}
结果:
7.请给出以下程序的输出结果
namespace TestPaper
{
class Program
{
static void Main(string[] args)
{
int x = 1, y = 3, z = 5;
test(ref x, out y, z);
Console.WriteLine("x is {0},y is {1},z is {2}", x, y, z); }
static void test(ref int x,out int y,int z) {
x = 10; y = 5; z = 13;
}
}
}
结果:
8.请给出以下程序的输出结果
namespace TestPaper
{
class Program
{
static void Main(string[] args)
{
test();
}
static void test()
{
try{
throw new ArgumentException("参数异常"); }
catch (ArgumentException ex) {
Console.WriteLine("ArgumentException"); }
catch (Exception ex) {
Console.WriteLine("Exception");
}
finally{
Console.WriteLine("Finally");
}
}
}
}
结果:
9.请指出以下程序错误
namespace TestPaper
{
public class TestClass
{
public TestClass() {
_readonLyValue = 5;
_constValue = 5;
}
public void OutputData() {
_readonLyValue = 10;
_constValue = 10;
}
private readonly int _readonLyValue;
private const int _constValue;
}
class Program
{
static void Main(string[] args)
{
TestClass test1 = new TestClass();
test1.OutputData();
}
}
}
结果:
10.请给出以下程序的输出结果
namespace TestPaper
{
public class BaseClass
{
public void DisplayName() {
Console.WriteLine("BaseClass");
}
}
public class DerivedClass : BaseClass
{
public virtual void DisplayName() {
Console.WriteLine("DerivedClass");
}
}
public class SubDerivedClass : DerivedClass
{
public override void DisplayName() {
Console.WriteLine("SubDerivedClass");
}
}
public class SuperSubDerivedClass : SubDerivedClass
{
public new void DisplayName() {
Console.WriteLine("SuperSubDerivedClass");
}
}
class Program
{
static void Main(string[] args)
{
SuperSubDerivedClass superSubDerivedClass = new SuperSubDerivedClass();
SubDerivedClass subDerivedClass = superSubDerivedClass;
DerivedClass derivedClass = superSubDerivedClass;
BaseClass baseClass = superSubDerivedClass;
superSubDerivedClass.DisplayName();
subDerivedClass.DisplayName();
derivedClass.DisplayName();
baseClass.DisplayName();
}
}
}
结果:
11.请给出以下程序的输出结果
namespace TestPaper
{
class Program
{
static void Main(string[] args)
{
OperatorA a1 = new OperatorA();
OperatorA a2 = new OperatorA();
a1.Num = 7;
a2.Num = 6;
Console.WriteLine(a1 == a2);
}
}
class OperatorA
{
public int Num{ get ;set ; }
public static bool operator == (OperatorA a1, OperatorA a2){
return (a1.Num > a2.Num);
}
public static bool operator != (OperatorA a1, OperatorA a2){
return !(a1 == a2);
}
public override bool Equals(object obj){
return (this.Num > ((OperatorA)obj).Num);
}
public override int GetHashCode(){
return base.GetHashCode();
}
}
}
结果:
12.请指出以下程序错误
namespace TestPaper
{
public interface Interface1
{
void DisplayInfo();
}
public interface Interface2
{
void OutputData();
}
public class TestClass : Interface1, Interface2
{
public TestClass() {}
void Interface1.DisplayInfo() {
Console.WriteLine("This is DisplayInfo of Interface1");
}
public void OutputData() {
Console.WriteLine("This is OutputData of Interface2");
}
}
class Program
{
static void Main(string[] args)
{
TestClass test = new TestClass();
test.OutputData();
test.DisplayInfo();
}
}
}
结果:
13.请给出以下程序的输出结果
namespace TestPaper
{
public class AnotherClass
{
public AnotherClass(string str) {
_value = str;
}
public void TestOutput() {
Console.WriteLine("_value = {0}", _value);
}
private string _value;
}
public class BaseClass
{
public BaseClass(string value) {
Value = value;
}
public static explicit operator AnotherClass(BaseClass baseClass) { Console.WriteLine("Converting from BaseClass to AnotherClass");
return new AnotherClass(baseClass.Value);
}
public string Value { get; set; }
}
public class DerivedClass : BaseClass
{
public DerivedClass(string value): base(value) {}
}
class Program
{
static void Main(string[] args)
{
BaseClass baseClass = new DerivedClass("Hello");
if (baseClass is DerivedClass)
Console.WriteLine("baseClass is DerivedClass");
else
Console.WriteLine("baseClass isn't DerivedClass");
if (baseClass is AnotherClass)
Console.WriteLine("baseClass is AnotherClass");
else
Console.WriteLine("baseClass isn't AnotherClass");
AnotherClass anotherClass = (AnotherClass)baseClass;
anotherClass.TestOutput();
}
}
}
结果:
14.请给出以下程序的输出结果
namespace TestPaper
{
class Program
{
static void Main(string[] args)
{
Action
run(fun, 5, 10);
}
static void test(int x,int y){
Console.WriteLine(x + y * x);
}
static void run(Action
fun(x, y);
}
}
}
结果:
15.请指出以下程序错误
namespace TestPaper
{
class Program
{
static void Main(string[] args)
{
using (Test tt = new Test()){
tt.a = 100;
tt.b = "host";
Console.WriteLine("a is {0},b is {1}", tt.a, tt.b);
}
}
}
class Test
{
public int a = 0;
public string b = "";
public void Dispose(){
a = 0;
b = "";
}
}
}
结果:
16.请给出以下程序的输出结果
namespace TestPaper
{
struct Test
{
public int Value { get { return _value; } set { _value = value; } }
public Test (int v) {
_value = v;
}
private int _value;
}
class Program
{
static void Main(string[] args)
{
Test t1 = new Test(5);
object o = t1;
Test t2 = (Test)o;
t2.Value = 10;
Console.WriteLine(t1.Value);
Console.WriteLine(t2.Value);
Console.WriteLine(((Test)o).Value);
}
}
}
结果:
17.请指出以下程序错误
namespace TestPaper
{
public class TestGenericClass
{
public TestGenericClass () {}
public void Add(T data) {
_list.Add(data);
}
public void Add(T[] data) {
_list.AddRange(data);
}
public void OutputCount() {
Console.WriteLine("The items count of the instance is {0}",_list.Count);
}
private List
}
class Program
{
static void Main(string[] args)
{
TestGenericClass
testClass.Add(5);
testClass.Add(new int[]{10,100});
testClass.OutputCount();
}
}
}
结果:
18.请给出以下程序的输出结果
namespace TestPaper
{
class Program
{
static ManualResetEvent mainEvent;
static ManualResetEvent doworkEvent;
public static void DoWork() {
Console.WriteLine("DoWork() started...");
doworkEvent.Set();
mainEvent.WaitOne();
Console.WriteLine("DoWork() ending...");
}
static void Main(string[] args)
{
using (mainEvent = new ManualResetEvent(false))
using (doworkEvent = new ManualResetEvent(false)) {
Console.WriteLine("Application started...");
Console.WriteLine("Starting task...");
Thread task = new Thread(DoWork);
task.Start();
doworkEvent.WaitOne();
Console.WriteLine("Thread executing...");
mainEvent.Set();
task.Join();
Console.WriteLine("Thread completed");
Console.WriteLine("Application shutting down...");
}
}
}
}
结果:
19.请指出以下程序错误
namespace TestClass
{
public class TestClass1
{
public static string StaticFun(int a) {
return a.ToString() + " static";
}
public string Fun(int a) {
return a.ToString();
}
}
}
以上类生成一个Dll文件,位于..\TestClass.dll
namespace Test
{
class Program
{
static void Main(string[] args)
{
Assembly ass = Assembly.LoadFile(@"..\TestClass.dll"); //路径没错误 Type type = ass.GetType("TestClass1");
MethodInfo methodinfo = type.GetMethod("Fun");
object obj = ass.CreateInstance("TestClass1");
string result = (string)methodinfo.Invoke(obj, new object[] { 134 }); Console.WriteLine(result);
Console.Read();
}
}
}
结果:
20.请给出以下程序的输出结果
namespace TestPaper
{
class Program
{
private static string[] Keywords = {
"abstract","add*","alias*","as","ascending","base","bool","break","by*","byte"
};
private static void TestOutput() {
int delegateCounter = 0;
Func
text =>
{
delegateCounter++;
return text;
};
IEnumerable
= from keyword in Keywords where keyword.Contains('*') select func(keyword);
Console.WriteLine("1. delegateCounter = {0}", delegateCounter);
Console.WriteLine("2. Contextual Keyword Count = {0}", selection.Count());
Console.WriteLine("3. delegateCounter = {0}", delegateCounter);
Console.WriteLine("4. Contextual Keyword Count = {0}", selection.Count());
Console.WriteLine("5. delegateCounter = {0}", delegateCounter);
List
Console.WriteLine("6. delegateCounter = {0}", delegateCounter);
Console.WriteLine("7. selectionCache Count = {0}", selectionCache.Count());
Console.WriteLine("8. delegateCounter = {0}", delegateCounter);
}
static void Main(string[] args)
{
TestOutput();
}
}
}
结果: