Saturday, November 8, 2008

Programming and Problem solving in C (MC151): April, 2005

Section A : Basic Concepts (30 Marks)
This section consists of questions with serial number 1 - 30.
Answer all questions.
Each question carries one mark.
Maximum time for answering Section A is 30 Minutes.
1. Read the following statements and choose the most appropriate one.
I. An algorithm is an actual program that can be executed on a machine directly.
II. An algorithm is a set of rules for carrying the calculations either by hand or on a machine.
III. An algorithm can be a program dependent and machine dependent code to define a solution for a given problem.
IV. An algorithm is an abstraction of a program to be executed on a machine.
(a) Both (I) and (IV) above (b) Both (I) and (III) above
(c) Both (II) and (IV) above (d) Both (I) and (II) above
(e) (II), (III) and (IV) above.
2. Let A, B are the integer variables, and P, Q are the integer pointers, then which of the following are valid in a C program?
(a) P = P*A; (b) P = A/P; (c) P = P/Q; (d) P = P*Q; (e) P = P+A;.
3. Read the following statements carefully, and pick the correct answer.
I. An external variable declaration must begin with storage class specifier extern.
II. Scope of an automatic variable can be smaller than the entire function.
III. An automatic variable keeps its final value even after its function is terminated.
(a) (I), (II) are TRUE and (III) is False (b) (I), (III) are TRUE and (II) is False
(c) (II), (III) are TRUE and (I) is False (d) All are TRUE
(e) All are FALSE.
4. Pick the equivalent control structure for the following repetitive control structure
For ( E1; E3; E2 ) { statements; }
(a) while( E1) { E2; statements; E3; }
(b) E1; while(E2) { statements; E3; }
(c) while( E3) { E1; statements; E2; }
(d) E1; while(E3) { statements; E2; }
(e) E1; E3; while(E2) { statements; }.
5. For the initial values as a=3, b=40, c=13, d=10, what is the final value of a,
a = b^cd&a;
(a) Syntax error (b) 39 (c) 3 (d) 35 (e) 31.
6. What is the output for the following statement, if the initial value of x is 20
Printf (“%d,%d,%d,%d,%d”, x++, x--, --x, ++x, x--);
(a) 20,21,19,20,20 (b) 20,21,19,20,21 (c) 18,19,19,20,20
(d) 19,19,19,20,20 (e) 19,18,19,20,19.
7. What is the correct output of the following program?
# include
void main()
{
char str[] = ”ICFAI UNIVERSITY”, rev[17];
int i = strlen(str), j=0;
for( ; i>=0; rev[j++] = str[i--]);
rev[j] = ‘\0’;
puts(rev);
}
(a) ICFAI (b) Syntax error (c) YTISREVINU IAFCI (d) nothing (e) YTISREVINU.
8. Choose the correct output for the following program
# include
void main()
{
int a=10, b=11, c=13, d;
d = (a=c, b+=a, c=a+b+c);
printf(“ %d”, d);
}
(a) 50 (b) 34 (c) garbage value (d) R value required (e) Error.
9. Consider the following code.
# include
void main()
{
= {6,8,3,9,0}, i=0;
if( i != 0)
{
break;
printf(“ %d”, a[i]);
}
else
printf(“%d”, a[i++]);
}
10.What is the output of the above program?
(a) 6 (b) 8 (c) Runtime error (d) no output (e) Syntax error.
10. While declaring an array in C as : datatype arrayname[size]; the size can be
(a) Any constant (b) An integer constant (c) Any variable
(d) Any initialized integer variable (e) Both (b) and (d) above.
11. In ‘C’ which of the following can be used to create the constants for float values
i) Enumerations ii) const iii) # define
(a) i & ii (b) ii only (c) ii & iii (d) i & iii (e) iii only.
12. Choose the correct output for the following given program.
# include
void fun(int a, int b, …)
{
int i=0, *p = &b;
for( printf(“ ”); iprintf(“%d, ”, ++(*p++));
}
void main()
{
fun(1, 0);
fun(3,1,2,3);
}
(a) Syntax error (b) 1, 2, 3, 4 (c) run time error (d) 0, 1,2,3 (e) 1, 1,2,3.
13. For the following input function scanf(“%d%*d%d”, &x, &y); if the values entered are 900 456 786.
Then what will be the values of integer variables x, y.
(a) 900 456 (b) 456 786 (c) 900 (d) 900 786 (e) Abnormal program termination.
14. What is the output of the following program?
# include
void main()
{
int a=5, *pa=&a, b=0, *pb=&b, **ppa=&pa;
pb=pa;
a += *pb += **ppa;
printf(“ %d, ” “%d, ” “%d”, *pa, b, **ppa);
}
(a) 20, 0, 20 (b) 20, 20, 20 (c) Syntax Error (d) 0, 20, 0 (e) 0, 20, 20.
15. What is the output of the following program
# include
struct temp {
int j, i;
}a;
void fun(struct temp *p)
{
printf(“%d, “, *p); p++; printf(“%d”, *p);
}
void main()
{
struct temp *z = &a;
z->i = 1, z->j = 10;
fun(z);
}
(a) 10, 10 (b) 10, 1 (c) 1, 10 (d) 0, 10 (e) 10, 0.
16. What would be the values assigned to a, b, c if the following statement is extended with input data item 123456 scanf(“%d %d %d”, &a, &b, &c );
(a) a=12, b=34, c=56 (b) a=123456 and nothing is assigned to b, c
(c) a=1, b=3, c=5 (d) a, b are not assigned any thing but c=123456
(e) a=123456, b=0, c=0.
17. If the following loop is implemented then ..
{
static int x;
do {
--x;
printf( “%d”, x++);
}while( x >= 0);
}
(a) The program will not enter in the loop
(b) There will be a compilation error reported
(c) The loop will run infinitely
(d) A run time error will be reported
(e) It prints 0 once and terminates the do – while loop successfully.
18. The expression (( fileptr = fopen(“somefile”, “w”)) == NULL ) would be true if
(a) The file somefile is read only
(b) May be the fileptr is not declared as a FILE pointer
(c) The file somefile does not exist while fopen function is being executed
(d) The file somefile could not be created for writing
(e) Both (b) and (c) above.
19. The given statement FILE *fptr;
(a) Defines a pointer to the pre-defined structure type FILE
(b) Defines a pointer to the user-defined structure type FILE
(c) Defines a pointer to the pre-defined data type FILE DESCRIPTOR
(d) Creates a file with the name fptr
(e) Creates a file pointed by fptr.
20. The getchar() library function
(a) Returns a character when any key is pressed
(b) Returns multiple characters when the keys are pressed
(c) Returns multiple characters when the keys are pressed followed by enter key
(d) Both (b) and (c) above
(e) Returns a single character when the keys are pressed followed by enter key.
21. If a=Oxaa and b=a<<1 then
(a) b=2a (b) b=a/2 (c) a=b-1 (d) b=a-1 (e) b=a.
22. Which of the following operator(s) has/have right to left associativity?
(a) ++ (b) – – (c) += (d) Both (a) and (b) above (e) All (a), (b) and (c) above.
23. With the following declaration , the correct answer is
enum rgb{red=-1, green, blue};
(a) red= -1 green= -2 blue= -3
(b) red= -1 green= 1 blue= 2
(c) red= -1 green= 0 blue= 1
(d) red= -1 green= 2 blue= 3
(e) All the above alternatives are illegal declarations.
24. What is the return value of the following function if the function is called as int value = fun(10);
int fun(int n)
{
if( n = = 1 n = = 2) return 0;
else return( n-1 + fun(n-2) );
}
(a) 20 (b) 26 (c) 22 (d) 24 (e) 0.
25. Which of the following is not a key word in ‘C’?
(a) void (b) getchar (c) sizeof (d) short (e) volatile.
26. Consider the following code:
struct account {
int acno;
}svar, *pv = &svar;
then the acno can be accessed by
(a) svar.acno (b) pv->acno (c) (*pv).acno (d) a & b & c (e) (a) & (b) only.
27. Trace out the correct outcome of the following program:
void fun(char *s1, char *s2)
{
while(*s2) *s1++ = *s2++;
*s1 = ‘\0’;
}
void main()
{
char s2[]= “lion”, s1[] = “monkey”;
fun(s2, s1);
puts(s2);
}
(a) lion (b) lion monkey (c) monkey (d) null (e) Syntax error.
28. While working with the binary streams for ‘C’ I/O file operations
(a) Only the binary values can be read and can be written
(b) No Character translation takes place
(c) Character translation takes place
(d) Number of characters written (read) are not same as the number that is stored on the external device
(e) Both (a) and (b) above.
29. The function islower (char) checks whether a character is in lower case or not and hence it should return
(a) The character in lower case (b) The character itself (c) Nothing (d) -1, 0, or 1 (e) 0 or 1.
30. The function sprintf( ) works like printf( ), but operates on
(a) strings (b) data in a file (c) stderr(d) stdin (e) standard output device
Section B : Problems (50 Marks)
This section consists of questions with serial number 1 – 5.
Answer all questions.
Marks are indicated against each question.
Detailed workings should form part of your answer.
Do not spend more than 110 - 120 minutes on Section B.
1. a. Write briefly about algorithm and flow chart.
b. Write an algorithm to find whether a number is prime or not.
c. Draw a flow chart to find the summation of first 50 even numbers. (4 + 3 + 3 = 10 marks)
2. a. Write about the different operators available in ‘C’ language.
b. What is a control structure? Write about the different control structures provided by ‘C’ language. (5 + 5 = 10 marks)
3. a. What is storage class? Write about the different storage classes with suitable example.
b. Write a ‘C’ program to reverse a given number and check whether it is palindrome or not.
(5 + 5 = 10 marks)
4. a. Write about the different predefined string handling function in ‘C’ with suitable examples.
b. Write a simple ‘C’ program to compare two strings with out using the standard library functions, but use the pointers. (6 + 4 = 10 marks)
5. a. Write how the files are handled in ‘C’. Write about the necessary functions needed for this.
b. Write a ‘C’ program to copy a file to another file to simulate copy command of DOS. (Pass the names of the files as command line arguments) (5 + 5 = 10 marks)
Section C : Applied Theory (20 Marks)
This section consists of questions with serial number 6 - 7.
Answer all questions.
Marks are indicated against each question.
Do not spend more than 25 -30 minutes on section C.
6. Write a C program using functions and pointers to multiply two matrices A, B of dimensions MxN, PxQ respectively. Use pointers only in your program. Array subscription is not allowed.
[Use Dynamic Memory Management Functions to allocate and de-allocate memory for ‘n’ names of size ‘m’ each]. (10 marks)
7. Write a modular ‘C’ program using structures to sort the books in ascending order of their cost. Define in your program, all the necessary members of the book structure to reflect the real time entity book. (10 marks)
Suggested Answers
1. Answer : (c)
Reason: Because according to the definition of the algorithms it is a set of rules for carrying the
calculations either by hand or on a machine, and it is an abstraction of a program to be
executed on a machine. It is not a program to execute directly on a computer, moreover it
should be program independent, machine independent and must define a generic solution
for a given problem.
2. Answer : (e)
Reason: According to integer arithmetic rules for pointers all the operations P = P*A; P = A/P; P
= P/Q; P = P*Q; are invalid where as the expression P = P+A; is purely valid.
3. Answer : (a)
Reason: Because the first two statements are perfectly valid as per the definitions of the external, automatic variables, and the statement III is wrong as an automatic variable can’t keeps or retains its final value even after its function is terminated.
4. Answer : (d)
Reason: Because the equivalent indefinite repetitive control structure (while) for the definite
repetitive control structure (for) is option D, others are wrong for(initialization (E1); condition (E3); increment loop invariant (E2)) {statements;} is same as initialization( E1); while(condition(E3))
{
statements;
increment loop invariant (E2 )
}
5. Answer : (b)
Reason: Because of the precedence of operators. The operator ^ (XOR) is having the high
precedence when compared with other two operators, followed by & (AND) and (OR) operators
6. Answer : (c)
Reason: Because the expressions in the predefined formatted output function
Printf (“%d,%d,%d,%d,%d”, x++, x--, --x, ++x, x--); are executed from right to left
because of the stack involvement in arguments [assign to the called function (Known as C
convention)]
7. Answer : (d)
Reason: Because the statement int i = strlen (str); stores the value 17 in i which is null ‘\0’ character. and hence when we run the for loop
For (; i>=0; rev[j++] = str[i--]);
rev [j] = ‘\0’; it first stores str [i] (nothing but null ‘\0’ character) in rev [j]. so once we print the output statement puts (rev); terminates because of null value a 0th location of string rev.
8. Answer : (a)
Reason: Because the initial values of variables is int a=10, b=11, c=13. then first a=c is evaluated
and hence a = 13 now. Then b += a (b = b + a) sets b to 24. then c=a + b + c is c=13+24+13 = 50 which is assigned to d. or other wise always the right most of the comma separated expression is itself the answer that can be assigned to variable at left hand side.
9. Answer : (e)
Reason: Syntax error because break can’t be used with selection control structures, but can be used with repetitive, or select control structure.
if( i != 0)
{ error
break;

}
10. Answer : (b)
Reason: Because the size at the declaration can’t be Any constant (what if it is 34.67 float constant ???), can’t be Any variable (what if it is some var where var is float variable ???), and Any initialized integer variable is also not allowed.
11. Answer : (c)
Reason: const, # define can be used to create any constant of any primitive data type, but
Enumerations are only used to create the integer symbolic constants
12. Answer : (b)
Reason: It is same as variable length arguments list. See any book for variable length arguments list. Be keen at printf (“%d, ”, ++(*p++));
13. Answer : (d)
Reason: Because this form %*format_specifier suppress the input and do not store it in any variable,and hence 456 is skipped.
14. Answer : (a)
Reason: Because of pointers (*pa=&a, *pb=&b) and pointer to pointer (**ppa=&pa) and the short form operators (+= )
pb=pa; a += *pb += **ppa; printf(“ %d, ” “%d, ” “%d”, *pa, b, **ppa);
15. Answer : (e)
Reason: Because of the set of statements struct temp *z = &a; z->i = 1, z->j = 10; pointer is pointing to first field and hence in response to printf(“%d, “, *p); it printed the value of j (see the
order int j, i; in structure definition temp). And once the pointer is incremented it crosses
the current structure variable (a) and pointing to some other location. So in response to
printf (“%d”, *p); the output was 0.
16. Answer : (b)
Reason: Because there is no width specification on input, hence a single input value 123456 is
assigned to first variable i.e., a, and b, c are not assigned with any value.
17. Answer : (c)
Reason: The loop will run infinitely, because the statement static int x; causes the value to x be set a 0, and in do – while (x >= 0), loop we are decrementing (--x) and after again incrementing
the value of x by one in printf (“%d”, x++); so the value of x remains 0 for each iteration of
loop. And hence infinite loop
18. Answer : (d)
Reason: Because according to the definition of fopen () it returns the pointer to the FILE pointer in success and returns NULL in failure (means could not open the file for writing). Hence it is
the most suitable answer when compared with all the other options given.
19. Answer : (a)
Reason: Because FILE is a pre-defined (system defined) structure of type FILE. Hence the given statement FILE *fptr; only creates a pointer to the pre-defined structure type FILE, also
FILE it neither a user-defined structure type (FILE) nor a pre-defined data type (FILE
DESCRIPTOR) by definition of itself. Also no file is created without using fopen ().
20. Answer : (e)
Reason: Because the getchar () function reads the input characters until enter key is pressed, and returns a single character only ( the first character in the input )
21. Answer : (a)
Reason: Because whatsoever the value of a may be the statement b = a<<1; shifts the bits of a
towards left by one position. Hence it is same a multiplying the number (a) by 2
22. Answer : (e)
Reason: Because all these operators ( ++ -- ++= )are having the same associativity.
23. Answer : (c)
Reason: Because in enum declaration all the enum members are going to be initialized by a default of zero for first element and plus one to previous for the remaining all. And if the first
element is assigned a value explicitly( red = -1 in these case ) with some integer value then
the remaining all are going to be initialized with plus one with the previous element. And
hence green = 0, blue =1
24. Answer : (d)
Reason: Because it is 9+7+5+3+0, where 9+7+5+3 for else return (n-1 + fun (n-2)); and
+0 for if (n = = 1 n = = 2) return 0;
25. Answer : (b)
Reason: Because getchar [getchar ()] is a name of the predefined unformatted character input
function and the remaining all are the key words.
26. Answer : (d)
Reason: Because all the ways are valid for accessing the members of a structure variable (svar)
pointed by pointer (pv) svar.acno pv->acno (*pv).acno
27. Answer : (c)
Reason: Because the following given code
void fun(char *s1, char *s2)
{
while(*s2) *s1++ = *s2++;
*s1 = ‘\0’;
}
copies the contents of s2 to s1 string and the call was made as
fun (s2, s1); where s1 = “monkey”
and the display was called for s2 as puts(s2 )
28. Answer : (b)
Reason: Because according to the definition of the binary files while working with the binary files
no character translation takes place
29. Answer : (e)
Reason: Because the prototype of the function islower () is int islower (char); Hence it returns 1
for true (if char is a lower case character) and 0 for false (if not)
30. Answer : (a)
Reason: Because according to the definition of sprintf (), it works with the strings as printing the
values in the string instead of console

No comments: