groups - Computer Engineering Department

advertisement
Computer Engineering Department
CMPE-231 1.st Midterm EXAMINATION
GROUPS:
03 & 04
Lecturer: Dr. Marifi Güler
minutes
Name, Surname ________________________________
Student ID #
_________________
Group #
__________________
Question 1
Question 2
Question 3
Question 4
TOTAL
TOTAL
Please, check your exam sheet and make sure that it consists
of 7 pages (including the cover page) and contains 4 questions.
In case of any missing pages, inform the invigilator
IMMEDIATELY !
If an answer box is provided in a question, you MUST give
your answer (and nothing else) in the corresponding box.
Otherwise, you will get no marks from that question !!!
Q1(60 pnts.):
A.) Consider the following pseudocode:
declare a stack of characters;
while ( there are more characters in the word to read )
{
read a character;
push the character on the stack;
}
while ( the stack is not empty )
{
write the stack's top character to the screen;
pop a character off the stack;
}
What is written to the screen for the input "CARPETS"?
SERC (or STEPRAC)
....................................................................................................................
B.) Consider the usual algorithm for determining whether a sequence of parentheses is
balanced or not. What is the maximum number of parentheses that will appear on the
stack AT ANY ONE TIME when the algorithm analyzes:
(()(())(())) ?
3
....................................................................................................................
C.) What is the value of the postfix expression 6324+-* ?
-18
....................................................................................................................
D.) Here is an infix expression: 4+3*(6*3-8). Suppose that we are using the usual stack
algorithm to convert the expression from infix to postfix notation. What is the maximum
number of symbols that will appear on the stack AT ONE TIME during the conversion
of this expression?
4
....................................................................................................................
E.) What is the postfix expression corresponding to : a+b$c$d ?
abcd$$+
....................................................................................................................
F.) Consider the following function:
void test(int n)
{
printf(“%d”, n);
if (n>0) test(n-2);
}
What is printed by the call test(4) ?
4 2 0
....................................................................................................................
G.) What is the output of the following code ?
char array[] = “some string”;
char *p = array + 1;
*p++ = ‘A’;
printf(“%s”, array);
sAme string
....................................................................................................................
H.) What is the output of the following code ?
int nx = 5, ny = 5;
int *pix = &nx, *piy = &ny;
if(pix == piy) printf(“TRUE”);
else printf(“FALSE”):
FALSE
....................................................................................................................
I.) What is the output of the following code ?
char c[] = “hacker”;
char *cp;
for(cp = &c[5]; cp >= &c[0];) printf(“%c”, *cp--);
rekcah
....................................................................................................................
J.) What is the output of the following code ?
void fun(int *p)
{
int i=1;
while(i<10)
{
*p = i; i *= 3; p++;
}
}
main()
{
int a[] = {8, 2, 9, 4, 6, 5, 7};
int i;
fun(a+2);
for(i=0; i<7; i += 2) printf(“%d “, a[i]);
}
8 1 9 7
....................................................................................................................
K.) The following function is meant to return –1 if the argument (char type) is not in
between ‘0’, ‘1’,...,’8’,’9’. Otherwise, it is supposed to return the value 0 for ‘0’, the
value 1 for ‘1’, and so on, the value 9 for ‘9’. Fill in the incomplete part (underlined) of
the function.
int fun(char ch)
{
if(ch < ‘0’ || ch > ‘9’) return –1;
return_______ch – ‘0’_____________________________;
}
....................................................................................................................
L.) Consider the following code:
struct tag
{ int x;
float y;
};
struct tag *pnt;
Now, assume that we want to allocate memory dynamically, just large enough to store
10 tag type variables and that pnt shall point to the first byte of the allocated block.
Write code just for doing this.
pnt = (struct tag *) calloc(10, sizeof(struct tag));
OR
pnt = (struct tag *) malloc(10*sizeof(struct tag));
....................................................................................................................
Q2(10 pnts.):
What is the output of the following code ?
char c[7] = “geyik”;
struct uuu
{
int value;
char *ptr;
} q;
struct uuu *p = &q;
main()
{
p -> value = 5;
p -> ptr = c+2;
printf(“%d%c%c\n”, ++p->value, (*(p->ptr))++, p->ptr[-1]);
}
6ye
....................................................................................................................
Q3(15 pnts.):
What is the output of the following code ?
struct rec {int a; int b;};
void fun(struct rec *, int);
struct rec s = {0, 0};
int a = 10;
main()
{
int a = 20;
fun(&s, 3);
printf(“\nmain: s.a=%d s.b=%d a=%d”, s.a, s.b, a);
}
void fun(struct rec *sp, int n)
{
struct rec s = {0, 0};
if(n>0)
{
(s.b)++;
sp -> a += s.b;
a--;
sp -> b += a;
printf(“\nfun: sp->a=%d sp->b=%d a=%d”, sp -> a, sp -> b, a);
fun(sp, n-1);
}
}
fun: sp->a = 1 sp->b = 9
a=9
fun: sp->a = 2 sp->b = 17
a=8
fun: sp->a = 3 sp->b = 24
a=7
main: s.a = 3
a = 20
s.b = 24
....................................................................................................................
Q4(20 pnts.):
Consider the usual algorithm to convert an infix expression to a postfix expression.
Suppose that you have read 10 input characters during a conversion and that the stack
now contains these symbols:
|
|
| + |
| (
|
bottom | *
|
Now, suppose that you read and process the 11th symbol of the input. Draw the stack for
the case where the 11th symbol is:
A.) A number:
|
|
B.) A left parenthesis:
| (
|
| + |
| + |
| (
|
| (
|
| *
|
| *
|
C.) A right parenthesis:
D.) A division sign:
|
|
|
|
|
| + |
|
|
| (
| * |
/ |
|
| * |
Download