• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Saturday, August 5, 2017

C# Programming Interview Questions

 Abhishek Tomer     August 05, 2017     C#, Interview Question, Programming, Programs     No comments   

Today i am going to tell you some of the basic programming questions that are frequently asked in interview for fresher and experienced as well.

In this article you all are going to learn and remember all the scenarios used by me to learn the programming and achieve the aim so quickly.

So, let's start with the very basic question to print a star pattern.

Q1- Write a Program to print the Sandwich star pattern.
Ans: Here is the answer to  this question with the code

Code:
for (int i = 1; i <= 5; i++)           // this line tell for-loop to execute it 5 times, this loop is to print upper pyramid of star
{
      for (int j = 0; j < i - 1; j++)   // this loop is to print each space between each star(*) in a row
      {
           Console.Write(" ");
      }
      for (int k = 5; k >= i; k--)     // this loop is to print each star(*) in a row
     {
            Console.Write("* ");
      }
      Console.WriteLine("");        // this line breaks the line to the new row
}
for (int i = 1; i <= 5; i++)       // this loop is executed 5 times, and it is used to print the lower pyramid
{
    for (int j = 4; j >= i; j--)     // this loop again print the space between each star(*) in a row.
    {
        Console.Write(" ");
    }
   for (int k = 1; k <= i; k++)  // this loop is to print each star(*) in a row
   {
      Console.Write("* ");
    }
    Console.WriteLine("");
}
Console.ReadLine();            // ReadLine() is used to hold the output window untill user presses enter or return key


Star Sandwich Pattern
Star Sandwich Pattern
Here comes another question to print the star pattern in the different form

Q2- Write a Program to print a Left aligned Pyramid.
Ans: Here is the answer to  this question with the code

Code:
for (int i = 1; i <= 5; i++)           // this line tell for-loop to execute it 5 times, this loop is to print upper pyramid of star
{
   for (int j = 5; j >= i; j--)          // this loop is to print each star(*) in a row, decreases stars(*) as row increases
   {
       Console.Write("*");
    }
    Console.WriteLine("");
}
for (int i = 0; i < 5; i++)
{
   for (int j = 0; j <= i; j++)          // this loop is to print each star(*) in a row, increases stars(*) as row increases
   {
        Console.Write("*");
    }
    Console.WriteLine("");
}
Console.ReadLine();

Output:
Star pattern for Left Pyramid
Star pattern for Left Pyramid
This is the very important question for freshers to learn, and this question is asked 9 out of 10 interviews.

Q3- Write a Program to reverse a string without using predefined methods of C#.
Ans: Here is the answer to  this question with the code

Code:
Console.WriteLine("Please Enter the String"); // this line prints the message to enter a string to be reversed.
string input = Console.ReadLine();                 // this line is to accept the input from user and store it in a input named variable.
char[] rev = new char[input.Length];           // here, i have initialized a variable to type char Array of length of the input string.
for (int i = 0; i < input.Length; i++)            // this loop runs from 0th index to the (n-1)th index of the string
{
      rev[input.Length - 1 - i] = input[i];      // here, the first character in the input string is stored at the last index of char Array
}
Console.WriteLine(rev);                           // this line prints the reversed string

Console.ReadLine();                               // ReadLine() is used to hold the output window untill user presses enter or return key

Output:
Reversing a string without using predefined methods
Reversing a string without using predefined methods
So here are more similar questions as these are also asked in the interview

Q4- Write a Program to print a Factorial of a number.
Ans: Here is the answer to  this question with the code

Code:
Console.WriteLine("Please Enter the Number");
int number = Convert.ToInt32(Console.ReadLine());
int fact = 1;
if (number == 0)
{
   Console.WriteLine("factorial is 1");
}
else
{
   for (int i = number; i >= 1; i--)
  {
       fact = fact * i;
  }
  Console.WriteLine("Factorial is {0}", fact);
}
Console.ReadLine();

Output:
Factorial of a number without recursion
Factorial of a number

Q5- Write a Program to sort an Integer Array.
Ans: Here is the answer to  this question with the code

