0% found this document useful (0 votes)
6 views15 pages

Introduction to Programming ii-EC 141

Uploaded by

klaus2254
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6 views15 pages

Introduction to Programming ii-EC 141

Uploaded by

klaus2254
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

00042

I:.'..:=~~. . . . .
Sri Lanka Institute of Information Technology

BEng (Honours) Degree


.
m
Electronic Engineering

Mid-Term Examination
Year 1, Semester II (20 14)

EC 141 - Introduction to Programming II

Duration: 1.5 Hours

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

Each question carries TWO marks. Underline the correct answer.

Use the following code to answer Questions 1,2 and3:


#include <stdio.h>

int add_va1(int ~ int J);

int i=6;

void main( void) {


int i=2j=3;

{ intj=4;
i=add_ va1(iJ);
printf(("i=%d, j=Oiod\n",iJ);
}
printf(("i=%d, j=Oiod\n",iJ);
}

int add_va1( int ~ int J) {


printf(("i=%d, j=%d\n",iJ);
return(i+J);
}

1. Consider the fimction add val()

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

2. Consider the code above (that includes :fi.mction add_valO):

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.

3. This code produces the following output:

a) i=2 j=4, i=6 j=4, i=6 j=3.


b) i=6 j=3, i=6 j=4, i=6 j=3.
c) i=2 j=3, i=6 j=4, i=6 j=3.
d) i=6 j=3, i=6 j=4, i=6 j=3.

Use the following code to answer the questions 4 and 5.

#include <stdio.h>

int add_between( int al, int bl){


int~
int ans=O;

for(i=al; i<= bl; i++){


ans=ans+~
}
return( ans );
}

void main( void) {


int a=l;
int b=3;
int answer;

answer= add_between(a, b);


printf("Addition of %d to %d = %d\n", a, b, answer);
}

3
00042

4. Select the answer below that is incorrect:

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.

5. Select the answer below that is incorrect.

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()).

Use the following code to ans\\er the questions 6 and 7.

#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

6. The above code prints

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 assignment i=2 modifies the global variable.


b) The assignment i=2 modifies the local variable 'i' defined in main().
c) The assignment i=2 is lost when the fl() function exits.
d) ALL of the above are true.

8. 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 ofthe above.

9. Consider the following statement: ''The C language uses static typing as-apposed-to
dynamic typing rules".

a) This statement means there is a data type called 'static' in C


b) This statement means you have to decide on the data type your program is going
to use, befOre compiling.
c) This statement means that after you declare a C variable to be of a particular data
type, you may not use it with any other data type . (ie. casting is not allowed.)
d) ALL of the above are true.

5
00042

Use the following code to answer the questions 10 and 11.

#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);
}

10. The above code prints

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

11. Select one of the following ans~ers looking at function fl()

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

Use the following code to answer the questions 12 and 13.

#include <stdio.h>

int fl(int );

void main(void) {

int n=3
printf("answer of fl(%d)=%d\n", n, fl(n));
}

int f1 (int val) {


if( val=l){
return( 1 );
}else{
return( val+fl (val-l));
}
}

12. Select the correct statement.

a) fimction fl 0 is a recursive fimction


b) 1bis code works by reducing a problem to a stmller problem
c) 1bis type of code requires careful selection of the stopping condition
d) ALL of the above are correct.

13. Select the correct statement.

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

Consider the following code for questions 14 and 15.

int fl(int i) {
switch(i){
case 0:
return(O);
break;
case 1:
return( I);
break;
defauh:
return(fl(i-1 )+fl(i-2));
}
}

14. Select the Answer that is incorrect.

a) This code always returns 1 or 0


b) Tiris code returns the Fibonacci sequence 0,1,1,2,3,5,8,13 .. etc. when calling
fl(N); for N=0,1,2,3,4,5,6 ..
c) Tiris is a rectn"Sive fi.mctio n
d) This code only works for positive integers (N>=O)

15. Select the answer that is incorrect.

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]);

printf('FIRST : %d, %d, %d\n", *ptr_ ~ *ptr_j, *ptr_ k);


printf("SECOND : %d, %d, %d\n", *(ptr_i), *(ptr_i+l), (*ptr_i-t-3));
}

16. Select from the list below, what this code does NOT do.

a) Declare an array i(3] and initialize i(0]=3, i[1]==5, i[2]=7


b) Declare 3 pointers to int called ptr_ ~ ptrj, ptr_ k.
c) Assign the starting address ofarray 'i' to ptr_~ and the address ofi[1] (&i[1]) to
ptrj
d) Assign ptr_ k the starting address of array 'i'

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);
}

struct point c _point(int xc, int yc) {


struct point p;
p.x=xc;
p.y=yc;

return(p);
}

void print_point(struct point p){


printf("(%d,%d)\n", p.x, p.y);
}

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};

a) This is the section where we dec1are the structure variables.


b) variables dec1ared are pl,p2,p3,p4
c) 'struct point' also refers to a variable we dec .!are.
d) variables p3 and p4 are initialized by this code.

•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.

22. The code :

I structpoint c_point(int xc, int yc);


a) Is a function prototype. The actual function appears elsewhere in the code.
b) This code is only used at compile time.
c) Has to appear above any other code that refers to this function.
d) ALL of the above are true.

23. Select the incorrect statementre1ating to the code below:

struct point c_point( int xc, int yc) {


struct point p;
p.x=xc;
4;;
p.y=yc;
,, return(p);
}

a) Creates a local variable called 'p' of type 'structpoint'.


b) Even though p.x and p. y are assigned values, these values are not visible outside this
function().
c) Because this functions calls return(p) and allows a return type of 'struct point' the values in
the local variable p can be sent back to the caller before the local variable p is destroyed.
d) The return type of this function is 'struct'

11
00042

Question 1 (14 marks)


Write 2 small programs (that will compile) to print out the factorial of a number. For the frrst program use
an iterative approach, for the second use a recursive approach.

Note: Factorial of5 is written as follows:

5! =5 X 4 X 3 X 2 X 1
Part 1- Iterative Approach: (5 marks)

12
00042

Part 2 - Recursive Approach: (5 marks)

·,
.,

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;
};

structPerson *makenode(char *,char*, int);


void print_records( structPerson *top);

void main(void){

structPerson *ptr=NULL;
structPerson *head=NUIL, *tail=NULL;

ptr=makenode("name", "address", 101);


head=ptr;
tail=ptr;
head->next=NULL;

ptr=makenode(" name2", "add res s2", 102);


tail->next=ptr;
tail=ptr;

ptr=makenode("name3", "address3", 103);


tail->next=ptr;
tail=ptr;
print_records(head);

struct Person * makenode(char name[],char address[], int id){


structPerson *tmp=NULL;

tmp= (structPerson *) malloc(sizeof{struct Person));

strcpy(tmp->name, name);
strcpy(tmp->address, address);
tmp->id=id;
tmp->next = NUlL;
retum(tmp );

14
00042

void print_records( struct Person *top){


struct Person *tmp;

for( ){
printf("name =o/os\n", );
printf("address =%s\n", );
printf("Id =%d\n", );
printf( "----------------------------\n'1;
}

15

You might also like