1: | Which general type of statement would you use to implement each of the following logical descriptions:
|
A: |
|
2: | Suppose that your program contains three variables called rainfall, wind, and temperature, all of type double. For each of the following points, write a few lines of code that implement the described logic (you don't need to write the whole program):
|
A: |
|
3: | No matter which value rainfall has, the following lines will always write "It's raining" onscreen. Why? if(rainfall > 10); Console.WriteLine("It's raining"); |
A: | The semicolon after the first line represents an empty statement, so the if statement could also be written as follows. If rainfall is greater than 0, the empty statement is executed; otherwise not. The last line is always executed. if(rainfall > 0) ; Console.WriteLine("It's raining"); |
4: | What is the purpose of nested if statements? |
A: | It allows a program to choose among more than two alternative branches. |
5: | Write logical expressions (using C#'s comparison operators and logical operators) that represent the following conditions:
|
A: |
|
6: | A method with the header bool IsEven(int number) returns true if the argument passed to its parameter number is even; otherwise, it returns false. Write a Boolean expression containing a call to IsEven that is true if the number passed along with the call is odd. |
A: | !(IsEven(someNumber)). |
7: | To what does the term scope refer? |
A: | Scope refers to the section of the source code where a particular variable identifier can be used. The scope of a variable begins at the point where it is declared and ends where the block in which it is declared terminates. The scope includes the blocks that might be nested inside this block. |
8: | What is one of the few correct uses of the goto statement? |
A: | To direct control to another switch section in a switch statement. |
9: | Rewrite the following if-else multibranch statement using a switch statement. if(timeOfDay == "morning") Console.WriteLine("Good Morning"); else if(timeOfDay == "midday") Console.WriteLine("Good Day"); else if(timeOfDay == "evening") Console.WriteLine("Good evening"); else Console.WriteLine("Invalid time"); |
A: | switch(timeOfDay) { case "morning": Console.WriteLine("Good Morning"); break; case "midday": Console.WriteLine("Good Day"); break; case "evening": Console.WriteLine("Good Evening"); break; default: Console.WriteLine("Invalid time"); break; } |
10: | To what does "falling through" in relation to the switch statement refer? How can "falling through" be prevented? |
A: | Falling through is when the flow of execution moves from one switch section directly to the next switch section. This can be prevented by either a break or a goto statement. |
11: | Your program contains variables called cost1, cost2, and minimumCost. Write a statement involving the conditional operator that assigns the smaller of the values in cost1 and cost2 to minimumCost. |
A: | minimumCost = (cost1 < cost2) ? cost1 : cost2; |
1: | The currency on planet Blipos is called sopilbs. The aliens on Blipos pay the following amount of tax depending on their income:
So if an alien earns 60000 sopilbs, it will have to pay the following amount in tax: 0 * 10000 + 0.05 * 15000 + 0.1 * 25000 + 0.15 * 10000 = 4750. Write a program that accepts a certain income as input and from that amount calculates and outputs the tax payable. |
A: | Exercise 1: using System; class TaxCalculator { public static void Main() { double income; double tax; Console.Write("Enter income: "); income = Convert.ToDouble(Console.ReadLine()); if(income <= 10000) tax = 0; else if((income > 10000) && (income <= 25000)) tax = (income - 10000) * 0.05; else if((income > 25000) && (income <= 50000)) tax = (15000 * 0.05) + ((income - 25000) * 0.1); else if((income > 50000) && (income <= 100000)) tax = (15000 * 0.05) + (25000 * 0.1) + ((income - 50000) * 0.15); else tax = (15000 * 0.05) + (25000 * 0.1) + (50000 * 0.15)+ ((income - 100000) * 0.2); Console.WriteLine("The tax is: {0}", tax); } } |
2: | Write a program (using a switch statement) that accepts one of the exam scores A, B, C, D, and E. In response, the program will output the corresponding percentage score, which is A: 90 100; B: 80 89; C: 70 79; D: 60 69; E: 0 59. |
A: | Exercise 2: using System; class ExamScoreConverter { public static void Main() { string score; Console.WriteLine("Enter score you wish to convert"); score = Console.ReadLine().ToUpper(); switch(score) { case "A": Console.WriteLine("90-100 percent score"); break; case "B": Console.WriteLine("80-89 percent score"); break; case "C": Console.WriteLine("70-79 percent score"); break; case "D": Console.WriteLine("60-69 percent score"); break; case "E": Console.WriteLine("0-59 percent score"); break; default: Console.WriteLine("Invalid exam result provided"); break; } } } |
3: | Write a program that can find a number between 0 and 100. The user decides on a number and the program repeatedly makes a guess and constantly narrows in on the number until the number is found. Hint: Let the computer guess on the number in the middle of the possible maximum number and the possible minimum number. For example, the first guess should be 50. If the user indicates the number to be greater, the next guess should be 75, and so on. |
A: | Exercise 3: using System; class NumberFinder { public static void Main() { int maxNumber = 100; int minNumber = 0; bool found = false; int guessCounter = 0; string reply; do { guessCounter++; Console.WriteLine("My guess is: {0}", ((maxNumber + minNumber) / 2)); Console.WriteLine("Enter H for H)igher or L for L)ower C for C)orrect?"); reply = Console.ReadLine().ToUpper(); if(reply == "H") minNumber = (maxNumber + minNumber) / 2; else if (reply == "L") maxNumber = (maxNumber + minNumber) / 2; else found = true; } while (!found); Console.WriteLine("I found the number in {0} guesses", guessCounter); } } |