Beruflich Dokumente
Kultur Dokumente
Progreamming in C
Note: Answer all of the following questions. READ EACH
QUESTION CAREFULLY. Each correct answer is worth 1
point. Each question has EXACTLY one correct answer.
int i = 5;
char number;
double x = 2.9;
double y,z;
scanf(%c,&number); /* line A */
y = (int)x + 0.1;
printf(%.1f,y); /* A */
z = (int)(x+0.1);
printf(%.1f,z); /* B */
(1) What is printed by the printf statement on line A?
(A) 2
(B) 2.1
(C) 2.9
(D) 3.0
(E) None of the above
(2) What is printed by the printf statement on line B?
(A) 2
(B) 2.1
(C) 2.9
(D) 3
(E) 3.0
(A) 10
(B) 15
(C) 20
(D) 120
(E) The program generates a run time error
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 */
(6) Assume that the user enters Z through the scanf on line A.
What output is displayed by the printf statement on line
B?
x
x
x
x
x
>= 2
< -2
> -2
< -2
< -2
&&
&&
&&
&&
&&
c
c
c
c
c
== f
== d
!= e
< e
!= e
(9-10) Consider the following code fragment (do not trust the
indentations used by the programmer)
int pH;
(12-14)
a*b.c/e+d->c
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
(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
(13) In the expression a*b.c/e+d->c, what is a possible type
for the variable b?
(A) double
(B) int
(C) Such an expression is illegal in a C program
(D) struct somestruct
(E) struct somestruct*
(14) In the expression a*b.c/e+d->c, what is a possible type
for the variable d?
(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)
Neutral
Acidic
Very Acidic
Neutral
(C)
Acidic
Very Acidic
(D)
Acidic
Neutral
(E)
Very Acidic
Neutral
(11) Consider the following code fragment (read carefully):
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.
generated.
(A) double
(B) int
(C) Such an expression is illegal in a C program
(D) struct somestruct
(E) struct somestruct*
A compilation error is
do
all[i] = third[j];
i++;
j++;
}while(third[j]!= \0);
printf(%s,all); /* line C */
return EXIT_SUCCESS;
}
return EXIT_SUCCESS;
}
(21) In the above program, chapter is
(A)
(B)
(C)
(D)
(E)
2) cascades->chain[1].name[0]
(A) Assuming that the text file chapter exists, it is copied into
(A) 1)
the file book
(B) Assuming that the text file book exists, it is copied into the
file chapter
(B) 1)
(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
(C) 1)
location of address file2
(E) The variable pointed to by file2 is copied into the memory location
of address file1
(D) 1)
(24) Which of the following is FALSE regarding a recursive
function?
(A) A recursive function can always be rewritten with a loop
(B) A recursive function always executes faster than its loop
equivalent
(C) A recursive function is a function that calls itself
(D) Recursion can sometimes yield a natural and simple solution
to a problem that would otherwise be very difficult to solve
(E) When executing a recursive function, the computer transfers
data to and from a memory area called the system stack.
mountain *
2) char *
mountain
2) char *
mountain
2) char
mountain *
2) char
(E) 1) mountain **
2) char
(27) Consider a language with the two operators and . is
left associative and binary. is right associative and unary.
has precedence over .
Add parentheses to the following expression to show how it
would be evaluated.
x y x
(A)
( (x ( ( y)))) x
(25) Consider the following truth table for the logical operation (B)
(x (( ( y)) x))
NAND
(C)
( x) ( (( y) x))
(D)
( x) ( ( (y x)))
P
Q
P NAND Q
(E)
(( x) ( ( y))) x
T
T
F
T
F
T
F
T
T
(28) If you see the following in a legal C program:
F
F
T
a = b->c[d(e)];
(25) Which of the following C conditional expressions would
NOT reproduce the above truth table?
one thing that you can say for sure is:
(A) !(P&&Q)
(B) !P || !Q
(C) !(P&&Q) || !P || !Q
(D) !(P&&Q) && (!P || !Q)
(E) (P&&Q) && (!P || !Q)
(26) Given the following definitions and declarations:
typedef struct{char name[20];
double latitude;
double longitude;
}
mountain;
typedef struct{mountain chain[10];} range;
range *cascades;
(A)
(B)
(C)
(D)
(E)
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
1
1
0
1
3
3
2
0
1
3
6
3
0
1
3
return 1;
return 0;
if (*pixel==1) return 0; else return 1;
*pixel = (*pixel+1)/2;
*pixel = (*pixel+1)%2;
/* line A */
/* line B */
/* line C */
/* line D */
(34) How would you fix the function code of question 33?
(A)
(B)
(C)
(D)
(E)