Engineering Programming A- EC144
Engineering Programming A- EC144
Mid-Term Examination
Year 1, Semester II (20 14)
Paper Version A
September 20 14
Instructions to Candidates:
• Answer ALL questions.
• There are 24 MCQ questions.
• This paper contains 9 pages with Cover Page. No additional data is attached.
• Write your answers in this paper itself.
Unless otherwise stated, underline the most suitable answer.
1. What is a "variable"?
2. Select the INCORRECT statement when referring to: rules in naming a 'C' variable:
a) MyName
b) LastNwn
c) 4You
d) Percentage%
e) if
4. Ifwe want to increase the value in variable 'a' by 1, which of the following is NOTa valid
increment statement.
a) a+1;
b) a+=1;
c) a++;
d) ++a·
'
a) if (x=1) then
b) if (x=1){ ; }
c) if(x>=1); else;
d) if (x>=1) { ; }else { ; }
2
7. Inspect the following code:
#include <stdio.h>
void main(void){
int ~
//AJ;
for (i=O;i<lO;i=i+ 1) { //while (i<10){
printf("i=%d\n",i); II printf("i='>/od\n",i);
} II i=i+1;
//}
}
8. You have seen in class that the following code capitalizes the ASCII letter 'c' stored in
variable x
charx='c';
charcap_x;
cap_x =x -'a'+'A';
Now using this knowledge, and by looking at the code below, fmd the Answer that is NOT
correct.
chary='8', z;
z=y-'0';
z=z+1;
printf("z=%d\n'', z);
a) This code uses the fact that '0' appears before '8' in the ASCII table to convert the
ASCII character '8' to digit (munber) 8.
b) The output of this code will be 9.
c) The output of this code will be ASCII varue for z plus 1
d) We do not print z using %c, in this printf statement, because z is not a ASCII varue
for a printable character..
3
9. Inspect the following code and select the answer which is NOT correct.
#include <stdio.h>
void main( void) {
charch='a';
switch(ch) {
case 'a':
putchar(ch);
defauh:
putchar(ch);
}
printf(''\n');
}
#include <stdio.h>
void main( void) {
int i=5;
int j=3;
printf(''%d\n", ilj);
printf(''%d\n", i%j);
}
a) 5
3
b) 1
2
c) 1
3
d) 5
1
11. SCOPE and LIFETIME of variables refer to:
a) The visibility of a variable in your code, and how long the variable remains in use.
b) How useful a variable is, and how long it lives before being destroyed
c) The difficulty experienced in using a variable, and how your life expectancy was reduced
by it.
d) None of the above.
#inch.Kle <stdio.h>
void fl(void);
int F=3;
void main(int argc, char *argv[]){
int F=2;
fl();
printf("f=O/od\n", i);
}
void f1 (void) {
printf("f=O/od\n", i);
i=4;
}
#inch.Kle <stdio.h>
int i=4;
void main (void){
int i=6;
printf("f=O/od\n", i);
}
5
14. Choose the answer which is NOT correct.
a) because int i=6 is defmed in main, the global variable int i=4 is deleted by the
compiler.
b) because int i=6 is defmed in main, the global variable int i=4 is not accessible in
main(), but remains in memory.
c) The global variable's (i=4) lifetime is longer than the lifetime of the local variable's
(i=6) lifetime.
d) A global variable and a local variable declared in main() both have the same scope
within a 'C' program.
#include <stdio.h>
int fl(int);
void main (void){
int i=4;
fl(i); printf("Fl/od\n", i);
fl(i); printf("Fl/od\n", i);
}
int fl(int i) {
int k=3;
return(k+i);
}
a) 4 b) 7 c) 7 d) 3
4 7 10 7
17. The following statement is incorrect, with reference to the above code.
a) the variable kin fl() is created and destroyed each time the function is called.
b) the variable i in main() is never overwritten, as we do not assign the function fl()'s
return value to it.
c) the variable k is initialized to 3 each time fl() is called.
d) the variable i in main() is over written, each time function fl() returns.
6
Use the following code to answer the questions 18 and 19.
#include <stdio.h>
int fl(void);
. int i=3;
void main(int argc, char *argv[]){
int i=2;
printf("f=O/od\n", i);
i=fl();
printf("f=O/od\n", i);
i=fl();
printf("f=Oiod\n", i);
}
int fl (void){
static int i=6;
i=i-1;
return(i);
}
a) The variable 'i' has global scope and lifetime of the function fl()
b) The variable 'i' has scope only within fl(), but has the lifetime of the program.
c) The variable 'i' is destroyed each time the function fl() exits. f
d) The variable 'i' is initialized to 6 each time the function fl() is called.
Use the following code to answer the questions 20, 21, 22,23 and 24.
#include <stdio.h>
struct point {
intx;
int y;
};
struct point c _point( int xc, int yc);
void print_point(struct point p);
void main( void) {
struct point pl,p2,p3={3,4} ,p4={5,6};
pl=c_point{1,2);
p2=c_point(4,5);
print_point(p 1);
print_point(p2);
print_point(p3);
print_point(p4);
}
retwn(p);
}
20. In the above program, consider the code fragment listed below and mark the incorrect
statement:
struct point {
int x;
int y;
};
a) This is the structure defmition. There is no memory allocated for this statement.
b) This is how we allocate memory for 'int x' and 'int y'
c) This code needs to appear above any code that uses it.
d) The name after the keyword 'struct' ('point' in this case) is user defmed.
21. In the above program, consider the code fragment listed below and mark the incorrect
statement:
22. Select the incorrect statement with reference to this code calling the fimction print_point(pl)