Introduction to Programming ii-EC 141
Introduction to Programming ii-EC 141
I:.'..:=~~. . . . .
Sri Lanka Institute of Information Technology
Mid-Term Examination
Year 1, Semester II (20 14)
September 20 14
Instructions to Candidates:
• 1his paper has two sections. Answer ALL questions.
• There are 23 MCQ questions in the first section. Underline the correct answer.
• There are two questions in the second section. Write your answer in the spaces
provided.
• 1his paper contains 15 pages with Cover Page. No additional data is attached.
1
00042
int i=6;
{ intj=4;
i=add_ va1(iJ);
printf(("i=%d, j=Oiod\n",iJ);
}
printf(("i=%d, j=Oiod\n",iJ);
}
a) This code does not compile, because the fimction add_val(~j) is being
called in the main program (befure the fimction declaration) ..
b) This code includes two entries fur fimction add_val(int ~ int j), which is not
allowed.
c) This code works fine. The first entry fur add_val(int ~ int j); is a fimction
prototype.
d) This code looks too weird to work.
2
00042
a) C language does not allow the declaration of variables outside a :fi.mction. So the
first declarations of variable i (int i=6;) is not allowed.
b) C language does not allow the use of { } without any statement such as furO or
whileO or ifO
c) The first declaration of i (int i=6;) is a global variable.
d) This code looks too weird to work.
#include <stdio.h>
3
00042
a) The fi.mction: add_between (int al, int bl) is called using: add_between(a, b);
this is allowed, as the variable names used in the caller do not need to match the
variable names in the called routine
b) There is no function prototype fur add_between(int al, int bl), this code does not
work
c) add_between( int al, int bl) does not require a prototype, as it's return type is int
d) add_between( int al, int b 1) does not require a prototype, because it has been
placed above the code that calls it.
a) ALL The variables in the fi.mction add_between(int al, int bl) are created only
when the fi.mction is called.
b) When the fi.mction add_between(int al, int bl) exits (returns), All the variables in
the fi.mction are deallocated, and the memory (the variables used) is freed.
c) The fi.mction add_between(int al, int bl) returns a value, which is made available
to the calling prograrr(main()), even though when one fi.mction exits all memory
used by the function is deallocated.
d) When the fi.mction add_between() finishes, it sends back the local variable 'ans' to
the caller (main()).
#include <stdio.h>
void fl(void);
int i=3;
void main(int argc, char *argv[]){
int i=4;
fl();
printf("i=%d\n", i);
void fl (void){
printf("i=%d\n", i);
i=2;
}
4
00042
a) i = 3, i=4
b) i=3,i=2
c) i=2,i=2
d) i = 3, i=3
7. In fimctio n fl 0
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 ofthe above.
9. Consider the following statement: ''The C language uses static typing as-apposed-to
dynamic typing rules".
5
00042
#include <stdio.h>
int fl(void);
int i=3;
void main(int argc, char *argv[]){
int i=2;
printf("i=«>lod\n", i);
i=fl();
printf("i=%d\n", i);
i=fl();
printf("i=«>lod\n", i);
}
int f1 (void) {
static int i=6;
i=i-1;
return(i);
}
a) i=2, i= 1, i=O
b) i=2, i=5, i=4
c) i=3, i=2, i=6
d) i=5, i=4, i=3
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.
d) The variable 'i' is initialized to 6 each time the function fl() is called.
6
00042
#include <stdio.h>
int fl(int );
void main(void) {
int n=3
printf("answer of fl(%d)=%d\n", n, fl(n));
}
a) 1bis code adds all positive mnnbers from 1 toN, where N is the mnnber we call
flO with.
b) 1bis code will produce and answer of6 when N=3
c) 1bis code will produce and answer of 5050, as Carl Friedrich Gauss fuund when
N=100
d) ALL of the above are correct
7
00042
int fl(int i) {
switch(i){
case 0:
return(O);
break;
case 1:
return( I);
break;
defauh:
return(fl(i-1 )+fl(i-2));
}
}
a) This code works by calling itself, with a smaller problem set than what it began with.
b) For this code to work, the ending conditions need to be precisely stated.
c) Once the ending conditions are met, the very last fi.mction call returns to the
previous caller, which in turn returns to its caller, when it has finished
processing.
d) Tiris code uses iteration to work.
8
00042
Use the following code to answer the questions 16, 17 and 18.
#include <stdio.h>
void main(void) {
int i[3]={3,5,7};
int *ptr-~ *ptr_j, *ptr-k;
ptr_i=i;
ptr_j=(i+l);
ptr_k=&(i[2]);
16. Select from the list below, what this code does NOT do.
17. This code prints the fullowing values fur the printf labeled 'FIRST'
a) 3,5,7
b) 3,4,5
c) 3,3,3
< d) 3,6,7
<
18. This code prints the following values fur the printf labeled 'SECOND'
a) 3,5,7
b) 3,5,lllldefined
c) 3,5,6
d) 3,4,6
9
00042
Use the following code to answer the questions 19, 20, 21,22 and 23.
#include <stdio.h>
struct point {
int x;
int y;
};
structpoint 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(pl);
print_point(p2);
print_point(p3);
print_point(p4);
}
return(p);
}
19. 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 memoiy 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.
10
00042
20. In the above program, consider the code fragment listed below and mark the incorrect statement:
structpoint pl,p2,p3={3,4},p4={5,6};
•t,..
21. Select the incorrect statement with reference to this code calling the function print_point(pl)
a) The function print_point() gets a copy of pl, and calls that copy p
b) The function print_point() gets a reference top 1, and calls that reference to the original
variable p
c) 'struct point p' in the function print_point() becomes unavailable when the function exits.
d) When the function print_point() gets called again a new copy of 'p' is created.
11
00042
5! =5 X 4 X 3 X 2 X 1
Part 1- Iterative Approach: (5 marks)
12
00042
·,
.,
13
00042
Question2 (4 marks)
Look at the following code and finish coding the function print_records() to print all the records in the
linked list.
#include <stdio.h>
#include <string.h>
#include <stdhb.h>
structPerson{
char name[80];
char address [80];
int id;
structPerson *next;
};
void main(void){
structPerson *ptr=NULL;
structPerson *head=NUIL, *tail=NULL;
strcpy(tmp->name, name);
strcpy(tmp->address, address);
tmp->id=id;
tmp->next = NUlL;
retum(tmp );
14
00042
for( ){
printf("name =o/os\n", );
printf("address =%s\n", );
printf("Id =%d\n", );
printf( "----------------------------\n'1;
}
15