250+ TOP MCQs on Pointer to Structures and Answers

C quiz on “Pointer to Structures”. One shall practice these quizzes 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 quiz comes with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C quiz 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 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) / 3);
  12.         if (x == sizeof(int) + sizeof(char))
  13.             printf("%dn", ptr1->x);
  14.         else
  15.             printf("falsen");
  16.     }

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

2. 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(ptr1));
  12.         if (x == 1)
  13.             printf("%dn", ptr1->x);
  14.         else
  15.             printf("falsen");
  16.     }

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

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

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

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

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

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

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

5. Which of the following is an incorrect syntax for pointer to structure?

(Assuming struct temp{int b;}*my_struct;)

a) *my_struct.b = 10;
b) (*my_struct).b = 10;
c) my_struct->b = 10;
d) Both *my_struct.b = 10; and (*my_struct).b = 10;
Answer: a
Clarification: None.

6. Which of the following is an incorrect syntax to pass by reference a member of a structure in a function?

(Assume: struct temp{int a;}s;)

a) func(&s.a);
b) func(&(s).a);
c) func(&(s.a));
d) none of the mentioned
Answer: d
Clarification: None.

7. Which of the following structure declaration doesn’t require pass-by-reference?
a)

   struct{int a;}s;
   main(){}

b)

   struct temp{int a;};
   main(){
       struct temp s;
   }

c)

   struct temp{int a;};
   main(){}
   struct temp s;

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

8. Which option is not possible for the following function call?

  1. func(&s.a); //where s is a variable of type struct and a is the member of the struct.

a) Compiler can access entire structure from the function
b) Individual member’s address can be displayed in structure
c) Individual member can be passed by reference in a function
d) None of the mentioned
Answer: a
Clarification: None.

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

  1.     #include 
  2.     struct temp
  3.     {
  4.         int a;
  5.     } s;
  6.     void change(struct temp);
  7.     main()
  8.     {
  9.         s.a = 10;
  10.         change(s);
  11.         printf("%dn", s.a);
  12.     }
  13.     void change(struct temp s)
  14.     {
  15.         s.a = 1;
  16.     }

a) Output will be 1
b) Output will be 10
c) Output varies with machine
d) Compile time error
Answer: b
Clarification: None.

10. 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, 92, 3, 94, 5, 96};
  10.         struct p *ptr1 = p1;
  11.         int x = (sizeof(p1) / 5);
  12.         if (x == 3)
  13.             printf("%d %dn", ptr1->x, (ptr1 + x - 1)->x);
  14.         else
  15.             printf("falsen");
  16.     }

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

250+ TOP MCQs on Variable Length Argument and Answers

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

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

1. The standard header _______ is used for variable list arguments (…) in C.
a)
b)
c)
d)
Answer: d
Clarification: None.

2. What is the purpose of va_end?
a) Cleanup is necessary
b) Must be called before the program returns
c) Cleanup is necessary & Must be called before the program returns
d) None of the mentioned
Answer: c
Clarification: None.

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

  1.     #include 
  2.     int f(char chr, ...);
  3.     int main()
  4.     {
  5.         char c = 97;
  6.         f(c);
  7.         return 0;
  8.     }
  9.     int f(char c, ...)
  10.     {
  11.         printf("%cn", c);
  12.     }

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

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

  1.     #include 
  2.     #include 
  3.     int f(...);
  4.     int main()
  5.     {
  6.         char c = 97;
  7.         f(c);
  8.         return 0;
  9.     }
  10.     int f(...)
  11.     {
  12.         va_list li;
  13.         char c = va_arg(li, char);
  14.         printf("%cn", c);
  15.     }

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

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

  1.     #include 
  2.     #include 
  3.     int f(char c, ...);
  4.     int main()
  5.     {
  6.         char c = 97, d = 98;
  7.         f(c, d);
  8.         return 0;
  9.     }
  10.     int f(char c, ...)
  11.     {
  12.         va_list li;
  13.         va_start(li, c);
  14.         char d = va_arg(li, char);
  15.         printf("%cn", d);
  16.         va_end(li);
  17.     }

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

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

  1.     #include 
  2.     #include 
  3.     int f(char c, ...);
  4.     int main()
  5.     {
  6.         char c = 97, d = 98;
  7.         f(c, d);
  8.         return 0;
  9.     }
  10.     int f(char c, ...)
  11.     {
  12.         va_list li;
  13.         va_start(li, c);
  14.         char d = va_arg(li, int);
  15.         printf("%cn", d);
  16.         va_end(li);
  17.     }

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

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

  1.     #include 
  2.     #include 
  3.     int f(int c, ...);
  4.     int main()
  5.     {
  6.         int c = 97;
  7.         float d = 98;
  8.         f(c, d);
  9.         return 0;
  10.     }
  11.     int f(int c, ...)
  12.     {
  13.         va_list li;
  14.         va_start(li, c);
  15.         float d = va_arg(li, float);
  16.         printf("%fn", d);
  17.         va_end(li);
  18.     }

a) Compile time error
b) Undefined behaviour
c) 97.000000
d) 98.000000
Answer: b
Clarification: None.

250+ TOP MCQs on Storage Management and Answers

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

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

1. Which of the following will return a result most quickly for searching a given key?
a) Unsorted Array
b) Sorted Array
c) Sorted linked list
d) Binary Search Tree
Answer: d
Clarification: None.

2. On freeing a dynamic memory, if the pointer value is not modified, then the pointer points to.
a) NULL
b) Other dynamically allocated memory
c) The same deallocated memory location
d) It points back to the location it was initialized with
Answer: c
Clarification: None.

3. Which of the following should be used for freeing the memory allocated in the following C code?

  1.     #include 
  2.     struct p
  3.     {
  4.         struct p *next;
  5.         int x;
  6.     };
  7.     int main()
  8.     {
  9.         struct p *p1 = (struct p*)malloc(sizeof(struct p));
  10.         p1->x = 1;
  11.         p1->next = (struct p*)malloc(sizeof(struct p));
  12.         return 0;
  13.     }

a)

b)

   free(p1->next);
   free(p1);

c) free(p1);
d) all of the mentioned
Answer: b
Clarification: None.

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

  1.     #include 
  2.     struct p
  3.     {
  4.         struct p *next;
  5.         int x;
  6.     };
  7.     int main()
  8.     {
  9.         struct p *p1 = calloc(1, sizeof(struct p));
  10.         p1->x = 1;
  11.         p1->next = calloc(1, sizeof(struct p));
  12.         printf("%dn", p1->next->x);
  13.         return 0;
  14.     }

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

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

  1.      #include 
  2.     struct p
  3.     {
  4.         struct p *next;
  5.         int x;
  6.     };
  7.     int main()
  8.     {
  9.         struct p* p1 = malloc(sizeof(struct p));
  10.         p1->x = 1;
  11.         p1->next = malloc(sizeof(struct p));
  12.         printf("%dn", p1->next->x);
  13.         return 0;
  14.     }

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

6. calloc() initialize memory with all bits set to zero.
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
Answer: a
Clarification: None.

7. What if size is zero in the following C statement?

a) Allocate a memory location with zero length
b) Free the memory pointed to by ptr
c) Undefined behaviour
d) Doesn’t do any reallocation of ptr i.e. no operation
Answer: b
Clarification: None.

.

250+ TOP MCQs on Enums and Answers

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

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

main()
{
    enum resut {pass, fail};
    enum result s1,s2;
    s1=pass;
    s2=fail;
    printf("%d",s1);
}

a) error
b) pass
c) fail
d) 0
Answer: a
Clarification: The code shown above results in an error, stating : storage size of s1 and s2 unknown. There is an error in the declaration of these variables in the statement: enum result s1,s2;

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

#include 
enum example {a = 1, b, c};
enum example example1 = 2;
enum example answer() 
{
    return example1;
}
 
int main()
{
   (answer() == a)? printf("yes"): printf("no");
   return 0;
}

a) yes
b) no
c) 2
d) error
Answer: b
Clarification: In the code shown above, the value of example1 is returned by the function answer. The ternary statement prints yes if this value is equal to that of ‘a’ and no if the value is not equal to that of ‘a’. Since the value of ‘a’ is 1 and that returned by the function is 2, therefore no is printed.

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

#include
#define MAX 4
enum 
{
    a,b=3,c
};
main()
{
    if(MAX!=c)
        printtf("hello");
    else
        printf("welcome");
}

a) error
b) hello
c) welcome
d) 2
Answer: c
Clarification: The output will be welcome. The value of the macro MAX is 4. Since 4 is equal to the value of c, welcome is printed.

4. Arithmetic operations such as addition, subtraction, multiplication and division are allowed on enumerated constants.
a) True
b) False
Answer: a
Clarification: Arithmetic operations such as addition, subtraction, multiplication and division are allowed on enumerated constants. Valid arithmetic operators are +, -, *, / and %.

5. Point out the error( if any) in the following code.

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

a) Error in the statement: a,b,c
b) Error in the statement: enum g;
c) Error in the statement: g++
d) No error
Answer: d
Clarification: The code shown above does not result in any error. This is because, although the value of enum constants cannot be changed, yet it is possible to modify the value of the enum instance variable(that is ‘g’ in this case).

