Saturday, November 15, 2008

Programming and Problem solving in C (MC151) – October 2005

Section A : Basic Concepts (30 Marks)

1.Which of the following puts program in the memory?
(a)preprocessor (b)compiler (c)linker (d)loader (e)editor.
2.int z, x = 5, y = -10, a = 4, b = 2;
z = x++ - --y * b / a;
What number will z in the sample code above contain?
(a)5 (b)6 (c)10 (d)11 (e)12.
3.With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed?
(a)unalloc() (b)dropmem() (c)dealloc() (d)release() (e)free().
4.Which escape character can be used to begin a new line in C?
(a)\a (b)\n (c)\l (d)\m (e)\t.
5.char* myFunc (char *ptr)
{
ptr += 3;
return (ptr);
}
int main()
{
char *x, *y;
x = "HELLO";
y = myFunc (x);
printf ("y = %s \n", y);
return 0;
}
What is the output when the sample code above is executed?
(a)y = HELLO (b)y = ELLO (c)y = LLO (d)y = LO (e)x = O.
6.What is the associativity of the conditional operator
(a)left to right (b)right to left (c)top to bottom (d)bottom to top (e)left to left.
7.What function will read a specified number of elements from a file?
(a)fileread() (b)getline (c)readfile() (d)fread() (e)gets().
8."My salary was increased by 15%!"
Select the statement which will EXACTLY reproduce the line of text above.
(a)printf("\"My salary was increased by 15/%\!\"\n");
(b)printf("My salary was increased by 15%!\n");
(c)printf("My salary was increased by 15'%'!\n");
(d)printf("\"My salary was increased by 15%%!\"\n");
(e)printf("\"My salary was increased by 15'%'!\"\n");
9.What is the difference between a declaration and a definition of a variable?
(a)Both can occur multiple times, but a declaration must occur first
(b)There is no difference between them
(c)A definition occurs once, but a declaration may occur many times
(d)A declaration occurs once, but a definition may occur many times
(e)Both can occur multiple times, but a definition must occur first.
10.What is the meaning of self-referential structure?
(a)Array of structures
(b)Single structure
(c)Structure calling it’s parent structure
(d)Structure calling another structure
(e)list of structures.
11.Which of the following function is used to copy one string to another string?
(a)strcopy (b)strcp (c)strcpy (d)stringcopy (e)srtcp.
12.Which of the following is a not a keyword in C language?
(a)void (b)volatile (c)sizeof (d)getchar (e)short.
13.Which of the following is equivalent to for (expr1;expr2;expr3)
(a)while(expr1){ expr2; statement; expr3;}
(b)expr1; while(expr2){statement; expr3;}
(c)while(expr2) {expr1; statement; expr3;}
(d)expr2; while(expr3){expr2; statement; expr1;}
(e)while(expr3) {expr1; statement; expr2;}.
14.Write the first line of the function definition for the following.
“a function called add accepts two integer arguments and returns a floating point value”
(a)float add(int ,int) (b)float add(int a,int b) (c)float add()
(d)float add(int a,b) (e)Float add(a,b int).
15.what is the output of the following program
#include
int c[10]={1,2,3,4,5,6,7,8,9,10};
main()
{ int a,b=0;
for(a=0;a<10;++a) 2="="1)" i="10;i++;i<15)" ch="’g’;i="ch-‘a’;" b="5,c="15,d="8,e="8,a;" a="b">c?c>d?12:d>e?13:14:15;
Printf(“%d”,a);
(a)13 (b)14 (c)15 (d)16 (e)garbage value.
30.A declaration float a, b; occupies ___ of memory
(a)1 byte (b)4 bytes (c)8 bytes (d)16 bytes (e)2 bytes.

