C Operators and Expressions Set 1

Ques 1: What will be the output of the following program?
#include<stdio.h>
int main()
{
    int a[]={0,1,2,3,4};
    int *ptr = a+3;
    int x = *ptr++;
    printf("%d %d %d",a[3],x,*ptr);
    return 0;
}
A. 3 3 4
B. 4 3 4
C. 4 4 3
D. None of these
Description
Answer: A
first *ptr i.e. 3 will be assign to x then pointer will be incremented by one as it is post increment.

Ques 2: What will be the output of the following program?
#include<stdio.h>
int main()
{
    int arr[] = { 0, 1, 2, 3, 4 };
    int *p1 = arr + 1;
    int *p2 = p1 + 2;
    int a = ++*p1 + *p2++;
    int b = *++p1 + *p2--;
    printf("%d %d", a, b);
    return 0;
}
A. 6 7
B. L value required error
C. 5 6
D. None of these
Description
Answer: C
first *p1 will be incremented then for *p2++ pointer will be incremented not value in the same way in line 2.

Ques 3: What will be the output of the following program?
#include<stdio.h>
int main()
{
    float f=10.0;
    double d=20;
    printf("%d %d\n",sizeof(f),sizeof(*&f));
    printf("%d %d\n",sizeof(d),sizeof(*&d));
    return 0;
}
A. 4 4
   8 4
B. 4 4
   8 8
C. Compile time error
D. Compiler dependent
Description
Answer: B

Ques 4: What will be the output of the following program?
#include<stdio.h>
int main()
{
    int x=5,y=5,r1,r2;
    r1 = x++==++y;
    r2 = x>6<<4;
    printf("%d %d",r1,r2);
    return 0;
}
A. 1 16
B. 0 0
C. 0 16
D. Compile time error
Description
Answer: B

Ques 5: What will be the output of the following program?
#include<stdio.h>
int main()
{
    int x=sizeof(int),sizeof(float),sizeof(char);
    printf("%d",x);
    return 0;
}
A. 4
B. 1
C. Compile time error
D. None of these
Description
Answer: C

Ques 6: What will be the output of the following program?
#include<stdio.h>
int main()
{
    int x=(sizeof(int),sizeof(float),sizeof(char));	
    printf("%d",x);
    return 0;
}
A. 1
B. 4
C. Compile time error
D. None of these
Description
Answer: A

Ques 7: What will be the output of the following program?
#include<stdio.h>
int main()
{
    int x,a=20;
    x = a++, a--, ++a;
    printf("%d %d",x,a);
    return 0;
}
A. 20 20
B. 20 21
C. 21 21
D. None of these
Description
Answer: B

Ques 8: What will be the output of the following program?
#include<stdio.h>
int main()
{
    int x=0,y=1;
    printf("%d %d",x>y,(x>y?x:y)++);
    return 0;
}
A. Compile time error
B. 0 2
C. 0 1
D. None of these
Description
Answer: A

Ques 9: What will be the output of the following program?
#include<stdio.h>
int main()
{
    int x=0,y=1,z=2;
    printf("%d",sizeof(printf("my program"))-(x>y?z:y>z?x:y));
    return 0;
}
A. my program3
B. 3
C. 4
D. Compile time error
Description
Answer: B

Ques 10: What will be the output of the following program?
#include<stdio.h>
int main()
{
    int x=1,y=2,val;
    val = x>y?~x:~y==0;
    printf("%d",val);
    return 0;
}
A. 2
B. 1
C. 0
D. Compile time error
Description
Answer: C


comments powered by Disqus