Practice Questions II
Practice Questions II
1, Math.Max(5, 10);
Note: The Math.Max(x,y) method can be used to find the highest value
of x and y:
2, Math.Min(5, 10);
Note: The Math.Min(x,y) method can be used to find the lowest value of
of x and y:
3, Math.Sqrt(64);
Note: The Math.Sqrt(x) method returns the square root of x:
4, Math.Abs(-4.7);
Note: The Math.Abs(x) method returns the absolute (positive) value of
x:
5, Math.Round(9.99);
Note: Math.Round() rounds a number to the nearest whole number:
1
Math.Ceiling(9.29);
Math.Floor(9.99);
2
Console.ReadKey();
3
Usage : Useful for scenarios where you need to capture keypresses
without requiring the user to press Enter. It's commonly used for
console-based menu systems or interactive command-line applications.
Note: Reads the next line of characters from the standard input stream.
Usage: Used when you need to read a whole line of input from the user,
typically for text-based input like names, messages, or other textual data.
Note: In the Console.Write method call you provided, {0}, {1}, and {2}
are placeholders that are replaced by the values of a, b, and the result of
a + b, respectively. This method of formatting strings is known as
composite formatting in C#. Here's what each part means:
4
{0} is a placeholder for the first argument passed to Console.Write,
which is a.
{1} is a placeholder for the second argument passed to Console.Write,
which is b.
{2} is a placeholder for the result of the expression a + b.
So when you execute Console.Write("{0} + {1} = {2}", a, b, a + b);, it
will print out the formatted string with the values replaced accordingly:
Usage: This way of formatting strings is quite flexible and allows you to
construct complex output strings with placeholders for variables and
expressions.
Note: Use the else if statement to specify a new condition if the first
condition is False.
6
string result = (time < 18) ? "Good day." : "Good evening.";
Console.WriteLine(result);
13, Use the switch statement to select one of many code blocks to be
executed.
int day = 4;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
7
break;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
default:
Console.WriteLine("Looking forward to the Weekend.");
break;
}
Note : A break can save a lot of execution time because it "ignores" the execution
of all the rest of the code in the switch block.
A break can save a lot of execution time because it "ignores" the execution of all
the rest of the code in the switch block.
switch (grade)
{
case 'A':
8
case 'B':
case 'C':
Console.WriteLine("Pass");
break;
case 'D':
case 'E':
Console.WriteLine("Fail");
break;
default:
Console.WriteLine("Invalid grade");
break;
}
15, break:
The break statement is used to exit or terminate a loop or switch
statement prematurely.
When break is encountered inside a loop or switch statement, the control
immediately exits the loop or switch statement, and the program
continues execution from the statement following the loop or switch.
9
}
16, goto:
The goto statement is used to transfer control to a specified label within
the same method, switch statement, or loop.
It's generally considered bad practice to use goto because it can lead to
spaghetti code and make the code harder to understand and maintain.
int i = 0;
start: // Label
Console.WriteLine(i);
i++;
if (i < 5)
goto start; // Jump back to the label
start:
Console.WriteLine("This is the start of the program.");
10
// Some code here
goto end;
end:
Console.WriteLine("This is the end of the program.");
17, Continue:
The continue statement is used to skip the current iteration of a loop and
move to the next iteration.
When continue is encountered inside a loop, the remaining statements in
the loop body are skipped, and the control moves to the next iteration of
the loop.
for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
continue; // Skip even numbers
11
Console.WriteLine(i);
}
do
{
Console.WriteLine("Count: " + count);
count++;
} while (count < 5);
12
int a = 5;
while (a < 10){
Console.WriteLine(a);
a++;
Console.WriteLine(b);
int c = 11;
do
{
13
Console.WriteLine(c);
c++;
}
while (c < 10);
14