250+ TOP MCQs on Arrays of Structures and Answers

C helps anyone preparing for Philips and other companies C interviews on C Programming language.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

a) Compile time error
b) 1 0
c) 1 somegarbagevalue
d) undefined behaviour
Answer: b
Clarification: None.

8. 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[2];
  9.         printf("%d", sizeof(s));
  10.     }

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

250+ TOP MCQs on Variable Length Argument and Answers

C programming questions on “Variable Length Argument”. One shall practice these 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 programming questions come with detailed explanation of the answers which helps in better understanding of C concepts.

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

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

  1.     #include 
  2.     #include 
  3.     void func(int, ...);
  4.     int main()
  5.     {
  6.         func(2, 3, 5, 7, 11, 13);
  7.         return 0;
  8.     }
  9.     void func(int n, ...)
  10.     {
  11.         int number, i = 0;
  12.         va_list start;
  13.         va_start(start, n);
  14.         while (i != 3)
  15.         {
  16.             number = va_arg(start, int);
  17.             i++;
  18.         }
  19.         printf("%d", number);
  20.     }

a) 3
b) 5
c) 7
d) 11
Answer: c
Clarification: None.

2. Which of the following function with ellipsis are illegal?
a) void func(…);
b) void func(int, …);
c) void func(int, int, …);
d) none of the mentioned
Answer: a
Clarification: None.

3. Which of the following data-types are promoted when used as a parameter for an ellipsis?
a) char
b) short
c) int
d) none of the mentioned
Answer: a
Clarification: None.

4. Which header file includes a function for variable number of arguments?
a) stdlib.h
b) stdarg.h
c) ctype.h
d) both stdlib.h and stdarg.h
Answer: b
Clarification: None.

5. Which of the following macro extracts an argument from the variable argument list (ie ellipsis) and advance the pointer to the next argument?
a) va_list
b) va_arg
c) va_end
d) va_start
Answer: b
Clarification: None.

6. The type va_list in an argument list is used ________
a) To declare a variable that will refer to each argument in turn;
b) For cleanup
c) To create a list
d) There is no such type
Answer: a
Clarification: None.

7. In a variable length argument function, the declaration “…” can _______
a) Appear anywhere in the function declaration
b) Only appear at the end of an argument list
c) Nothing
d) None of the mentioned
Answer: b
Clarification: None.

8. Each call of va_arg _______
a) Returns one argument
b) Steps va_list variable to the next
c) Returns one argument & Steps va_list variable to the next
d) None of the mentioned
Answer: c
Clarification: None.

250+ TOP MCQs on Storage Management and Answers

C problems on “Storage Management”. One shall practice these problems to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These problems 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 problems come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C problems on “Storage Management” along with answers, explanations and/or solutions:

1. The function ____ obtains a block of memory dynamically.
a) calloc
b) malloc
c) both calloc & malloc
d) free
Answer: c
Clarification: None.

2. void * malloc(size_t n) returns?
a) Pointer to n bytes of uninitialized storage
b) NULL if the request can be satisfied
c) Nothing
d) None of the mentioned
Answer: a
Clarification: None.

3. calloc() returns storage that is initialized to.
a) Zero
b) Null
c) Nothing
d) One
Answer: a
Clarification: None.

4. In function free(p), p is a _______
a) int
b) pointer returned by malloc()
c) pointer returned by calloc()
d) pointer returned by malloc() & calloc()
Answer: d
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         char *p = calloc(100, 1);
  5.         p = "welcome";
  6.         printf("%sn", p);
  7.     }

a) Segmentation fault
b) Garbage
c) Error
d) welcome
Answer: d
Clarification: None.

6. Memory allocation using malloc() is done in _________
a) Static area
b) Stack area
c) Heap area
d) Both Stack & Heap area
Answer: c
Clarification: None.

7. Why do we write (int *) before malloc?

int *ip = (int *)malloc(sizeof(int));

a) It is for the syntax correctness
b) It is for the type-casting
c) It is to inform malloc function about the data-type expected
d) None of the mentioned
Answer: b
Clarification: None.

8. Which of the following is used during memory deallocation in C?
a) remove(p);
b) delete(p);
c) free(p);
d) terminate(p);
Answer: c
Clarification: None.

contest

250+ TOP MCQs on Enums and Answers

C MCQs on “Enums – 1”.

1. A user defined data type, which is used to assign names to integral constants is called ____________
a) Union
b) Array
c) Structure
d) Enum
Answer: d
Clarification: Enumeration (enum) is a user defined data type in C. It is used to assign names to integral constants. The names make a program easy to read and maintain.

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

#include
enum colour
{
    blue, red, yellow
};
main()
{
    enum colour c;
    c=yellow;
    printf("%d",c);
}

a) 1
b) 2
c) 0
d) Error
Answer: c
Clarification: Enum variables are automatically assigned values if no value is specified. The compiler by default assigns values starting from 0. Therefore, in the above code, blue gets 0, red gets 1 and yellow gets 2.

3. Point out the error (if any) in the following C code?

#include
enum hello
{
    a,b,c;
};
main()
{
    enum hello m;
    printf("%d",m);
}

a) No error
b) Error in the statement: a,b,c;
c) Error in the statement: enum hello m;
d) Error in the statement: printf(“%d”,m);
Answer: b
Clarification: In the above code, there is a semi colon given at the end of the list of variables. This results in an error. Semi colon is to be put only after the closing brace of the enum, not after the list of variables.

4. String handling functions such as strcmp(), strcpy() etc can be used with enumerated types.
a) True
b) False
Answer: b
Clarification: Enumerated types are not strings. Hence it is not possible to use string handling functions with enumerated data types.

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

#include
enum hello
{
    a,b=99,c,d=-1
};
main()
{
    enum hello m;
    printf("%dn%dn%dn%dn",a,b,c,d);
}

a)

    1
    99
    100
    -1

b) Error
c)

    0
    99
    100
    -1

d)

    0
    1
    2
    3

View Answer

Answer: c
Clarification: We can assign values to some of the symbol names in any order. All unassigned names get the value as the value of previous name plus one.

 
 

6. Pick the incorrect statement with respect to enums.
a) Two enum symbols cannot have the same value
b) Only integer constants are allowed in enums
c) It is not possible to change the value of enum symbols
d) Enum variables are automatically assigned values if no value is specified
Answer: a
Clarification: The statement that two enum symbols cannot have the same value is incorrect. Any number of enum symbols can have the same value.

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

#include
enum 
{
    a=2,b=3.56
};
enum  s;
main()
{
    printf("%d%d",a,b);
}

a) 2 3
b) 0 1
c) 2 3.56
d) Error
Answer: d
Clarification: The above code will result in an error because 3.56 is not an integer constant. Only integer constants are allowed in enums.

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

#include
enum class
{
    a,b,c
};
enum class m;
main()
{
    printf("%d",sizeof(m));
}

a) 3
b) Same as the size of an integer
c) 3 times the size of an integer
d) Error
Answer: b
Clarification: The output will be the same as the size of an integer, that is 4 on a 32 bit platform.

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

#include
enum hi{a,b,c};
enum hello{c,d,e};
main()
{
    enum hi h;
    h=b;
    printf("%d",h);
    return 0;
}

a) 2
b) 1
c) Error
d) 0
Answer: c
Clarification: The code shown above results in an error: re-declaration of enumerator ‘c’. All enumerator constants should be unique in their scope.

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

#include
enum 
{
    a,b,c=5
};
enum  s;
main()
{
    c++;
    printf("%d",c);
}

a) Error
b) 5
c) 6
d) 2
Answer: a
Clarification: The above code results in an error because it is not possible to modify the value of enum constants. In the above code, we have tried to increment the value of c. This results in an error.

250+ TOP MCQs on Diagnostics and Answers

C Multiple Choice Questions & Answers on “Diagnostics – 2”.

1. The following message is displayed in stderr.
Assertion failed: expression, file filenum, line nmn
a) true
b) false
Answer: a
Clarification: The message written might be of the form
Assertion failed: expression, file filename, line nmn
This message is displayed on stderr only when the expression in the statement void assert(int expression) is zero.nmn is the line number.

2. The source filename and line number come from the preprocessor macros ________ and ______
a) _ _FILE_ _ and _ _LINE_ _
b) _ _NAME_ _ and _ _NUMBER_ _
c) _ _FILENAME_ _ and _ _NMN_ _
d) _ _FILE_ _ and _ _NUM_ _
Answer: a
Clarification: The source filename and line number come from the preprocessor macros
_ _FILE_ _ and _ _LINE_ _. Filename and line number are displayed in the error message when the expression of the assert is zero. It is displayed as
Assertion failed: expression, file filename, line nmn.

3. The function abort() is defined in which of the following header file?
a) stdio.h
b) stdarg.h
c) stdlib.h
d) assert.h
Answer: c
Clarification: abort() function is declared inside the header file stdlib.h .This function is used to terminate the program abnormally.

4. Correct code to turn assertions ON at various places throughout a source file is _________
a)

#undef NDEBUG 
#include 

b)

#define NDEBUG 
#include 

c)

#define NDEBUG 
#include 

d)

#undef NDEBUG 
#include 

View Answer

Answer: a
Clarification: you can turn assertions on and off at various places throughout a source file, so to turn assertions on, you write:

#undef NDEBUG 
#include 

 
 

5. Correct code to turn assertions OFF at various places throughout a source file is _________
a)

#undef NDEBUG 
#include 

b)

#define NDEBUG 
#include 

c)

#define NDEBUG 
#include 

d)

#undef NDEBUG 
#include 

View Answer

Answer: b
Clarification: you can turn assertions on and off at various places throughout a source file, so to turn assertions off, you write:

#define NDEBUG 
#include 

 
 

6. Which line from the given code is the passive form?

#undef assert 
#ifdef NDEBUG 
#define assert (test) ( (void) 0) 
#else 
#define assert (test)  
#endif

a) #define assert(test)
b) #define assert(test) ((void)0)
c) #ifdef NDEBUG
d) #undef assert
Answer: b
Clarification: In the given code, the line #define assert (test)((void)0) is in the pasive form.

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

#include  
#include 
 #include  
void -Assert (char *mesg) 
{
   fputs (mesg, stderr); 
   fputs (" -- assertion failedn" , stderr); 
   abort () ; 
}

a) prints only assertion message
b) program is just aborted
c) prints assertion message and aborts
d) no action takes place
Answer: c
Clarification: The given function uses assert function as defined in assert.h.The program writes assertion message on the stanard error file(stderr) and then calls abort() function to terminate the execution of the program.

8. Which macro can be used to detect and report exceptional conditions?
a) extern
b) edom
c) assert
d) lbdl_min 1e-37
Answer: c
Clarification: assert macro is used to detect and report exceptional conditions. It is used to write diagnostics information on standard error file.

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

#include  
#include   
void main() 
{ 
   int n=11; 
   char str[50]="program";
   assert(n >= 10); 
   printf(" output: %dn", n); 
   assert(str != NULL); 
   printf("output: %sn", str); 
}

a) output: 11
b) error message
c)

output: 11
output: program

d) output: program
Answer: c
Clarification: The condition defined in the macro assert is true hence the statements following it are displayed.

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

#include  
#include   
void main() 
{ 
   int n=12; 
   char str[50]="";
   assert(n >= 10); 
   printf(" output: %dn", n); 
   assert(str != NULL); 
   printf("output: %sn", str); 
}

a)

   output: 12
   output: 

b) output: 12
c)

  output: 12
  assertion error

d) error
Answer: c
Clarification: In the given code the first condition in the assert is true, hence the statement following it is displayed. The string is initialized null therefore the second assert condition is false and error message is written on the stderr.

250+ TOP MCQs on Stringizers and Answers

C Multiple Choice Questions & Answers on “Stringizers”.

1. Which of the following is a stringizing operator?
a) < >
b) #
c) %
d) ##
Answer: b
Clarification: # is the stringizing operator. It allows formal arguments within a macro definition to be converted to a string.

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

#define (s,n) #s #n
main()
{
    printf((hello,world));
}

a) (hello,world)
b)
c) hello,world
d) helloworld
Answer: d
Clarification: The output to this code will be helloworld because when we use the stringizing operator, the resulting string will automatically be concatenated (combined) with any adjacent strings.

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

#define display(text) printf(#text "@")
main()
{
    display(hello.);
    display(good morning!);
}

a) [email protected] morning!
b) error
c) hello.good [email protected]
d) [email protected] [email protected]
Answer: d
Clarification: Each actual argument is converted into string within the printf function. Each argument is concatenated with ‘@’, which is written as a separate string within the macro definition.

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

#define display(a) #a
main()
{
    printf(display("56#7"));
}

a) Error
b) “56#7”
c) 56#7
d) 567
Answer: b
Clarification: In this case, it is not necessary for the argument in the printf function to be enclosed in double quotes. However, if the argument is enclosed in double quotes, no error is thrown. The output of the code shown will be “56#7”.

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

#define HELLO(a) #a
main()
{
    printf(HELLO(good        morning)); 
}

a) good morning
b) goodmorning
c) good morning
d) error
Answer: c
Clarification: The output of the code shown above will be: good morning
In the resulting string, the consecutive blank spaces are replaced by a single blank space when we use the stringizing operator.

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

#include 
#define (x)  #x
int main()
{
    int marks=100;
    printf("value of %s is = %dn",(marks),marks);
    return 0;
}

a) error
b) value of marks=100
c) value of=100
d) 100
Answer: b
Clarification: In the code shown above, the variable name(marks) is passed as an argument. By using the # operator, we can print the name of the variable as a string.

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

#define hello(c) #c
main()
{
    printf(hello(i,am));
}

a) i,am
b) iam
c) i am
d) error
Answer: d
Clarification: The above code will result in an error. This is because we have passed to arguments to the macro hello, but it should be talking only one.

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

#define hello(c,d) #c #d
main()
{
    printf(hello(i,"am"));
}

a) iam
b) i“am”
c) am
d) “am”
Answer: b
Clarification: The output for the following code will be i”am”. Since 2 arguments are passed and the macro hello takes two arguments, there is no error.

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

#define F abc
#define B def
#define FB(arg) #arg
#define FB1(arg) FB(arg)
main()
{
    printf(FB(F B));
    FB1(F B);
}

a) F B
b) Error
c) FB
d) “FB”
Answer: a
Clarification: The argument F B(only one space between F and B) is passed to the macro FB. This argument is converted to a string with the by the stringizing operator. Thus F B is printed.

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

#define display(text) "$" #text
main()
{
    printf(display(hello	   world));
}

a) hello world
b) $helloworld
c) $hello world
d) error
Answer: c
Clarification: The output of the code shown above is $hello world
The argument “hello  world” is passed to the macro text. The symbol “$” is present from before. In the resulting string, all the blank spaces are replaced by a single blank space. In addition to this, “$” is concatenated to the beginning of the resultant string.