ENEL2CAH1 - Computer Methods 1
ENEL2CAH1 - Computer Methods 1
Section A
Answer on the multiple-choice sheet. Use an HB pencil only. Negative marking will be used. Correct (+2%), not attempted (0%), incorrect (-0.5%). This section is worth 40 marks. For each question choose the best of the available answers.
1.
If the following declarations are made: int a = 5, b = 11, c; What will be result of c = a += b++ - 22; a) -11 b) -6 c) -5 d) -7
e) None of these
3. a) 0 4. a) 5
e) 3
If p and q are declared as floats and q has the value 9, what is the result of the expression p = q++ * 0.5; b) 4.0 c) 4.5 d) 9 e) 9.0
5. What is the value of q after the execution of the expression in Question 4? a) 5.0 b) 10.0 c) 10.5 d) 9 e) 10
Study the following lines of code and answer the questions that follow: #include <stdio.h> main() { int TwoDArr[3][5] = {{0},{0}}; int i, j; i = 0; do { for (j = 4;j>0;j-=2) { TwoDArr[i][j] = i + j; } i++; } while (i<=2); printf("%d\n",i); printf("%d\n",j); return 0; } 6. How many times does the do while loop run before the first printf statement? (a) 2 (b) 4 (c) 3 (d) None of these 7. a) 2 What is the value of j printed in the second printf statement? b) -2 c) 4 d) 0
8. How many of the elements of the array have been changed as a result of the two loops? a) 15 b) 6 c) 12 d) 9
9. Which of the following statements is correct in order to print the value in the last element of the last row, given that this statement is inserted just before the return 0; statement? a) printf("%d\n",TwoDArr[i-1][j+4]); b) printf("%d\n",TwoDArr[i][j-4]); c) printf("%d\n",TwoDArr[i+1][j+2]); d) printf("%d\n",TwoDArr(3,5)); e) None of these
The markers $$10$$ to $$12$$ show where portions of the program have been omitted. /*This program sorts an array's values into ascending order */ #include <stdio.h> #define SIZE 10 int main(void) { $$10$$ a[SIZE] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37}; int i, pass, hold; printf("Data items in original order\n"); $$11$$ printf("%4d", a[i]); for (pass = 1; pass <= SIZE - 1; pass++) /* passes */ $$12$$ /* one pass */ if (a[i] > a[i + 1]) /* one comparison */ { hold = a[i]; /* one swap */ a[i] = a[i + 1]; a[i + 1] = hold; } printf("\nData items in ascending order\n"); for (i = 0; i <= SIZE - 1; i++) printf("%4d", a[i]); printf("\n"); return 0; } 10. a) c) e) 11. a) c) e) What is missing in position marked $$10$$ ? long b) int array d) float double What is missing in position marked $$11$$ ? for(i = 0; i <= SIZE + 1; i++) b) for(i = 0; i <= SIZE - 1; i++) for(i = 0; i <= SIZE; i++ ) d) for(i = 1; i <= SIZE; i++) for(i = 1; i <= SIZE - 1; i++)
12. a) c) e)
What is missing in position marked $$12$$ ? for(i = 0; i <= SIZE - 2; i++) b) for(i = 1; i <= SIZE - 2; i++) for(i = 0; i <= SIZE ; i++) d) for(i = 1; i <= SIZE ; i++) for(i = 1; i <= SIZE + 1; i++)
The following answers apply to the next five questions: a) Prototype b) Statement c) Expression d) Declaration e) Format string 13. Which of the above options best describe the following? scanf(%s,str1); Which of the above options best describe the following? char A[2] = {0}; Which of the above options best describe the following? n++ * (*p) Which of the above options best describe the following? int strlen(const char *str); Which of the above options best describe the following? %0.2f
14.
15.
16.
17.
18.
An until loop statement in a bash script will stop execution when the condition expression is true or false? a) True b) False c) positive d) negative e) non-zero 19. Which of the following is an invalid variable declaration in bash? a) p=33 b) pp=sup c) q=hello d) c=Comp Eng e) None of these 20. Which of the following lines of code redirects the output of a shell script go to a file called tmp? go is executable. a) ./go > tmp b) go > tmp c) go.sh > tmp d) go | tmp e) None of these
Write a C application that returns the dot product of two arrays. The main program defines the arrays. It then calls a function that returns the dot product. This code is provided below. The function must be prototyped as follows: float DotFunc( int*, int*, int); The function has three parameters: Parameter 1 is a pointer to the first element in an array of floats (vector) Parameter 2 is similar to parameter 1 but refers to the second array (vector) Parameter 3 is the number of elements in each array (both have the same size) The function returns a float that contains the dot product of the arrays.
#include<stdio.h> float DotFunc( int*, int*, int); #define SIZE 3 int main(void) { float x[SIZE]={1.0,2.3,3.2}; float y[SIZE]={4.1,5.0,6.5}; printf("\n The dot product of x and y is %f\n\n", DotFunc(x, y, SIZE)); return 0; }
Question 23
[10 marks]
Show how the regular expression a?[^0-9]{2}..R{0,1}.i+[mp-uhT]{2,2}m matches the entire word Algorithm (ie. a full word match)
Section C
Question 24
[30 marks]
Write a program that reads a C source file from the standard input and then searches the text for ANSI-C formatted comments and prints them to the standard output. Only comments are printed The program starts printing each new comment on a new line Comments that span several lines are also printed Example of program output follows: Assume that your program is called comment.c and it is complied to an executable called comment. Also assume that the C source file FirstProg.c contains the following text...
/* This is my first C program and this is my first header comment. It spans 3 lines!*/ #include<stdio.h> /* main function is now defined*/ int main(void) { printf("Hello World!"); /*this is an in-line comment*/ return 0; }
With both files in the current directory, we run the application as follows linux-prompt> more FirstProg.c | ./comment The following output is produced.
/* This is my first C program and this is my first header comment. It spans 3 lines!*/ /* main function is now defined*/ /*this is an in-line comment*/
Your program must behave in the same manner as described in the example above.
Operators
() + * + < == && || ?: = , += -= *= /= %= [] / <= != > >= ++ ++ -! * & (type)
Associativity
left to right right to left left to right left to right left to right left to right left to right left to right right to left right to left left to right
Type
highest unary multiplicative additive relational Equality logical and logical OR conditional assignment comma