Saturday 31 October 2015

C operator questions with answers

C programming tricky objective type operators questions and answers with explanation for written test and interview
(1)
What will be output of the following program?

#include<stdio.h>
int main(){
    float a=0.7;d 
    if(a<0.7){
         printf("C");
    }
    else{
         printf("C++");
    }
    return 0;
}


EXPLANATION

Output: 

Turbo C++ 3.0: c

Turbo C ++4.5: c

Linux GCC: c

Visual C++: c


Explanation: 
0.7 is double constant (Default). Its binary value is written in 64 bit.

Binary value of 0.7 = (0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011 )

Now here variable a is a floating point variable while 0.7 is double constant. So variable a will contain only 32 bit value i.e.

a = 0.1011 0011 0011 0011 0011 0011 0011 0011 while
0.7 = 0.1011 0011 0011 0011 0011 0011 0011 0011 0011 0011 0011....
It is obvious a < 0.7



(2)
What will be output of the following program?
        
#include<stdio.h>
int main(){
    int i=5,j;
    j=++i+++i+++i;
    printf("%d %d",i,j);
    return 0;
}

EXPLANATION

Output: 

Turbo C++ 3.0: 8 24

Turbo C ++4.5: Compilation error

Linux GCC: Compilation error

Visual C++: Compilation error


Explanation:

Rule :- ++ is pre increment operator so in any arithmetic expression it first increment the value of variable by one in whole expression then starts assigning the final value of variable in the expression.

Compiler will treat this expression j = ++i+++i+++i; as
i = ++i + ++i + ++i;

Initial value of i = 5 due to three pre increment operator final value of i=8.
Now final value of i i.e. 8 will assigned to each variable as shown in the following figure:

So,
j=8+8+8
j=24 and
i=8



(3)
What will be output of the following program?

#include<stdio.h>
int main(){
    int i=1;
    i=2+2*i++;
    printf("%d",i);
    return 0;
}

EXPLANATION

Output: 

Turbo C++ 3.0: 5

Turbo C ++4.5: 5

Linux GCC: 5

Visual C++: 5


Explanation:
i++ i.e. when postfix increment operator is used any expression the it first assign the its value in the expression the it increments the value of variable by one. So,
i = 2 + 2 * 1
i = 4
Now i will be incremented by one so i = 4 + 1 = 5



(4)
What will be output of the following program?

#include<stdio.h>
int main(){
    int a=2,b=7,c=10;
    c=a==b;
    printf("%d",c);
    return 0;
}

EXPLANATION

Output: 

Turbo C++ 3.0: 0

Turbo C ++4.5: 0

Linux GCC: 0

Visual C++: 0


Explanation:
== is relational operator which returns only two values.
0: If a == b is false
1: If a == b is true
Since
a=2
b=7
So, a == b is false hence b=0



(5)
What will be output of the following program?

#include<stdio.h>
void main(){
    int x;
    x=10,20,30;
    printf("%d",x);
    return 0;
}

EXPLANATION

Output: 

Turbo C++ 3.0: 10

Turbo C ++4.5: 10

Linux GCC: 10

Visual C++: 10


Explanation :
Precedence table:

Operator
Precedence
Associative
 =
More than ,
Right to left
 ,
Least
Left to right

Since assignment operator (=) has more precedence than comma operator .So = operator will be evaluated first than comma operator. In the following expression
x = 10, 20, 30
First 10 will be assigned to x then comma operator will be evaluated.



(6)
What will be output of the following program?

#include<stdio.h>
int main(){
    int a=0,b=10;
    if(a=0){
         printf("true");
    }
    else{
         printf("false");
    }
    return 0;
}

EXPLANATION

Output: 

Turbo C++ 3.0: false

Turbo C ++4.5: false

Linux GCC: false

Visual C++: false


Explanation:
As we know = is assignment operator not relation operator. So, a = 0 means zero will assigned to variable a. In c zero represent false and any non-zero number represents true.
So, if(0) means condition is always false hence else part will execute.



(7)
What will be output of the following program?

#include<stdio.h>
int main(){
    int a;
    a=015 + 0x71 +5;
    printf("%d",a);
    return 0;
}

EXPLANATION

Output: 

Turbo C++ 3.0: 131

Turbo C ++4.5: 131

Linux GCC: 131

Visual C++: 131


Explanation:
015 is octal number its decimal equivalent is = 5 * 8 ^ 0 + 1 * 8 ^ 1 = 5 + 8 = 13
0x71 is hexadecimal number (0x is symbol of hexadecimal) its decimal equivalent is = 1 * 16 ^ 0 + 7 * 16 ^ 1 = 1 + 112 = 113
So, a = 13 + 113 + 5 = 131



(8)
What will be output of the following program?

#include<stdio.h>
int main(){
    printf("%d %d %d",sizeof(3.14),sizeof(3.14f),sizeof(3.14L));
    return 0;
}

EXPLANATION

Output: 

Turbo C++ 3.0: 8 4 10

Turbo C ++4.5: 8 4 10

Linux GCC: 8 4 12

Visual C++: 8 4 8

Explanation:
3.14f is floating point constant. Its size is 4 byte. 3.14 is double constant (default). Its size is 8 byte. 3.14L is long double constant. Its size is 10 byte. sizeof() operator always return the size of data type which is written inside the(). It is keyword.




(9)
What will be output of the following program?

#include<stdio.h>
int main(){
    int x=100,y=20,z=5;
    printf("%d %d %d");
    return 0;
}

EXPLANATION

Output:

Turbo C++ 3.0: 5 20 100

Turbo C ++4.5: 5 20 100

Linux GCC: Garbage values

Visual C++: 5 100 20

By default x, y, z are auto type data which are stored in stack in memory. Stack is LIFO data structure. So in stack first stores 100 then 20 then 5 and program counter will point top stack i.e. 5. Default value of %d in printf is data which is present in stack. So output is revere order of declaration. So output will be 5 20 100.



(10)
What will be output of the following program?

#include<stdio.h>        
int main(){
    int a=2;
    a=a++ + ~++a;
    printf("%d",a);
    return 0;
}

EXPLANATION

Output: 

Turbo C++ 3.0: -1

Turbo C ++4.5: 0

Linux GCC: 0

Visual C++: 0


Explanation:
Same theory as question (2) and (13).

C Aptitude Questions On Functions With Answers

              C Aptitude Questions On Functions With Answers                    

1. What is the output of the following program?
void f1()
{
    int a=0; 
    ++a;
    printf(“n%d”,a);

int main()
{
    int a=10;
    f1();
    f1();
    f1();
    printf(“%d”,a);
    return 0;
}
Ans: 1 1 1 10

2. What is the output of the following program?
void f1()
{
          static int s=5;
         ++s;
          printf(“n%d”,s);
}
int main()
{
    f1();
    f1();
    printf(“%d”,s);
}
Ans: Error

3. What is the output of the following program?
void f1()
{
     static int s=5;
     ++s;
     printf(“n%d”,s);
}
int main()
{
    f1();
    f1();
}
Ans: 6 7

4. What is the output of the following program?
void abc(int a)
{
   ++a;
}
void main()
{
   int a=10;
   abc();
  abc();
  printf(“%d”,a);
}
Ans: a=10

5. What is the output of the following program?
void abc(int a)
{
       ++a;
}
void main()
{
         int a=10;
         abc(a);
         abc(a);
         printf(“%d”,a);
}
Ans: a=10

6. What is the output of the following program?
void f1()
{
   extern int g;
   static int s=5;
   int a;
   ++g;
   a=s++;
   printf(“n%d %d %d”,a,s,g);
   if(a<=6)
    f1();
   printf(“n%d %d %d”,a,s,g);
}
void f2()
{
   static int s;
   int a;
   a=++s;
   ++g;
   printf(“n%d %d %d”,a,s,g);
   if(a<=2)
   f2();
    printf(“n%d %d %d”,a,s,g);
}
int main()
{
  f1();
  f2();
}
Ans: Error undefine symbol _g(Linking Error)

7. What is the output of the following program?
void abc(int a)
{
   ++a;
   printf(“n%d”,a);
}
void main()
{
   int a=10;
   abc(++a);
   abc(a++);
   printf(“%d”,a);
}
Ans: 12 12 12

8. What is the output of the following program?
void abc(int a,int b)
{
     printf(“n%d %d”,++a,++b);
}
void main()
{
    int a=10;
    abc(++a,a++);
    abc(a++,++a);
    printf(“%d”,a);
}
Ans: 13 11
         14 14
        14

9. What is the output of the following program?
void xyz(int p1,int *p2)
{
   ++p1;
   ++*p2
   printf(“n%d %d”,p1,*p2);
}
void main()

   int a=10;
   xyz(a++,&a);
   xyz(a++,&a);
   printf(“n%d”,a);
}
Ans: 11 12
         13 14
         14

10. What is the output of the following program?
void xyz(int p1,int p2)
{
    ++p1;
    ++p2;
    printf(“n%d %d”,p1,p2);
}
void main()
{
    int a=10;
    xyz(a++,++*(&a));
    xyz(a++,++*(&a));
    printf(“n%d”,a);
}
Ans: 12 12
         14 14
         14

11. What is the output of the following program?
void xyz(int p1,int *p2)
{
   ++p1;
   ++*p2
   printf(“n%d %d”,p1,*p2);
   if(p1<=12)
     xyz(p1,p2);
   printf(“n%d %d”,p1,*p2);
}
void main()
{
    int a=10;
    xyz(a++,&a);
    xyz(a++,&a);
    printf(“n%d”,a);
}
Ans: 11 12
         12 13
         13 14
         13 14
         12 14
         11 14
        15 16
        15 16
        16

12. What is the output of the following program?
#include<stdio.h>
void main()
{
   int a=1;
  void xyz(int,int);
  xyz(++a,a++);
  xyz(a++,++a);
  printf(“n%d”,a);
}
void xyz( int x,int y)
{
   printf(“n%d %d”,x,y);
}
Ans: 3 1
         4  4
         5

13. What is the output of the following program?
#include<stdio.h>
void display(int a)
{
       printf(“n%d”,a);
       if(–a)
      display(a);
      printf(“n%d”,a);
}
void main()
{
     display(4);
}
Ans: 4 3 2 1 0 1 2 3

14. What is the output of the following program?
#include<stdio.h>
void xyz(int x)
{
         printf(“n%d”,x);
        if(x)
        xyz(x-1);
        printf(“n%d”,x);
}
void abc(int a)
{
     printf(“n%d”,a);
     xyz(a);
     if(a)
     abc(a-1);
    printf(“n%d”,a);
}
void main()
{
    abc(2);
}
Ans: 2————>abc
         2 1 0 0 1 2–>xyz
         1————>abc
         1 0 0 1——>xyz
         0————>abc
         0 0———->xyz
        0 abc
        1 abc
        2 abc