250+ TOP MCQs on Declarations and Answers

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

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

1. Which of the following declaration is illegal?
a) char *str = “Best C programming classes by ”;
b) char str[] = “Best C programming classes by ”;
c) char str[20] = “Best C programming classes by ”;
d) char[] str = “Best C programming classes by ”;
Answer: d
Clarification: char[] str is a declaration in Java, but not in C.

2. Which keyword is used to prevent any changes in the variable within a C program?
a) immutable
b) mutable
c) const
d) volatile
Answer: c
Clarification: const is a keyword constant in C program.

3. Which of the following is not a pointer declaration?
a) char a[10];
b) char a[] = {‘1’, ‘2’, ‘3’, ‘4’};
c) char *str;
d) char a;
Answer: d
Clarification: Array declarations are pointer declarations.

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

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

a) Compile time error
b) 4
c) 4.0000000
d) 4.4
Answer: a
Clarification: Since the variable k is defined both as integer and as float, it results in an error.
Output:
$ cc pgm8.c
pgm8.c: In function ‘main’:
pgm8.c:5: error: conflicting types for ‘k’
pgm8.c:4: note: previous definition of ‘k’ was here
pgm8.c:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
pgm8.c:7: error: expected ‘;’ before ‘}’ token

5. Which of the following statement is false?
a) A variable defined once can be defined again with different scope
b) A single variable cannot be defined with two different types in the same scope
c) A variable must be declared and defined at the same time
d) A variable refers to a location in memory
Answer: c
Clarification: It is not an error if the variable is declared and not defined. For example – extern declarations.

6. A variable declared in a function can be used in main().
a) True
b) False
c) True if it is declared static
d) None of the mentioned
Answer: b
Clarification: Since the scope of the variable declared within a function is restricted only within that function, so the above statement is false.

7. The name of the variable used in one function cannot be used in another function.
a) True
b) False
Answer: b
Clarification: Since the scope of the variable declared within a function is restricted only within that function, the same name can be used to declare another variable in another function.

contest

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

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

Here is a listing of C programming Objective Questions on “Precedence and Order of Evaluation” 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.         int b = 5 - 4 + 2 * 5;
  5.         printf("%d", b);
  6.     }

a) 25
b) -5
c) 11
d) 16
Answer: c
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int b = 5 & 4 & 6;
  5.         printf("%d", b);
  6.     }

a) 5
b) 6
c) 3
d) 4
Answer: d
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int b = 5 & 4 | 6;
  5.         printf("%d", b);
  6.     }

a) 6
b) 4
c) 1
d) 0
Answer: a
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int b = 5 + 7 * 4 - 9 * (3, 2);
  5.         printf("%d", b);
  6.     }

a) 6
b) 15
c) 13
d) 21
Answer: b
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int h = 8;
  5.         int b = (h++, h++);
  6.         printf("%d%dn", b, h);
  7.     }

a) 10 10
b) 10 9
c) 9 10
d) 8 10
Answer: c
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int h = 8;
  5.         int b = h++ + h++ + h++;
  6.         printf("%dn", h);
  7.     }

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

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int h = 8;
  5.         int b = 4 * 6 + 3 * 4 < 3 ? 4 : 3;
  6.         printf("%dn", b);
  7.     }

a) 3
b) 33
c) 34
d) Run time error
Answer: a
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int a = 2 + 3 - 4 + 8 -  5 % 4;
  5.         printf("%dn", a);
  6.     }

a) 0
b) 8
c) 11
d) 9
Answer: b
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         char a = '0';
  5.         char b = 'm';
  6.         int c = a && b || '1';
  7.         printf("%dn", c);
  8.     }

a) 0
b) a
c) 1
d) m
Answer: c
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         char a = 'A';
  5.         char b = 'B';
  6.         int c = a + b % 3 - 3 * 2;
  7.         printf("%dn", c);
  8.     }

a) 65
b) 58
c) 64
d) 59
Answer: d
Clarification: None.

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

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

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

1. What is the return-type of the function sqrt()?
a) int
b) float
c) double
d) depends on the data type of the parameter
Answer: c
Clarification: None.

2. Which of the following function declaration is illegal?
a)

   double func();
   int main(){}
   double func(){}

b)

   double func(){};
   int main(){}

c)

   int main()
   {
       double func();
   }
   double func(){//statements}

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

3. What will be the output of the following C code having void return-type function?

  1.     #include 
  2.     void foo()
  3.     {
  4.         return 1;
  5.     }
  6.     void main()
  7.     {
  8.         int x = 0;
  9.         x = foo();
  10.         printf("%d", x);
  11.     }

a) 1
b) 0
c) Runtime error
d) Compile time error
Answer: d
Clarification: None.

4. What will be the data type returned for the following C function?

  1.     #include 
  2.     int func()
  3.     {
  4.         return (double)(char)5.0;
  5.     }

a) char
b) int
c) double
d) multiple type-casting in return is illegal
Answer: b
Clarification: None.

5. What is the problem in the following C declarations?

   int func(int);
   double func(int);
   int func(float);

a) A function with same name cannot have different signatures
b) A function with same name cannot have different return types
c) A function with same name cannot have different number of parameters
d) All of the mentioned
Answer: d
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int k = m();
  5.         printf("%d", k);
  6.     }
  7.     void m()
  8.     {
  9.         printf("hello");
  10.     }

a) hello 5
b) Error
c) Nothing
d) Junk value
Answer: a
Clarification: None.

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

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

a) 5
b) Junk value
c) 0
d) Error
Answer: a
Clarification: None.

8. 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("hello ");
  7.         printf("%d", k[0]);
  8.     }
  9.     int *m()
  10.     {
  11.         int a[2] = {5, 8};
  12.         return a;
  13.     }

a) hello 5 8
b) hello 5
c) hello followed by garbage value
d) Compilation error
Answer: c
Clarification: None.

250+ TOP MCQs on Macro Substitution and Answers

C questions and puzzles on “Macro Substitution”. 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 “Macro Substitution” along with answers, explanations and/or solutions:

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

  1.     #include 
  2.     #define foo(m, n) m ## n
  3.     int main()
  4.     {
  5.         printf("%sn", foo(k, l));
  6.     }

a) k l
b) kl
c) Compile time error
d) Undefined behaviour
Answer: c
Clarification: None.

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

  1.     #include 
  2.     #define foo(m, n) " m ## n "
  3.     int main()
  4.     {
  5.         printf("%sn", foo(k, l));
  6.     }

a) k l
b) kl
c) Compile time error
d) m ## n
Answer: d
Clarification: None.

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

  1.     #include 
  2.     #define foo(x, y) #x #y
  3.     int main()
  4.     {
  5.         printf("%sn", foo(k, l));
  6.         return 0;
  7.     }

a) kl
b) k l
c) xy
d) Compile time error
Answer: a
Clarification: None.

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

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

a) Divided by zero exception
b) Compile time error
c) -8
d) -4
Answer: c
Clarification: None.

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

  1.     #include 
  2.     void f();
  3.     int main()
  4.     {
  5.         #define foo(x, y) x / y + x
  6.         f();
  7.     }
  8.     void f()
  9.     {
  10.         printf("%dn", foo(-3, 3));
  11.     }

a) -8
b) -4
c) Compile time error
d) Undefined behaviour
Answer: b
Clarification: None.

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

  1.     #include 
  2.     void f();
  3.     int main()
  4.     {
  5.         #define max 10
  6.         f();
  7.         return 0;
  8.     }
  9.     void f()
  10.     {
  11.         printf("%dn", max * 10);
  12.     }

a) 100
b) Compile time error since #define cannot be inside functions
c) Compile time error since max is not visible in f()
d) Undefined behaviour
Answer: a
Clarification: None.

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

  1.     #include 
  2.     #define foo(x, y) x / y + x
  3.     int main()
  4.     {
  5.         int i = -6, j = 3;
  6.         printf("%d ", foo(i + j, 3));
  7.         printf("%dn", foo(-3, 3));
  8.         return 0;
  9.     }

a) -8 -4
b) -4 divided by zero exception
c) -4 -4
d) Divided by zero exception
Answer: a
Clarification: None.

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

  1.  #include 
  2.     int foo(int, int);
  3.     #define foo(x, y) x / y + x
  4.     int main()
  5.     {
  6.         int i = -6, j = 3;
  7.         printf("%d ",foo(i + j, 3));
  8.         #undef foo
  9.         printf("%dn",foo(i + j, 3));
  10.     }
  11.     int foo(int x, int y)
  12.     {
  13.         return x / y + x;
  14.     }

a) -8 -4
b) Compile time error
c) -8 -8
d) Undefined behaviour
Answer: a
Clarification: None.

9. What is the advantage of #define over const?
a) Data type is flexible
b) Can have a pointer
c) Reduction in the size of the program
d) None of the mentioned
Answer: a
Clarification: None.

250+ TOP MCQs on Multidimensional Arrays and Answers

C MCQs (multiple choice questions) on “Multidimensional Arrays”. One shall practice these MCQs 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 multiple choice questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C multiple choice questions on “Multidimensional Arrays” 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.         int a[2][3] = {1, 2, 3, 4, 5};
  5.         int i = 0, j = 0;
  6.         for (i = 0; i < 2; i++)
  7.         for (j = 0; j < 3; j++)
  8.         printf("%d", a[i][j]);
  9.     }