Code:
int[] a = { 2, 1, 8, 6, 4 };
for (int i = 0; i < a.Length; i++)
{
   for (int j = i + 1; j < a.Length; j++)
    {
        if (a[i] > a[j])
         {
             a[i] = a[i] + a[j];
             a[j] = a[i] - a[j];
             a[i] = a[i] - a[j];
          }
     }
}
for (int i = 0; i < a.Length; i++)
     Console.WriteLine(a[i]);
Console.ReadLine();

Output:
Sorting an Integer Array
Sorting an Integer Array
Q6- Write a Program to swap two numbers without using third variable.
Ans: Here is the answer to  this question with the code

Code:
Console.WriteLine("Enter First Number");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Second Number");
int b = Convert.ToInt32(Console.ReadLine());
if (b > a)
{
  a = a + b;
  b = a - b;
  a = a - b;
}
Console.WriteLine("\nSwapped numbers are {0} and {1}", a, b);
Console.ReadLine();

Output:
swaping two numbers without using third variable
swaping two numbers
Q7- Write a Program to print Fibonacci Series.
Ans: Here is the answer to  this question with the code

Code:
Console.WriteLine("How many numbers do you want in the fibonacci series");
int Target = int.Parse(Console.ReadLine());
int PreviousNumber = -1, NextNumber = 1;
for (int i = 0; i < Target; i++)
{
     int Sum = PreviousNumber + NextNumber;
    PreviousNumber = NextNumber;
    NextNumber = Sum;
    Console.Write(NextNumber + "  ");
}
Console.ReadLine();

Output:
Fibonacci Series
Fibonacci Series
Q8- Write a Program to check whether a number is prime or not.
Ans: Here is the answer to  this question with the code

Code:
Console.WriteLine("Please Enter the Number");
int num = Convert.ToInt32(Console.ReadLine());
int count = 0;
for (int i = 1; i <= num; i++)
{
   if (num % i == 0)
        count++;
}
if (count == 2)
{
   Console.WriteLine("This is the Prime Number");
}
else
{
   Console.WriteLine("This is not the Prime Number");
}
Console.ReadLine();

Output:
Program to check whether a number is prime or not
Program to check whether a number is prime or not
Q9- Write a Program to print even numbers up to the entered number
Ans: Here is the answer to  this question with the code

Code:
Console.WriteLine("Please Enter the last number");
int num = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i <= num; i++)
{
  if (i % 2 == 0)
  {
      Console.Write(i + " ");
  }
}
Console.ReadLine();

Output:
Program to print even numbers
Program to print even numbers
Q10- Write a Program to print small and capital Alphabets using Char variable.
Ans: Here is the answer to  this question with the code

Code:
for (char i = 'a'; i <= 'z'; i++)
{
     Console.Write(i + " ");
}
Console.WriteLine("");
for (char j = 'A'; j <= 'Z'; j++)
{
   Console.Write(j + " ");
}
Console.ReadLine();

Output:
Alphabets using Char variable
Alphabets using Char variable
Q11- Write a Program to print small and capital Alphabets using ASCII values.
Ans: Here is the answer to  this question with the code

Code:
for (int i = 0; i < 26; i++)
{
    Console.Write(Convert.ToChar(i + (int)'a') + " ");
}
Console.WriteLine("");
for (int i = 0; i < 26; i++)
{
   Console.Write(Convert.ToChar(i + (int)'A') + " ");
}
Console.ReadLine();

Output:
Alphabets using ASCII values.
Alphabets using ASCII values
Q12- Write a program to print all the elements of an array in forward and backward order using single loop. ARRAY is 1,2,3,4,5 output should be 1,2,3,4,5,5,4,3,2,1.
Ans: Here is the answer to  this question with the code

Code:
int[] arr = { 1, 2, 3, 4, 5 };
for (int i = 0; i < arr.Length * 2; i++)
{
    if (i < arr[arr.Length - 1])
    {
       Console.Write(arr[i]);
    }
   else
    {
       Console.Write(arr[(arr.Length * 2 - i) - 1]);
    }
}
Console.ReadKey();

Output:
Array in forward and backward order
Array in forward and backward order
Q13- Write a program to Revers a sentence word without any built-in function.
Ans: Here is the answer to  this question with the code

