Sunteți pe pagina 1din 5

CHHATRAPATI SHIVAJI INSTITUTE OF TECHNOLOGY, DURG

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

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.

(D) the compiler automatically switches to the more powerful


function mode when compiling a program with functions
(E) the more functions in a program the less bugs

(6 - 7) Consider the following code fragment:


(1-2) Suppose the following code fragment has been executed:

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

(3) Which of the following statements is TRUE?


(A) C is an object oriented programming language
(B) struct is a legal name for a variable in a C program
(C) The variable declaration car ford;is illegal in any C
program
(D) A function may return more than one value
(E) A variable name may begin with the symbol #
(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)

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

(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) Consider the following code fragment:
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
(5) Functions are useful in a program because
(B)
1
(C)
-1
(A) they increase the execution speed of a program
(D)
3
(B) they make the program organization clearer
A run time error is generated because of a division by
(C) a program cannot compile if it does not contain a least one(E)
zero
function

CHHATRAPATI SHIVAJI INSTITUTE OF TECHNOLOGY, DURG


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

(9-10) Consider the following code fragment (do not trust the
indentations used by the programmer)
int pH;

(12-14)

Consider the following expression

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.

(12) Add parentheses to the above expression to make clear the


order in which C will perform the operations:

(A) double
(B) int
(C) Such an expression is illegal in a C program
(D) struct somestruct
(E) struct somestruct*

(15-17) Consider the following program (Be careful):


#include <stdio.h>
#include <stdlib.h>
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 */

A compilation error is
do

CHHATRAPATI SHIVAJI INSTITUTE OF TECHNOLOGY, DURG


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
{

all[i] = third[j];
i++;
j++;
}while(third[j]!= \0);
printf(%s,all); /* line C */
return EXIT_SUCCESS;
}

(19) Which statement is a CORRECT C statement? (correct


means that no error or warning is generated at the
compilation)
(A) char name[3] = Kathryn;
(B) 2x = 2.0;
(C) double y[6] = {1.0,2.0};
(D) char *x = a;
(E) &x = 10;

(15) What is printed by the printf statement on line A?


(A) The oldman
(B) T
(C) The old man
(D) Whichever memory address is stored in all
(E) None of the above
(16) What is printed by the printf statement on line B?
(A) The old man and
(B) T
(C) he old man and
(D) The old mannd
(E) None of the above
(17) What is printed by the printf statement on line C?
(A)
(B)
(C)
(D)
(E)

(20) Among the following, which one is NOT a valid C


identifier?
(A) concrete
(B) brick
(C) stone
(D) struct
(E) truss
(21-23) Consider the following program:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
FILE *file1, *file2;
char letter;

The old man nd


The old man and
The old man and the sea
The old mannd sea
None of the above

file1 = fopen(chapter, r);


file2 = fopen(book, w);
while(fscanf(file1, %c,&letter)!=EOF)
fprintf(book, %c,letter);
fclose(file1);
fclose(file2);

(18) Consider the following code fragment:


int i,j;
for ( i = 1 ; i <= 5 ; i++)
{
for ( j = i ; j > 0 ; j--)
printf( );
printf(*);
printf(\n);
}

return EXIT_SUCCESS;
}
(21) In the above program, chapter is

(A)

a line of * with the shape of

(A) a variable of type double


(B) a variable of type FILE
(C) the name of a file known to the computer outside the C
program
(D) part of a message printed on the computer screen when the
program is executed
(E) a member of the FILE struct book

(B)

a line of * with the shape of

(22) In the above program, EOF is?

(C)

line of * with the shape of

(D)

a line of * with the shape of

(E)

a triangle of * with the shape of

(A) a variable of type FILE


(B) the value assigned to the variable letter by fscanf when
reaching the end of the file read.
(C) a counter to keep track of the number of characters read
(D) A constant defined in the header file stdio.h which is
used by some input/output functions (e.g. fscanf)
(E) The name of a function that detects viruses when reading
files

(18) When executed this code fragment displays

CHHATRAPATI SHIVAJI INSTITUTE OF TECHNOLOGY, DURG


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(23) What is the result of the execution of the above program?

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;

What are the types of the following expressions?


1) &(cascades->chain[1])

(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

(29) Given the function body:


{
int i;
char c;
for(i=0;i<max||fscanf(quark,%c,&c)!=EOF;
i++)
fprintf(particle, %c,c);
}
What would be the correct header for this function body?
(A) FILE *func(int quark, int particle,
double max)
(B) void func(FILE *quark, FILE *particle,
int max)
(C) FILE func(FILE *quark, FILE *particle,
int max)

CHHATRAPATI SHIVAJI INSTITUTE OF TECHNOLOGY, DURG


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
(D) void func(FILE quark, FILE particle, int
max)
(E) void func(struct quark, struct particle,
int max)

(30) Consider the following program:


#include <stdio.h>
#include <stdlib.h>
int func(int i);
int main(void)
{
int x[4];
int i;
for (i=0; i<4; i++)
{
x[i] = func(i);
printf(%i ,x[i]);
}
return EXIT_SUCCESS;
}
int func(int i)
{
if (i>0)
return i + func(i-1);
else
return 0;
}
(30) What is the output once the above program is executed?
(A) 0
(B) 0
(C) 0
(D) 1
(E) 3

1
1
0
1
3

3
2
0
1
3

6
3
0
1
3

(31) In the context of computer science, a library is


(A) another name for a function prototype
(B) a memory area where functions variables are stored while the
program executes
(C) a set of compiled functions that can be called by the
programmer without having to write them all over again.
(D) a list of C books that can be used as reference
(E) another name for hard drive of a computer
(32) Consider the following code fragment:
void onoff(int *pixel)
{
/* change *pixel to 1 if *pixel is 0 */
/* change *pixel to 0 if *pixel is 1 */
/* missing code */
}

(32) What should be written instead of the /* missing code


*/ line for the function to behave as expected?
(A)
(B)
(C)
(D)
(E)

return 1;
return 0;
if (*pixel==1) return 0; else return 1;
*pixel = (*pixel+1)/2;
*pixel = (*pixel+1)%2;

(33) What is wrong with the following recursive function?


int nope(int i)
{
if (i<=0)
return 1;
else if (i%2 == 0)
return i;
else
nope(i-3);
}
(A)
(B)
(C)
(D)
(E)

/* line A */
/* line B */
/* line C */
/* line D */

When executed it may get stuck in an infinite loop


Not all paths of the function algorithm return a value
A recursive function must have a void type
The condition i%2==0 is always true
The function name nope might be confused with a logical
operator by the compiler

(34) How would you fix the function code of question 33?
(A)
(B)
(C)
(D)
(E)

replace line A by void nope(int i)


replace line A by int nono(int i)
replace line B by return EXIT_SUCCESS;
replace line C by else if (i==0)
replace line D by return nope(i-3);

(35) C is called C because


(A) it is not a very good language and as such deserves a C
grade
(B) it started as the B language (B for Bell laboratories) and
later evolved into the C language (big hint: This could be
the right answer)
(C) the programmer who wrote it was fond of whistling the C
note
(D) all the other letters of the alphabet were already used for
names of computer languages
(E) in its early developments, the programs written in C often
crashed. The language was nicknamed C as a short for
Crash.

S-ar putea să vă placă și