UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING MODULE CODE: MODULE TITLE: ET4131 Introduction to Computer Programming SEMESTER: Autumn 2005 DURATION OF EXAM: LECTURER: 2.5 Hours Tom Newe INSTRUCTIONS TO CANDIDATES: Part A : - 30% (Multiple Choice) all 10 questions. Only one correct answer per question. All questions carry equal marks. Correct answer = 3% Wrong answer = 0% No answer = 0% Part B:- 40% (Written) 2 Questions only. All Questions carry equal marks. Operator precedence sheet attached. (% Lab Worth = 10%) and (% Mid Term = 20%) Questions on Exam Paper in Space provided. CALCULATORS NOT ALLOWED Student Name: Student ID: Course:
--------------------------------------PART A-------------------------------------- All Questions in Part A worth 3% each. (Total 30%) -------------------------------------------------------------------------------------- Q.1 What is the binary equivalent of 136 16? (a) 0010 1100 0110 (b) 0100 0011 0110 (c) 0001 0011 0110 (d) 0001 0111 1100 (e) 0100 0100 1110 Q.2 What is the Hexadecimal equivalent of 14 10? (a) C 16 (b) D 16 (c) E 16 (d) F 16 (e) None of the above. Q.3 What is the decimal equivalent of 10 16 (Hexadecimal)? (a) 15 10 (b) 16 10 (c) 17 10 (d) 18 10 (e) None of the above. Q.4 What does the term PSEUDO CODE refer to? (a) Mix of symbols and text to represent an algorithm. (b) Text based representation of an algorithm. (c) Graphical representation of an algorithm. (d) None of the above. (e) More than one of the above. Q.5 What does the term SYNTAX refer to in a programming language? (a) Correct use of punctuation/formatting. (b) How something is written. (c) How something is structured. (d) The meaning of a command. (e) More than one of the above. Page 1 of 13
Q.6 What is the result of the following expression? 10 4 + 3 * 2 (a) 0 (b) 12 (c) 18 (d) 6 (e) None of the above. Q.7 What is the result of the following expression? (10 + 4 2) * 3 % 5 (a) 4 (b) 3 (c) 2 (d) 1 (e) None of the above. The following program is used for questions 8, 9 and 10. /*--------------------------------------------------- * File Name :bits.c * Function :Bitwise operations on numbers. *--------------------------------------------------*/ #include <stdio.h> /* Standard I/O library */ void main(void) /* Main program, no return value*/ { /* Hex numbers */ int i = 0x1C, j = 0x20; } printf( i = (%d,0x%x), j = (%d,0x%x)\n\n, i, i, j, j); printf( i j = 0x%X\n, i j); printf( i & j = 0x%X\n, i & j); printf( i ^ j = %6d\n, i ^ j); printf( i << 3 = 0x%X\n, i << 3); printf( j >> 2 = 0x%X\n\n, j >> 2); printf( i >> 2 << 2 = 0x%X\n, i >> 2 << 2); printf( ~i & ~j i = 0x%X\n, ~i & ~j i); Continued over Page 2 of 13
Q8. What is displayed on the screen by the first printf statement? (a) i = (27,0x1C), j = (32,0x21) (b) i = (28,0x1C), j = (32,0x20) (c) i = (29,0x1C), j = (31,0x20) (d) i = (30,0x1C), j = (31,0x21) (e) None of the above. Q.9 What is displayed on the screen by the seventh printf statement? (a) i >> 2 << 2 = 0x1C (b) i >> 2 << 2 = 0x0C (c) i >> 2 << 2 = 0x1D (d) i >> 2 << 2 = 0x0B (e) None of the above. Q.10 What is displayed on the screen by the fourth printf statement? Note that represents a space. (a) i ^ j = 0x3C (b) i ^ j = 60 (c) i ^ j = 60 (d) i ^ j = 0x3C (e) None of the above. Page 3 of 13
--------------------------------------PART B-------------------------------------- All Questions in Part B worth 20% each 2 Questions only (Total 40%) --------------------------------------------------------------------------------------- Q.1 A Patio supplier supplies patio slabs on a cost per square metre (area) basis. Your task is to write a C program that allows the supplier to calculate the cost of a patio. This is to include the facility of calculating the cost of rectangular, circular and semicircle patterns where: Area of a rectangle = length * width Area of a Circle = PI * radius * radius Area of a semi-circle = (PI * radius * radius) / 2 and PI = 3.1415926 Your program is to loop continuously and keep a running TOTAL area of all areas calculated in that session until the user decides to end the session. {20%} Ans: Page 4 of 13
Page 5 of 13
Q.2 Write a C program to display a square on screen that is composed of a symbol. The length of the side of the square and the symbol it s composed of are entered as input from the keyboard. For example, if the input values are 4 and *, the output would be as follows: **** **** **** **** Your solution is to use at lease one function called square, which has the following function definition: void square(int length, char symbol); {20%} Ans: Page 6 of 13
Page 7 of 13
Q.3 Write a C program to read in measurements from a user. The entered measurements are in inches and your program is to convert the entered value into millimetres and re-display it on the screen. The conversion formula is as follows: 1 inch = 25.4 mm ex. 10 Inches = 254mm; Ans: The program is to continue looping until the user enters a value of -1 for inches. This will end the program. Your solution is to use at lease one function called convert, which has the following function definition: float convert(int inches); The return value from this function is the converted value in millimetres. Ex: milli = convert(10); // From this milli is equal to 254.00 {20%} Page 8 of 13
Page 9 of 13
Spare Sheet for Q: Page 10 of 13
Spare Sheet for Q: Page 11 of 13
Spare Sheet for Q: Page 12 of 13
Operator Priority Table : Operator Description Example Associativity ( ) Function call sqrt(x) L to R [ ] Array element ref val[2] -> Ptr to structure member mem_ptr -> name. Structure member ref member.no - Unary minus -10 R to L ++ Increment k++ ++k -- Decrement --count count--! Negation!number ~ One s complement ~0x2F * Pointer Indirection *ptr & Address of &x sizeof Size in bytes sizeof(float) (type) Cast/Type conversion (float) sum / n * Multiplication x * y L to R / Division x / y % Modulus x % y + Addition a + b L to R - Subtraction a - b << Left shift test << 4 L to R >> Right shift test > 3 < Less than x < 10 L to R <= Less than or equal to x <= 3 > Greater than f > 5 >= Greater than or equal to d >= 6 == Equal to count == 4 L to R!= Not equal to count!= 3 & Bitwise AND num & 0x10 L to R ^ Bitwise Exclusive OR num ^ 0x32 L to R Bitwise OR num 3 L to R && Logical AND x > 4 && t < 8 L to R Logical OR x == 3 f < 9 L to R?: Conditional Expression (t > j)? t : j R to L = *= /= %= Assignment j = 4; R to L += -= &= ^= j += 2; num &= 3; = <<= >>= num >>= 2, Sequence (comma) k = 0, g = 4; L to R Page 13 of 13