2.8. Chapter Review
Which of these expressions are legal in C#? Think of the results. Explain.
"a" + "b" "a" + 'b' "a" + 2 2 + "a" "a" + 2 * 3; "a" + 2 + 3 2 + 3 + "a" 2 + 3 * "a"
Think first; try in csharprepl; reconsider if necessary.
Write a single
WriteLine
statement that would produce output on two separate lines, not one.What is printed?
Console.WriteLine("{1} {0} {2} {1} {0}", 'B', 2, "or not");
Which of these casts is necessary, and which could be left out (and be legal and mean the same thing)? Before testing, think what the values of the variables will be for the first two or three:
int x= (int)5.8; double y = (double)6; char c = (char)('a' + 1) int z = (int)'a' + 1;
Write a program file, call it add3.cs, that prompts the user for three numbers, not necessarily integers, and lists all three, and their sum, in similar format to:
using System; class GoodSum { static void Main() { Console.Write ( "Enter an integer: "); string xString = Console.ReadLine(); int x = int.Parse(xString); Console.Write( "Enter another integer: "); string yString = Console.ReadLine(); int y = int.Parse(yString); int sum = x + y; Console.WriteLine("They add up to " + sum); } }
Write a program that prompts the user for two integers, and then prints them out in a sentence with an integer division problem like:
The quotient of 14 and 3 is 4 with a remainder of 2.
Review remainders if you forget the integer division or remainder operator.