250+ TOP MCQs on Conditional Inclusion and Answers

C Objective Questions on “Conditional Inclusion”. One shall practice these Objective Questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C Objective Questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C Objective Questions on “Conditional Inclusion” along with answers, explanations and/or solutions:

1. For each #if, #ifdef, and #ifndef directive.
a) There are zero or more #elif directives
b) Zero or one #else directive
c) One matching #endif directive
d) All of the mentioned
Answer: d
Clarification: None.

2. The #else directive is used for _________
a) Conditionally include source text if the previous #if, #ifdef, #ifndef, or #elif test fails
b) Conditionally include source text if a macro name is not defined
c) Conditionally include source text if a macro name is defined
d) Ending conditional text
Answer: a
Clarification: None.

3. What will be the output of the following C code?

  1.     #include 
  2.     #define MIN 0
  3.     #if MIN
  4.     #define MAX 10
  5.     #endif
  6.     int main()
  7.     {
  8.         printf("%d %dn", MAX, MIN);
  9.         return 0;
  10.     }

a) 10 0
b) Compile time error
c) Undefined behaviour
d) None of the mentioned
Answer: b
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     #define MIN 0
  3.     #ifdef MIN
  4.     #define MAX 10
  5.     #endif
  6.     int main()
  7.     {
  8.         printf("%d %dn", MAX, MIN);
  9.         return 0;
  10.     }

a) 10 0
b) Compile time error
c) Undefined behaviour
d) None of the mentioned
Answer: a
Clarification: None.

5. What will be the output of the following C code?

  1.     #include 
  2.     #define MIN 0
  3.     #if defined(MIN) + defined(MAX)
  4.     #define MAX 10
  5.     #endif
  6.     int main()
  7.     {
  8.         printf("%d %dn", MAX, MIN);
  9.         return 0;
  10.     }

a) 10 0
b) Compile time error
c) Undefined behaviour
d) Somegarbagevalue 0
Answer: a
Clarification: None.

6. What will be the output of the following C code?

  1.     #include 
  2.     #define MIN 0
  3.     #if defined(MIN) - (!defined(MAX))
  4.     #define MAX 10
  5.     #endif
  6.     int main()
  7.     {
  8.         printf("%d %dn", MAX, MIN);
  9.         return 0;
  10.     }

a) 10 0
b) Compile time error
c) Undefined behaviour
d) Somegarbagevalue 0
Answer: b
Clarification: None.

7. What will be the output of the following C code?

  1.     #include 
  2.     #define MIN 0
  3.     #ifdef(MIN)
  4.     #define MAX 10
  5.     #endif
  6.     int main()
  7.     {
  8.         printf("%d %dn", MAX, MIN);
  9.         return 0;
  10.     }

a) 10 0
b) Compile time error
c) Run time error
d) Preprocessor error
Answer: d
Clarification: None.

8. What will be the output of the following C code?

  1.     #include 
  2.     #define MIN 0);
  3.     #ifdef MIN
  4.     #define MAX 10
  5.     #endif
  6.     int main()
  7.     {
  8.         printf("%d %dn", MAX, MIN
  9.         return 0;
  10.     }

a) 10 0
b) Compile time error due to illegal syntax for printf
c) Undefined behaviour
d) Compile time error due to illegal MIN value
Answer: a
Clarification: None.

250+ TOP MCQs on Initialization of Pointer Arrays and Answers

C helps anyone preparing for Ericsson and other companies C interviews. One should practice these Objective Questions and answers continuously for 2-3 months to clear Ericsson interviews on C Programming language.

Here is a listing of tough C Objective Questions on “Initialization of Pointer Arrays” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         char *p[1] = {"hello"};
  5.         printf("%s", (p)[0]);
  6.         return 0;
  7.     }

a) Compile time error
b) Undefined behaviour
c) hello
d) None of the mentioned
Answer: c
Clarification: None.

2. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         char **p = {"hello", "hi", "bye"};
  5.         printf("%s", (p)[0]);
  6.         return 0;
  7.     }

a) Compile time error
b) Undefined behaviour
c) hello
d) Address of hello
Answer: b
Clarification: None.

3. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 0, j = 1;
  5.         int *a[] = {&i, &j};
  6.         printf("%d", (*a)[0]);
  7.         return 0;
  8.     }

a) Compile time error
b) Undefined behaviour
c) 0
d) Some garbage value
Answer: c
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 0, j = 1;
  5.         int *a[] = {&i, &j};
  6.         printf("%d", *a[0]);
  7.         return 0;
  8.     }

a) Compile time error
b) Undefined behaviour
c) 0
d) Some garbage value
Answer: c
Clarification: None.

5. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 0, j = 1;
  5.         int *a[] = {&i, &j};
  6.         printf("%d", (*a)[1]);
  7.         return 0;
  8.     }

a) Compile time error
b) Undefined behaviour
c) 1
d) Some garbage value
Answer: d
Clarification: None.

6. Which of the following are generated from char pointer?
a) char *string = “Hello.”;
b)

char *string;
scanf("%s", string);

c) char string[] = “Hello.”;
d) char *string = “Hello.”; and char string[] = “Hello.”;
Answer: a
Clarification: None.

7. Which of the following declaration are illegal?
a) int a[][] = {{1, 2, 3}, {2, 3, 4, 5}};
b) int *a[] = {{1, 2, 3}, {2, 3, 4, 5}};
c) int a[4][4] = {{1, 2, 3}, {2, 3, 4, 5}};
d) none of the mentioned
Answer: a
Clarification: None.

250+ TOP MCQs on Pointer to Structures and Answers

’s MCQs on C helps anyone preparing for placement in Citrix and other companies. Anyone looking for Citrix placement papers should practice these questions continuously for 2-3 months, thereby ensuring a top position in placements.

Here is a listing of C programming questions on “Pointer to Structures” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

  1.     #include 
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student m;
  9.         struct student *s = &m;
  10.         s->c = "hello";
  11.         printf("%s", s->c);
  12.     }

a) hello
b) Run time error
c) Nothing
d) Depends on compiler
Answer: a
Clarification: None.

2. What will be the output of the following C code?

  1.     #include 
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student *s;
  9.         s->c = "hello";
  10.         printf("%s", s->c);
  11.     }

a) hello
b) Segmentation fault
c) Run time error
d) Nothing
Answer: b
Clarification: None.

3. What will be the output of the following C code?

  1.     #include 
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student m;
  9.         struct student *s = &m;
  10.         s->c = "hello";
  11.         printf("%s", m.c);
  12.     }

a) Run time error
b) Nothing
c) hello
d) Varies
Answer: c
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student m;
  9.         struct student *s = &m;
  10.         (*s).c = "hello";
  11.         printf("%s", m.c);
  12.     }

a) Run time error
b) Nothing
c) Varies
d) hello
Answer: d
Clarification: None.

5. What will be the output of the following C code?

  1.     #include 
  2.     struct student
  3.     {
  4.         char *c;
  5.     };
  6.     void main()
  7.     {
  8.         struct student n;
  9.         struct student *s = &n;
  10.         (*s).c = "hello";
  11.         printf("%pn%pn", s, &n);
  12.     }

a) Different address
b) Run time error
c) Nothing
d) Same address
Answer: d
Clarification: None.

6. What will be the output of the following C code?

  1.     #include 
  2.     struct p
  3.     {
  4.         int x[2];
  5.     };
  6.     struct q
  7.     {
  8.         int *x;
  9.     };
  10.     int main()
  11.     {
  12.         struct p p1 = {1, 2};
  13.         struct q *ptr1;
  14.         ptr1->x = (struct q*)&p1.x;
  15.         printf("%dn", ptr1->x[1]);
  16.     }

a) Compile time error
b) Segmentation fault/code crash
c) 2
d) 1
Answer: b
Clarification: None.

7. What will be the output of the following C code?

  1.     #include 
  2.     struct p
  3.     {
  4.         int x[2];
  5.     };
  6.     struct q
  7.     {
  8.         int *x;
  9.     };
  10.     int main()
  11.     {
  12.         struct p p1 = {1, 2};
  13.         struct q *ptr1 = (struct q*)&p1;
  14.         ptr1->x = (struct q*)&p1.x;
  15.         printf("%dn", ptr1->x[0]);
  16.     }

a) Compile time error
b) Undefined behaviour
c) Segmentation fault/code crash
d) 1
Answer: b
Clarification: None.

8. What will be the output of the following C code?

  1.     #include 
  2.     struct p
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p1[] = {1, 2, 3, 4, 5, 6};
  10.         struct p *ptr1 = p1;
  11.         printf("%d %dn", ptr1->x, (ptr1 + 2)->x);
  12.     }

a) 1 5
b) 1 3
c) Compile time error
d) 1 4
Answer: a
Clarification: None.

9. What will be the output of the following C code?

  1.     #include 
  2.     struct p
  3.     {
  4.         int x;
  5.         char y;
  6.     };
  7.     int main()
  8.     {
  9.         struct p p1[] = {1, 92, 3, 94, 5, 96};
  10.         struct p *ptr1 = p1;
  11.         int x = (sizeof(p1) / sizeof(struct p));
  12.         printf("%d %dn", ptr1->x, (ptr1 + x - 1)->x);
  13.     }

a) Compile time error
b) Undefined behaviour
c) 1 3
d) 1 5
Answer: d
Clarification: None.

250+ TOP MCQs on Formatted Input and Answers

C test on “Formatted Input”. One shall practice these test questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C test questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C test questions on “Formatted Input” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int n;
  5.         scanf("%d", n);
  6.         printf("%dn", n);
  7.         return 0;
  8.     }

a) Compilation error
b) Undefined behavior
c) Whatever user types
d) Depends on the standard
Answer: b
Clarification: None.

2. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         char *n;
  5.         scanf("%s", n);
  6.         return 0;
  7.     }

a) Compilation error
b) Undefined behavior
c) Nothing
d) None of the mentioned
Answer: b
Clarification: None.

3. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         char n[] = "hellonworld!";
  5.         char s[13];
  6.         sscanf(n, "%s", s);
  7.         printf("%sn", s);
  8.         return 0;
  9.     }

a) hellonworld!
b)

hello
world!

c) hello
d) hello world!
Answer: c
Clarification: The array n contains a string which has a newline character in between the strings “hello” and “world”. A newline character is considered as a whitespace character for inputs for the scanf(), sscanf() and fscanf() functions. So, the sscanf() function will only copy upto the string “hello” into the array s. Hence, the output of the printf() function be only the string “hello”.

4. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%hd", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

a) Compilation error
b) Undefined behavior
c) Whatever user types
d) None of the mentioned
Answer: c
Clarification: None.

5. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%*d", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

a) Compilation error
b) Somegarbage value
c) Whatever user types
d) Depends on the standard
Answer: b
Clarification: None.

6. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%*hd", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

a) Compilation error
b) Somegarbage value
c) Whatever user types
d) Depends on the standard
Answer: b
Clarification: None.

7. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         short int i;
  5.         scanf("%h*d", &i);
  6.         printf("%hd", i);
  7.         return 0;
  8.     }

a) Compilation error
b) Undefined behavior
c) Somegarbage value
d) Depends on the standard.
Answer: a
Clarification: None.

8. Which of the following is NOT a delimiter for an input in scanf?
a) Enter
b) Space
c) Tab
d) None of the mentioned
Answer: d
Clarification: None.

9. If the conversion characters of int d, i, o, u and x are preceded by h, it indicates?
a) A pointer to int
b) A pointer to short
c) A pointer to long
d) A pointer to char
Answer: b
Clarification: None.

250+ TOP MCQs on Mathematical Functions and Answers

C questions and puzzles on “Mathematical Functions”. One shall practice these questions and puzzles to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C questions and puzzles on “Mathematical Functions” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

  1.     #include 
  2.     #include 
  3.     int main()
  4.     {
  5.         int i = 90;
  6.         printf("%fn", sin(i));
  7.         return 0;
  8.     }

a) Compile time error
b) Undefined behaviour
c) 0.893997
d) 1.000000
Answer: c
Clarification: None.

2. What will be the output of the following C code?

  1.     #include 
  2.     #include 
  3.     int main()
  4.     {
  5.         unsigned int i = -1;
  6.         printf("%fn", fabs(i));
  7.         return 0;
  8.     }

a) Compile time error
b) 1
c) -1
d) None of the mentioned
Answer: d
Clarification: None.

3. function fabs defined math.h header file takes the argument of type integer.
a) True
b) False
c) Depends on the implementation
d) Depends on the standard
Answer: b
Clarification: None.

4. log(x) function defined in math.h header file is __________
a) Natural base logarithm
b) Logarithm to the base 2
c) Logarithm to the base 10
d) None of the mentioned
Answer: a
Clarification: None.

5. What will be the output of the following C code?

  1.     #include 
  2.     #include 
  3.     int main()
  4.     {
  5.         int i = 10;
  6.         printf("%fn", log10(i));
  7.         return 0;
  8.     }

a) Compile time error
b) 1.000000
c) 2.302585
d) None of the mentioned
Answer: b
Clarification: None.

6. What type of inputs are accepted by mathematical functions?
a) short
b) int
c) float
d) double
Answer: d
Clarification: None.

7. In linux, apart from including math header file, the program is successfully executed by which of the following?
a) cc filename.c
b) cc filename.c -lc
c) cc -math filename.c
d) cc -lm filename.c
Answer: d
Clarification: None.

8. Which of the following is not a valid mathematical function?
a) frexp(x);
b) atan2(x,y);
c) srand(x);
d) fmod(x);
Answer: d
Clarification: None.

250+ TOP MCQs on Typedef and Answers

C Multiple Choice Questions & Answers (MCQs) on “Typedef”.

1. Which of the following keywords is used to define an alternate name for an already existing data type?
a) default
b) volatile
c) typedef
d) static
Answer: c
Clarification: The keyword typedef is used to define an alternate name for an already existing data type. It is mostly used for used defined data types.

2. We want to create an alias name for an identifier of the type unsigned long. The alias name is: ul. The correct way to do this using the keyword typedef is ____________
a) typedef unsigned long ul;
b) unsigned long typedef ul;
c) typedef ul unsigned long;
d) ul typedef unsigned long;
Answer: a
Clarification: The syntax of the keyword typedef is: keyword ;
Hence if we want to create an alias name (ul) for an identifier of type unsigned long, the correct way to do this would be: typedef unsigned long ul;

3. What will be the output of the following C code?

#include
main()
{
    typedef int a;
    a b=2, c=8, d;
    d=(b*2)/2+8;
    printf("%d",d);
}

a) 10
b) 16
c) 8
d) error
Answer: a
Clarification: In the code shown above, the keyword typedef is used to give an alias name (a) to an identifier of the type int. The expression on evaluation gives the answer 10. Hence the output of the code shown above is 10.

4. WWhat will be the output of the following C code? (If the name entered is: )

#include
#include
typedef struct employee
{
    char  name[50];
    int   salary;
} e1;
void main( )
{
    printf("Enter Employee name");
    scanf("%s",e1.name);
    printf("n%s",e1.name);
}

a) .name
b) n
c)
d) Error
Answer: d
Clarification: The code shown above will result in an error because we have used the data type e1 (defined using the keyword typedef) in the form of an identifier.

5. The keyword typedef cannot be used to give alias names to pointers.
a) True
b) False
Answer: b
Clarification: The statement given in the question is incorrect. The keyword typedef can be used to give an alias name to all data types as well as pointers.

6. What is the size of myArray in the code shown below? (Assume that 1 character occupies 1 byte)

typedef char x[10];
x myArray[5];

a) 5 bytes
b) 10 bytes
c) 40 bytes
d) 50 bytes
Answer: d
Clarification: The size of myArray will be equal to 50 bytes. In the code shown above, we have defined a character array x, of size 10. Hence the output of the code shown above is 10*5 = 50 bytes.

7. We want to declare x, y and z as pointers of type int. The alias name given is: intpt The correct way to do this using the keyword typedef is:
a)

    int typedef* intptr;
    int x,y,z;

b)

     typedef* intptr;
     int x,y,z;

c)

    int typedef* intptr;
    intptr x,y,z;

d)

     typedef int* intptr;
     intptr x,y,z;

View Answer

Answer: d
Clarification: It shows the correct way to declare x, y and z as pointers of type int using the keyword typedef. The advantage of using typedef with pointers is that we can declare any number of pointers in a single statement.

 
 

8. Consider this statement: typedef enum good {a, b, c} hello; Which of the following statements is incorrect about hello?
a) hello is a typedef of enum good
b) hello is a structure
c) hello is a variable of type enum good
d) the statement shown above is erroneous
Answer: a
Clarification: The keyword typedef is used to give an alternate name to an existing data type. Hence hello is the new name for enum good.

9. One of the major difference between typedef and #define is that typedef interpretation is performed by the _________________ whereas #define interpretation is performed by the _____________
a) pre-processor, compiler
b) user, pre-processor
c) compiler, pre-processor
d) compiler, user
Answer: c
Clarification: The major difference between typedef and #define is that the typedef interpretation is performed by the compiler whereas #define interpretation is performed by pre-processor.

10. What will be the output of the following C code?

#include
int main()
{
    typedef union a
    {
        int i;
        char ch[2];
    }hello;
    hello u;
    u.ch[0] = 3;
    u.ch[1] = 2;
    printf("%d, %d", u.ch[0], u.ch[1]);
    return 0;
}

a) 2, 3
b) 3, 2
c) 32
d) error
Answer: b
Clarification: In the code shown above, we have defined hello, which is the instance variable of a union (a) using typedef. On the execution of the code shown above, we obtain the output: 3, 2