0% found this document useful (0 votes)
19 views14 pages

Practice Questions II

The document provides a series of practice questions related to C# programming concepts, including methods for mathematical operations, console input/output, conditional statements, and loops. It explains the usage of various functions like Math.Max, Math.Min, and control flow structures such as if-else, switch, and loops (for, while, do-while). Additionally, it covers advanced topics like the break, continue, and goto statements, along with examples for each concept.

Uploaded by

yosefmuluye42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views14 pages

Practice Questions II

The document provides a series of practice questions related to C# programming concepts, including methods for mathematical operations, console input/output, conditional statements, and loops. It explains the usage of various functions like Math.Max, Math.Min, and control flow structures such as if-else, switch, and loops (for, while, do-while). Additionally, it covers advanced topics like the break, continue, and goto statements, along with examples for each concept.

Uploaded by

yosefmuluye42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Practice Questions II

Write the output of the following code

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);

6, int input = Console.Read();


Console.WriteLine(input );

Note : console. Write and console.writeline

Console.WriteLine("write your first name");


string ab = Console.ReadLine();
Console.WriteLine("write your last name");
string cd = Console.ReadLine();

Console.WriteLine("by using writeline");


Console.WriteLine(ab);
Console.WriteLine(cd);

Console.WriteLine("by using write only");


Console.Write(ab);
Console.Write(cd);

2
Console.ReadKey();

7, Console.Write("Enter a character: ");


char inputChar = (char)Console.Read();
Console.WriteLine("You entered: " + inputChar);
Note: The Read() method reads a single character from the standard
input stream (usually the keyboard) and returns the Unicode value of the
character.
Usage : Typically used when you need to read a single character from
the user's input.

8, Console.Write("Press any key: ");


ConsoleKeyInfo keyInfo = Console.ReadKey();
Console.WriteLine("\nYou pressed: " + keyInfo.KeyChar);
Description: Obtains the next key pressed by the user, optionally
displaying it in the console window.

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.

9, Console.Write("Enter your name: ");


string name = Console.ReadLine();
Console.WriteLine("Hello, " + name + "!");

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.

10. double a = 15.5;


int b = 14;
...
Console.Write("{0} + {1} = {2}", a, b, a + b);
// 15.5 + 14 = 29.5

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:

15.5 is substituted for {0}.


14 is substituted for {1}.
29.5 (result of a + b) is substituted for {2}.

Usage: This way of formatting strings is quite flexible and allows you to
construct complex output strings with placeholders for variables and
expressions.

11, int time = 22;


if (time < 10)
{
Console.WriteLine("Good morning.");
}
else if (time < 20)
{
5
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
}

Note: Use the else if statement to specify a new condition if the first
condition is False.

12, int time = 20;


if (time < 18)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
}
Note : There is also a short-hand if else, which is known as the ternary
operator because it consists of three operands. It can be used to replace
multiple lines of code with a single line. It is often used to replace simple if else
statements:

int time = 20;

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.

14 Multiple label in switch

char grade = 'B';

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.

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


{
if (i == 5)
break; // Exit the loop when i equals 5
Console.WriteLine(i);

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;

// More code here

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);
}

18, int count = 8;

do
{
Console.WriteLine("Count: " + count);
count++;
} while (count < 5);

Console.WriteLine("End of the loop.");

Console.WriteLine("By using while loop");

12
int a = 5;
while (a < 10){

Console.WriteLine(a);
a++;

Console.WriteLine("By using For loop");

for(int b = 5;b < 10 ; b++)


{

Console.WriteLine(b);

Console.WriteLine("By using do while loop");

int c = 11;
do
{

13
Console.WriteLine(c);
c++;

}
while (c < 10);

14

You might also like