Section B : Problems (50 Marks)
1.
a. What are decision tables? Describe its structure with example.
b. Differentiate flow chart and an algorithm. (7 + 3 = 10 marks)
2.
a. What are the different data types available in C? Explain each data type’s size and control string?
b. Write briefly about the scope and lifetime of a variable with an example?
(5 + 5 = 10 marks)
3.
a. What are formatted and unformatted functions?
b. Write a C Program that contains a function to find prime factors of any given number.Every non prime number can be expressed in mulitples of primes. For example, prime factors of 12 are 2, 2 and 3. Prime factors of 62 are 2 and 31.
(5 + 5 = 10 marks)
4.
a.Write 3 applications of pointers in C language.
b.Taking a two-dimensional array (N x N) of integers as input to your program, display only the elements in the upper-tringle of the matrix.The program should display the diagonal and the lower-triangle elements of the matrix as X irrespective of its values.
Example:
Input:
2 3 4 5
6 0 2 6
9 8 -9 7
8 -9 10 0
Output:
X 3 4 5
X X 2 6
X X X 7
X X X X (4 + 6 = 10 marks)
5.10 integers are put in a file, one per line. Write a program that reads the numbers and then prints their sum and average. (10 marks)

Section C : Applied Theory (20 Marks)
6.
a. Write a program to find the minimum number of currency notes required to make up the given amount as input.(Assume that 500,100, 50, 20, 10, 5, 2, 1 currency notes are available).
If the given input is 638
Ouput:
500*1=500
100*1=100
20*1=20
10*1=10
5*1=5
2*1=2
1*1=1
b. Explain about recursion and write a program to generate Fibonacci series using recursive functions. (10 + 10 = 20 marks)

Suggested Answers1.
Answer : (d)
Reason : Loader puts the program into memory whereas others functionality is different.
2.
Answer : (c)
Reason : based on the hierarchy of operations in c 10 is the correct answer.
3.
Answer : (e)
Reason : Remaining all are irrelevant methods.
4.
Answer : (b)
Reason : \n is for new line character and \t for tab space and remaining are not appropriate.
5.
Answer : (d)
Reason : LO is the correct choice because when myfunc is called and hello is passed as an argument.
LO is extracted as address is increased by 3 bytes.
6.
Answer : (b)
Reason : Right to left is the correct associativity remaining all don’t match.
7.
Answer : (d)
Reason : free() is the correct method to read specified number of element s from a file.
8.
Answer : (d)
Reason : Choice d produces the required output.
9.
Answer : (a)
Reason : Remaining all are contradictory according to c language
10.
Answer : (c)
Reason : Self referential structure stands for structure calling its parents structure but not array of structures, etc.,
11.
Answer : (b)
Reason : strcp is the correct function to copy one string to another string
12.
Answer : (d)
Reason : Remaining all are keywords of c except getchar
13.
Answer : (b)
Reason : firstly variable is initialized ,Condition is checked, stmts are executed and incrementation takes place and condition is checked
14.
Answer : (b)
Reason : The syntax for function prototype is float functionname(int,int )and infunction declaration arguments are passed whose syntax returntype functionname(datatype argument1,datatype agr2,..)
15.
Answer : (c)
Reason : Firstly 1 is stored in b and then 3 is addedto it 4,4+5,9+7,16+9=25.
16.
Answer : (c)
Reason : addresses of the memory cant be added.
17.
Answer : (a)
Reason : malloc function defines how many memory bytes to be allocated.so result is 10 bytes
18.
Answer : (b)
Reason : Remaining choices are contradictory to above declaration.
19.
Answer : (c)
Reason : the remaining are address operator,multiplication,addition,subtraction operator
20.
Answer : (a)
Reason : Remaining is contradictory to the definition of union
21.
Answer : (c)
Reason : Remaining all are irrelevant
22.
Answer : (b)
Reason : two dimension array is indicated with two square brackets first bracket value indicate number of rows and second bracket indicate column value
23.
Answer : (b)
Reason : Remaining all are contradictory to the functionality of linker.
24.
Answer : (b)
Reason : #insert,#copy,#swap are irrelevant.#define is for symbolic constants.
25.
Answer : (b)
Reason : Remaining all donot match with the question.as they return single value.
26.
Answer : (e)
Reason : Just i value is assigned 0 and comes out of the loop.
27.
Answer : (a)
Reason : The difference of ascii values is 6.
28.
Answer : (b)
Reason : long int ,float,double are greater than int.char cant be because comparision is made between numeric data types.
29.
Answer : (c)
Reason : in the order of evaluation of conditional operator the final result is 15.
30.
Answer : (c)
Reason : float data type occupies 4 bytes. So totally a, b occupies 8 bytes.

No comments: