Professional Documents
Culture Documents
Final
Final
y = (int)x + 0.1;
printf(“%.1f”,y); /* A */
z = (int)(x+0.1);
printf(“%.1f”,z); /* B */
(A) 2
(B) 2.1
(C) 2.9
(D) 3.0
(E) None of the above
(A) 2
(B) 2.1
(C) 2.9
(D) 3
(E) 3.0
(4) Using De Morgan’s law, how would you rewrite the following
conditional statement (that is rewrite the statement using && instead of ||)
!(x >= -2 || c == ‘e’)
scanf(“%c”,&number); /* line A */
switch(number)
{
default:
i = 2 * i;
case ‘1’:
case ‘2’:
i = 3 * i;
case ‘3’:
case ‘4’:
i = 4 * i;
break;
}
printf(“%i”,i); /* line B */
CSC142 – Final – Fall 98 3
(6) Assume that the user enters Z through the scanf on line A. What
output is displayed by the printf statement on line B?
(A) 10
(B) 15
(C) 20
(D) 120
(E) The program generates a run time error
(7) The above piece of code is executed once again. Assume now that the
user enters 2 through the scanf on line A. What output is displayed by
the printf statement on line B?
(A) 5
(B) 10
(C) 15
(D) 60
(E) 120
int i=1;
int j=3;
int k=4;
printf(“%i”,i-j*5/k%2); /* A */
(8) What is the output displayed by the statement on line A once the code is
executed? (recall that %has the same precedence as * and /)
(A) 0
(B) 1
(C) -1
(D) 3
(E) A run time error is generated because of a division by zero
CSC142 – Final – Fall 98 4
(9-10) Consider the following code fragment (do not trust the indentations
used by the programmer)
int pH;
scanf(“%i”,&pH); /* line A */
if (pH<7)
printf(“Acidic\n”);
if (pH<2)
printf(“Very Acidic\n”);
else
printf(“Alkaline\n”);
if (pH>12)
printf(“Very Alkaline\n”);
else if (pH%7==0)
printf(“Neutral\n”);
(9) Assume that the user inputs the integer 14 on line A. What is the
output displayed once the code is executed?
(A) Neutral
(B) Acidic
Neutral
(C) Alkaline
Very Alkaline
(D) Very Alkaline
(E) Alkaline
Very Alkaline
Neutral
(10) The above code is executed once again. Assume that the user inputs
the integer 0 on line A. What is the output once the code is executed?
(A) Neutral
(B) Acidic
Very Acidic
Neutral
(C) Acidic
Very Acidic
(D) Acidic
Neutral
(E) Very Acidic
Neutral
CSC142 – Final – Fall 98 5
scanf(“%i”,&i); /* line A */
if ( (i==1) && (i=2) )
i=i+1;
printf(“i=%i”,i); /* line B */
(11) Assume that the user enters 1 through the scanf on line A. What is
printed by the printf statement on line B?
(A) i=1
(B) i=2
(C) i=3
(D) i=i
(E) This program cannot execute. A compilation error is generated.
a*b.c/e+d->c
(12) Add parentheses to the above expression to make clear the order in
which C will perform the operations:
(A) (((a*b).c)/e)+(d->c))
(B) (((a*b).c)/e)+d)->c)
(C) (((a*(b.c))/e)+(d->c))
(D) (((a*b).(c/e))+(d->c))
(E) Such an expression is illegal in a C program
int main(void)
{
char first[] = “The old man”;
char second[] = “and”;
char third[] = “the sea”;
char all[100];
int i,j;
for(i=0;*(first+i)!= ‘\0’;i++)
all[i] = first[i];
printf(“%c\n”,*all); /* line A */
all[i] = ‘ ’;
all[i] = ‘\0’;
printf(“%s\n”,all); /* line B */
do
{ all[i] = third[j];
i++;
j++;
}while(third[j]!= ‘\0’);
printf(“%s”,all); /* line C */
return EXIT_SUCCESS;
}
CSC142 – Final – Fall 98 7
int main(void)
{
FILE *file1, *file2;
char letter;
return EXIT_SUCCESS;
}
(A) Assuming that the text file chapter exists, it is copied into the file book
(B) Assuming that the text file book exists, it is copied into the file chapter
(C) The program cannot execute since there is an error at the compilation
(D) The variable pointed to by file1 is copied into the memory location of address file2
(E) The variable pointed to by file2 is copied into the memory location of address file1
CSC142 – Final – Fall 98 10
(25) Consider the following truth table for the logical operation NAND
P Q P NAND Q
T T F
T F T
F T T
F F T
range *cascades;
1) &(cascades->chain[1])
2) cascades->chain[1].name[0]
(A) 1) mountain *
2) char *
(B) 1) mountain
2) char *
(C) 1) mountain
2) char
(D) 1) mountain *
2) char
(E) 1) mountain **
2) char
CSC142 – Final – Fall 98 12
♠ x ♠ ♠ y x
(A) (♠ (x (♠ (♠ y)))) x
(B) ♠ (x ((♠ (♠ y)) x))
(C) (♠ x) (♠ ((♠ y) x))
(D) (♠ x) (♠ (♠ (y x)))
(E) ((♠ x) (♠ (♠ y))) x
a = b->c[d(e)];
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int x[4];
int i;
int func(int i)
{
if (i>0)
return i + func(i-1);
else
return 0;
}
(A) 0 1 3 6
(B) 0 1 2 3
(C) 0 0 0 0
(D) 1 1 1 1
(E) 3 3 3 3
(A)return 1;
(B)return 0;
(C)if (*pixel==1) return 0; else return 1;
(D)*pixel = (*pixel+1)/2;
(E)*pixel = (*pixel+1)%2;
(34) How would you fix the function code of question 33?
where str is the string and len is the length of the string (not counting ‘\0’)
if (len==1)
{
if (*str == ‘s’)
/* to be completed */
return 1;
else
/* to be completed */
return 0;
}
else
{
if (*str == ‘s’)
/* to be completed */
return 1 + count_s(str+1,len-1);
else
/* to be completed */
return count_s(str+1,len-1);
}
}
CSC142 – Final – Fall 98 17
(2) [5 points]
The elements of an array a of integers are ordered in increasing order. The
array a has a dimension given by the integer size.
Write a function that takes as parameters a, size, and an integer val. The
function returns an integer which is the position of the element of a less or
equal to val and closest to val. If there is no such element, the function
returns –1.
for(i=size-1;i>=0;i--)
if (a[i]>val) index = i-1;
return index;