Introduction to Programming version D- EC 144
Introduction to Programming version D- EC 144
Electronic Engineering
Final Examination
Year 1, Semester II (20 14)
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.
·.#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 main(void){
intj=3;
fl(&j);
printf("%d\n", j);
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;
printf("%s\n", b);
}
--- ·----------
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;
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 main(void){
struct Person p 1,p2,p3;
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}
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,
5 of 15
9) The code,
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
#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;
};
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
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.
return( );
}
I2) write a printf statement that correctly prints a point in the format: (2,3)
printf( ----------'~---------- );
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.
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;
};
, 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);
}
tmp=hd;
hd=hd-;:>next;
free(tmp);
return(hd);
}
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
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:
tmp=hd;
hd=hd->next;
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.
int fl (int);
int fl (int i) {
int k=2; ~
return(k+i);
}
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]);
---------~--------~-------------------- -- --- -
A) 11
B) 12
C) 21
D) 31
A) 11
B) 12
C) 21
0)31
A) 11
B) 12
C) 21
D) 31
13 of 15
23) The code given below prints
··---------------- --------------------~--------------- --------- --- --
#include <stdio.h>
int fl (void);
int fl(void){
static int k=2;
k++;
return(k);
}
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