6. What will be the output of the following C code if input given is 2?

#include
enum day
{
    a,b,c=5,d,e
};
main()
{
    printf("Enter the value for a");
    scanf("%d",a);
    printf("%d",a);
}

a) 2
b) 0
c) 3
d) Error
Answer: d
Clarification: The code shown above results in an error. This is because memory is not allocated for constants in enum. Thereore ampersand(&) cannot be used with them. Also, a value has already been assigned to ‘a’, which cannot be changed.

7. What will be the output of the following C code if the code is executed on a 32 bit platform?

#include 
enum 
 {
    c = 0,
    d = 10,
    h = 20,
    s = 3
} a;
 
int main()
{
        a = c;
	printf("Size of enum variable = %d bytes", sizeof(a));
	return 0;
}

a) Error
b) Size of enum variable = 2 bytes
c) Size of enum variable = 4 bytes
d) Size of enum variables = 8 bytes
Answer: c
Clarification: The size of an enum variable is equal to the size of an integer. The size of an integer on a 32 bit platform is equal to 4 bytes.

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

#include
enum 
{
    a=1,b,c,d,e
};
int main()
{
    printf("%d",b*c+e-d);
}

a) Error
b) 7
c) 2
d) 4
Answer: b
Clarification: Since arithmetic operations are allowed on enum constants, hence the expression given is evaluates. b*c+e-d = 2*3+5-4 = 6+5-4 = 7

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

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

a) Error
b) 10
c) 1
d) 4
Answer: a
Clarification: The above code results in an error. This is because it is not possible to change the values of enum constants. In the code shown above, the statement: b=10; causes the error.

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

#include
enum 
{
    a=1,b
};
enum 1
{
    c,d
};
int main()
{
    enum 1 s1=c;
    enum 1 s=a;
    enum  s2=d;
    printf("%d",s);
    printf("%d",s1);
    printf("%d",s2);
}

a) Error
b) 011
c) 110
d) 101
Answer: d
Clarification: The output of the code shown above is 101. This code shows that it is possible to store the symbol of one enum in another enum variable.

250+ TOP MCQs on Variable Argument Lists and Answers

C Multiple Choice Questions & Answers (MCQs) on “Variable Argument Lists”.

1. How many macros are defined in the header file stdarg.h?
a) one
b) two
c) three
d) four
Answer: c
Clarification: The header file stdarg.h has three macros defined in it.

2. The header file stdarg.h defines a variable type ________
a) va_list
b) v_list
c) size_t
d) var_list
Answer: a
Clarification: The variable type declared under the header file stdarg.h is va_list. This holds information required by three macros.

3. The three macros defined by stdarg.h is _________
a) start(), arg() and end()
b) var_start(), var_arg() and var_end()
c) va_start(), va_arg() and va_end()
d) v_start(), v_arg() and v_end()
Answer: c
Clarification: The header file stdarg.h has three macros defined in it. They are va_start(), va_arg() and va_end(). va_list is a variable type that holds information needed by the three macros.

4. If access to the varying arguments is desired then the called function shall declare ________ having type va_list.
a) class
b) object
c) function
d) variable
Answer: b
Clarification: An object of type va_list has to be created in order to access the varying arguments.

5. Which macro retrieves the next argument in the parameter list of the function with type type?
a) type va_arg(va_list ap, type)
b) type var_arg(va_list ap, type)
c) type v_arg(va_list ap, type)
d) type val_arg(va_list ap, type)
Answer: a
Clarification: type va_arg(va_list ap,type)
This macro is used to retrieve the next argument in the parameter list of the function with type type.

6. The _______ macro shall be invoked before any access to the unnamed arguments.
a) va_arg
b) va_end
c) va_list
d) va_start
Answer: d
Clarification: va_start macro shall be invoked before any access to the unnamed arguments.void va-start (va-list ap, p); The va-start macro initializes ap for subsequent use by va-arg and va-end.

7. ______ macro must be called before using ______ and ________
a) va_arg, va_end and va_start
b) va_start, va_end and va_arg
c) va_end, va_arg and va_start
d) v_arg, v_end and v_start
Answer: b
Clarification: va_start macro must be called before using macros va_end and va_arg.
The macro void va_start(va_list p, last_arg) initializes p variable to be used with the va_arg and va_end macros.

8. The C library macro type _________ retrieves the next argument in the parameter list of the function with type.
a) va_end
b) va_arg
c) va_start
d) no macros
Answer: b
Clarification: va_arg is the C library macro defined under stdarg.h which retrieves the next argument in the parameter list of the function.

9. What is the role of the given C function?

