C101 Mid Semister Examination detailed solutions


1. Any statement which is not part of any case is called unreachable statement and it will never gets executed. Such statements will be ignored by the compiler and no error or warning gets generated.
ANS: b

2. ANS: d, infinite.
As 'i' is declared as unsigned integer it never gets decremented below '0' and the loop never terminates.

3. z = x+++y; gets expanded as z = x++ + y; now z will be 7 and then x gets incremented to 3.
ANS: a

4. %% can be used specifically. But \% works in most cases with gcc (not always).
ANS: c

5. x-=y+1;
This short hand operator ( -= ) will be expanded as follows x = x - (y+1) which is equivalent to x = x-y-1
ANS: a

6. -1 is internally represented in binary format as All '1' (2's complement notation). When All '1' is converted
into unsigned integer will give us max value of unsigned integer.
ANS: d

7. Syntax error. We cannot break a string across sentences. If you want to break it then do it this way,
printf("he \
llo");

ANS: d

8. Relational operators gets evaluated from left to right. This expression continue to get evaluated fully
as value of the expression cannot be determined any where in the middle.

~ is bit wise not which inverts all bits of a number passed as argument. "arg1 << arg2" shifts arg2 number of
bits of number arg2 to left, arg2 zeroes are added to number on right.

now ~(~0 << 4) is 15.

ANS: d

9. When an integer is added/subtracted/multiplied/divided with an unsigned integer then unsigned integer will be
converted to integer and the operation is performed.
Signed and unsigned are of same size in bytes. rember that sizeof operator will return the size in bytes required
to store the object passed as argument, here integer and unsigned integer.

ANS: a) 0, -21

10. fun(i) takes a integer and returns a value one greater than i. So, i will be 11 after the function call.
-1 << 4 will be -16 (recall from question 8).

ANS: d

11. It prints c = 2. c = - -2 is treated as c = -(-2).

ANS: d

12. Prints hai\
reason: \\ - prints a \
\r moves cursor to the begging of the line (the same line).
Then the next printed characters will replace the previously printed characters.

ANS: c

13. In Function arguments get evaluated from right to left. Start doing it from right and figure out ;-).

ANS: B

14. Just note difference between b++ and ++b. if an expression contains b++, then in expression evaluation of
value of b is used and after expression evaluation b gets incremented. where as in case of ++b, b gets
incremented first and then used in the expression evaluation.

ANS: C

15. Recall that ','(comma operator) will return the last evaluated expression value. So every time the loop
condition gets evaluated we will get the value of j < 25 as condition. Which makes the loop run from 25
iterations. For each iteration i, j are incremented in increment section of for loop.
Which makes i = 25, j = 25
ANS: c

16. printf fuctions returns number of characters printed on the screen. See the loop as normal loop, instead
of i or j we have a printf statement. The condition will be false when i becomes 9. Till then
"12 "(from initialization), "11 " (from first evaluation of condition), "11 " (from increment),
"10 " (from 2nd evaluation of condition), "10 " (from increment section), "9 " (from condition). NOW condition
becomes false and loop exit.

ANS: "12 11 11 10 10 9 "

17. x = y%2 will return the value assigned to x, which is zero. So if body is not entered. z left uninitialized.
ANS: b

18. j || i++ && printf("YOU CAN"). In this expression after seeing j the value of expression is known to be true.
as j is true. So True (1) gets assigned to j.

ANS: B

19. Comma operator returns the value of last evaluated expression. So, 1 is returned and gets assigned to i.

ANS: B

20. default case can be placed any where in switch statement but it will be executed only if all cases are failed.

ANS: D

21. Default return of value of a function in c is int.

ANS: B

22. comma operator will return value of last evaluated expression. Here it is y. So condition is true and
"Hello world" gets printed.

ANS: A

23. In C, C++, Java the higher dimensional arrays are stored in row major order meaning the elements will be stored consecutively, one row followed by the other. The formula to calculate the address of the element a[i][j] = a + ( i * col_size + j ) * sizeof(datatype). By substituting the values given in the problem we would get a[4][5]= 2005 + ( 4 * 20 + 5 ) * 4 = 2345

ANS: C

24. Header files contains only declarations or prototypes of the functions not their definitions. Please look at any header file in /usr/include directory like 'stdio.h' for sample, u will figure out the same.

ANS: C

25. The path of C language header files is : /usr/include  ( Please try to see the content of the header files )

ANS: C

26. The function "recursive" is a useless function which does nothing and miss the terminating condition of the recursion, so the recursion continues till the entire Stack area is used up by the program and when the program sees that Stack space is exhausted, it give a runtime error saying "Stack overflow".

ANS: B

27. If you take 2 or 3 sample values for a and b and work out the code, you will find that this program will calculate the LCM of a and b.

ANS: B

28. Array elements are as good as any other variable and thus can be incremented or decremented without any problem. So compiler will not give any warning and each array element is incremented 3 times. so it would print 4,7,6.

ANS:C

29. This is trivial, various parts of a while loop are put together in a single line in a for loop.

ANS: B

30. In a case label you can either use a integer constant or a character constant but not a string constant.

ANS: C


 

Some useful suggestions :


1.Try to read the lecture notes after going back to hostel if possible immediately after the class, in this way you will absorb the concepts very fast and lay your hands on the machine, do experiments with programs and try to learn new things on your own. If possible get the previous placement papers available at 'Navya' server and try to test your skills on one question paper at a time by keeping time limit as well, which will help you to assess your capability ( concentrate more on the papers of companies in which u want to be likely placed )

2.Please don't miss lectures and tutorials because they will cover both concepts and applications for the placements. I think the lecture notes given in the class is more than sufficient for the C language and try to go through Kernighan and Ritchie whenever you have any doubts. I also recommend another book " Programming with C - Byron Gottfried, Schaumm's Outline Series " which is very well written and most widely used book in world.

3.Most of the students have not worked on the question paper space to solve the questions instead they have done calculations on the fly in their mind. Please try to avoid it, even best of the programmers can do silly mistakes in over confidence. Try to use paper and pencil to work out the program by tracking the various variables just like the way i do in the class.

4.Most of the students were unable to manage their time for the exam, as i observed during the invigilation. A Placement written test is as good as any other competitive exam which can be framed as an optimization problem as below :

Maximize : Total marks

Subject to constraints : 1. Time_taken <= Duration of exam 2. Errors <= epsilon ( which is very small )

U don't need either a Operations research technique like Simplex method to solve this optimization problem, instead you require simple commonsense and strong basics of the concepts. Before answering the question paper, please try to read entire question paper once, by doing this you will know the level of the question paper. Every question has equal weight-age and make a note that you don't get extra credit for solving harder problems. So try to finish off easy questions first and then try to solve difficult questions iteratively one after another. According to the principle of relative grading, its not important how much you have performed rather more important thing is how better you have performed compared to your neighbour. So don't do mistakes in easy questions which will be solved by everyone any how and try to solve as many difficult problems as you can which will help you to always be in a safe position.

5.Try to improve your communication skills also, its not only technical knowledge which matters in placements, this factor also plays a very important role either in HR interview or technical interview. You should be in a position to sell yourself better in the present market :) .. Also practice puzzles which is most common in almost all the written tests or interviews.   

I hope all of you will perform very good in the upcoming placements and get very good jobs. All the very best.