Академический Документы
Профессиональный Документы
Культура Документы
(4) Using De Morgans law, how would you rewrite the following
conditional statement (that is rewrite the statement using && instead of ||)
!(x >= -2 || c == e)
(A)
(B)
(C)
(D)
(E)
x
x
x
x
x
>= 2
< -2
> -2
< -2
< -2
&&
&&
&&
&&
&&
c
c
c
c
c
== f
== d
!= e
< e
!= e
(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
(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)
(B)
(C)
(D)
(E)
0
1
-1
3
A run time error is generated because of a division by zero
(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)
(B)
(C)
(D)
(E)
Neutral
Acidic
Neutral
Alkaline
Very Alkaline
Very Alkaline
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)
(B)
(C)
(D)
(E)
Neutral
Acidic
Very Acidic
Neutral
Acidic
Very Acidic
Acidic
Neutral
Very Acidic
Neutral
(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.
double
int
Such an expression is illegal in a C program
struct somestruct
struct somestruct*
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] = ;
for(j=1; second[j]!= \0; j++){
all[i] = second[j];
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;
}
The oldman
T
The old man
Whichever memory address is stored in all
None of the above
(B)
(C)
(D)
(E)
concrete
brick
stone
struct
truss
int main(void)
{
FILE *file1, *file2;
char letter;
file1 = fopen(chapter, r);
file2 = fopen(book, w);
while(fscanf(file1, %c,&letter)!=EOF)
fprintf(book, %c,letter);
fclose(file1);
fclose(file2);
return EXIT_SUCCESS;
}
Assuming that the text file chapter exists, it is copied into the file book
Assuming that the text file book exists, it is copied into the file chapter
The program cannot execute since there is an error at the compilation
The variable pointed to by file1 is copied into the memory location of address file2
The variable pointed to by file2 is copied into the memory location of address file1
(25) Consider the following truth table for the logical operation NAND
P
T
T
F
F
Q
T
F
T
F
P NAND Q
F
T
T
T
!(P&&Q)
!P || !Q
!(P&&Q) || !P || !Q
!(P&&Q) && (!P || !Q)
(P&&Q) && (!P || !Q)
1) mountain *
2) char *
(B)
1) mountain
2) char *
(C)
1) mountain
2) char
(D)
1) mountain *
2) char
(E)
1) mountain **
2) char
( (x ( ( y)))) x
(x (( ( y)) x))
( x) ( (( y) x))
( x) ( ( (y x)))
(( x) ( ( y))) x
b is a pointer to a double
e is an integer
c is a function that returns a struct
d is a function that returns an integer
a and b have the same type
FILE
void
FILE
void
void
0
0
0
1
3
1
1
0
1
3
3
2
0
1
3
6
3
0
1
3
/* line A */
/* line B */
/* line C */
/* line D */
(34) How would you fix the function code of question 33?
(A)replace line A by void nope(int i)
(B)replace line A by int nono(int i)
(C)replace line B by return EXIT_SUCCESS;
(D)replace line C by else if (i==0)
where str is the string and len is the length of the string (not counting \0)
Thus count_s(I miss Mississipi,17) is 6
int count_s(char str[],int len)
{
/* complete the following code */
/* your algorithm must be recursive */
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);
}
}
(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.
The prototype for the function is