EMU CMPE EXAM Key - MT - 2122 - 2
EMU CMPE EXAM Key - MT - 2122 - 2
Department of Computer
Engineering
Instructors: Prof. Dr. M. Güler (Grs. 01,02), Prof. Dr. Omar Ramadan (Grs. 03,04)
Duration: 90 minutes
Question Grade
Instructions: PartI (10 pnts.)
OPERATORS ASSOCIATIVITY
* / % Left to right
+ - Left to right
== != Left to right
|| Left to right
?: Right to left
= += -= *= /= %= Right to left
, Left to right
Part I [10 points]: Multiple choice questions
1) Which of the following is the correct C assignment statement for the formula: root1 =
–b + b2 – 4ac
2a
A. root1 = (-b + sqrt(b*b - 4*a*c)) / 2*a;
B. root1 = (-b + sqrt(b*b - 4*a*c) / (2*a));
C. root1 = (-b + sqrt(b*b - 4*a*c)) / (2*a);
D. root1 = -b + sqrt(b*b - 4*a*c)/(2*a);
10 ≤ m ≤ 14
A. 3 . 4 * 3 . 4 5 * 3 . 4 5 6 0
B. 3 . 5 * 3 . 4 6 * 3 . 4 5 6 0
C. 3 . 5 * 3 . 4 6 * 3 . 4 5 6 0
D. 3 . 4 * 3 . 4 5 * 3 . 4 5 6 0
A. 3 4 5 * 3 4 5 * 3 4 5
B. 3 4 5 * 3 4 5 * 3
C. 3 4 5 * 3 4 5 * 3
D. 3 4 5 * 3 4 5 * 3 4 5
A. 3.141592
B. 3.141000
C. 3.142000
D. 3.000000
Part II [20 points]: What will be the outputs of the following programs?
A)
B)
#include <stdio.h>
Value of x typed by Program
int main() {
user output
int x;
printf("Enter a value for x >"); 15 D
scanf("%d",&x);
7 C
if(x >= 6 ) 3 F
if(x < 10)
if(x > 7)
printf("A");
else
printf("C");
else
if ( x > 12)
printf("D");
else
printf("B");
else
printf("F");
return 0;
}
C)
D)
sum = 0;
for(j=0; j<7; j++) {
if(j%2==0 || j%3==0)
continue;
sum += j*j;
if(sum>5)
break;
}
printf("%d %d\n", j, sum);
return 0;
}
Part III [20 points]
Complete the following C program that will take from the user, in the main function, two real
numbers (a, b) and one operator (op) (‘+’, ‘-’, ‘/’, or ‘*’). The C code contains a function called
“ComputeResult” that will return to the main function the result of the operation. The function
prototype is double ComputeResult(double, double, char);
The function to be written is required to compute the result of the operation using switch
statement. This result should be displayed in the main function as shown in the sample runs.
Sample run 1
Enter a op b: 10.5 * 9 Sample run 2
10.5*3.9 = 40.95 Enter a op b: 4.5 / 0
Error cannot divide 0
Sample run 3
Enter a op b: 12.5 $ 3.45
$ is unknown operation
#include <stdio.h>
double ComputeResult (double, double, char); // function prototype
int main(){
double a,b,result;
char op;
printf("Enter operation, a op b : ");
scanf("%lf %c %lf",&a,&op,&b);
1. i += j != k
2. k = ++j+i++
3. !j<i
4. ++i==j--
5. k-j*2>=i?j:++k
6. i=j>=k
7. (i-=2)&&++j
8. --i||++j
9. i&&(k -= 9)
10. j < i <k
11. i = (k = j++, k += j)
exp i j k
1 3 3 4 9
2 7 3 5 7
3 1 2 4 9
4 0 3 3 9
5 10 2 4 10
6 0 0 4 9
7 0 0 4 9
8 1 1 4 9
9 0 2 4 0
10 1 2 4 9
11 9 9 5 9
12 2 3 4 9
Part V [6 points]
Give the output of the following program:
#include <stdio.h>
main()
{ 2
int i, j, k;
float x; 4.50
i = 11; j = 4; k = i/j; 4
printf(“%d\n”, k);
5
x = i/j + 2.5;
printf(“%4.2f\n”, x); 5.25
k = i/j + 2.5; 4.50
printf(“%d\n”, k);
k = (float)i/j + 2.5;
printf(“%d\n”, k);
x = (float)i/j + 2.5;
printf(“%4.2f\n”, x);
x = (float)(i/j) + 2.5;
printf(“%4.2f\n”, x);
}
Part VI [20 points]
Give the outputs of the following programs:
1.)
#include <stdio.h>
int main()
{
void fun(int);
int x=5;
12
fun(x); 5
printf("%d\n", x);
}
void fun(int x)
{
x += 7;
printf("%d\n", x);
}
2.)
#include <stdio.h>
main() ii10 18
{
int i=2,k=18;
for(;i<10;i++)
{
static int k=10;
if(k%i==0) printf("i");
}
printf("%d %d\n",i,k);
}
3.)
#include <stdio.h>
main()
{ i=8 j=1
int i = 7, j = -2; i=9 j=4
i=10 j=7
while(i++, j += 3, i>j+1)
printf("i=%d j=%d\n", i, j);
}
4.)
#include<stdio.h>
main()
{
int j, k, a;
printf("a=%d\n", a);
}