NEWS

COURSE INFO

TUTORS &
LAB HOURS

GRADING &
HOMEWORK

FAQs


CSE 5a: Midterm
Results & Solutions

  • The mean was 20.3668. We will apply curve.

Q #1: Which one of the following items are vaild C identifiers? Give reason(s) for your answer.
i) ComputerScience&Engineering

NOT VALID
because the & cannot be used as an identifier.

ii) Vitamin-A

NOT VALID
because "-" cannot be used as an identifier.

iii) 805_North

NOT VALID
because identifiers must start with a letter or underscore.

Q #2: The following declarations are given.
int x = 5, y = 9, z = 20, w = 11, v = -2, a;
What is the value of each of the follwing C language expressions when they are executed in isolation? List the sequence of computations you made to arrive at answers for each item.
i) a = x != y && z

Answer : 1
since != has higher precedence than any other operators in this statement, x != y is the first thing to be executed. and then && and = follows. Thus:

a = ( ( x != y ) && z )
a = ( ( 5 != 9 ) && z)
a = ( 1 && z )
a = ( 1 && 20 )
a = 1

Thus, the answer is 1.

ii) a = y & x + v

Answer : 1
since the % has higher precedence than other operators in this statemetn, y % x is the first operator to be evauluated. And then the "+" operator, then "=" operator follows. Thus:

a = ( ( y % x ) + v )
a = ( ( 9 % 5 ) + v )
a = ( 4 + v )
a = ( 4 + -3 )
a = 1

Thus, the answer is 1.

iii) a = ( z - y ) == w

Answer : 1
since "( )" have the higest, then "==" next, then "=" next, the stateuemtn will look like a = ( ( z - v ) == w ). Thus:

a = ( ( 20 - 9 ) == w )
a = ( 11 == w )
a = ( 11 == 11 )
a = 1

Thus, the answer is 1

Q #3: If x, y, z, and w are declared as integer variables, which of the following expressions are valid in C?
i) x + 5 = y + z

Answer : NO
The left side of a "=" operator cannot be a statement. It must be an identifier.

ii) x *= 1 + a + b

Answer : NO
because identifier a and b are not declared, they can't be used.

iii) x = y = z + w

Answer : YES
Q #4: Write a sample C program using the "while" loop statement to compute the avarage of first 100 non-negative integers.
First non-negative integer means nubmer from 0 to 99. The avarage of the first non-negative inters means dividing the sum of number 0 to 99 and divide it by 100. The follwing is one of the possiblities

#include < stdio.h >

int main() {
	
    // initialization
    int sum = 0, counter = 0, ave = 0;
	
    // find the sum
    while ( counter < 100 ) {
        sum += counter;
        counter++;
    }
    
    // calculate the average 
    // print the result
    ave = sum / 100;
    printf (" The average is %d\n", ave);
}
There are many other ways of doing this. This is just a possibility
Q: Rewrite the following program fragment using a switch statement. The resulting fragment should be semantically equivalent to the original. Assume a, b, c, d are integer variables.
if ( a == 5 ) {
    b = c + d;
}
if ( a == -5 ) {
    b = c % d;
}
if ( a == 0 ) {
    b = c - d;
} else b = c * d;
A lot of people missed this question. We tried to give as much partial credits as possible. The answer is as follows:
switch(a) {
    case 5:
        b = c + d;
        break;
    case -5:
        b = c % d;
        break;
    case 0:
        b = c - d;
        break;
    default:
        b = c * d;
}



Comments? eMail Webmaster