Menu

Saturday, November 28, 2015

UGC NET Notes Series: C++(Part 1)

Interesting Code Problems


I have given here some tricky code snippets. The answers with explanation are also given at the end. But, for the sake of learning, you must first run the code yourself before reading the answers. Your practical hands-on experience will provide a stable learning experience rather than just reading the answers.

 What will be the output of the code snippets given below?

1
for(int a=1;a<=10;a=a+0.5)
    printf("%f",a);

2.
for(int 1=1;a<=10;a=a+0.5)
    printf("%f",a++);

3.
for(int 1=1;a<=10;a=a+0.5)
    printf("%d",a++);

4.
for(int 1=1;a<=10;a++)
    printf("%f",a);

5.
for(int 1=1;a<=10;a=a+0.5)
    printf("%f",(float)a);

6.
double num=5.2;
printf("%d", sizeof(!num));

7.
int var=5;
printf("%d", sizeof(var=15/2));
printf("%d", var);

8.
printf("%f", (float) 9/5);

9.
Is the below variable declaration valid? Why?
     int x=10, 20, 30;

10. 
What this declaration means?
int (*test) (void) []

11. 
int num=20;
if(10<num)
        printf("yes!");
else
        printf("no!");

Answers to the questions:

Q 1. This code will run into infinite loop. Why? The answer lies in the basics of C. Whenever you assign a value to a variable, assignment rules will be followed according to data type as well as according to LValue and RValue rules. Since a is declared as int, increment of 0.5 will be rounded off to 0 and in effect, 
a=a+0.5 
is nothing but 
a=a+0.
So, code will not progress and will never satisfy the loop condition.

Q 2. The a++ statement will ensure that code does not run into infinite loop and will be terminated after 10 iterations. Now, what will be the output? It will be 0.000000 printed 10 times. 

Why not 1.000000, 2.000000 and so on??

The reason is, the variable is of int type and format string in printf tries to print the value as float, which is not supported by C.

Q 3. This question is very much like previous question, the value of a is incremented by 1 due to "a++". Hence, it will print first 10 natural no.s

Q 4. This code will cause run-time error. In TurboC, it gave error, "Floating-point formats not linked".

Q 5. The code is very similar to Q 1. The only difference is that we have typecasted the output value to float before sending to output. The effect is that, the infinite loop will keep on printing 1.000000.

Q 6. This code beautifully manipulates rules of C language. "!num" statement actually means "not true." Since num variable stores a positive value (true). ! negates it and is interpreted as not true, i.e. 0. Since 0 is an integer constant, the sizeof operator will return 2. Hence, output will be 2.

Q 7. Interestingly, assignment statement within sizeof operator does not assign any new value to var variable. Hence, the second printf statement will print the original value of the variable.

Q 8. The (float) will first convert the result of "9/5" into float, hence, the result of division operation will be treated as float and will print it as a float variable.(1.800000).

Q 9. The declaration is not valid because x is not an array. An ordinary variable can be assigned only one value at a time.

Q 10. It declares test as pointer to function of void type returning an array of int type.

Q 11. C language never prohibits you to compare a literal on LHS with a variable on RHS or vice versa. Hence, it will process the if condition correctly as "if (10<20)" and will return false.

No comments:

Post a Comment