Name: Please fill in your Student Number and Name. Student Number : Student Number: University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ 2009 Supplementary Test 1 Question Max Mark Internal External 1 10 2 10 3 10 TOTAL 30 Marks : 30 Time : 40 minutes Instructions: a) Answer all questions. b) Write your answers in the space provided.
c) Show all calculations where applicable. 2
Question 1 [10] a) In computer hardware, what is a multicore CPU? [1] a CPU that can execute multiple instructions simultaneously [1] b) What is the purpose of the memory in a typical Von Neumann architecture? [1] store data and instructions while computer is turned on c) What is a variable? [1] location in memory that holds a value d) What is an algorithm? [1] an algorithm is any sequences of instructions to solve a problem [1] e) Why are modern computer programs not usually written in low level languages? [1] difficult for humans to understand [1] geared towards machines [1] f) Give 3 examples of low level programming languages. [2] assembler, machine code, bytecode [2 for all, 1 for 1 2, 0 for none] g) Describe the steps in the process of compiling and executing a Java program. [3] compiler compilers program into bytecode. [1] JVM translates bytecode into machine code. [1] Machine code is executed on CPU.[1] 3
Question 2 [10] Consider the following program and answer the questions that follow. import java.util.scanner; class test { public static void main ( String [] args ) { Scanner input = new Scanner (System.in); int a = input.nextint(); int b = input.nextint(); } } if (a / b == b / a) System.out.println ( yes ); else System.out.println ( no ); a) What is the output of this program if the input is the numbers 3 and 7? [1] no b) Give an example of the name of a variable, from the program. [1] a, b, input,... c) What real world hardware does the input object correspond to? [1] keyboard or keyboard buffer d) What is the purpose of the nextint method? [1] gets an integer from keyboard e) For what values of a and b will the program not work? [1] a=0 or b=0 f) In the if statement above, if the == symbol was converted to!=, what changes would be needed to maintain the meaning of the program? [1] swap the two output statements g) What is the dangling else problem? [2] When you have nested ifs without braces [1] and Java cannot match an else to its corresponding if [1] 4
h) Comments are missing in this program. Write out typical comments to be inserted at the top of the program. [2] // program to check equality of 2 numbers [1] // hussein suleman [1/2] // 12 march 2009 [1/2] 5
Question 3 [10] a) Briefly describe an algorithm to find the maximum of a set of 5 integers. You may write a textual description of the solution or a fragment of a program. Assume the numbers have been input already and are stored as variables a, b, c, d and e. [3] assume a is the maximum. Compare max with b and update max if necessary. Compare max with c and update max if necessary. Compare max with d and update max if necessary. Compare max with e and update max if necessary. b) Write the Java statement to input the first integer into the variable a. You may assume a is already declared as an int and there is already a Scanner object named input. [1] a = input.nextint(); c) Write the Java statement to calculate maximum as the maximum of the 2 floating point values a and b. You may assume a, b and maximum are already declared as float variables. [2] minimum = Math.max (a, b); [2] if (a>b) maximum=a; else maximum=b; [2] d) Write the Java statement to output The maximum is followed by the value of the variable maximum. [2] System.out.println ( The maximum is + maximum); e) Is your algorithm the most efficient solution? Explain why or why not. [2] Yes. It is possible to use different combinations of comparisons but any combination will involve the same amount of work. [marks for explanation no marks for yes/no] 6