0% found this document useful (0 votes)
177 views10 pages

EMU CMPE EXAM Key - MT - 2122 - 2

Here are the steps: 1) i = 11, j = 4 2) k = i/j k = 11/4 k = 2 3) printf("%d\n", k) Prints the value of k which is 2 4) x = 11.5; x = 11.5 5) printf("%f\n", x) Prints the value of x which is 11.50 So the output is: 2 4.50

Uploaded by

Sammy T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views10 pages

EMU CMPE EXAM Key - MT - 2122 - 2

Here are the steps: 1) i = 11, j = 4 2) k = i/j k = 11/4 k = 2 3) printf("%d\n", k) Prints the value of k which is 2 4) x = 11.5; x = 11.5 5) printf("%f\n", x) Prints the value of x which is 11.50 So the output is: 2 4.50

Uploaded by

Sammy T
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Eastern Mediterranean University

Department of Computer
Engineering

CMPE 112 Midterm Exam


Spring Semester 2021-2022
April 27, 2022

Name, Surname :…SOLUTION KEY………


Student No :………………………………
Group No :………………………………

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.)

➢ The number of pages is 10 in PartII (20 pnts.)


total. Please check !!!!!!
PartIII(20 pnts.)
➢ Calculators are not allowed. PartIV(24 pnts.)

➢ GSM phones should be PartV (6 pnts.)


turned off.
PartVI(20 pnts.)
➢ A table of operators for TOTAL:
precedence and associativity (out of 100)
is attached.

➢ Passing any material


including rubbers, pencils
etc. to anybody else is strictly
prohibited in the exam.

➢ If an answer box is given in a


question, you must give your
answer (and nothing else) in
the box !!!
PRECEDENCE AND ASSOCIATIVITY

OPERATORS ASSOCIATIVITY

( ) [ ] -> . Left to right

! ++ -- + - * & (type) sizeof Right to left (Unary)

* / % Left to right

+ - Left to right

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

2) Which of the following is a correct implementation of the condition

10 ≤ m ≤ 14

A. (m >= 10 || m <= 14)


B. (10 <= m <= 14)
C. (m >= 10 && <= 14)
D. (m >= 10 && m <= 14)

3) What will be the output of the following C code fragment?


double x = 3.456;
printf("%4.1f*%5.2f*%.4f\n", x, x, x);

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

4) What will be the output of the following C code fragment?


int x = 345;
printf("%4d*%5d*%1d\n", x, x, x);

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

5) What will be the output of the following code fragment?

printf("%f\n ",((int) (3.141592*1000))/1000.0);

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)

#include <stdio.h> Output


int main(void) 7
{ 20
int x= 7, y = 4,z;
printf("%d\n",x--);
z= --x * y++;
printf("%d \n", z);
return 0;
}

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)

#include <stdio.h> // 3 points Value of y Program output


int main() { typed by user
int y;
7 10
scanf("%d",&y);
switch(y){ 2 9
case 7: y=y+1;
case 3: y=y+2; 4 8
break;
case 2: y=y+3;
default: y=y+4;
}
printf("%d\n",y);
return 0;
}

D)

#include <stdio.h> Output


--------------
int main(void)
{ 5 26
int i, j, sum;

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

if(op=='/' && b==0 )


printf("Error cannot divide by 0\n");

else if (op == '*' || op == '+' || op == '-' || op == '/')


{
result = operation(a,b,op); // Call the function to compute the result
printf("%.2f %c %.2f = %.2f \n",a,op,b,result);
}
else printf("%c is unknown operation\n", op);
return 0;}
// define the function ComputeResult. Use switch statement
double operation(double a, double b, char op){
double result;
switch(op){
case '*': result=a*b;
break;
case '+': result=a+b;
break;
case '-': result=a-b;
break;
case '/': result=a/b; }
return result;}
Part IV [24 points]
Assuming that int i = 2, j = 4, k = 9; is given before each one of the following expressions,
evaluate the value of each expression (exp) and also give new values of i, j, and k after each
expression. Note that your answers must be given in the table below.

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)

12. !!k > 4?++j:i++

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;

for(a=0, j=0, k=3; j<k; j+=2, k++) a += j+k;

printf("a=%d j=%d k=%d\n", a, j, k);

for(a=0, j=0, k=3; j<k; j+=2, k++)


{
a += j+k;
if(j+k == 6) break;
} a= 18 j= 6 k= 6
a= 9
printf("a=%d\n", a);
a= 12
for(a=0, j=0, k=3; j<k; j+=2, k++)
{
if(j%k == 2) continue;
a += j+k;
}

printf("a=%d\n", a);
}

You might also like