Showing posts with label c programming questions with explanation. Show all posts
Showing posts with label c programming questions with explanation. Show all posts

Thursday, March 2, 2017

C Programming Questions with Explanation

Q:: C programming:::
What will be the output?
void main()
{
         static int i=0;
         if(++i)
        {
                  printf("℅d",i);
                  main();
        }
}

(A) 1,1,1,1...
(B) 0,0,0,0...
(C) 1,2,3,4...
(D) None of these

Answer:: (D)

Explanation:

It will go upto range of integer here we are considering int takes 2 bytes.

1,2,3,4 ...32766,32767,-32768,-32767...-2,-1

Wednesday, March 1, 2017

C Programming Questions with Explanation

Q::What will be the output::

void main()
{
              static char ch=1;
              while (ch++)
               {
                          printf("℅d",ch);
               }
}

(A) error
(B) It will run infinite time
(C) Prints  2,3,4...127 ,-128-127-126...-1
(D) Prints  2,3,4...127, -128-127-126...-1,0
     
Answer:: (D)

Explanation::

Firstly ch= 1 so , control enter the while loop and ch is incremented by 1
and ch =2    gets printed
going on this way the value of ch will become 127 and again it will be incremented by 1 which will make it -127 which is again true in C Language then -126,-125... -1,0 also get printed . Before printing value of ch is always incremented by 1.

Tuesday, February 28, 2017

C Programming Questions with Explanation

Q:: What will be the output::
void main()
{
          int x;
          scanf("%d",&x);
          if((x)&&(!x))
               printf("Hello");
          if((x)||(!x))
               printf("World");
}
(A) HelloWorld                                                       (B) World
(C)Hello                                                                  (D) Depends upon value of 'x'

Ans:: (B)
Explanation::

In C programming the logical not operator performs the complement operation i.e. it converts a condition from true to false or false to true. In other words it converts a non-zero number to zero and zero to one.
So the first 'if' will always be false and the second 'if' always be true.

Monday, February 20, 2017

C Programming Questions with Explanation

The following statement ::
     
               printf("%d",++5);
will print:

a)5  b) 6  c) error  d)garbage

Answer:: (C)

Explanation::

The statement will be expanded like
         5=5+1;
i.e. a constant on left side of expression which is not allowed in 'C' hence it will generate error.

Sunday, February 19, 2017

C Programming Questions with Explanation

Q:: What will the output of following?

           for(i=0;i<10;++i)
                   printf("%d",i&1);
(a)0101010101 (b)0111111111
(c)0000000000 (d)1111111111

Answer :: (A)
Explanation::

Binary representation of 1 is 0000000000000001 in 16 bit

In first iteration i=0

     i=0000000000000000
    1=0000000000000001
i&1=0000000000000000=(0)

In second iteration i=1

     i=0000000000000001
    1=0000000000000001
i&1=0000000000000001=(1)

In third iteration i=2

     i=0000000000000000
    1=0000000000000010
i&1=0000000000000000=(0)
In fourth iteration i=3

     i=0000000000000011
    1=0000000000000001
i&1=0000000000000001=(1)

So it will produce 0 when 'i' is even
and  '1' if 'i' is odd
Output will be 0101010101

Coding Acceleration Program

🚀 CODING ACCELERATION PROGRAM (90 DAYS) Build Strong Foundations Learn to Think Like a Programmer Get Placement Ready 💡 Learn C Progra...