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

Engineering Programming A- EC144

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)
13 views

Engineering Programming A- EC144

Uploaded by

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

Sri Lanka Institute of Information Technology

B.Sc. Eng. (Honours) Degree

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

EC 144 - Engineering Programming

Paper Version A

Duration: 1.5 Hours

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"?

a) Variables are locations in memory where values can be stored.


b) Each location in memory has a memory address, which is a nwnber.
c) We use a hwnan understandable name to refer to this nwnber (e.g. age, quantity). The
compiler maps this name to the memory address nwnber.
d) ALL of the above are correct.

2. Select the INCORRECT statement when referring to: rules in naming a 'C' variable:

a) allowed characters are: a-z, A-Z, 0-9, and_


b) 'C' variables are Case sensitive.
c) The flfSt character must be a letter or _ then any combination of the allowed
characters can be used. But Keywords and reserved words may not be used as
variables.
d) 'C' variables always need to start with the letters var_

3. Which of the following list can be used as a 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·
'

5. The following is NOT a valid 'if statement in 'C'

a) if (x=1) then
b) if (x=1){ ; }
c) if(x>=1); else;
d) if (x>=1) { ; }else { ; }

6. Asswning 'int x=10;' the following is a valid 'C' loop.

a) while ( x!=O) { x=x-1; }


b) while ( 1 ) { printf(''Endless loop\n"); }
c) do{ x=x+1;} while(x<15);
d) ALL of the above are valid 'C' loops.

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

a) The while() loop code is commented out. It does not run.


b) The for() loop code works exactly like the while() loop code woukl have worked.
c) The fJrSt secti>n in the for loop runs 1 time. The middle <condition> section in the for
loop is tested every time, before entering the loop body. And the third section of the
for loop is done each time, after the loop body is run.
d) ALL of the above are true.

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

a) The output of this code is: a


b) The output of this code is : aa
c) The code prints both case 'a': and defauh: as there is no break statement after ca~e 'a'
d) If the variable ch was not initialized to 'a', then this code will only print the defaalt::
section.

10. Inspect the following code:

#include <stdio.h>
void main( void) {
int i=5;
int j=3;

printf(''%d\n", ilj);
printf(''%d\n", i%j);
}

This code produces the outputs:

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.

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

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

12. The above code prints:

a) F=3 b) F=3 c) F=2 d) F=3


F=2 i=4 F=2 F=3

13. In fimction fl()

a) the assigmnent i=4 modifies the global variable.


b) the assigmnent i=4 modifies the local variable 'i defmed in main()
c) the assigmnent i=4 is lost when the fl() fimction exits.
d) ALL of the above are true.

Use the following code to answer the questions 14 and 15.

#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) int i=4 is a global variable.


b) int i=6 is a variable local to the main() function.
c) the printf statement in main() will output i=6
d) the printf statement in main() will output i=4

15. Choose the answer which is 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.

Use the following code to answer the questions 16and 17.

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

16. The above code produces the output:

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

18. The above code prints:


a) i=2 b) i=2 c) i=3 d) i=5
i=1 i=5 i=2 i=4
i=O i=4 i=6 i=3

19. Select one of the following answers 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. 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);
}

struct point c_point( int xc, int yc ){


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

retwn(p);
}

void print_point(struct point p) {


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

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:

structpoint p1,p2,p3={3,4} ,p4={5,6};

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


b) variables declared are p1,p2,p3,p4
c) 'struct point' also refers to a variable we declare.
d) variables p3 and p4 are initialized by this code.

22. Select the incorrect statement with reference to this code calling the fimction print_point(pl)

a) The fimction print_point() gets a copy of p 1, and calls that copy p


b) The fimction print_point() gets a reference top 1, and calls that reference to the
original variable p
c) 'struct point p' in the fimction print_point() becomes unavailable when the fimction
exits.
d) When the fimction print_point() gets called again a new copy of 'p' is created.

23. The code :

I struct point c _point( int xc, int yc );

a) Is a fimction prototype. The actual fimction 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 fimction.
d) ALL of the above are true.

24. Select the incorrect statement relating to the code below:

struct point c_point(int xc, int yc) {


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

a) creates a local variable called 'p' of type 'struct point'.


b) Even though p.x and p.y are assigned values, these values are not visible outside this
fimction().
c) Because this fimctions calls return(p) and allows a return type of'structpoint' 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 fimction is 'struct'

You might also like