CMPE108 Sample Final Exam questions
CMPE108 Sample Final Exam questions
OPERATORS ASSOCIATIVITY
() [] Left to right
* / % Left to right
+ - Left to right
== != Left to right
|| Left to right
= += -= *= /= %= Right to left
, Left to right
1
QUESTIONS WITH SOLUTIONS
Q1. (10 pts) Write a C arithmetic expressions of the following algebraic expressions.
1 1− a
+ 1/c + (1-a) / (1+b)
c 1+ b
− b + a2
(-b+a*a) / (2*a+1)
2a + 1
Q2. (20 pts) Write a C program that finds the largest in a series of numbers entered by
the user. The program must prompt the user to enter numbers one by one. When the user
enters 0 or negative number, the program must display the largest entered nonnegative
number and the number of entered numbers. A sample output of the program is given as
follows:
Enter a number: 60
Enter a number: 38.3
Enter a number: 4.78
Enter a number 105.62
Enter a number: 70.2295
#include <stdio.h>
int main()
{
double number, max;
int counter=0;
max = 0;
printf("Enter a number:");
scanf("%lf", &number);
while(number>0)
{
counter++;
printf("Enter a number:");
scanf("%lf", &number);
}
a) Declare a one-dimensional int array arr of length 10 and initialize all elements
to 1.
int arr[10];
int i;
for(i=0; i<10; i++) arr[i] = 1;
b) Assign 10 and 40 to the first and fifth element of the array arr in part (a)
respectively and display these elements on the screen.
arr[0] = 10;
arr[4] = 40;
printf(“first value %d fifth value %d\n”,
arr[0], arr[4]);
c) Enter new values for the array arr defined in part (a) from the keyboard and
print the array elements in one row on the screen.
int i;
d) Find the summation of array arr elements defined in part (c) and print the result
on the screen.
int sum = 0;
for(i=0; i<10; i++)
sum += sum[i];
printf(“summation %d \n”, sum);
3
Q4. (16 pts) Trace the following C program and write the output into the corresponding
boxes given below.
Note that;
• Four numbers will be displayed in the output and while you are writing the
output, you must fill the given boxes below according to the display order of these
numbers.
• Every box must contain only one number.
#include <stdio.h>
int main()
{
int R=0;
do
{
switch (R)
{
case 0:
printf("%d\n",R);
case 1:
R += 1;
break;
case 2:
printf("2\n");
case 3:
printf("%d\n",R * 2);
case 4:
R = R + 3;
break;
default:
R = R * 3;
printf("%d\n",R / 2);
}
}
while(R<=10);
return 0;
}
0
2
4
7
4
Q5. (14 pts) The aim of the following C program is to initialize 4x4 matrix, change the
diagonal elements in the matrix to 20 and display the average of all elements on the first
and last columns of the matrix. Now, complete the missing parts in the following C
program by considering both the comments given in the program and the aim.
Note that;
• In the program, the array “A” must be used as your 4x4 matrix.
• You must NOT use additional variables.
• You must fill only the missing parts.
#include<stdio.h>
#define L 4
int main()
{
// In the following statement, the array “A” is declared as 4X4 matrix
// and initialized as 4x4 matrix.
int A[L][L]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
A[i][i]=20 ;
return 0;
}
5
Q6. (20 pts) In the following program, the arrays “A” and “B” are declared and
initialized. Furthermore, the array “Max” is declared.
Now, complete the following program to compare the first element of the array “A” with
the first element of the array “B” and write the maximum element into the first element of
the array “Max”, compare the second element of the array “A” with the second element
of the array “B” and write the maximum element into the second element of the array
“Max”, compare the third element of the array “A” with the third element of the array
“B” and write the maximum element into the third element of the array “Max” and so on.
The process of comparison and storing maximum element in “Max” array must be done
for every element in the arrays.
#include<stdio.h>
int main()
{
int A[8] = { 10 , 22 , 15 , 31 , 25 , 62 , 44 , 36 };
int B[8] = { 47 , 15 , 81 , 72 , 16 , 29 , 41 , 33 };
int Max[8];
int i;
return 0;
}
6
Q7. (10 points) What is the output of the following program?
return 0;
}
Q8. (15 points) The following program calculates the median of three integer numbers
entered by the user. Complete the missing parts of the program.
#include <stdio.h>
SAMPLE OUTPUTS:
int median(int x, int y, int z) {
int result; Enter three numbers: 45 27 39
Median of 45, 27, and 39 is 39
if (x <= y)
if ( y<=z ) result = y;
else if ( x<=z ) result = z; Enter three numbers: 67 145 44
else result = x; Median of 67, 145, and 44 is 67
else {
if (z <= y) result = y; Enter three numbers: 15 25 35
else if ( x<=z ) result = x; Median of 15, 25, and 35 is 25
else result = z;
}
return result;
}
int main()
{
int x, y, z;
printf("Enter three numbers: ");
scanf("%d%d%d", &x, &y, &z);
printf("Median of %d, %d, and %d is %d\n", x,y,z, median(x, y, z));
return 0;
}
7
Q8.(10 points) Trace and find the output of the following program. Write your output in
the box provided below. Please enter one character in every box.
#include <stdio.h>
#define N 4
{ OUTPUT
int i,j; 2
2 1 1 1
int A[N]={1,2,3,4};
-2
int B[N]={1,-1,1,-1};
1 -2 1 1
int C[N]; 4
0 0 4 0
int D[N][N]={{1,1,1,1},{1,1,1,1}};
-4
for (i=0; i<N; i++)
0 0 0 -4
{
if ( i %2 ==1)
C[i]=A[i]*B[i];
else
C[i]=A[i]+B[i];
D[i][i]=C[i];
printf (“\n”);
return 0;
8
Q9. (15 points) The dot product of two vectors a = [a 1 , a 2 , ..., a n ] and b = [b 1 , b 2 ,
..., b n ] is defined as.
where denotes summation notation and n is the dimension of the vector space.
For instance, in three-dimensional space, the dot product of vectors a=[1, 2, -6] and
b=[4, 2, −1] is:
[1,2,-6] [4,2,-1]=(1)(4)+(2)(2)+(-6)(-1)=14
Fill in the missing parts in the following program such :
-that the user will enter two 5 dimesional vectors a and b into the arrays A and B
respectively
- the dot product of the two vectors will be calculated and printed as the output.
Note: Use only the spaces provided. Do not add any extra lines or delete any given code.
#include <stdio.h>
int i, sum=0;
____sum+=A[i]*B[i]__________________________________________ ;
_____printf(“%d”, sum)_______________________________________;
return 0;
9
Question 1-) (22 pts.) Trace the following C program and write the
output into the corresponding box, which is divided into cells, below.
Assume that you have 4-cell-to-8-cell display screen and each cell
shows one character on the screen. Note: if any character is not
printed, let the corresponding cell empty.
#include <stdio.h>
int main()
{
int n=10;
int j=10;
return 0;
}
OUTPUT:
1 2 3 4 5 6 7 8
1 6 6 6 6 6 6 6 C
2 4 4 4 4 4 C
3 2 2 2 C
4 0 C
10
Question 2-) (22 pts.) Trace the following C program and write the
output into the corresponding box below.
#include <stdio.h>
int main()
{
int Number=0;
while (Number<5)
{
switch (Number)
{
default:
printf("Magusa\n");
case 0:
printf("Girne\n");
break;
case 3:
printf("Lefkosa\n");
break;
case 2:
++Number;
case -1:
printf("Iskele\n");
break;
case 1:
printf("Karpaz\n");
}
++Number;
}
return 0;
}
OUTPUT:
Girne
Karpaz
Iskele
Magusa
Girne
11
Question 3-) (32 pts.) Complete the missing parts in the folllowing C
program to produce the output below. Be aware of odd numbers will
be displayed one time and even numbers will be displayed according to
its value. For example; 1 is an odd number and displayed one time; 2
is an even number and displayed 2 times; 3 is an odd number and
displayed one time; 4 is an even number and displayed 4 times; etc.
Notes: Do not use any variable other than the variables declared in the
program.
OUTPUT:
122344445666666788888888
#include <stdio.h>
int main()
{
int N=8;
int i=1;
int j;
while (i <= N)
{
if (i % 2 == 0)
{
for( j=1 ; j <= i ; j++)
printf( "%d " , i );
}
else
printf( "%d " , i );
i++;
}
return 0;
}
12
Question 4-) (32 pts.) Complete the following C program to search
the number “number”, given by the user, in the elements of the array
“A”. If it finds the number, it will give the message “It is found!!”. If
it does not find the number, it will give the message “It is NOT
found!!”. Some sample outputs are given below.
Note: If you need, you can use additional variable(s).
#include <stdio.h>
#define L 15
int main()
{
int A[L]={1,4,6,9,23,2,5,91,21,45,67,54,44,88,27};
int number;
int i;
int check=0;
if (check==1)
printf("It is found!!\n");
else
printf("It is NOT found!!\n");
return 0;
}
13
QUESTIONS WITHOUT SOLUTIONS (for you to test yourself)
Q1) Write a C code which is equivalent to the flowchart shown below. (15 points)
Begin
x=1
y=5
sum=0
F
x< 5?
T PRINT x,y,
sum
sum=sum+y
END
x=x+2
14
Q2) a) Evaluate the following expressions and write your answers in the boxes provided.
(7 points)
ANS
i) 10.0+15/2*6.0
ii) 3*4.0/6+6
iii) 10+17/3.+4
iv) 3.0*4%6+6
v) 10+17%3+4.0
vi) 2*5==4*4
vii) 5%2*4>5||4%2*5<7
bc
i) a+
x + 2y
ii) − x 3 + 3 x 2 + 5 x − 6
15
Q3) Consider the while loop below:
i=10;
While ( i >= 1)
{
printf (“ok\n”);
i=i-2;
}
16
Q4) Write the output of each of the following codes below to the boxes provided.
(Only the answer box will be graded-no partial credit points for work done!)
a) (5 points)
ANSWER
for (i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf(“*”);
printf(“\n);
}
b) (5 points)
ANSWER
i=5;
while(i>=1)
{
j=1;
while (j<=i)
{
printf(“*”);
j++;
}
printf(“\n);
i--;
}
c) (8 points)
ANSWER
j = 0;
do
{
j++;
switch(j)
{
case 2: printf("C\n");
case 1: printf("B\n"); break;
case 3: printf("D\n");
default: printf("E\n");
}
printf("X\n");
}while(j < 6);
17
Q5) Consider the following C program whose description and functionality is given below:
The program calculates the tax pay for a 100 employees. The tax pay for each employee
depends on the age and marital status (m_status=1 for single and m_status=2 for married) of
the employee. Data (age, salary and marital status) is entered by an operator who must have
a valid security code between 1 and 9999. The operator cannot enter data if his security code
is not valid and is asked to re-enter his code every time he enters it wrongly. There are 4
different tax rates, rate1, rate2, rate3, and rate4. The tax rate which of an employee is
determined using the following table and the tax to be paid is given by: tax=salary*tax rate
Age Marital status Tax rate
<30 single rate3
<30 married rate4
≥30 single rate1
≥30 married rate2
Complete the missing parts of the code such that the program functions correctly (25 points)
#include <stdio.h>
main() {
int age, salary, emp_no, m_status, sec_code;
float tax;
do{
printf (“\n Please enter security code”);
scanf (“ %d”, &sec_code);
} while (_________________________________)
emp_no_1;
while (_______________________) {
printf (“\n enter age, salary and marital status”);
scanf( ________________________________);
if (__________________________)
tax=salary*rate1;
else if (________________________)
if (_____________________________)
tax=salary*rate2;
else
tax=______________________;
else
tax=______________________;
}
}
18
Q6) Given the definition of the function
a) 3.0*func(2,Total) ……………………
b) func(b,b)/func(b,Sum+1) ……………………
a) int j = 2, k = 6, m;
m = ++j + k--; ------------------
b) int j = 2, k = 3, m;
m = j + (1 > k-j); ------------------
c) int i = 1, m;
m = !i != 5; ------------------
d) int i = 1, j = 3, m;
m = (I < j) % j; ------------------
e) int i = 1, j = 1, m;
m = ++i && --j; ------------------
f) int j = 1, k = 6, m;
m = --j ? –-k : ++k; ------------------
19