a) 1 2 3 4 5 0
b) 1 2 3 4 5 junk
c) 1 2 3 4 5 5
d) Run time error
Answer: a
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int a[2][3] = {1, 2, 3, , 4, 5};
  5.         int i = 0, j = 0;
  6.         for (i = 0; i < 2; i++)
  7.         for (j = 0; j < 3; j++)
  8.         printf("%d", a[i][j]);
  9.     }

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

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

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

a) 0 3 0 0 0 0
b) Junk 3 junk junk junk junk
c) Compile time error
d) All junk values
Answer: a
Clarification: None.

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

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

a) 0 3 0 0 0 0
b) Junk 3 junk junk junk junk
c) Compile time error
d) All junk values
Answer: c
Clarification: None.

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

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

a) 0 3 0 0 0 0
b) Junk 3 junk junk junk junk
c) Compile time error
d) All junk values
Answer: c
Clarification: None.

6. Comment on the following C statement.

int (*a)[7];

a) An array “a” of pointers
b) A pointer “a” to an array
c) A ragged array
d) None of the mentioned
Answer: b
Clarification: None.

7. Comment on the following 2 arrays with respect to P and Q.

  1.    int *a1[8];
  2.    int *(a2[8]);
  3.    P. Array of pointers
  4.    Q. Pointer to an array

a) a1 is P, a2 is Q
b) a1 is P, a2 is P
c) a1 is Q, a2 is P
d) a1 is Q, a2 is Q
Answer: b
Clarification: None.

8. Which of the following is not possible statically in C?
a) Jagged Array
b) Rectangular Array
c) Cuboidal Array
d) Multidimensional Array
Answer: a
Clarification: None.

250+ TOP MCQs on Arrays of Structures and Answers

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

1. The correct syntax to access the member of the ith structure in the array of structures is?

Assuming: struct temp
    {
        int b;
    }s[50];

a) s.b.[i];
b) s.[i].b;
c) s.b[i];
d) s[i].b;
Answer: d
Clarification: None.

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

  1.     #include 
  2.     struct temp
  3.     {
  4.         int a;
  5.         int b;
  6.         int c;
  7.     };
  8.     main()
  9.     {
  10.         struct temp p[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  11.     }

a) No Compile time error, generates an array of structure of size 3
b) No Compile time error, generates an array of structure of size 9
c) Compile time error, illegal declaration of a multidimensional array
d) Compile time error, illegal assignment to members of structure
Answer: a
Clarification: None.

3. Which of the following uses structure?
a) Array of structures
b) Linked Lists
c) Binary Tree
d) All of the mentioned
Answer: d
Clarification: None.

4. What is the correct syntax to declare a function foo() which receives an array of structure in function?
a) void foo(struct *var);
b) void foo(struct *var[]);
c) void foo(struct var);
d) none of the mentioned
Answer: a
Clarification: None.

5. What will be the output of the following C code? (Assuming size of int be 4)

  1.     #include 
  2.     struct temp
  3.     {
  4.         int a;
  5.         int b;
  6.         int c;
  7.     } p[] = {0};
  8.     main()
  9.     {
  10.         printf("%d", sizeof(p));
  11.     }

a) 4
b) 12
c) 16
d) Can’t be estimated due to ambiguous initialization of array
Answer: b
Clarification: None.

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

  1.     #include 
  2.     struct student
  3.     {
  4.         char *name;
  5.     };
  6.     struct student s[2];
  7.     void main()
  8.     {
  9.         s[0].name = "alan";
  10.         s[1] = s[0];
  11.         printf("%s%s", s[0].name, s[1].name);
  12.         s[1].name = "turing";
  13.         printf("%s%s", s[0].name, s[1].name);
  14.     }

a) alan alan alan turing
b) alan alan turing turing
c) alan turing alan turing
d) run time error
Answer: a
Clarification: None.

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

  1.     #include 
  2.     struct student
  3.     {
  4.         char *name;
  5.     };
  6.     struct student s[2], r[2];
  7.     void main()
  8.     {
  9.         s[0].name = "alan";
  10.         s[1] = s[0];
  11.         r = s;
  12.         printf("%s%s", r[0].name, r[1].name);
  13.     }

a) alan alan
b) Compile time error
c) Varies
d) Nothing
Answer: b
Clarification: None.

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

  1.     #include 
  2.     struct student
  3.     {
  4.         char *name;
  5.     };
  6.     void main()
  7.     {
  8.         struct student s[2], r[2];
  9.         s[1] = s[0] = "alan";
  10.         printf("%s%s", s[0].name, s[1].name);
  11.     }

a) alan alan
b) Nothing
c) Compile time error
d) Varies
Answer: c
Clarification: None.

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

  1.     #include 
  2.     struct student
  3.     {
  4.     };
  5.     void main()
  6.     {
  7.         struct student s[2];
  8.         printf("%d", sizeof(s));
  9.     }

a) 2
b) 4
c) 8
d) 0
Answer: d
Clarification: None.