0% found this document useful (0 votes)
5 views

Introduction to Programming version D- EC 144

Uploaded by

klaus2254
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Introduction to Programming version D- EC 144

Uploaded by

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

Index Number:

Sri Lanka Institute of Information Technology

BEng (Honours) Degree


.
Ill

Electronic Engineering
Final Examination
Year 1, Semester II (20 14)

EC 144 - Introduction to Programming


Version D
------------l

ion: 2 Hours
------- _____ j
October 2014
Instructions to Candidates:
• This paper has 3 types of Questions. Answer ALL questions.
• There are 23 questions in the first part, j
o Underline the correct answer or answers for The 19 MCQ Questilms
-• (38 Marks)
o There are 4 questions that require a short written answer.
• (8 Marks)
• There is 1 short question where you have to write a small program. (4 Marks)
• This paper contains 15 pages with Cover Page.
• No additional data is attached.
• Write your answers on this paper itself.

10 total

l-
20

1 of 15
Section 1 - MCQ AND Short answer Questions.
Each question carries TWO marks.

For MCQ Questions, Indicate the correct answer(s) by clearly making a mark in front of your
selection(s).

For short answer questions, write your answer in the space provided.

l) The following code segment will print:

·.#include <stdio.h>
i •• •
, mt t=8;

void main(void){

printf("i=%d\n", i);
int i=4;
I
printf("i=%d\n", i);
.}
l.

A) i=8, i=4 J
B) i=4, i=8 I
C) i~,i=4
D) i=8, i=8

2) It's good practice to use functions in a 'C' program due to the following reasons:
A) functions allow us to write re-usable code so that we can write the code once, and
call it many times.
B) Use of functions allows the programmer to break the program into logical sections, so
that he or she is not forced to write all the code into the main routine.
C) functions Allow for easier maintainability of code, as only a function needs to be
changed, when there is a corresponding requirement change.
D) ALL of the above are true. ·

2 of 15
3) The following code will print:
,--·
:
1
#include <stdio.h>

void fl (int *);

void main(void){
intj=3;
fl(&j);
printf("%d\n", j);

void fl (int *jptr){


printf("%d\n", *jptr);
*jptr=6;
i}
I"------ --~-~--~- - - - ------------ --- . -

A) 3, 6
B) 3, 3
C) 6, 6
D) 6, 3

4) Consider the following code segment and select the INCORRECT statement from the
options given below:

\#include <stdio.h>
'

i void main(void){
char a[ I O]="test";
char b[ 1O]="nullify";
char *i,*j;

for(i=a, j=b ; (*j++ = * i++)! ='\0' ; );

printf("%s\n", b);
}
--- ·----------

A) This code copies the contents of array a[] to array b[].


B) a is the starting address of array a[], b is the starting address of array
b[]. pointers i and j are set to a and b respectively.
C) The code works by copying *i to *j then moving i to i=i+ I and j to j=j+ 1
D) This code will not work, as the third section of the for loop is empty.

3 of 15
::

5) Write a small program to copy the contents of array a[] to b[] by filling the underlined space
in the code stub below.

#include <stdio.h>

void main(void){
char a[ 1O]="test";
char b[ 1O]="nullify";

inti;

for(i=O ;(' - - - - --~


)!='\0'; i++);

printf("%s\n", b);
}

6) Select All that apply with respect to Non-static Variables declared inside a function:
A) They are created only. when the function is called.
B) They are destroyed when the function exits.
C) They can be initialized in their declaration statement.
D) ALL of the above are true.

Use the following program to answer questions 7,8,9 and 10. Some parts of this code, that
are relevant to each question, are reprinted next to the question.

!#include <stdio.h>
#include <string.h>

struct Person {
char name[ 100];
char address[ 100];
int id;
};

void print_person(struct Person p );


struct Person create_person_rec(char name[], char address[], int id);

void main(void){
struct Person p 1,p2,p3;

p 1=create_person_rec("name1 ", "address 1", 101);


p2=create_person_rec("name2", "address2", 102);

print_person(p 1);
print_person(p2);
}

4 of 15
struct Person create_person_rec(char name[], ch~~-~ddr~~;[]:i-~t id){
struct Person p;
r
I strcpy(p.name, name);
strcpy(p.address,address);
I p.id=id;

1
1
return(p);
i}

void print_person(struct Person p ){


printf("Name :%s\n", p.name);
printf("Address :%s\n", p.address);
printf("Id :%d\n", p.id);
pr1n ----------------------------\n")·,
. tf1("

7) The code,

struct Person {
char name[ I 00];
char address[ I 00];
int id;
};
IS
A) the structure definition. There is no storage allocated here.
B) the section where we create the structure variables.
C) how we initialize the structure variables. i
D) how the programmer assigns values to the structure called Person. f

8) The code,

struct Person p 1,p2,p3;


IS
A) the structure definition. There is no storage allocated here.
B) the section where we create the structure variables.
C) how we initialize the structure variables.
D) how the programmer assigns values to the structure called Person.

5 of 15
9) The code,

pI =create_person_rec("name I", "address!", 101);


p2=create_person_rec("name2", "address2", 102);

A) calls the function create_person_rec() and passes as arguments values


for name, address, id
B) allows us to send in "name","address", id data into the function for processing.
C) allows us to assign the values returned by the function to our structure variables.
D) all of the above are correct.

I 0) Select the INCORRECT statement related to the code below:

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


struct Person p;

strcpy(p.name, name);
strcpy(p.address,~ddress );
p.id=id;

return(p);
}

A) uses a local variable 'p' to copy the values arriving via the function arguments into the
structure of type 'struct Person'
B) Even though the local variable will be destroyed when the function exits, we use the
function return value to send a copy of'struct Person' back to the caller. (vja the
~~- 1
C) This code sends a reference to the memory location 'p', back to the caller, so that
the memory location 'p' is reused in the caller.
D) Values sent into the function via the function arguments become unavailable when the
function exits.

6 of 15
Questions 11 to 14 use the following code. Refer to this code when answering these questions.
Parts of the code that are relevant to each question, are reprinted next
r-- - to the--question.
~--- ~ ~-~- ~ ~ -~~ --~

:;*
1

1 cordinates. Using structs to store cordinates on and X Y plane


i */
!

#include <stdio.h>

struct Point{
int x;
int y;
};

struct Line{
struct Point first;
struct Point second;

i struct Circle{
struct Point center;
int radius;
};

struct Point make_point(int xc, int yc );


void print_p(struct Point p);
void print_line ( struct Line I);
, void print_circle( struct Circle c);

void main(void){
struct Po!nJ p1={2,3},p2,p3;
struct Line 11={p1,{5,6}};

//p 1=make_point(2,3);
p2=make_point( 5,2);
print_p(p 1);
print_p(p2);

I/p3 .x=p2.x;
//p3.y=p2.y;

p3=p2;
print_p(p 1); //print a point
print_line(l1 ); //print a line

struct Circle cl ={p2, 5);


print_circle(c 1); //print a circle
I}
~---·-·

7 of 15
II) fill in the blanks in the function make_point() below, to copy x and y cordinate values to a
variable of type struct Point. Return that variable back to the caller, before the function exists.

struct Point make_point(int xc, int yc){


struct Point p;
____=xc;
____=yc;

return( );
}

I2) write a printf statement that correctly prints a point in the format: (2,3)

void print_p(struct Point p){

printf( ----------'~---------- );

13) Given a Line II defined as :

struct Line II={ {2,3},{5,6} };


!.
write a function to print the two points in this line. You may use the function print_pQ that you
already wrote to write this new function. (call print_p() from this function, if you like, or else
write your own 2 printf statements).

Fill in the function stub below:

void print_line ( struct Line I){


printf("first point on Line:"); _ _ _ _ _ _ _ __

printf("second point on Line:"); _ _ _ _ _ _ _ _ __

8 of 15
14) Write a function to print the cordinates of the center of a circle, and the radius. write your
answer inside the function stub provided. Your code should work with the rest of the code listed
above.

void print_circle(struct Circle c){

Use the following program to answer questions 15, 16, 17, 18. Parts of the code that are relevant
!o_~~C~_qu~Sf!()!!,~r_t:_repri!Jted D~Xt
to _!~~_qllest!on. un. _m________ _ _ ~
i #include <stdio.h> f
#include <stdlib-:h>

struct node{
int data;
struct node *next;
};

struct node * create_node(int val);


void print_list(struct node* top);
struct node* remove_node(struct node* hd); II do not forget to update the
II head pointer with this return value
1 void main( void){
· struct node *head, *tail;
struct node *ptr;
I

, ptr=create_node( 5);
I printf("the node contains value: %d\n", (*ptr).data);
! tail=ptr·
L _______ , ----------

9 of 15
,.._

. - -··--·-·· -------------- -------~---~---~-~------------------ --- ----

head=ptr;

ptr=create_ node(7);
printf("the node contains value: %d\n", (*ptr).data);
tail->next=ptr;
tail=ptr;

ptr=create_ node(8);
printf("the node contains value: %d\n", (*ptr).data);
tail->next=ptr;
tail=ptr;

head=remove_ node(head);
//print_list(head);
}

void print_list(struct node *top){


struct node *tmp;

for(tmp=top; tmp!=NULL; tmp=tmp->next ){


printf("value in node: %d\n", tmp->data);
}

struct node* remove_node(struct node* hd){


struct node *tmp;

tmp=hd;
hd=hd-;:>next;

//print whats in tmp and free tmp

free(tmp);
return(hd);
}

struct node * create_node(int val){


struct node *tmp;

tmp=(struct node *)malloc(sizeof(struct node));

tmp->data =val;
tmp->next =NULL;
return(tmp);
}

10 of 15
15) With reference struct node* create_node(int val) given above, select the INCORRECT
answer

A) the function create_node() uses malloc() to dynamically allocate memory.


B) malloc() returns the memory requested in bytes.
C) we use casting to obtain the first address of the chunk we requested.
D) malloc() memory is destroyed when the function create_node() exists.

16) The pointer variables head and tail point to the start of the queue and the end of the
queue. This means:

A) the pointer variable head contains the address of the first node in the linked list.
B) the pointer variable tail contains the address of the last node in the linked list.
C)To traverse from one end of the list to the other, (using a temporary pointer variable),
we need to start from the node pointed to by head and then move to the next node
by following head->next then follow the link given by the 'next' field of the second
node and keep repeating this process.
D) All of the above are true.

17) Inspect the code below and select the INCORRECT entry:

struct node* remove_node(struct node* hd){


struct node *tmp;

tmp=hd;
hd=hd->next;

//print whats in tmp and free tmp

free(tmp);
rehirn(hd);
}

A) hd is a copy of the pointer (head) sent into this function. any change we make to 'hd'
is not reflected in 'head'. (changing hd does not change-head)
B) When we assign 'hd' to 'tmp' and then free 'tmp', the pointer in 'head' becomes invalid.
C) Because we move hd->next to 'hd' and return that value to 'head' in main, a valid
pointer is sent back to head.
D) we could easily remove the pointer variable 'tmp' from this code, and free(hd) rather
than free(tmp) and this code would work the same.

11 of 15
18) Consider the code:
ptr=create_node(5);
ptr=create_ node(7);

if we coded these 2 statements next to each other, as above, we create a memory leak. Select the
most suitable reason for this.

A) Because malloc() memory is known only by remembering it's base address.


re-assigning a new address to ptr, leaves us with no way to get to the memory first
malloc'ed.
B) Beacuse ptr was first used by ptr=create_node(5); it becomes read only,we cannot use
it again with ptr=create_ node(7);
C) ptr is a pointer variable. pointer variables leak memory.
D) ptr is readonly. Assigning values to ptr leak's memory.

19) The output ofthe ~2qegi_yen below_is_:·----------------~-----­


#include <stdio.h>

int fl (int);

void main (void){


int i=5;
i=fl(i); printf("i=%d\n", i);
i=fl(i); printf("i=%d\n", i);
i=fl(i); printf("i=%d\n", i);
}

int fl (int i) {
int k=2; ~
return(k+i);
}

A) i=5, i=5, i=5


B) i=7 , i=9 ' i=ll
C) i=7, i=7, i=7
D) i=5 i=7 i=9
' '

12 of 15
Use the following code for questions 20, 21, 22
-----~-------------- ----

#include <stdio.h>

void main(void){
inti;
int t[ 10]={ 11,21,31,41 ,51 ,61, 71,81,91, 10 I};
int *ptr;

ptr = &(t[O]);

printf("array printed normally ---------------t[%d]=%d\n", i, t[i]);

printf("FIRST %d\n", *t);


printf("SECOND o/od\n", *ptr+ 1);
printf("THIRD %d\n", *(ptr+1));
printf("FOURTH %d\n", *(++ptr));

---------~--------~-------------------- -- --- -

20) The printf labeled SECOND prints:

A) 11
B) 12
C) 21
D) 31

21) The printf labeled THIRD prints

A) 11
B) 12
C) 21
0)31

22) The printf labeled FOURTH prints

A) 11
B) 12
C) 21
D) 31

13 of 15
23) The code given below prints
··---------------- --------------------~--------------- --------- --- --
#include <stdio.h>

int fl (void);

void main (void){


int i=5;
i=fl(); printf("i=%d\n", i);
i=fl(); printf("i=%d\n", i);
i=fl(); printf("i=%d\n", i);
}

int fl(void){
static int k=2;
k++;
return(k);
}

A) i=3, i=4, i=5


B) i=3, i=3, i=3
C) i=2, i=3, i=4
D) i=5 i=2 i=3
' '

14 of 15
. _.

Section 2
4Marks
Write a small program to add the numbers from 1 to 100. You should show all the code needed
for this program to compile correctly.

15 of 15

You might also like