mid - ans

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Index Number:

…………………………
……….

Sri Lanka Institute of Information Technology

B.Sc. Eng. (Honours) Degree


Mid-Term Examination
Year 1, Semester II (2015)

EC1440 – Engineering Programming


Paper Version A

Duration: 1 Hour

August 2015

Instructions to Candidates:
 Answer ALL questions.
 There are 20 MCQ questions.
 This paper contains 8 pages with Cover Page. No additional data is attached.
 Write your answers in this paper itself.

Page 2 3 4 5 6 7 8 9 10 Total
Sub
total

1
Each Question is worth 1 mark. Underline the most suitable answer.

1. What are “variables”?


(I ) Variables are locations in memory where values can be stored. int=x;
(II) Each Variable has a memory address, which is a number.
(III) We use a human understandable name for a variable (e.g. age, quantity). The compiler
maps this name to the memory address number.

a) (I) is correct.
b) (I) and (II) are correct
c) (I) and (III) are correct
d) (I), (II), (III) are all correct.

2. Select the INCORRECT statement regarding 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 first 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) When used in a C program: Name, NAME refer to the same variable.

3. Which of the following can be used as a variable?

a) Check_This
b) 007Bond
c) Upper%half
d) if

4. Assuming int a=4; which of the following produces the output :

value = 5
postfix - a++ print unata passe wadi wenne
a) printf(“value=%d\n”, a); prefix- ++a print wenna kalin wadi
b) printf(“value=%d\n”, a++); wenawa.
c) printf(“value=%d\n”, ++a);
d) printf(“value=%d\n”, a--);

5. Which of 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 { ; }
1 line ekak nm wahanna ona na.

6. Assuming 'int x=10;' which of the following is a NOT valid 'C' loop.

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


b) while ( x = =10 ) { printf(“Endless loop\n”); }
c) for( ; x<15 ; x++ ) printf(“Hello\n”);
d) loop( x<14 ) begin printf(“Hello\n”) end;
c wala mehema ekak na

2
7. Inspect the following code:

#include <stdio.h>

void main(void){
int i;
4 wenakn witarai.
i=0;
while( i < 10 ){
printf(" i=%d,", i);
i=i+1; 4+1=5, hinda 4n nawatinawa.
if (i = = 5){
break; continue tibbot etanin pahalata yanne na.ayet
} udata yanawa.loop eke
}
}

This code prints :

a) i=0, i=1, i=2, i=3, i=4, i=5, i=6, i=7, i=8, i=9,
b) i=0, i=1, i=2, i=3, i=4, i=5,
c) i=0, i=1, i=2, i=3, i=4,
d) i=6, i=7, i=8, i=9,

8. Inspect the following code:

#include <stdio.h>

void main(void){
int i;

i=0;
while(i<10){
i=i+1;
if((i%2) = = 1){
continue; ghanna ona eka wadinne na. (anit ekata continue
}
printf(" i=%d,", i); wenawa.)
}
}

This code prints :

a) i=1, i=3, i=5, i=7, i=9,


b) i=0, i=2, i=4, i=6, i=8,
c) i=0, i=1,
d) i=2, i=3, i=4, i=5, i=6, i=7, i=8, i=9,

3
9. Inspect the following code:

void main(void){
int i=3;

switch(i%2){
case 0:
printf("%d is an even number\n",i);
break;
case 1:
printf("%d is an odd number\n",i);
}
}

The above code prints:


a) 3 is an even number
b) 3 is an odd number
c) 0 is an even number
d) 1 is an odd number

10. Inspect the following code:

#include <stdio.h>

void f1(int);

void main(void){
int i=5; function eka call karanawa
f1(i);
printf("value = %d\n", i);
}

void f1(int i){


i=i+1;
printf("value = %d\n", i);
}

The above code prints:


a) value = 5 b) value = 6 c) value = 5 d) value = 6
value = 6 value = 5 value = 5 value = 6

4
11. Inspect the following code:

#include <stdio.h>

void main(void){

int i=5; int walin dashama gahanna ba.


int j=3;

printf("%d\n", i/j);

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

This code produces the output:

a) 5 b) 1 c) 1 d) 5
3 2 3 1

12. Inspect the following code

#include <stdio.h>

int i=2;

void main(void){
printf("i=%d\n", i);
int i=3;
{
int i=4;
printf("i=%d\n", i);
}

printf("i=%d\n", i);

This code produces the output:

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


i=4 i=3 i=4 i=4
i=3 i=4 i=3 i=4

5
13. Inspect the following code:

#include <stdio.h>

void f1(void);

void main(void){
int i=5;
f1();
f1();
f1();
} static - eka parak change karot eka save wenawa.

void f1(void){
static int i=4;
printf("i=%d\n", i);
i=i-1;
}
This code prints:

a) i=4 b) i=4 c) i=5 d) i=5


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

14. Inspect the following code

#include <stdio.h>

void f1(void);

void main(void){
int i=5;
f1();
f1();
f1();
}

void f1(void){
int i=4;
printf("i=%d\n", i);
i=i-1;
}
This code prints:

a) i=4 b) i=4 c) i=5 d) i=5


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

6
15.
“SCOPE and LIFETIME of variables refer to.”

a) Where a variable can be seen in your code, and how long the variable remains in use.
b) How useful a variable is, and the story of it's life.
c) The difficulty experienced in using a variable, and how your life expectancy was reduced
by it.
d) Who is allowed to use a variable, and what the computer feels about it.

Use the following code to answer the questions 16 and 17

#include <stdio.h>
function ekata eliyen tiyanawa nm global.function eka asse tinawa nm
int i=5; local.
void main (void){
int i=6; wena function ekak gattama i print karot print
printf("i=%d\n", i); wenne 5(global variable hinda)
}

16. Choose the answer which is NOT CORRECT.

a) int i=5 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=5

17. Choose the answer which is CORRECT. global variable eka delete wenne na
a) because int i=6 is defined in main, the global variable int i=5 is deleted by the
compiler.
b) because int i=6 is defined in main, the global variable int i=5 is not accessible in
main(), but remains in memory.
c) The local variable's (i=6) is by default accessible outside the main function, by other
functions, as main is special. eliye nemei,atule tiyenne.
d) global variables and local variable declared in main() have the same scope within a
'C' program.

local variable eka main eka atule tiyenne..

7
Use the following code to answer the questions 18 and 19.

#include <stdio.h>

void f1();
int i=5;

void main(void){
f1();
printf("i=%d\n",i);
}

void f1(){
i=7;
printf("i=%d\n",i);
}

18. The above code prints:

a) i=5 b) i=7 c) i=5 d) i=7


i=7 i=7 i=5 i=5

19. With respect to function f1(), select the CORRECT answer.


A) the assignment i=7 modifies the global variable.
B) the assignment i=7 modifies a local variable 'i' defined in main()
C) the assignment i=7 is lost when the f1() function exits.
D) ALL of the above are true.

20. Inspect the following code:


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

int f1(int i){


i=i-1;
printf("i=%d\n", i);
return(i);
}
The above code prints:

a) i=5 b) i=5 c) i=4 d) i=4


i=4 i=5 i=3 i=4
i=3 i=5 i=2 i=4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy