C Programming Quiz – Set 1

Programming Questions & Answers -
You should practice these quizzes to improve your C programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams.

#1: Which is valid C expression?
a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;

Show Answer

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

#include <stdio.h>
int main()
{
	printf("Hello World! %d \n", x);
	return 0;
}

a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!

Show Answer

#3: What will happen if the below program is executed?

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

a) It will cause a compile-time error
b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will experience infinite looping

Show Answer

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

    #include  <stdio.h>
    int main()
    {
       char chr;
       chr = 128;
       printf("%d\n", chr);
       return 0;
    }

a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned

Show Answer

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

    #include <stdio.h>
    int main()
    {
        char *p[1] = {"hello"};
        printf("%s", (p)[0]);
        return 0;
    }

a) Compile time error
b) Undefined behaviour
c) hello
d) None of the mentioned

Show Answer

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

    #include <stdio.h>
    int main()
    {
        printf("crazyfor\code\n");
        return 0;
    }

a) crazyforcode
b) crazyfor
code
c) codeyfor
d) crazyfor

Show Answer

#7: Which among the following is NOT a logical or relational operator?
a) !=
b) ==
c) ||
d) =

Show Answer

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

    #include <stdio.h>
    int main()
    {
        int a = 10;
        if (a == a--)
            printf("TRUE 1\t");
        a = 10;
        if (a == --a)
            printf("TRUE 2\t");
    }

a) TRUE 1
b) TRUE 2
c) TRUE 1 TRUE 2
d) No output

Show Answer

#9: The scope of an automatic variable is:
a) Within the block it appears
b) Within the blocks of the block it appears
c) Until the end of program
d) Both (a) and (b)

Show Answer

#10: Default storage class if not any is specified for a local variable, is auto
a) true
b) false
c) Depends on the standard
d) None of the mentioned

Show Answer

0 Thoughts on “C Programming Quiz – Set 1

  1. Aiswarya on October 7, 2014 at 6:04 pm said:

    Can anybody please explain me why is the answer for q#8
    #include
    int main()
    {
    int a = 10;
    if (a == a–)
    printf(“TRUE 1\t”);
    a = 10;
    if (a == –a)
    printf(“TRUE 2\t”);
    }
    is TRUE1 TRUE2

    • Prashant on October 15, 2014 at 4:06 am said:

      Its True 2.

    • the answer shold be TRUE 1

    • surendra maharjan on January 2, 2015 at 10:23 am said:

      there is a term called “sequence point” in c programming. Until the squence point is not over in the expression, the side effect of the operation remains in the expression and standard c doesn’t guarantee the output.

      Here, the statement
      if(a == a–) there is a side effect and compiler doesn’t guarantee its result.
      similar for if(a == –a).
      In gcc compiler the output is TRUE2 which is not matching with the output given in the solution of the question.

    • See TRUE1 should be clear to you. And TRUE2 is also true because –a is evaluated before the whole expression so a becomes 9 before comparison starts hence on both side its 9 == 9 that is true.

  2. Vignesh M on June 21, 2015 at 9:33 am said:

    Operator precedence

  3. pagsiu on June 15, 2016 at 3:50 pm said:

    #6 is wrong, there is no \c escape sequence…

  4. atanu on July 29, 2016 at 1:32 am said:

    To,
    surendra maharajan
    1st let me tell u that — before variable is knowm as prefix operator and other one is postfix operator,
    in prefix operator first the value of the variable is changed and then its assigned to the left variable,
    in case of if(a==a–),
    so the value of a is defined to be 10,and when a– is executed it first decreases the value of a by 1 below(==) operator and now the value of a is 9 and now the work of (a–) comes thats it has to decrease by 1,since the value of a is now 9 the value will become 8 for (a–),
    now what happens in
    if(a=–a)
    value of a=10;
    first it will become 9 due to(–a) and then it will assign to 9 to the left variable,so the value of a is 9 on both side,

    thank u

  5. balaji ramani on August 5, 2016 at 2:48 pm said:

    for #8 given answer is correct, a becomes 9 when –a and comparison results if(9==9) ok, what happens if (–a==a) in this case also TRUE2 is coming

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