C Midterm Exam Fall 2008 - Problem Solutions - Prof. Manuel E. Bermudez, Exams of Computer Science

The solutions to the midterm exam for the cgs-3460 fall 2008 class taught by manuel e. Bermudez. Problem-solving steps for various c programming questions, covering topics such as expressions, arrays, control structures, functions, and unix commands.

Typology: Exams

Pre 2010

Uploaded on 09/17/2009

koofers-user-m0c-1
koofers-user-m0c-1 🇺🇸

7 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CGS-3460 Fall 2008
Instructor: Manuel E. Bermudez
Midterm Exam
Maximum Points: 100
Time: 50 Minutes
Problem 1:____________(20 pts)
Problem 2:____________(30 pts)
Problem 3:____________(30 pts)
Problem 4:____________(20 pts)
Total: :____________(100 pts)
Instructions:
1. There are 4 questions. Attempt all the questions.
2. Keep your answers brief.
3. If you want, you can use the back side of pages for your answers.
4. Write your name and UFID on the top center of all pages
Name: _____________________________________
UFID:__________________
pf3
pf4
pf5

Partial preview of the text

Download C Midterm Exam Fall 2008 - Problem Solutions - Prof. Manuel E. Bermudez and more Exams Computer Science in PDF only on Docsity!

CGS-3460 Fall 2008

Instructor: Manuel E. Bermudez

Midterm Exam

Maximum Points: 100

Time: 50 Minutes

Problem 1:____________(20 pts)

Problem 2:____________(30 pts)

Problem 3:____________(30 pts)

Problem 4:____________(20 pts)

Total: :____________(100 pts)

Instructions:

1. There are 4 questions. Attempt all the questions.

2. Keep your answers brief.

3. If you want, you can use the back side of pages for your answers.

4. Write your name and UFID on the top center of all pages

Name: _____________________________________

UFID:__________________

UFID:__________________

Problem 1 (20 points). Answer the following questions, briefly.

(a) What is the value of the following expression in C?

('a' < 'b')? 1 : 2;

1

(b) Is the following a valid initialization of the array arr? If yes, indicate the value of each element in arr. Otherwise indicate why this is invalid.

int arr[3] = {2, 3};

(c) Write a printf statement to print “Hello” (including the double quotes), not just Hello.

printf(“\”Hello\””);

(d) Is the control expression in the following statement valid in C? Explain why or why not. if(a = 1 && b = 2) a=3;

It is invalid since logical operators have higher precedence i.e (a = 1 && b = 2) is equivalent to (a = (1 && b) = 2); which in turn is (a = 1 = 2); compiler wouldn’t compile the program due to an invalid lhs value.

(e) What is the value of the control expression in the following statement?

if((25 && 30) && -1) a=3;

true, since all non-zero values evaluate to true i.e.: ((true && true) && true)

(f) Placing a new-line character at the end of a scanf format string is usually a bad idea. Why? scanf("%d\n", &i);

scanf will continue reading until it encounters a non-whitespace character.

UFID:__________________

Problem 2 (30 points). Write the exact output generated by the following C program. Indicate any spaces and new lines clearly.

#include <stdio.h>

void triangle(char c){ int i,j; for(i = 0; i<4; i++){ for(j=4-i;j>0;j--){ printf("%c",c); } printf("\n"); } }

char xOrY(int number){

if(number %2 == 0){ return 'X'; }

return 'Y'; }

int main(){ triangle('*'); triangle(xOrY(99)); triangle(xOrY(100)); return 0;

}

Output:



**

YYYY YYY YY Y XXXX XXX XX X**

UFID:__________________

Problem 3 (30 points). Find errors (either syntax errors, or errors that will cause bad run- time behavior) in the following C program. Indicate the necessary correction for each error.

#include #include<stdio.h> int main(void){ int n,k; printf("Enter value of N: "); scanf("%d", n); scanf("%d", &n); k=(n>0)?(5: 0); k=(n>0)?5: 0;

if(k) printf("Hello world");

for(int i=0, j=10;i<5&&j>0;i++,j--){

printf("%d--%d",i,j); }

do { { printf("%d\n",n); n-- n--; } }while(n>0) while(n>0);

return 0 return 0;

}

Line 11: for(int i=0, j=10;i<5&&j>0;i++,j--){

There is a problem in this expression because i cannot be declared inside a for statement and j has not been declared previously. But, we have not deducted any marks for this problem. The main idea of this for statement was to check if the students are familiar with multiple variable initialization and increment in a for statement separated by commas. The correct expression should be:

int i,j; for(i=0, j=10;i<5&&j>0;i++,j--)