a) allows a function with variable arguments which used the va_start macro to return
b) retrieves the next argument in the parameter list
c) initializes ap variable to be used with the va_arg and va_start macros
d) returns the next additional argument as an expression
Answer: a
Clarification: void va_end(va_list ap) defined under the header file stdarg allows a function with variable arguments which used the va_start macro to return.
The result is undefined if va_end is not called before returning from the function.

10. Which header file should be included if a function has to be defined such that it can accept variable number of arguments?
a) stdlib.h
b) stdarg.h
c) assert.h
d) setjmp.h
Answer: b
Clarification: stdarg.h is the header file which should be included if a function has to be defined such that it can accept variable number of arguments.

contest

250+ TOP MCQs on Conditional Preprocessor Directives and Answers

C MCQs on “Conditional Preprocessor Directives – 1”.

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

#include
#define max 100
main()
{
    #ifdef max
    printf("hello");
}

a) 100
b) hello
c) “hello”
d) error
Answer: d
Clarification: The code shown above results in an error. This is because the preprocessor #endif is missing in the above code. If the identifier is defined, then the statements following #ifdef are executed till #endif is encountered.

2. _______________ is the preprocessor directive which is used to end the scope of #ifdef.
a) #elif
b) #ifndef
c) #endif
d) #if
Answer: c
Clarification: The #ifdef preprocessor directive is used to check if a particular identifier is currently defined or not. If the particular identifier is defined, the statements following this preprocessor directive are executed till another preprocessor directive #endif is encountered. #endif is used to end the scope of #ifdef.

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

#include
void main()
{
    #ifndef max
    printf("hello");
    #endif
    printf("hi");
}

a) hello
b) hellohi
c) error
d) hi
Answer: b
Clarification: The code shown above illustrates the preprocessor directive #ifndef. If the identifier checked is not defined, then the statements following #ifndef are executed. Here, since the identifier max is not defined, the statement after #ifndef is executed. Hence the output is: hellohi

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

#include
#define san 557
main()
{
    #ifndef san
    printf("yes");
    #endif
    printf("no");
}

a) error
b) yes
c) no
d) yesno
Answer: c
Clarification: The output to the above code will be no. Since we have used the preprocessor directive, #ifndef and defined the identifier san, “yes” is not printed. Hence only “no” is printed as output.

5. The preprocessor directive which checks whether a constant expression results in a zero or non-zero value __________
a) #if
b) #ifdef
c) #undef
d) #ifndef
Answer: a
Clarification: #if checks whether a constant expression results in zero or not. If the expression is a non-zero value, then the statements between #if and #endif will be executed. If the constant expression results in a zero value, the statements between #if and #endif will not be executed.

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

#include
#define max 100
void main()
{
    #if(max%10)
    printf("san");
    #endif
    printf("foundry");
}

a) error
b) san
c) foundry
d)
Answer: d
Clarification: The code shown above is an illustration of the preprocessor directive #if. The value of the defined identifier max is 100. On checking the condition (100510==), which is true, the statements after #if are executed till #endif is encountered. Hence the output is .

7. The preprocessor directive which is used to remove the definition of an identifier which was previously defined with #define?
a) #ifdef
b) #undef
c) #ifndef
d) #def
Answer: b
Clarification: #undef is used to remove the definition of any identifier which had been previously defined in the code with #define.

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

#include
#define hello 10
void main()
{
    printf("%d",hello);
    #undef hello
    printf("%d",hello);
}

a) 10
b) hello
c) error
d) 1010
Answer: c
Clarification: Error: hello undeclared. An error is thrown when the code shown above is executed because before the second call to the printf function, the macro hello was undefined using #undef.

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

#include 
#define a 2
main()
{
    int r;
    #define a 5
    r=a*2;
    printf("%d",r);
}

a) 10
b) 4
c) 2
d) 5
Answer: a
Clarification: The macro ‘a’ is redefined in the code shown above. Hence the value of a, which is initially 2, is redefined to 5. The value stored in ‘r’ is the result of the expression (a*2), which is equal to 10. Hence the output of this code will be 10.

10. What will be the output of the following C code if the value of ‘p’ is 10 and that of ‘q’ is 15?

#include
int main()
{
    int p,q;
    printf("Enter two numbersn");
    scanf("%d",&p);
    scanf("%d",&q);
    #if(4<2)
    printf("%d",p);
    #elif(2>-1)
    printf("%d",q);
    #else
    printf("bye");
    #endif
}

a) 10
b) 15
c) bye
d) error
Answer: b
Clarification: The output of the code shown above is 15. The values given for ‘p’ and ‘q’ are 10 and 15 respectively. Since the condition given for #elif is true, the value stored in ‘q’ will be printed, that is 15.