Search

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, June 1, 2023

Top interview questions on FOR LOOP with example programs in C#

What is a for loop in C#?

A for loop is a control flow statement in C# that allows you to repeatedly execute a block of code based on a specified condition.

What is the syntax of a for loop in C#?

The syntax of a for loop in C# is as follows:

for (initialization; condition; iteration)

{

    // code to be executed

}


What is the purpose of the initialization expression in a for loop?

The initialization expression is used to initialize the loop counter or any other variables before the loop starts executing.


What is the purpose of the condition expression in a for loop?

The condition expression is evaluated before each iteration. If the condition evaluates to true, the loop continues executing. If it evaluates to false, the loop terminates.


What is the purpose of the iteration expression in a for loop?

The iteration expression is executed after each iteration. It typically updates the loop counter or performs any necessary modifications.


Can you have multiple initialization expressions in a for loop?

Yes, you can have multiple initialization expressions in a for loop. They should be separated by commas.


Can you have multiple condition expressions in a for loop?

No, you can only have a single condition expression in a for loop. However, you can use logical operators (e.g., &&, ||) to combine multiple conditions.


Can you have multiple iteration expressions in a for loop?

Yes, you can have multiple iteration expressions in a for loop. They should be separated by commas.


What happens if the condition expression in a for loop is initially false?

If the condition expression is false initially, the loop will not execute at all.


Can you omit the initialization, condition, or iteration expressions in a for loop?

Yes, you can omit any of the expressions in a for loop. However, at least one semicolon (;) is required to separate the expressions.


How can you create an infinite loop using a for loop?

You can create an infinite loop by omitting the condition expression or by using a condition that always evaluates to true.


How can you exit a for loop before it has finished executing?

You can use the break statement within the loop to exit it prematurely based on a certain condition.


Can you nest for loops in C#?

Yes, you can nest for loops in C#. This allows you to create loops within loops to perform complex iterations.


How can you skip the current iteration and continue to the next iteration in a for loop?

You can use the continue statement within the loop to skip the current iteration and proceed to the next iteration.


What is the difference between a while loop and a for loop?

The main difference is that a for loop provides a compact way to specify the initialization, condition, and iteration expressions within a single line, while a while loop requires explicit statements for these expressions.


Can the loop counter variable be modified inside the loop body in a for loop?

Yes, the loop counter variable can be modified inside the loop body. However, you should be cautious when modifying it, as it may affect the loop's behavior.


Write a program to display numbers from 1 to 10 using a for loop.

for (int i = 1; i <= 10; i++)

{

    Console.WriteLine(i);

}


Write a program to calculate the sum of all numbers from 1 to 100 using a for loop.

int sum = 0;

for (int i = 1; i <= 100; i++)

{

    sum += i;

}

Console.WriteLine("Sum: " + sum);

Write a program to print the factorial of a given number using a for loop.

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

int factorial = 1;

for (int i = 1; i <= number; i++)

{

    factorial *= i;

}

Console.WriteLine("Factorial: " + factorial);

Write a program to check if a given number is prime or not using a for loop.

Console.Write("Enter a number: ");

int number = Convert.ToInt32(Console.ReadLine());

bool isPrime = true;

for (int i = 2; i <= Math.Sqrt(number); i++)

{

    if (number % i == 0)

    {

        isPrime = false;

        break;

    }

}

 

if (isPrime)

{

    Console.WriteLine("Prime number");

}

else

{

    Console.WriteLine("Not a prime number");

}

Write a program to print a pattern of asterisks using a for loop.

for (int i = 1; i <= 5; i++)

{

    for (int j = 1; j <= i; j++)

    {

        Console.Write("*");

    }

    Console.WriteLine();

}

No comments:

Post a Comment