130 likes | 143 Views
Top 20 C# Programming Interview Questions for SDET
E N D
Top20C#ProgrammingInterviewQuestionsForSDET 1.WriteaprograminC#tofindthesecondmostfrequentcharacterinstring“DevLabsAlliance”. usingSystem;namespaceDLA { publicclassSecondFrequentCharacter { staticintNO_OF_CHARS=256; publicstaticvoidMain(String[]args) { //StringtofindsecondfrequentcharacterStringstr="DevLabsAlliance"; charres=getSecondMostFreq(str);if(res!='\0') Console.Write("Secondmostfrequentcharis"+res); else Console.Write("Nosecondmostfrequentcharacter"); } staticchargetSecondMostFreq(Stringstr) { //countnumberofoccurencesofeverycharacterint[]count=newint[NO_OF_CHARS]; inti; for(i=0;i<str.Length;i++)(count[str[i]])++; //Traversethroughthecount[]andfindsecondhighestelement.intfirst=0,second=0; for(i=0;i<NO_OF_CHARS;i++) { /*Ifcurrentelementissmallerthanfirstthenupdatebothfirstandsecond*/ if(count[i]>count[first]) { second=first;first=i; } /*Ifcount[i]isinbetweenfirstandsecondthenupdatesecond*/ elseif(count[i]>count[second]&&count[i]!=count[first]) second=i; } return(char)second; } } }
Output:SecondmostfrequentcharisL WriteaprograminC#toReverseaString. usingSystem;namespaceDLA { publicclassReverseAString { publicstaticvoidMain(String[]args) { Strings="DevLabsAlliance";StringrevS=""; for(inti=s.Length-1;i>=0;i--) { revS=revS+s[i]; } Console.Write("Thereversedstringis"+revS); } } } Output:ThereversedstringisecnaillAsbaLveD WriteaprograminC#toreversetheorderofwordsinastring. usingSystem; namespaceDLA { publicclassReverseWordsInString { publicstaticvoidMain(String[]args) { Stringstr="LearnLeadAndSucceedinDevLabsAlliance";String[]a=str.Split(''); intk=a.Length-1;inttemp=k; //Stringrev=""; for(inti=k;temp>=0;k--) { Console.Write(a[temp]+""+''); --temp; } } } } Output:Thereversedstringis:DevLabsAllianceinSucceedAndLeadLearn
WriteaprograminC#tofindthefrequencyofcharacterinastring. usingSystem; namespaceDLA { publicclassFrequencyOfCharacter { publicstaticvoidMain(String[]args) { Stringstr="DevLabsAllianceisawesome.";charch='e'; intfrequency=0; for(inti=0;i<str.Length;i++) { if(ch==str[i]) { ++frequency; } } Console.Write("Frequencyof"+ch+"="+frequency); } } } Output:Frequencyofe=4 WriteaprograminC#tosortStringsinLexicographical(Dictionary)order. usingSystem;namespaceDLA{ publicclassSortStringInDictionaryOrder { publicstaticvoidMain(String[]args) { String[]words={"Ruby","C","Python","Java"};for(inti=0;i<3;++i) { for(intj=i+1;j<4;++j) { if(words[i].CompareTo(words[j])>0) { //swapwords[i]withwords[j]Stringtemp=words[i];words[i]=words[j]; words[j]=temp; } } } Console.Write("Inlexicographicalorder:");for(inti=0;i<4;i++) { Console.Write(words[i]); } } }
} Output:Inlexicographicalorder: C JavaPythonRuby WriteaprograminC#toreplacetheSubStringwithanotherstring. usingSystem; namespaceDLA { publicclassReplaceSubStringWithAnotherString { publicstaticvoidMain(String[]args) { Stringstr="Learn,LeadandSucceedinDevLabsAlliance";StringtoBeReplaced="in"; StringtoReplacedWith="with";Console.Write(str.Replace(toBeReplaced,toReplacedWith)); } } } Ouput:Learn,LeadandSucceedwithDevLabsAlliance WriteaprograminC#tofindthelargestvaluefromthegivenarray. namespaceDLA { publicclassLargestNumberInArray { publicstaticvoidMain(String[]args) { int[]arr={28,3,15,9,17,4,23,2}; intval=arr[0]; for(inti=0;i<arr.Length;i++) { if(arr[i]>val) { val=arr[i]; } } Console.Write("LargestvalueintheGivenArrayis"+val); } } } Output:LargestvalueintheGivenArrayis28
WriteaprograminC#tofindwhetherastringispalindromeornot. usingSystem;namespaceDLA { publicclassPalindrome { publicstaticvoidMain(String[]args) { Stringoriginal="nitin",reverse="";intlength; length=original.Length; for(inti=length-1;i>=0;i--) { reverse=reverse+original[i]; } if(original.Equals(reverse)) { Console.Write("Thestringispalindrome"); } else { Console.Write("Thestringisnotapalindrome"); } } } } Output:Thestringispalindrome WriteaprograminC#tofindtheduplicatecharactersinastring. usingSystem;namespaceDLA { publicclassDuplicateCharacters { publicstaticvoidMain(String[]args) { stringstr="DevLabsAlliance";intcount=0; char[]chars=str.ToCharArray(); Console.Write("Duplicatecharactersare:");for(inti=0;i<str.Length;i++) { for(intj=i+1;j<str.Length;j++) { if(chars[i]==chars[j]) { Console.Write(chars[j]);count++; break; } } } }
} } Output:DuplicateCharactersare:e al WriteaprograminC#tofindthesecondhighestnumberinanarray. usingSystem;namespaceDLA { publicclassSecondHighestNumberInArray { publicstaticvoidMain(String[]args) { int[]arr={14,46,47,94,94,52,86,36,94,89}; intlargest=arr[0]; intsecondLargest=arr[0]; for(inti=0;i<arr.Length;i++) { if(arr[i]>largest) { secondLargest=largest;largest=arr[i]; } elseif(arr[i]>secondLargest&&arr[i]!=largest) { secondLargest=arr[i]; } } Console.Write("Secondlargestnumberinanarrayis:"+secondLargest); } } } Output:Secondlargestnumberinanarrayis:89 WriteaPrograminC#tocreatepyramidofnumberswith5rows? usingSystem;namespaceDLA { publicclassPyramidWith5Rows { publicstaticvoidMain(String[]args) { intnumberOfRows=5,Space,number;Console.WriteLine("HereIsYourPyramid");
for(inti=1;i<=numberOfRows;i++) { for(Space=1;Space<=(numberOfRows-i);Space++)Console.Write(""); for(number=1;number<=i;number++)//increasethevalueConsole.Write(number); for(number=(i-1);number>=1;number--)//decreasethevalueConsole.Write(number); Console.WriteLine(); } Console.ReadLine(); } } } Output:HereIsYourPyramid 1 121 12321 1234321 123454321 12.WriteaprograminC#tocountoccurrencesofeachcharacterinastring. usingSystem;namespaceDLA { publicclassEachCharCountInString { staticintMAX_CHAR=256; publicstaticvoidMain(String[]args) { //Createanarrayofsize256i.e.ASCII_SIZEint[]count=newint[MAX_CHAR]; Stringstr="devlabsalliance"; intlen=str.Length; //Initializecountarrayindexfor(inti=0;i<len;i++) count[str[i]]++; //CreateanarrayofgivenStringsizechar[]ch=newchar[str.Length]; for(inti=0;i<len;i++) { ch[i]=str[i];intfind=0; for(intj=0;j<=i;j++) { //Ifanymatchesfoundif(str[i]==ch[j]) find++; } if(find==1) Console.Write("NumberofOccurrenceof"+str[i]+"is:"+ count[str[i]]); } } }
} Output:NumberofOccurrenceofdis:1NumberofOccurrenceofeis:2NumberofOccurrenceofvis:1NumberofOccurrenceoflis:3NumberofOccurrenceofais:3NumberofOccurrenceofbis:1NumberofOccurrenceofsis:1NumberofOccurrenceofiis:1NumberofOccurrenceofnis:1NumberofOccurrenceofcis:1 WriteaprograminC#tomoveallzerostotheendofarray. usingSystem;namespaceDLA { publicclasspushZerosToEnd { publicstaticvoidMain(String[]args) { int[]arr={1,9,8,4,0,0,2,7,0,6,0,9}; intn=arr.Length; intcount=0;//Countofnon-zeroelements //Traversethearray.Ifelementencounteredis //non-zero,thenreplacetheelementatindex'count' //withthiselementfor(inti=0;i<n;i++) if(arr[i]!=0)arr[count++]=arr[i]; //Nowallnon-zeroelementshavebeenshiftedto //frontand'count'issetasindexoffirst0. //Makeallelements0fromcounttoend.while(count<n) arr[count++]=0; Console.Write("Arrayafterpushingzerostotheend:");for(inti=0;i<n;i++) Console.Write(arr[i]+""); } } } Output:Arrayafterpushingzerostotheend:198427690000 WriteaprograminC#toprintFloyd’strianglewith5rows. usingSystem;namespaceDLA { publicclassFloydTriangleWith5Rows { publicstaticvoidMain(String[]args) { introws=5;inti,j,k=1; for(i=1;i<=rows;i++) {
for(j=1;j<i+1;j++) { Console.Write(k+++""); } Console.Write("\n"); } } } Output: 1 23 456 78910 1112131415 WriteaprograminC#toprintallprimefactorsofagivennumber. usingSystem;namespaceDLA { publicclassAllPrimeFactors { publicstaticvoidMain(String[]args) { intn=315; //Printthenumberof2sthatdividenwhile(n%2==0) { Console.Write(2+"");n/=2; } //nmustbeoddatthispoint.Sowecan //skiponeelement(Notei=i+2) for(inti=3;i<=Math.Sqrt(n);i+=2) { //Whileidividesn,printianddividenwhile(n%i==0) { Console.Write(i+"");n/=i; } } //Thisconditionistohandlethecasewhen //nisaprimenumbergreaterthan2if(n>2) Console.Write(n); } } } Output:3357 WriteaprograminC#toprintallprimenumberstill15. usingSystem;namespaceDLA { publicclassPrimeNumberTill15
{ • publicstaticvoidMain(String[]args) • { • intmaxNum=15; • //printingprimernumberstillthelimit(1to100)Console.Write("Printingprimenumberfrom1to"+maxNum);for(intnumber=2;number<=maxNum;number++) • { • //printprimenumbersonlyif(isPrime(number)) • { • Console.Write(number); • } • } • } • /* • Primenumberisnotdivisiblebyanynumberotherthan1anditself • * • @returntrueifnumberisprime • */ • publicstaticboolisPrime(intnumber) • { • for(inti=2;i<number;i++) • { • if(number%i==0) • { • returnfalse;//numberisdivisiblesoitsnotprime • } • } • returntrue;//numberisprimenow • } • } • } • Output:Printingprimenumberfrom1to152 • 3 • 5 • 7 • 11 • 13 • 17.WriteaprograminC#toreversedigitsofaninteger. • usingSystem;namespaceDLA • { • publicclassReverseDigits • { • publicstaticvoidMain(String[]args) • { • intnum=3689;intrev_num=0;while(num>0) • { • rev_num=rev_num*10+num%10;num=num/10;
} Console.WriteLine("Reverseofno.is"+rev_num);Console.ReadLine(); } } } Output:Reverseofno.is9863 WriteaprograminC#tofindtheanglebetweenhourandminutehandsofaclockat9.30. usingSystem;namespaceDLA { publicclassFindAngleinTime { publicstaticvoidMain(string[]args) { inthours=9;intmins=30; doublehourDegrees=(hours*30)+(mins*30.0/60);doubleminuteDegrees=mins*6; doublediff=Math.Abs(hourDegrees-minuteDegrees);if(diff>180) { diff=360-diff; } Console.Write("Theanglebetweenhourhandandminutehandis{0}degrees", diff); } } } Output:Theanglebetweenhourhandandminutehandis105degrees WriteaprograminC#toprintFibonacciSeries. usingSystem;namespaceDLA { publicclassFibonacciSeries { publicstaticvoidMain(string[]args) { intnumber=5;intf_0=0;intf_1=1;intsum; Console.Write("{0}{1}",f_0,f_1);for(inti=2;i<number;i++) { sum=f_0+f_1;f_0=f_1; f_1=sum; intvalue=f_0+f_1;Console.Write("{0}",value); }
} } } Output:01235 20.WriteaprograminC#tocheckwhether371isArmstrongnumberornot. usingSystem;namespaceDLA { publicclassArmstrongExample { publicstaticvoidMain(string[]args) { intr,sum=0,temp;intn=371; temp=n; while(n>0) { r=n%10; sum=sum+(r*r*r);n=n/10; } if(temp==sum) { Console.Write("ThisisArmstrongNumber."); } else { Console.Write("ThisisnotArmstrongNumber."); } } } } Output:ThisisArmstrongNumber.