250+ TOP MCQs on Pragma and Answers

C MCQs on “Pragma”.

1. The preprocessor directive used to give additional information to the compiler, beyond which is conveyed in the language _____________
a) #include
b) #define
c) #pragma
d) #elif
Answer: c
Clarification: The preprocessor directive #pragma is used to give additional information to the compiler, other than what is conveyed in the language itself.

2. What will be the output of the following C code, if it is run on a 32 bit platform?

#include
#pragma(1)
struct test
{
    int i;
    char j;
};
main()
{
    printf("%d",sizeof(struct test));
}

a) Error
b) 1
c) 4
d) 8
Answer: d
Clarification: #pragma pack(n), where n is the number of alignment in bytes. #pragma pack(1) is the directive for the compiler to pack the structure.

3. In the directive, #pragma pack(n), which of the following is not a valid value of n?
a) 1
b) 2
c) 3
d) 4
Answer: c
Clarification: Valid arguments are 1,2,4 and 8. 3 is not a valid value for n.

4. Which of the following attributes is used to specify that the minimum required memory to be used to represent the types?
a) packed
b) aligned
c) unused
d) deprecated
Answer: a
Clarification: The keyword __attribute__ allows you to specify special attributes of struct type. 6 attributes are currently defined for types: aligned, packed, transparent_union, unused, deprecated and may_alias. The attribute “packed” is used to specify that the minimum required memory to be used to represent the types.

5. In the directive #pragma pack(n), if the value of ‘n’ is given to be 5, then what happens?
a) Error
b) Warning but no error
c) Executes the pragma statement
d) Ignores the pragma statement and executes the program
Answer: d
Clarification: Valid values for n are 1,2,4 and 8. If the value of n is one that the compiler does not recognize, then it simply ignores the pragma statement without throwing any error or warning.

6. The correct syntax of the attribute packed is _________
a) __attribute__((packed));
b) _attribute(packed);
c) _attribute_((packed));
d) __attribute__(packed);
Answer: a
Clarification: The correct syntax of the attribute packed is: __attribute__((packed));

7. The pragma ___________________ is used to remove an identifier completely from a program.
a) GNU piston
b) GCC poison
c) GNU poison
d) GCC piston
Answer: b
Clarification: There are several pragmas defines. One such pragma is GCC poison which is used to remove an identifier.

8. The function of __attribute__((packed)); can also be performed using _________
a) #pragma pack(1);
b) #pragma pack(2);
c) #pragma pack(4);
d) #pragma pack(8);
Answer: a
Clarification: __attribute((packed)); and #pragma(1) are used to perform the same function, that is, to direct the compiler to pack the structure.

9. #pragma GCC poison should be followed by a list of identifiers that are _________
a) even in number
b) odd in number
c) valid
d) invalid
Answer: d
Clarification: #pragma poison GCC is used to remove an identifier from a program. It should be followed by a list of identifiers which are not valid in the program, for example: scanf, printf etc.

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

#include
#pragma GCC poison printf
main()
{
    printf("");
    return 0;
}

a) error is thrown
b) is printed
c) warning but no error
d) yrdnoufnas is printed
Answer: a
Clarification: The code shown above results in an error: attempt to use poisoned printf
When the above program is compiled, it results in an error since #pragma was used to specify that the identifier printf should not be used in the program.

250+ TOP MCQs on Arithmetic Operators and Answers

C test on “Arithmetic Operators”. 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 “Arithmetic Operators” 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 i = -3;
  5.         int k = i % 2;
  6.         printf("%dn", k);
  7.     }

a) Compile time error
b) -1
c) 1
d) Implementation defined
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 3;
  5.         int l = i / -2;
  6.         int k = i % -2;
  7.         printf("%d %dn", l, k);
  8.         return 0;
  9.     }

a) Compile time error
b) -1 1
c) 1 -1
d) Implementation defined
Answer: b
Clarification: None.

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

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

a) Implementation defined
b) 1
c) 3
d) Compile time error
Answer: b
Clarification: None.

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

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

a) Implementation defined
b) -1
c) -3
d) Compile time error
Answer: b
Clarification: None.

5. What will be the final value of x in the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int x = 5 * 9 / 3 + 9;
  5.     }

a) 3.75
b) Depends on compiler
c) 24
d) 3
Answer: c
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int x = 5.3 % 2;
  5.         printf("Value of x is %d", x);
  6.     }

a) Value of x is 2.3
b) Value of x is 1
c) Value of x is 0.3
d) Compile time error
Answer: d
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int y = 3;
  5.         int x = 5 % 2 * 3 / 2;
  6.         printf("Value of x is %d", x);
  7.     }

a) Value of x is 1
b) Value of x is 2
c) Value of x is 3
d) Compile time error
Answer: a
Clarification: None.

250+ TOP MCQs on Precedence and Order of Evaluation and Answers

C Objective Questions on “Precedence and Order of Evaluation”. 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 “Precedence and Order of Evaluation” along with answers, explanations and/or solutions:

1. Which of the following operators has an associativity from Right to Left?
a) <=
b) <<
c) ==
d) +=
Answer: d
Clarification: None.

2. Which operators of the following have same precedence?

P. "!=", Q. "+=", R. "<<="

a) P and Q
b) Q and R
c) P and R
d) P, Q and R
Answer: b
Clarification: None.

3. Comment on the following statement.

   n = 1;
   printf("%d, %dn", 3*n, n++);

a) Output will be 3, 2
b) Output will be 3, 1
c) Output will be 6, 1
d) Output is compiler dependent
Answer: d
Clarification: None.

4. Which of the following option is the correct representation of the following C statement?

a) e = (a * (b +(c /(d * f))));
b) e = ((a * b) + (c / (d * f)));
c) e = ((a * b) + ((c / d)* f));
d) Both e = ((a * b) + (c / (d * f))); and e = ((a * b) + ((c / d)* f));
Answer: d
Clarification: Verified by e = 1 * 2 + 3 / 4 * 5; and then using respective braces according to the option.

5. While swapping 2 numbers what precautions to be taken care?

    b = (b / a);
    a = a * b;
    b = a / b;

a) Data type should be either of short, int and long
b) Data type should be either of float and double
c) All data types are accepted except for (char *)
d) This code doesn’t swap 2 numbers
Answer: b
Clarification: None.

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

  1.     #include
  2.     int main()
  3.     {
  4.         int a = 1, b = 2, c = 3, d = 4, e;
  5.         e = c + d = b * a;
  6.         printf("%d, %dn", e, d);
  7.     }

a) 7, 4
b) 7, 2
c) 5, 2
d) Syntax error
Answer: d
Clarification: None.

7. Which of the following is the correct order of evaluation for the given expression?

a) % / * =
b) / * % =
c) = % * /
d) * % / =
Answer: a
Clarification: None.

8. Which function in the following expression will be called first?

a = func3(6) - func2(4, 5) / func1(1, 2, 3);

a) func1();
b) func2();
c) func3();
d) Cannot be predicted
Answer: d
Clarification: None.

9. Which of the following operator has the highest precedence in the following?
a) ()
b) sizeof
c) *
d) +
Answer: a
Clarification: None.

10. Which of the following is a ternary operator?
a) &&
b) >>=
c) ?:
d) ->
Answer: c
Clarification: None.

250+ TOP MCQs on Functions Returning Non-integers and Answers

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

Here is a listing of online C test questions on “Functions Returning Non-integers” along with answers, explanations and/or solutions:

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

  1.     #include 
  2.     int *m();
  3.     void main()
  4.     {
  5.         int k = m();
  6.         printf("%d", k);
  7.     }
  8.     int *m()
  9.     {
  10.         int a[2] = {5, 8};
  11.         return a;
  12.     }

a) 5
b) 8
c) Nothing
d) Varies
Answer: d
Clarification: None.

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

  1.     #include 
  2.     void m(int k)
  3.     {
  4.         printf("hi");
  5.     }
  6.     void m(double k)
  7.     {
  8.         printf("hello");
  9.     }
  10.     void main()
  11.     {
  12.         m(3);
  13.     }

a) hi
b) hello
c) Compile time error
d) Nothing
Answer: c
Clarification: None.

3. What is the default return type if it is not specified in function definition?
a) void
b) int
c) double
d) short int
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int foo();
  3.     int main()
  4.     {
  5.         int i = foo();
  6.     }
  7.     foo()
  8.     {
  9.         printf("2 ");
  10.         return 2;
  11.     }

a) 2
b) Compile time error
c) Depends on the compiler
d) Depends on the standard
Answer: a
Clarification: None.

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

  1.     #include 
  2.     double foo();
  3.     int main()
  4.     {
  5.         foo();
  6.         return 0;
  7.     }
  8.     foo()
  9.     {
  10.         printf("2 ");
  11.         return 2;
  12.     }

a) 2
b) Compile time error
c) Depends on the compiler
d) Depends on the standard
Answer: b
Clarification: None.

6. Functions can return structure in C?
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
Answer: a
Clarification: None.

7. Functions can return enumeration constants in C?
a) true
b) false
c) depends on the compiler
d) depends on the standard
Answer: a
Clarification: None.

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

  1.     #include 
  2.     enum m{JAN, FEB, MAR};
  3.     enum m foo();
  4.     int main()
  5.     {
  6.         enum m i = foo();
  7.         printf("%dn", i);
  8.     }
  9.     int  foo()
  10.     {
  11.         return JAN;
  12.     }

a) Compile time error
b) 0
c) Depends on the compiler
d) Depends on the standard
Answer: a
Clarification: None.

250+ TOP MCQs on Macro Substitution and Answers

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

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

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

  1.     #include 
  2.     void main()
  3.     {
  4.         #define max 37;
  5.         printf("%d", max);
  6.     }

a) 37
b) Compile time error
c) Varies
d) Depends on compiler
Answer: b
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         #define max 37
  5.         printf("%d", max);
  6.     }

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

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

  1.     #include 
  2.     void main()
  3.     {
  4.         #define const int
  5.         const max = 32;
  6.         printf("%d", max);
  7.     }

a) Run time error
b) 32
c) int
d) const
Answer: b
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         #define max 45
  5.         max = 32;
  6.         printf("%d", max);
  7.     }

a) 32
b) 45
c) Compile time error
d) Varies
Answer: c
Clarification: None.

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

  1.     #include 
  2.     # define max
  3.     void m()
  4.     {
  5.         printf("hi");
  6.     }
  7.     void main()
  8.     {
  9.         max;
  10.         m();
  11.     }

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

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

  1.     #include 
  2.     #define A 1 + 2
  3.     #define B 3 + 4
  4.     int main()
  5.     {
  6.         int var = A * B;
  7.         printf("%dn", var);
  8.     }

a) 9
b) 11
c) 12
d) 21
Answer: b
Clarification: None.

7. Which of the following Macro substitution are accepted in C?
a)

   #define A #define
   A VAR 20

b)

   #define A define
   #A VAR 20

c)

   #define #A #define
   #A VAR 20

d) None of the mentioned
Answer: d
Clarification: None.

8. Comment on the output of the following C code.

  1.     #include 
  2.     #define var 20);
  3.     int main()
  4.     {
  5.         printf("%dn", var
  6.     }

a) No errors, it will show the output 20
b) Compile time error, the printf braces aren’t closed
c) Compile time error, there are no open braces in #define
d) None of the mentioned
Answer: a
Clarification: None.

9. Which of the following properties of #define is not true?
a) You can use a pointer to #define
b) #define can be made externally available
c) They obey scope rules
d) All of the mentioned
Answer: d
Clarification: None.

250+ TOP MCQs on Multidimensional Arrays and Answers

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

Here is a listing of C multiple choice questions on “Multidimensional Arrays” along with answers, explanations and/or solutions:

1. What is the correct syntax to send a 3-dimensional array as a parameter? (Assuming declaration int a[5][4][3];)
a) func(a);
b) func(&a);
c) func(*a);
d) func(**a);
Answer: a
Clarification: None.

2. What are the applications of a multidimensional array?
a) Matrix-Multiplication
b) Minimum Spanning Tree
c) Finding connectivity between nodes
d) All of the mentioned
Answer: d
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int ary[2][3];
  5.         foo(ary);
  6.     }
  7.     void foo(int *ary[])
  8.     {
  9.         int i = 10, j = 2, k;
  10.         ary[0] = &i;
  11.         ary[1] = &j;
  12.         *ary[0] = 2;
  13.         for (k = 0;k < 2; k++)
  14.         printf("%dn", *ary[k]);
  15.     }

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

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int ary[2][3];
  5.         foo(ary);
  6.     }
  7.     void foo(int (*ary)[3])
  8.     {
  9.         int i = 10, j = 2, k;
  10.         ary[0] = &i;
  11.         ary[1] = &j;
  12.         for (k = 0;k < 2; k++)
  13.         printf("%dn", *ary[k]);
  14.     }

a) Compile time error
b) 10 2
c) Undefined behaviour
d) segmentation fault/code crash
Answer: a
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         foo(ary);
  5.     }
  6.     void foo(int **ary)
  7.     {
  8.         int i = 10, k = 10, j = 2;
  9.         int *ary[2];
  10.         ary[0] = &i;
  11.         ary[1] = &j;
  12.         printf("%dn", ary[0][1]);
  13.     }

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

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int ary[2][3][4], j = 20;
  5.         ary[0][0] = &j;
  6.         printf("%dn", *ary[0][0]);
  7.     }

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

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int ary[2][3];
  5.         ary[][] = {{1, 2, 3}, {4, 5, 6}};
  6.         printf("%dn", ary[1][0]);
  7.     }

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