C Programming Quiz – Set 3

C Programming Questions and Answers -

#1: Predict the output of the following C snippets?

int main()
{
	char *g = NULL;
	char *h = 0;
	if (g)
		printf(" g ");
	else
	printf("nullg");
	if (h)
		printf("h\n");
	else
		printf(" nullh\n");
}

a) nullg nullh
b) Depends on the compiler
c) g nullh
d) g h

Show Answer

#2: What is the output of this C code?

void main ()
{
	int a=2;
	if(a==2)
	{
		a=~a+2<<1;
		printf("%d",a);
	}
	else 
	{
		a=~a;
	}
}

a) 6
b) -2
c) 0
d) None of above

Show Answer

#3: What is the output of the below c code?

int main()
{
     static int i;
     int j;
     for(j=0;j<=5;j+=2)
     switch(j)
     {
        case 1: i++;break;
        case 2: i+=2;
        case 4: i%=2;j=-1;continue;
        default: --i; continue;
     }
     printf ("%d", i);
     return 0;
}

a) Different address is printed
b) Same address is printed
c) Run time error
d) Nothing

Show Answer

#4: What is the output of this C code?

int main()
{
	int a=8,b=2,c;
	c=a<<b;
	printf("%d",c);
	return 0;
}

a) 32
b) 16
c) 8
d) 4

Show Answer

#5: What is the output of this C code?

int main()
{
	char c='8';
	printf("%d",c);
	return 0;
}

a) 56
b) 16
c) 8
d) 4

Show Answer

#6: What is the output of this C code?

int main()
{
    int x = 100;
    printf("decimal = %d; octal = %o; hex = %x\n", x, x, x);
    printf("decimal = %d; octal = %#o; hex = %#x\n", x, x, x);
    return 0;
}

a) decimal = 100; octal = 414; hex = 46
b) decimal = 100; octal = 414; hex = 64
c) decimal = 100; octal = 144; hex = 64
d) decimal = 100; octal = 144; hex = 46

Show Answer

#7: What is the output of this C code?

struct student
{  };
int main()
{
	struct student s;
	printf("%d", sizeof(s));
}

a) 1
b) 0
c) Compilation Error
d) Runtime Error

Show Answer

#8: What is the output of this C code?

	int main(){
	int a=10;
	switch(0)
	printf("%d",a);
	switch(1)
	printf("%d",a);
	return 0;
}

a) 10 10
b) 10
c) Compilation Error
d) No output

Show Answer

#9: Output?

int main()
{
	printf("%d", max);
}
#define max 23

a) 23
b) 0
c) Compilation Error
d) Runtime Error

Show Answer

#10: Output?

int main()
{
	int arri[] = {1, 2 ,3};
	int *ptri = arri;
	char arrc[] = {1, 2 ,3};
	char *ptrc = arrc;
	printf("sizeof arri[] = %d ", sizeof(arri));
	printf("sizeof ptri = %d ", sizeof(ptri));
	printf("sizeof arrc[] = %d ", sizeof(arrc));
	printf("sizeof ptrc = %d ", sizeof(ptrc));
	return 0;
}

a) sizeof arri[] = 12 sizeof ptri = 8 sizeof arrc[] = 12 sizeof ptrc = 8
b) sizeof arri[] = 12 sizeof ptri = 8 sizeof arrc[] = 3 sizeof ptrc = 8 ANS
c) sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 8
d) sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 8

Show Answer

8 Thoughts on “C Programming Quiz – Set 3

  1. Hello,
    justification for the #3 is wrong, altough the result is correct. Here is the one I get after checking assembly output.
    /*
    * j=0, i=0 -> j=0, i=-1
    * j=2, i=-1 -> j=-1, i=1
    * j=1, i=1 -> j=1, i=2
    * j=3, i=2 -> j=3, i=1
    * j=5, i=1 -> j=5, i=0
    * j=7, i=0
    */

    • Ranjeet Kumar on September 15, 2015 at 4:56 pm said:

      Hi, Can anyone give the proper justification for the question #3 …
      whatever justification here given is not understandable .

  2. Rakesh Thota on January 2, 2016 at 9:30 pm said:

    #10 ans
    sizeof arri[] = 12 sizeof ptri = 4 sizeof arrc[] = 3 sizeof ptrc = 4
    explanation: pointer value is an integer size of integer is 4bytes so two arrays pointer value will be same

  3. Adrian on January 28, 2016 at 7:22 pm said:

    Rakesh Thota, the sizeof a pointer depends on the architecture of that computer (32 or 64 bytes). On a 32 bytes computer, the sizeof a pointer will always be 4 and on a 64 bytes computer, the sizeof a pointer will always be 8. Answer b) it’s correct :)

  4. Give some example

  5. Maniyuvi on May 29, 2019 at 11:57 pm said:

    #10 and is 8 2 4 2

  6. #2 is wrong, left-shift for signed type is Undefined Behavior in C and C++. So it could output `-2`, but there is no guarantee

  7. Ganeshkumar on January 4, 2020 at 12:15 pm said:

    #9 will give compilation error, so ans is c

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation