Midterm Review 2
For solutions to the "what get's printed?" questions, run the program.
Problem 1
/* what gets printed? */
#include <stdio.h>
int f( int a ) { return (a + a); }
int g( int *x ) { return ( *x + *x++);}
int main(void)
{
int i = 5, j = 6;
printf(" f = %d\n", f(i + j));
printf(" g = %d\n", g(&i));
i = i + 2;
printf(" g = %d\n", g(&i));
printf(" g = %d\n", g(&j));
printf(" i = %d j = %d\n", i, j);
return 0;
}
Problem 2 - solutions at end
Fill in answer:
a) EOF is usually the int constant ..........
b) Circle the operator with the highest precedence
1) +
2) >=
3) ++
c) The storage class which describes function is ..........
d) The function tolower is found in ..........
e) If int requires two bytes then long int needs ..........
f) A C program begins execution at ..........
Problem 3
What gets printed?
#include <stdio.h>
int main(void)
{
printf("%c\n", '1' - 1);
printf(" %d\n", 'd' - 'a');
printf("%d\n", !('d' - 'a'));
printf("%c\n", 'B' + 'i' - 'j');
printf("%d\n", 'b' - 'd');
return(0);
}
Problem 4
What gets printed?
#include <stdio.h>
int main(void)
{
int i, j;
for(i = 0; i < 10; i++)
if (i % 2)
printf("%d\n", ++j);
else
printf("%d\n", j++);
return 0;
}
Problem 5 - Solution at end
Fully Parenthesize the following expressions and evaluate them:
Assume int a = 1, b = 2, c =3;
Treat each line as separate with these values applying.
1. a + b + c
2. a++ + b++ * c
3. a%b * c
4. a/b * c
5. (double)a/b * c
6. a/(double)(b + c)
7. (a + b) * c
8. a + b * c
9. a * b + c
10. a * (b + c)
Problem 6
/* What gets Printed? */
#include <stdio.h>
int a = 4; /* extern */
int f(int a){ int i = 0; i++; return ++a + i;}
int g(int a){ static int i = 0; i++; return a + i;}
int h(void){ int i = 0; i++; return ++a + i;}
int main()
{
int a = 2, b = 3;
printf("f= %d, g= %d, h= %d\n", f(a), g(a), h());
printf("f= %d, g= %d, h= %d\n", f(b), g(b), h());
{
int a = 1;
printf("f= %d, g= %d, h= %d\n", f(a), g(a), h());
printf("f= %d, g= %d, h= %d\n", f(b), g(b), h());
}
return 0;
}
Problem 7 What gets printed ? #includeconst int n = 4; int main(void) { int i, sum = 0, a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; i = 0; while (i <= n){ sum += a[i]; printf("%d is sum\n", sum); ++i; }; return 0; }
Problem 8
Fill in the integer value of each expression assuming the following
declarations:
int i = 1, j = 3, k = 4;
Expression Value
i <= j
j % k
k / j
2 * i + 1 < = j
!i <= 2
Problem 9
What gets printed?
#include < stdio.h>
int f (int a) {return (2* a + 1); }
int g (int *y) { *y += 1; return (*y + *y); }
int main(void)
{
int i = 11, j = -2;
printf(" f = %d\n", f(i + j));
printf(" g = %d\n", g(&i));
i = i + 3;
printf(" g = %d\n", g(&i));
printf(" g = %d\n", g(&j));
printf(" i = %d j = %d\n", i, j);
return 0;
}
Problem 10
What gets printed?
#include < stdio.h>
int rec (int i)
{
int j = 0;
printf ("%d\n",i);
if (i > 2)
j = rec(i - 2) + 1;
else
j = i;
printf("%d\n", j);
return j;
}
int main(void)
{
rec(5);
return 0;
}
Problem 11
What gets printed?
#include < stdio.h>
void swap (int *m, int *n)
{
int temp = *m;
*m = *n;
*n = temp;
}
int main (void)
{
int a[4] = {2,7,4,8};
int i, j;
for (i = 3; i > 0; --i) {
for (j = 0; j < i; ++j)
if (a[j] < a[j+1])
swap (&a[j], &a[j+1]);
for (j = 0; j < 4; ++j)
printf ("%d ",a[j]);
printf ("\n");
}
return 0;
}
Problem 12
What gets printed?
#include < stdio.h>
void func1(int a, int b)
{
static int count = 0;
count += a + b;
a += b;
printf ("%d %d %d\n", a, b, count);
}
void func2(int *a, int *b)
{
int count = 0;
count += *a + *b;
*a += *b;
printf ("%d %d %d\n", *a, *b, count);
}
int main(void)
{
int i=2, j=3;
func1 (i,j);
printf("%d %d\n", i, j);
func2 (&i,&j);
printf("%d %d\n", i, j);
func1 (i,j);
return 0;
}
Problem 13
What gets printed?
#include < stdio.h>
int main(void)
{
int i, j = 5;
for (i = 0; i < 6; ++i)
switch (i % 4) {
case 0:
printf("%d\n", ++j);
break;
case 1:
printf("%d\n", j++);
break;
case 2:
printf("%d\n", j);
break;
case 3:
printf("%d\n", --j);
case 4:
printf("%d\n", j);
break;
default:
printf("error\n");
break;
}
return 0;
}
Solution for Problem 2
EOF is usually -1.
++ has highest precedence.
functions are storage class extern.
tolower is found in ctype.h .
long int is at least 2 bytes if int is 2 bytes,
but would be typically in that case 4 bytes.
C program start execution with main().
Solution to Problem 5 Here are fully parenthesized expressions and evaluations based on int a = 1, b = 2, c = 3; ((a + b) + c) 6 (( (a++) + (b++)) + c) 6 ((a%b) * c) 3 ((a/b) * c) 0 (( ((double)a)/b) * c) 1.5 (a/((double)(b + c))) 0.2 ( (a + b) * c) 9 (a + (b * c)) 7 ( (a * b) + c) 5 ( a * ( b + c)) 5 Feel free to report any site problems to the current Webmaster, Debra Dolsberry.