BSc (Hons) Computer Science with Network Security/ BSc (Hons) Software Engineering/ BSc (Hons) Web Technologies Cohort: BCNS/16A/FT BSE/16A/FT- BWT/16A/FT Examinations for 2016 Semester 1 Resit Examination for BCN/15B/FT MODULE: PROGRAMMING CONCEPTS MODULE CODE: PROG1101C Duration: 3 Hours Instructions to Candidates: 1. Answer ALL questions 2. Questions may be answered in any order but your answers must show the question number clearly. 3. Always start a new question on a fresh page. 4. Use of calculator is allowed. 5. Total marks 100. This question paper contains 5 questions and 10 pages. Page 1 of 10
QUESTION 1: (20 MARKS) ANSWER ALL QUESTIONS a) Explain the terms : i. Machine Language ii. Assembly Language iii. High Level Language (3 Marks) b) The machine language for the Java Virtual Machine is called Java bytecode. Give one advantage of bytecode. c) Explain briefly the two ways used to solve a problem using an algorithm? d) A student scored 77 marks in English examination paper. From the algorithm below, give the award classification he got for this subject. Input mark If mark >= 80 Display 'Distinction Else If mark >= 60 AND mark<80 Display Merit Else If mark >=40 AND mark <60 Display Pass Else If mark < 40 Display Fail Page 2 of 10
e) Draw a flowchart to represent the algorithm for finding the average of eight numbers and the sum of the numbers. (5 Marks) f) Write a pseudo-code to describe an algorithm which will accept two numbers from the keyboard and calculate the sum and the division displaying the answer on the monitor screen. (8 Marks) Page 3 of 10
QUESTION 2: (20 MARKS) a) What is the difference between a primitive Java data type and a complex Java data type? Give an example of each. b) What is the difference between a float and a double in Java? c) You are writing a simple Java function to calculate the factorial of a number. The factorial of a number is defined as n! = 1*2*3*...*(n-2)*(n-1)*(n). i. What is the largest value of n whose factorial can be stored in an integer? ii. What happens if you try to store the result of a factorial of 1 greater than the n you answered in (2c i) above. d) What do you understand by the following terms i. A Java Statement. ii. A Java Expression. e) Rewrite the following Java instructions using compound statements. i. int result = result - 1; ii. int result = result / 3; f) What is the difference between a prefix increment and a postfix increment? Page 4 of 10
g) Given the following Java snippet, what is printed on the screen? int i = 10; i += 5; System.out.println(++i); System.out.println(i--%4); h) Write a simple Java program which prints numbers 1 to 100 inclusive on a separate line. For every multiple of 3 the program must print Fizz, instead of that number and for every multiple of 5 the program must print Buzz, instead of that number. For numbers which are multiples of both 3 and 5, the program must print FizzBuzz, instead of that number. (6 Marks) Page 5 of 10
QUESTION 3: (20 MARKS) a) What is the main difference between a Loop statement and IF statement? b) What are the types of loops that Java provide, give a brief explanation on each of them? (6 Marks) c) What do you understand by the term infinite loop? d) Explain the difference between the terms i++ and i-- used when doing loop statements? e) What is the output for the code snippet below? int coreint = 100; for (int i = 4; i> 0; i--) { coreint -= i; System.out.println(coreInt); (4 Marks) Page 6 of 10
f) What is the output for the code snippet below? public class do_while_loop { public static void main(string args[]){ int x = 1; do{ System.out.println("value of x : " + ((x * x)- 1)); x++; while( x < 5 ); g) What is the output for the below snippet of code? public class if_statement { public static void main(string args[]){ int x = 5; int y = 3; if (x > 0 && x < 5) { if (y!= 3){ System.out.println(x-y); else System.out.println(x + 2); else System.out.println(((x + y) * 3) - 2); (2 marks) Page 7 of 10
QUESTION 4: (20 MARKS) a) Explain clearly with a suitable example what is an array? b) Create an array named temperatures which will hold 7 temperatures in degrees centigrade. (3 Marks) c) In what way does an array differ from an ordinary variable? d) Given int[] numbers = {1,2,3,4,5; What is the output of the following? i. numbers[1] =? ii. numbers[5] =? e) Describe, with an example, how to access all elements of an array using a for loop? (3 Marks) f) Declare a two dimensional array called surname of data type String. The array should be initialize with Mr., Mrs., Ms. in the first bracket and with Smith and Jane in the second bracket. Write a java program such that it will display the output as shown below: Mr. Smith Ms. Jane (8 Marks) Page 8 of 10
QUESTION 5: (20 MARKS) a) What is the purpose of using functions (methods) in Java? b) How do you define a function (method) signature in Java? c) What is the return type of a Java function (method) which does not return anything? d) What is the purpose of the main() function (method) in a Java Program? (3 Marks) e) Given the following, what is the output when fun3(fun1(10), 2*5,30); is executed? int fun1(int a) { System.out.println(a); return (a+=10); double fun2(double a) { System.out.print(a); return (a/=5); void fun3(int a, int b, int c) { System.out.println(fun2(fun1(a*3+b+(c/4)))); (4 Marks) Page 9 of 10
f) You need to capture an integer followed by a string from the keyboard input. The integer is to be stored in a variable named productqty and the string is to be stored in a variable named productname. Fill in the appropriate Java instruction(s) at X and Y to achieve this. System.out.println( Enter quantity of product : ); X System.out.println( Enter product name : ); Y (4 Marks) g) You are writing a Java program which will enable a user to convert from miles per gallon (mpg) to liters per 100 km (l/100) and vice versa. You are given the following conversions 1 mile = 1.609 km and 1 gallon = 4.546 liters. Write a simple Java function which will convert from mpg to l/100. Do not use magic numbers, instead declare and use constants where appropriate. (5 Marks) ***END OF QUESTION PAPER*** Page 10 of 10