FACULTY OF SCIENCE AND TECHNOLOGY SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1 ACADEMIC SESSION 2014; SEMESTER 3 PRG102D: BASIC PROGRAMMING CONCEPTS
Section A Compulsory section Question 1 (50 marks) a) Figure 1 Figure 1 shows the Java environment. Answer the following: i) What is the output of Java compiler? ii) What is X and Y? iii) What does X do? iv) Explain ONE benefit of writing a program using Java. (1 mark) (2 marks) (2 marks) (2 marks) b) Copy Table 1 to your answer booklet. Choose the best data types for each of the following so that no memory storage is wasted. Give an example of a typical value that would be held by the variable. Justify your choice. (15 marks) No. Item name Data type i) Car license plate ii) Car registration year iii) Car price iv) Car speed v) Car color Table 1 Justification for choosing the data type Item value 1
c) Identify THREE flaws in the following while statement. Explain how the flaws occur and provide a solution. (6 marks) public class flawedprogram { public static void main(string[] args) { int z = 0; int sum = 0; while (z >= 0) { sum += z; System.out.println(sum); d) What is the output from the following segment of code? (6 marks) public class DisplayObjects { public static void printobj(){ int a = 5; for (int i=1; i<=a;i++ ){ for (int j=5; j>=i;j-- ){ System.out.print("o"); System.out.println(""); public static void main(string[] args) { for (int cnt=1; cnt<3; cnt++){ printobj(); e) Determine whether each of the following is true or false. If false, please provide a reason or an explanation. (10 marks) i) The statement int[ ] idnum=new int[35]; reserves enough memory for exactly 34 integers. ii) When you create a StringBuilder object with an initial value of June, its capacity is 16. iii) You can improve loop performance when two conditions must both be true by testing for the most likely occurrence first. iv) The value of any false operation becomes true when preceded by the NOT operator. v) The statement if(fees <5.00 && fee >20.00) can be used to select fee values that are higher or lower than the specified limits. 2
f) Rewrite the following for statements as while statements, and provide the output. (6 marks) int sum; for (int i = 0; i < 5; i ++) { sum = 0; for (int j = 0; j <= i; j++) sum = sum + j; System.out.println(sum); 3
Section B Answer any TWO questions from this section. Question 2 (25 marks) a) Choose a word from the following box to complete the sentence. Write your answer in the answer booklet. (10 marks) decision nested float subscript reference precedence unary nested new while A(n) i) structure involves choosing between alternative courses of action based on some value within a program. A(n) ii) data type can hold floating-point values of up to six or seven significant digits of accuracy. When you combine mathematical operations in a single statement, you must understand operator iii), or the rules for the order in which parts of a mathematical expression are evaluated. A(n) iv) is an integer contained within square brackets that indicates one of an array s variables, or elements. As an object, a String variable name is not a simple data type. It is a(n) v) ; that is, a variable that holds a memory address. The prefix and postfix increment operators are vi) operators because you use them with one value. When loops are vii), each pair contains an inner loop and an outer loop. An inside block is viii), or contained entirely within, the outside block. To allocate the needed memory for a class object, you must use the ix) operator. A(n) x) loop is one in which the loop-controlling Boolean expression is the first statement in the loop. b) Write separate partial Java codes for each of the following tasks using an integer array mark of size 20: (15 marks) i) Get 20 integer data from user. (3 marks) ii) Find the highest and lowest value. (6 marks) iii) Count the number of negative and positive values. (6 marks) 4
Question 3 (25 marks) a) Complete the following sentences. Write your answers in the answer booklet. (9 marks) i), and display information in the command window. ii) iii) All programs can be written in terms of three types of control structures:,, and. The operator can be used to ensure that two conditions are both true before choosing a certain path of execution. iv) An array is a group of (called elements or components) containing values that all have the same. b) What does the following program print? (7 marks) 5
c) The following code has some syntactic and/semantic errors. The expected output has also been provided below. Analyze the given output and determine the problems. Fix it by re-writing the entire code. (9 marks) // Print every character between Unicode 65 and 122 // Start new line after 20 characters public class DebugQ3c { public static void main(string args[]) { char letter; int a; final int MIN = 65; final int MAX = 122; final int STOPLINE1 = 85; final int STOPLINE2 =!)%; for(a = MMAX; a <= MIN; a++) { letter = (char)a; System.out.print(" " + letter); if((a == STOPLINE) (a == STOPLINE2)) System.out.println(); System.out.println("\nEnd of application"); Expected output: 6
Question 4 (25 marks) a) Write an application that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. The expected output is as follows: (13 marks) Part (a): 1 2 3 4 Part (b): 1 2 3 4 Part (c): 1 2 3 4 Note that your expected output must use the following statements: Part (a): 1 2 3 4 use one System.out.println statement Part (b): 1 2 3 4 - use four System.out.print statements Part (c): 1 2 3 4 - use one System.out.printf statement b) Convert below pseudo code into Java statements enclosed by main method. (12 marks) Process_Input Display message Enter 1, 2, or 3, or 4 to Exit Get number from user while (number!= 4) If (number equal 1 or number equal 2 or number equal 3) Display message Good Job Else Display message You should enter 1,2,3, or 4 Display message Enter 1, 2, or 3, or 4 to Exit Get number from user EndWhile END ~ END OF PAPER ~ 7