Code:
string Sentance = "welcome to c# programming";
string[] word = Sentance.Split(' ');
string reversSentance = "";
int length = word.Length - 1;
while (length >= 0)
{
  reversSentance += word[length] + " ";
  length--;
}
Console.WriteLine(reversSentance);
Console.ReadKey();

Output:
Reversing a sentence
Reversing a sentence
Q14- Write a program to Revers an integer Number.
Ans: Here is the answer to  this question with the code

Code:
int n = 12345;  //input
int rem = 0;
int revers = 0;
while (n > 0)
{
   rem = n % 10;
   revers = revers * 10 + rem;
   n = n / 10;
}
Console.WriteLine($"Revers Number Is:={revers}");
Console.ReadKey();

Output:
Revers an integer Number
Revers an integer Number
Q15- Write a program to Check given string is palindrome or not.
Ans: Here is the answer to  this question with the code

Code:
string s = "welcome";
string rev = string.Empty;
int length = s.Length - 1;
while (length >= 0)
{
   rev += s[length];
   length--;
}
Console.WriteLine($"Actual String:={s}");
Console.WriteLine($"Reversed String:={rev}");
if (s == rev)
{
   Console.WriteLine("String is palindrome ");
}
else
{
   Console.WriteLine("String is not palindrome ");
}
Console.ReadKey();

Output:
String is palindrome or not
String is palindrome or not
Q16- Write a program to Remove Duplicate Characters from a given string.
Ans: Here is the answer to  this question with the code

Code:
string s = "google";
string encounter = "";
string result = "";
foreach (char c in s)
{
   if (encounter.IndexOf(c) == -1)
   {
       encounter += c;
       result += c;
   }
}
Console.WriteLine($"Actual string is:= {s}");
Console.WriteLine($"Result is:= {result}");
Console.ReadKey();

Output:
Removing Duplicate Characters
Removing Duplicate Characters
Q17- Write a program to Sum of an integer Number.
Ans: Here is the answer to  this question with the code

Code:
int n = 12345;  //input
int rem = 0;
int revers = 0;
int sum = 0;
while (n > 0)
{
    rem = n % 10;
    revers = revers * 10 + rem;
    sum = sum + rem;
    n = n / 10;
}
Console.WriteLine($"sum of an integer Number Is:= {sum}");
Console.ReadKey();

Output:
Sum of an integer Number
Sum of an integer Number
Q18- Write a program to Find Length Of String Without using Length Function.
Ans: Here is the answer to this question with the code

Code:
string s = "Abhishek Tomer";
int x = 0;
foreach (char c in s)
{
   Console.Write(s[x]);
   x++;
}
Console.WriteLine($" result is:= {x}");
Console.ReadKey();

Output:
Length Of String
Length Of String
Q19- Write a program to Remove Spaces From String.
Ans: Here is the answer to  this question with the code

Code:
string s = "welcome to c#";
string rev = string.Empty;
int length = s.Length - 1;
for (int i = 0; i <= length; i++)
{
  if (s[i] == ' ')
  {
      rev += "";
  }
 else
  {
     rev += s[i];
  }
}
Console.WriteLine($"Actual String:={s}");
Console.WriteLine($"String after remove space:={rev}");
Console.ReadKey();

Output:
Remove Spaces From String
Remove Spaces From String
Summary:
So, Guys this was all about the interview programming questions in C#.
I hope you have enjoyed this post.
Please share this post with your friends and colleagues to help them clearing the interview.

For any queries please post a comment below.

Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • Navigation Menus in ASP.NET Core
    In this article, I am going to discuss how to create Responsive Navigation Menus in ASP.NET Core Application using bootstrap and JQuery. P...
  • ASP.NET Web API Basic Authentication
    In this article, I am going to discuss how to implement the ASP.NET Web API Basic Authentication step by step with an example. Please read...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Refresh Token in Web API
    In this article, I am going to discuss how to implement Refresh Token in Web API by validating the clients, as well as I, will also discus...
  • HTTP Client Message Handler in Web API
    In this article, I am going to discuss HTTP Client Message Handler in Web API with real-time examples. As we already discussed in HTTP Mes...
  • Anonymous Types in C#
    Hi friends! Today we are going to learn about Anonymous Types in C#. Let's start with the Introduction Anonymous is a type that does...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape