Three test questions

Source: Internet
Author: User

On MSDN, I saw three test questions. I feel very good. I will post them to see them:

There are three subjects in this test. The requirements are as follows:

1. Use C # to complete

2. You can search for documents by reading books or surfing the Internet. If any dishonest behavior is discovered, the company will not be admitted and the consequences will be borne by you.

 

Question 1:

1. Write a Bubble sorting program

Requirements:

1) use C # To compile a program for Bubble sorting,

2) DATA to be sorted is read from file C: \ DATA. DAT, and DATA is separated by commas.

Solution:

Using System;

Using System. IO;

Class Test {

Static void BubbleSort (int [] ai ){

For (int j = ai. Length-1; j> 0; j --) {// outer loop (backward)

For (int I = 0; I <j; I ++) // inner loop (forward)

If (ai [I]> ai [I + 1]) Swap (ref ai [I], ref ai [I + 1]);

}

}

Static void Swap (ref int x, ref int y ){

X = x + y;

Y = x-y;

X = x-y;

}

Static void Main (string [] args ){

Int [] ai = getData ();

BubbleSort (ai );

Foreach (int I in ai) Console. WriteLine (I );

Console. ReadLine ();

}

Public static int [] getData (){

Try {

StreamReader sr = new StreamReader ("data. dat ");

String sLine;

SLine = sr. ReadLine ();

String [] astr = sLine. Split (new Char [] {','});

Int [] ai = new int [astr. Length];

For (int I = 0; I <astr. Length; I ++)

Ai [I] = Convert. ToInt32 (astr [I]);

Return ai;

}

Catch (Exception e ){

// Let the user know what went wrong.

Console. WriteLine ("The file cocould not be read :");

Console. WriteLine (e. Message );

Return null;

}

}

}

 

2. Complete the following functions:

Public static string Left (string sSource, int iLength)

{

// Complete the function similar to the Left function of VB (return the Left iLength character string of the string sSource ).

}

Solution:

Using System;

 

Class Test {

Public static void Main (){

// Complete the function similar to the Left function of VB (return the Left iLength character string of the string sSource ).

Console. WriteLine ("Please enter a string to treaded :");

String s = Console. ReadLine ();

Int I = 0;

Bool bInputValid = false;

While (! BInputValid ){

Try {

Console. WriteLine ("Please enter the length from left most :");

I = Convert. ToInt32 (Console. ReadLine ());

If (I> s. Length) throw new Exception ("");

}

Catch (Exception e ){

Console. WriteLine ("It seen that your input data is not a integer or over flow, try again! ");

Console. WriteLine (e. Message );

Continue;

}

BInputValid = true;

}

Console. WriteLine ("The left part string is {0}", Left (s, I ));

}

Public static string Left (string sSource, int iLength ){

Char [] achar = new char [iLength];

For (int I = 0; I <iLength; I ++)

Achar [I] = sSource [I]; // you can directly use a single character in a string. The key is here.

String sLeftPart = new string (achar); // directly construct a string

Return sLeftPart;

}

}

 

2. cat and dog in animals (animals) both have bit actions. Use the object polymorphism Technology in. net to show the actions of cat and dog bites. C # code is required.

 

 


 

Solution:

Using System;

 

Public class animals {

// String sName;

Public virtual void bit (){

Console. Write ("I am animals ");

}

}

 

Public class dog: animals {

Public override void bit (){

Console. Write ("bit by a dag! ");

}

}

 

Public class cat: animals {

Public override void bit (){

Console. Write ("bit by a cat! ");

}

}

 

Public class Test {

Public static void Main (){

Animals [] aa = new animals [2];

Aa [0] = new dog ();

Aa [1] = new cat ();

Foreach (animals a in aa) a. bit ();

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.