UNIVERSITY OF THE FREE STATE DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS CSIS1614 DATE: 7 May 2015 MARKS: 130 ASSESSOR: Prof. P.J. Blignaut (Bonus marks: 5) MODERATOR: Dr. L. de Wet TIME: 180 minutes SECTION A (61) Answer the following questions in the answer book that is provided. The computer must be switched off while you are busy with Section A. 1.1 Which one of the following is not a method or method call? A. event handler B. constructor C. property D. Click() E. Main() 1.2 When a user is asked a question, Another one?, in a while loop to end execution of the loop, it is called a loop. A. state-controlled B. nested while C. sentinel-controlled D. counter-controlled E. infinite 1.3 Which one of the following are important with regard to parameters in a method call? A. Sequence, type and number B. Reference, number and type C. Return type, sequence and number D. Access modifier, return type and method name 1.4 How can the compound conditional expression ( (average > 79) && (average <= 89) ) be written to eliminate the logical operator, without changing the logic? A. remove the && and create a nested if statement B. (average > 79 <= 89) C. by replacing the && with D. (79 > average <= 89) E. (79 < average <= 89) 1.5 What value is stored in ianswer after the following expression is evaluated? int i = 6; int k = 3; int ianswer = 10; ianswer *= ++i + k++; A. 9 B. 10 C. 90 D. 100 E. 110
2 2. Explain the difference between each one of the following pairs of terminology: 2.1 Syntax errors and logic errors (2) 2.2 Step over and step into while debugging a program in Visual Studio (2) 2.3 Pre-test and post-test loops (2) 2.4 Static methods and instance methods (2) 2.5 public and private access modifiers (2) 2.6 Actual parameters and formal parameters (2) 2.7 Value parameters and reference parameters. (2) 3. Identify each of the underlined elements in the following lines of code. You may choose from the following list: assignment, access modifier, value parameter, constructor call, reference parameter, event, return type, variable, method call, method definition 3.1 private int Calculate (ref int inum1, int inum2) (1) 3.2 private int Calculate (ref int inum1, int inum2) (1) 3.3 private int Calculate (ref int inum1, int inum2) (1) 3.4 private int Calculate (ref int inum1, int inum2) (1) 3.5 private int Calculate (ref int inum1, int inum2) (1) 4. Write down the output of the following code: 4.1 static void Main(string[] args) int inum1 = 4; int inum2 = inum1; int ivalue = Calculate(ref inum1, inum2); Console.WriteLine("The result of the calculation of 0 and 1 is 2.", inum1, inum2, ivalue); private int Calculate(ref int _inum1, int _inum2) _inum1 *= _inum1; return _inum1 + _inum2; (3) 4.2 int a = 5; a %= 3; Console.WriteLine(a); (1) 4.3 int a = 1; int b = 1; Console.WriteLine(a!= b); (1) 4.4 int a = 1; int b = 1; Console.WriteLine(a <= b); (1) 4.5 double x = 3.0, y = 2.0; Console.WriteLine( (x + y x * y % 3).ToString() ); (1) 4.6 int x = 3, y = 2; Console.WriteLine(y%x); (1) 4.7 int a = 23; Console.WriteLine(a/5); (1)
3 5. Consider the following program fragment: 1 static void Main(string[] args) 2 3 string sf; 4 do 5 6 Console.Clear(); 7 Console.Write("Degrees Fahrenheit ('X' to exit) : "); 8 sf = Console.ReadLine(); 9 if (sf.toupper()!= "X") 10 11 double F; 12 if (double.tryparse(sf, out F)) 13 14 double C = (F - 32) / 1.8; 15 Console.WriteLine(F.ToString() + (char)176 + "F = " + C.ToString("0.0") + (char)176 + "C"); 16 17 18 Console.WriteLine("Invalid input"); 19 Console.Write("Press any key to enter another value... "); 20 Console.ReadKey(); 21 22 while (sf.toupper()!= "X"); 23 //Main 5.1 Describe the general purpose of the program. (2) 5.2 Why is sf declared in line 3 and not in line 8? (1) 5.3 What is the purpose of line 6? (1) 5.4 Explain in full what line 12 does. (4) 5.5 What is the purpose of (char)176 in line 15? (1) 5.6 What is the purpose of "0.0" in line 15? (2) 5.7 Explain the working of the do while ( ) structure as it is used in this program. What type of loop is this? (3) 5.8 Write down the exact output as printed by line 15 if the user enters 32 as input. (2) 5.9 What will the output be if the user enters "x" as input in line 8? (1) 5.10 What will the output be if line 20 is omitted? (1) 6. A prime number is an integer with no factors besides 1 and itself. Develop a flowchart to determine if a given number is a prime number. You may use Visio if you want to. (10) Dec 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F Bin 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 0 0000 16 1 0001 32 2 0010 sp! # $ % & ( ) * +, -.. / 48 3 0011 0 1 2 3 4 5 6 7 8 9 : ; < = >? 64 4 0100 @ A B C D E F G H I J K L M N O 80 5 0101 P Q R S T U V W X Y Z [ \ ] ^ _` 96 6 0110 ` a b c d e f g h i j k l m n o 112 7 0111 p q r s t u v w x y z ~ 128 8 1000, ˆ Š Œ 144 9 1001 š œ ž Ÿ 160 A 1010 ª «176 B 1011 ± ² ³ µ ¹ º» ¼ ½ ¾ 192 C 1100 À Á Â Ã Ä Å Æ Ç È É Ê Ë Ì Í Î Ï 208 D 1101 Ð Ñ Ò Ó Ô Õ Ö Ø Ù Ú Û Ü Ý Þ ß 224 E 1110 à á â ã ä å æ ç è é ê ë ì í î ï 240 F 1111 ð ñ ò ó ô õ ö ø ù ú û ü ý þ ÿ
4 NB. You must submit Section A before you may switch on the computer to do Section B. SECTION B (74) Create a folder for CSIS1614 on the T-drive. te that this is not an open-book test. Question 1 (18) Create a new Console application in the folder T:\Studentnumber_Surname\Question1. Name the application Question1 and make sure that you check the Create directory for solution checkbox. The application should allow the user to enter his name and age. Make provision for invalid input. Depending on the user s age, the program must then display a message as in the example below according to the following scheme: 0 12 : Child 13 19 : Teenager 20 25 : Young adult 26 65 : Adult > 65 : Pensioner Example: Name: John Age : 23 Good day, John! You are a young adult. Press any key to exit... Question 2 (10) Create a new Console application in the folder T:\Studentnumber_Surname\Question2. Name the application Question2 and make sure that you check the Create directory for solution checkbox. The application should calculate the squares and cubes of the numbers from 1 to 10 and prints the resulting values in table format, as shown below. 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 All calculations should be done in terms of a counter x of a for-loop. Ensure that the values in each column are right-aligned.
5 Question 3 (21) In Gymnastics and many other sports where judges award a subjective point for an activity, it is custom to disregard the lowest and highest points and allocate the average of the remaining points to the athlete. The user must be able to decide how many judges there are as long as it is at least three. Create a new Console application in Visual Studio in the folder T:\Studentnumber_Surname\ Question3. Name the application Question3 and make sure that you check the Create directory for solution checkbox. Consider the following flowchart and example output and develop the C# application to solve the problem. error handling is expected. Start Number of judges Number of judges >= 3? i = 1 Min=11 Max = -1 Total = 0 i<=number of judges? Enter point Subtract min Add point to total Subtract max Point>Max? Max = Point Calculate average Point<Min? Min = Point Display average i++ End
6 Question 4 (25) Create a new Console application in the folder T:\Studentnumber_Surname\ Question4. Name the application Question4 and make sure that you check the Create directory for solution checkbox. The application should calculate the area of two dimensional geometrical figures. You should first ask a user if he/she wants to calculate the area of a rectangle or circle. Depending on his/her choice, an appropriate method should be called to get the input parameters (radius or length and width), calculate and display the area. You should guard against repetition of similar lines of code. The formulas for the area of the respective figures must be implemented in different overloaded methods. The methods must receive the inputs through value parameters and return a double with the area of the figure. Make provision for invalid input.
7 MEMORANDUM Section A 1.1 C 1.2 A 1.3 A 1.4 A 1.5 D 2.1 Syntax errors occur when the rules of the language are transgressed. Logic errors occur when program compiles but the output is wrong. 2.2 Step over (F10) does not go into method when a method call is reached. Step into (F11) goes into a method when it is called. 2.3 Pre-test: Condition tested before the loop is entered. Post-test: Condition tested after loop execution. 2.4 Static method belongs to a class. Instance method belongs to an object. 2.5 Public methods are available to other classes also. Private method is only available in the class where it is declared. 2.6 Actual parameter: In the method call. Formal parameter: In the method header. Part of signature. 2.7 Value parameter: Value is copied from the actual to the formal parameter. Reference parameter: Actual and formal parameters point to the same memory cell. 3.1 access modifier 3.2 return type 3.3 method definition 3.4 reference parameter 3.5 value parameter 4.1 "The result of the calculation of 16 and 4 is 20." 4.2 2 4.3 False 4.4 True 4.5 5 4.6 2 4.7 4 5.1 Allow the user to enter degrees Fahrenheit and convert to degrees Celsius. 5.2 Scope of line 22 is outside the loop. 5.3 Clear the console window after previous entry. 5.4 Convert string variable to double if possible. Assign numeric value to F if possible, assign 0. Continue with conversion to Celsius if the string is valid numeric. 5.5 Prints the character in the UTF table with numeric value 176 ( ). 5.6 Formats the output to one decimal digit. 5.7 User can enter Fahrenheit values until he is done. Then enter "X" to exit the loop. Post-test, sentinel controlled loop. 5.8 "32 F = 0.0 C" 5.9 output. Program will exit. 5.10 User will not have opportunity to read output. Console will be cleared and user prompted for next input.
8 6. Start Read Number isprime = true ifactor = 2 isprime && Factor < SQRT(Number) isprime Number divisible by Factor Prime t prime Increment factor isprime = false End
9 Section B Question 1 (18) static void Main(string[] args) //Input Console.Write("Name: "); string sname = Console.ReadLine(); Console.Write("Age : "); try int iage = int.parse(console.readline()); Console.WriteLine(); //Output if (iage <= 12) Console.WriteLine("Good day, " + sname + "! You are a child."); if (iage <= 19) Console.WriteLine("Good day, " + sname + "! You are a teenager."); if (iage <= 25) Console.WriteLine("Good day, " + sname + "! You are a young adult."); if (iage <= 65) Console.WriteLine("Good day, " + sname + "! You are an adult."); Console.WriteLine("Good day, " + sname + "! You are a pensioner."); catch Console.WriteLine("Invalid input."); Console.Write("\nPress any key to exit..."); Console.ReadKey(); Question 2 (10) static void Main(string[] args) for (int x = 1; x <= 10; x++) Console.WriteLine(x.ToString().PadLeft(4, ' ') + "\t" + (x * x).tostring().padleft(4, ' ') + "\t" + (x * x*x).tostring().padleft(4, ' ')); Console.ReadKey(); //Main
10 Question 3 (21) Question 4 (25) static void Main(string[] args) Console.WriteLine(); //Get number of judges int n = 0; do Console.Write("\tNumber of judges: "); n = int.parse(console.readline()); while (n < 3); //Initialise int imin = 11; int imax = -1; int itotal = 0; //Get input from judges for (int i = 1; i <= n; i++) Console.Write("\tPoint " + i + " : "); int ipoint = int.parse(console.readline()); itotal += ipoint; if (ipoint > imax) imax = ipoint; if (ipoint < imin) imin = ipoint; //Subtract min and max and calculate average itotal = itotal - imax - imin; double davg = itotal * 1.0 / (n - 2); //Display output Console.WriteLine("\tFinal point : " + davg.tostring("0.0")); //Opportunity to read output Console.Write("\n\tPress any key to exit..."); Console.ReadKey(); //Main class Program static void Main(string[] args) Console.Write("Rectangle (R), Circle (C) :"); char cchoice = Console.ReadKey().KeyChar; Console.WriteLine(); Console.WriteLine(); switch (cchoice) case 'R': Rectangle(); break; case 'C': Circle(); break; Console.Write("Press any key to exit..."); Console.ReadKey(); //Main
11 private static double Area(double _dlength, double _dwidth) return _dlength * _dwidth; //Area private static double Area(double _dradius) return Math.PI * _dradius * _dradius; //Area private static void Rectangle() bool isvalid = true; double dwidth=0, dlength=0; string smsg; Console.Write("Width : "); isvalid = isvalid && double.tryparse(console.readline(), out dwidth) && dwidth > 0; Console.Write("Length : "); isvalid = isvalid && double.tryparse(console.readline(), out dlength) && dlength > 0; if (isvalid) double darea = Area(dLength, dwidth); smsg = string.format("area : 0:F2", darea); smsg = "Invalid input."; Console.WriteLine(sMsg); //Rectangle private static void Circle() bool isvalid; double dradius = 0; string smsg; Console.Write("Radius : "); isvalid = double.tryparse(console.readline(), out dradius) && dradius > 0; if (isvalid) double darea = Area(dRadius); smsg = string.format("area : 0:F2", darea); smsg = "Invalid input."; Console.WriteLine(sMsg); //Circle //class Program