Upload
vukhuong
View
213
Download
0
Embed Size (px)
Citation preview
CSIS1614 Main Half Year Examination 2015
SECTION A (76)
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. Write down True or False for each one of the following statements:
1.1 Debugging, in a programming context, refers to the act of removing jargon from the
source code. (1)
1.2 The basic unit of information that can be stored in memory is a byte. (1)
1.3 The latest version of the .Net framework is 3.5. (1)
1.4 A serial port is a physical interface through which information transfers in or out one byte
at a time. (1)
1.5 DVD-R is a rewritable storage medium. (1)
1.6 switch statements can only test an expression against constant values. (1)
[6]
2.1 Which one of the following is not an output device?
A. Keyboard
B. Printer
C. Screen
D. Disk drive
E. Speaker
2.2 Which one of the following denotes the content of a C# program that is human
understandable?
A. Source code
B. Object code
C. Assembler
D. Compiled version
E. Pseudocode
2.3 What is the base-3 equivalent of 5110?
A. 16 B. 122 C. 221 D. 1220 E. Not possible to determine
UNIVERSITY OF THE FREE STATE
MAIN CAMPUS
CSIS1614
DEPARTMENT: COMPUTER SCIENCE AND INFORMATICS
CONTACT NUMBER: 4012754
EXAMINATION: Main Half Year Examination 2015
ASSESSORS : Prof. P.J. Blignaut
MODERATOR : Dr. L. de Wet
TIME: 3½ hours MARKS: 154
(5 bonus marks)
CSIS1614 Main Half Year Examination 2015
2
2.4 Which one of the following statements about pseudocode is false?
A. High level description of an algorithm
B. Provides detailed instructions of how to solve a problem
C. Intended for human reading
D. Sketching out the structure of the program before coding
E. Is used as basis for implementing a solution
2.5 The scope of an object is determined by the place where it is …
A. assigned
B. called
C. declared
D. initialised
E. instantiated
2.6 Which one of the following prefixes is preferred for a text box?
A. tex B. text C. txt D. tb E. tbx
2.7 If a method does not define an access modifier its default accessibility is …
A. reference
B. public
C. private
D. protected
E. static
2.8 Which 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
E. Access modifier, sequence and return type
2.9 A company pays R5,000.00 bonuses at the end of the year to all employees who earn less than
R100,000. salary and bonus are both declared as decimal data types. Which of the
following selection statements would assign the correct amount to bonus?
A. if (salary < R100,000) bonus == R5,000;
B. if (salary < 100000) bonus = 5000;
C. if (salary <= 100000) bonus = 5000;
D. if (salary < 100000); bonus = R5,000;
E. if (salary < 100,000); bonus = R5,000;
2.10 What is another way of writing the following expression?
ans *= v1 - 1;
A. ans = v1 * ans - 1; B. ans = ans * v1--; C. ans = v1 * (ans - 1); D. ans = ans * --v1; E. ans = ans * v1 – 1; [10]
CSIS1614 Main Half Year Examination 2015
3
3. What does each of the following acronyms stand for or what does it refer to?
3.1 PSU
3.2 OOP
3.3 bit
3.4 CD-RW
3.5 HDD
3.6 .cs (file extension) [6]
4. Explain the difference between each one of the following pairs of terminology:
4.1 double and float (2)
4.2 Byte and byte (2)
4.3 Visual Studio and C# (2)
4.4 Events and event handlers (2)
4.5 Syntax errors and logic errors (2)
4.6 Pre-test and post-test loops (2)
4.7 Static methods and instance methods. (2)
4.8 Value parameters and reference parameters (2)
[16]
5. Identify the underlined element in each of the following lines of code. You must choose the
most specific item from the following list:
assignment, class, namespace, object, constructor call, equals, error, instantiate, property,
literal, scope, parameter, event, message, type, variable, method call
5.1 System.Windows.Forms.Button btnClose = new Button(); (1)
5.2 System.Windows.Forms.Button btnClose = new Button(); (1)
5.3 System.Windows.Forms.Button btnClose = new Button(); (1)
5.4 System.Windows.Forms.Button btnClose = new Button(); (1)
5.5 System.Windows.Forms.Button btnClose = new Button(); (1)
[5]
6. Provide the output of each of the following statements:
6.1 int x = 9; Console.WriteLine((x++).ToString()); (1)
6.2 int x = 3 / 4.0; Console.WriteLine(x.ToString()); (1)
6.3 int x = 12 % 5; Console.WriteLine(x.ToString()); (1)
6.4 int x = 5; x *= 3; Console.WriteLine(x.ToString()); (1)
6.5 int x = 11; x /= 5; Console.WriteLine(x.ToString()); (1)
CSIS1614 Main Half Year Examination 2015
4
6.6 int i = 3; do { Console.WriteLine(i); i--; } while (i >= 0); Console.WriteLine(i); (2)
[7]
7. Consider the following program fragment:
1 static void Main(string[] args) 2 { 3 const double dVAT = 14.0; 4 double dProfit = 25; 5 Console.Write("Cost price : "); 6 decimal mCostPrice = decimal.Parse(Console.ReadLine()); 7 decimal mRetailPrice = mCostPrice + mCostPrice * dProfit/100; 8 mRetailPrice = mRetailPrice + mRetailPrice * dVAT/100; 9 Console.WriteLine(mRetailPrice.ToString("C")); 10 Console.Write("Press any key to exit ..."); 11 Console.ReadKey(); 12 } //Main
7.1 Describe the general purpose of the program. (2)
7.2 What does static in line 1 mean? (1)
7.3 What does void in line 1 mean? (1)
7.4 What does const in line 3 mean? (1)
7.5 What is the purpose of the m-prefixes in lines 6 and 7? (1)
7.6 Why is the assignment in line 4 considered to be a bad programming practice? Rewrite
the line of code with one or more lines of proper coding. (4)
7.7 Why is the coding in line 6 not acceptable? Rewrite line 6 with one or more lines of
proper coding. (3)
7.8 Why does the compiler underline the parts of code in lines 7 and 8? Rewrite lines 7 and
8 to get rid of these underlinings. (2)
7.9 What is the purpose of the "C" in line 9? (1)
[16]
8. Develop a flowchart for the following scenario: The user must enter an integer and then the
system must respond with a message to indicate if the number is even or odd, positive or
negative. The user may not enter 0 or a non-integer, else he must enter the number again. [10]
NB. You must submit Section A before you may switch on the computer to do Section B.
CSIS1614 Main Half Year Examination 2015
5
SECTION B (83)
Make sure that you enter your name, student number and question number for every
question in a comment block at the top of the code window. You will not get marks if you
do it, but you will lose 3 marks for every question if you don’t do it.
Make sure that you give appropriate names to all controls and variables.
Note that this is not an open-book examination.
Question 1 (24)
Use CSEC to develop a Windows Forms application as in the screen print. Save your program as
T:\CSIS1614\Studentnumber_Surname_Question1.cs. The form must contain a label, a text box
and two buttons.
The user must enter a decimal number in the text box. When the Convert button is clicked, the
decimal number must be converted to binary and the result must be displayed in a message box.
Remember that the Text property of a text box takes a string value which should be parsed to a
numeric (e.g. int) before you can do calculations. No error handling is necessary. You may use a
string to represent the binary number. The Close button must terminate the application.
Hint: Remember that a decimal number can be converted to binary through continuous division by
2 until the number is zero. The remainders at each step can be concatenated to form the resulting
binary number, reading backward from the last result.
Example: 52 / 2 = 26 rem 0 26 / 2 = 13 rem 0 13 / 2 = 6 rem 1 6 / 2 = 3 rem 0 3 / 2 = 1 rem 1 1 / 2 = 0 rem 1 Thus: 5210 = 1101002 Notes:
Start with the label, text box and buttons.
Do not waste time to get the layout exactly as in the example. If it looks more or less as
above it is OK.
If you cannot do the conversion, display a message box with the message “Sorry, Prof. This
is too difficult for me!” (You will then loose 8 marks.)
CSIS1614 Main Half Year Examination 2015
6
Question 2 (20)
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.
Look at the following flowchart and create a console application from it. The parallelograms are
just normal messages displayed on the screen.
Start
How to fix
something
Does it move?Should it? Yes
Is it human?
It might be
an alien
No
Yes
Is it big?No
Is the wound
big?Yes
No
Send them
to Hospital,
you are not
a doctor
Give them a
band-aid
Yes
No
Use Q20 or
duct-tape
Yes
No
Does it look
fixable?
Find
something
else to fix!
Yes
No
Well that’s
just sad
End
CSIS1614 Main Half Year Examination 2015
7
Question 3 (18)
Create a new Console application in the folder T:\Studentnumber_Surname\ Question3. Name the
application Question3 and make sure that you check the “Create directory for solution” checkbox.
A geometric sequence is a sequence of numbers where each term after the first is found by
multiplying the previous one by a fixed non-zero number called the common ratio. For example, the
sequence 2, 6, 18, 54, ... is a geometric sequence with common ratio 3. Similarly, 10, 5, 2.5, 1.25,
... is a geometric sequence with common ratio ½. The sum of the terms of a geometric sequence is
known as a geometric series.
We know that there are formulas to do the job, but you are
requested to write a program that will make use of a loop to
calculate the sum of a geometric sequence. The program
must request the user to enter the first term (a), the common
ratio (r) and the number of terms (n).
Question 4 (21)
The volume of a cylinder can be calculated by using the formula:
Volume = pi radius2
height
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.
Write a method GetValue. The method should display a prompt to the user to enter a value
between a specified minimum and maximum until the acceptable value is received. The
method should have three parameters:
- A string value parameter that will be used as a prompt to the user;
- Two double parameters (Maximum and Minimum) that will be used to check that the user's
input is within the specified interval.
- Check for erroneous input as well.
The method should return a value of type double.
In the Main method of your program:
- Declare and initialise double variables for the Maximum and Minimum.
- Declare the variables for Volume, Radius and Height.
- Call the GetValue method for the Radius and Height variables.
- Calculate and display the volume of the cylinder according to the above formula.
Hint: You may use the System.Math.PI constant.
Look at the screen print below for an example.
CSIS1614 Main Half Year Examination 2015
8
MEMORANDUM Section A 1.1 False 1.2 False 1.3 False 1.4 True 1.5 False 1.6 False 2.1 A 2.2 A 2.3 D 2.4 B 2.5 C 2.6 C 2.7 C 2.8 A 2.9 B 2.10 D 3.1 Power supply unit 3.2 Object oriented programming 3.3 binary digit 3.4 Compact disk Rewritable 3.5 Hard disk drive 3.6 C#
4.1 double has higher precision and larger maximum and smaller minimum value than float.
double occupies 64 bits. float occupies 32 bits.
4.2 Byte is the original .Net class. byte is the C# alias for Byte.
4.3 Visual Studio is an IDE. C# is one of the programming languages that can be used in
Visual Studio.
4.4 Event occurs when something happens with an object, e.g. MouseMove
Event handler is a method that is executed when an event occurs.
4.5 Syntax errors occur when the rules of the language are transgressed.
Logic errors occur when program compiles but the output is wrong.
4.6 Pre-test: Condition tested before the loop is entered.
Post-test: Condition tested after loop execution.
4.7 Static method belongs to a class.
Instance method belongs to an object.
4.8 Value parameter: Value is copied from the actual to the formal parameter.
Reference parameter: Actual and formal parameters point to the same memory cell.
5.1 Namespace
5.2 Class
5.3 Object
5.4 Instantiate
5.5 Constructor call
6.1 9
6.2 Error
6.3 2
6.4 15
6.5 2
6.6 3
2
1
0
-1
CSIS1614 Main Half Year Examination 2015
9
7.1 Allow the user to enter cost price of an item and then display the retail price after profit and
VAT were added.
7.2 The method belongs to the class.
7.3 The method does not return a value.
7.4 The value for dVAT cannot change.
7.5 To indicate that the variables belong to decimal type.
7.6 Hard coding. Does not allow the user to change the profit percentage. Console.Write("Profit percentage: "); try { double dProfit = decimal.Parse (Console.ReadLine());} catch { MessageBox.Show("Invalid input."); } 7.7 No error checking. try { decimal mCostPrice = decimal.Parse(Console.ReadLine()); } catch { MessageBox.Show("Invalid input."); } 7.8 Cannot do * with decimal and double. decimal mRetailPrice = mCostPrice + mCostPrice * (decimal) dProfit/100; mRetailPrice = mRetailPrice + mRetailPrice * (decimal)dVAT/100; 7.9 Format display as currency.
8. 20 ½
Number % 2 == 0?
Stop
Yes
Msg = “Neg & Even”
No
Msg = “Pos & Odd”
Display
message
Number > 0?
No Yes
Msg = “Pos & Even”
Number > 0
Yes
Msg = “Neg & Odd”
No
Start
Read
number
Number is an
integer and != 0?
Yes
No
CSIS1614 Main Half Year Examination 2015
10
Section B
Question 1 (24)
public class CProgram { private static TextBox txtDecimal; //Main method public static void Main() { //Declare and instantiate form Form frmMain = new Form(); frmMain.StartPosition = FormStartPosition.CenterScreen; //Label Label lblDecimal = new Label(); lblDecimal.Parent = frmMain; lblDecimal.AutoSize = true; lblDecimal.Text = "Decimal value"; lblDecimal.Top = 10; lblDecimal.Left = 10; //Text box txtDecimal = new TextBox(); txtDecimal.Parent = frmMain; txtDecimal.Top = lblDecimal.Top + lblDecimal.Height; txtDecimal.Left = 10; txtDecimal.Text = "52"; //Test value for debugging //Button for conversion Button btnConvert = new Button(); btnConvert.Parent = frmMain; btnConvert.Text = "Convert"; btnConvert.Left = 10; btnConvert.Top = txtDecimal.Top + txtDecimal.Height; btnConvert.Click += btnConvert_Click; //Button to exit application Button btnExit = new Button(); //Declare and instantiate the button btnExit.Text = "&Close"; //Set the text to be displayed on the button btnExit.Left = frmMain.ClientSize.Width - btnExit.Width - 8; btnExit.Top = frmMain.ClientSize.Height - btnExit.Height - 8; btnExit.Click += btnExit_Click; btnExit.Parent = frmMain; //Run application and display form Application.Run(frmMain); } //End of Main //Event handler for Click event of btnExit private static void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } //End of btnExit_Click
CSIS1614 Main Half Year Examination 2015
11
private static void btnConvert_Click(object sender, EventArgs e) { int iNumber = int.Parse(txtDecimal.Text); int rem = 0; string sBin = ""; do { rem = iNumber % 2; iNumber = iNumber / 2; sBin = rem.ToString() + sBin; } while (iNumber != 0); MessageBox.Show(sBin); } //btnConvert_Click } //End of class
Question 2 (20)
class Program { static void Main(string[] args) { Console.WriteLine("How to fix something!"); Console.Write("Does it move?(Y/N) : "); if (Console.ReadLine().ToUpper() == "Y") { Console.Write("Should it move?(Y/N) : "); if (Console.ReadLine().ToUpper() == "Y") { Console.Write("Is it human?(Y/N) : "); if (Console.ReadLine().ToUpper() == "Y") { Console.Write("Is the wound big?(Y/N) : "); if (Console.ReadLine().ToUpper() == "Y") Console.Write("Send them to hospital. You are not a doctor!\n Find something else to fix!"); else Console.Write("Give them a band-aid!\nFind something else to fix!"); } else Console.Write("It mighht be an alien!\nFind something else to fix!"); } else Console.Write("It mighht be an alien!\nFind something else to fix!"); } else { Console.Write("Is it big?(Y/N) : "); if (Console.ReadLine().ToUpper() == "Y") { Console.Write("Does it look fixable?(Y/N) : "); if (Console.ReadLine().ToUpper() == "Y") Console.Write("Use Q20 or duct-tape.\nFind something else to fix!"); else Console.Write("Well that's just sad.\nFind something else to fix!"); } else Console.Write("Use Q20 or duct-tape.\nFind something else to fix!"); } Console.WriteLine("\n\nPress any key to exit..."); Console.ReadKey(); } //Main } //class
CSIS1614 Main Half Year Examination 2015
12
Question 3 (18)
class Program { static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.White; Console.Clear(); Console.ForegroundColor = ConsoleColor.Black; Console.WriteLine(); //Get parameters Console.Write("\ta = "); double a = double.Parse (Console.ReadLine()); Console.Write("\tr = "); double r = double.Parse (Console.ReadLine()); Console.Write("\tn = "); int n = int.Parse (Console.ReadLine()); //Set for first term double dSum = a; double t = a; //Step through remainder of terms for (int i = 2; i <= n; i++) { t = t*r; dSum += t; } //Display answer Console.WriteLine("\tSum of {0} terms: {1}", n, dSum); //Provide opportunity to read output Console.Write("\n\tPress any key to exit ..."); Console.ReadKey(); } //Main
CSIS1614 Main Half Year Examination 2015
13
Question 4 (21)
static void Main(string[] args) { Console.BackgroundColor = ConsoleColor.White; Console.Clear(); Console.ForegroundColor = ConsoleColor.Black; Console.WriteLine(); double dMax = 5.0; double dMin = 0.5; double radius, height, volume; //Input radius = GetValue("radius", dMin, dMax); Console.Write("\n"); height = GetValue("height", dMin, dMax); //Calculation volume = Math.PI * radius * radius * height; //Output Console.WriteLine(); Console.WriteLine("\tThe volume of the cylinder is {0:F}", volume); Console.WriteLine(); Console.Write("\tPress any key to exit ..."); Console.ReadKey(); //One for proper exit instruction } //Main static double GetValue(string sPrompt, double dMin_Value, double dMax_Value) { double dValue; bool isValid = false; do { Console.Write("\tGive the " + sPrompt + " of the cylinder between " + dMin_Value + " and " + dMax_Value + " : "); isValid = double.TryParse (Console.ReadLine(), out dValue); } while ((dValue < dMin_Value) || (dValue > dMax_Value) || !isValid); return dValue; } //GetValue