250+ TOP MCQs on Pointers to Functions and Answers

C quiz on “Pointer to Functions”. 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 Functions” along with answers, explanations and/or solutions:

1. Which function is not called in the following C program?

  1.     #include 
  2.     void first()
  3.     {
  4.         printf("first");
  5.     }
  6.     void second()
  7.     {
  8.         first();
  9.     }
  10.     void third()
  11.     {
  12.         second();
  13.     }
  14.     void main()
  15.     {
  16.         void (*ptr)();
  17.         ptr = third;
  18.         ptr();
  19.     }

a) Function first
b) Function second
c) Function third
d) None of the mentioned
Answer: d
Clarification: None.

2. How to call a function without using the function name to send parameters?
a) typedefs
b) Function pointer
c) Both typedefs and Function pointer
d) None of the mentioned
Answer: b
Clarification: None.

3. Which of the following is a correct syntax to pass a Function Pointer as an argument?
a) void pass(int (*fptr)(int, float, char)){}
b) void pass(*fptr(int, float, char)){}
c) void pass(int (*fptr)){}
d) void pass(*fptr){}
Answer: a
Clarification: None.

4. Which of the following is not possible in C?
a) Array of function pointer
b) Returning a function pointer
c) Comparison of function pointer
d) None of the mentioned
Answer: d
Clarification: None.

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

  1.     #include 
  2.     void first()
  3.     {
  4.         printf("Hello World");
  5.     }
  6.     void main()
  7.     {
  8.         void *ptr() = first;
  9.         ptr++
  10.         ptr();
  11.     }

a) Illegal application of ++ to void data type
b) pointer function initialized like a variable
c) Illegal application of ++ to void data type & pointer function initialized like a variable
d) None of the mentioned
Answer: c
Clarification: None.

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

  1.     #include 
  2.     int mul(int a, int b, int c)
  3.     {
  4.         return a * b * c;
  5.     }
  6.     void main()
  7.     {
  8.         int (*function_pointer)(int, int, int);
  9.         function_pointer  =  mul;
  10.         printf("The product of three numbers is:%d",
  11.         function_pointer(2, 3, 4));
  12.     }

a) The product of three numbers is:24
b) Run time error
c) Nothing
d) Varies
Answer: a
Clarification: None.

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

  1.     #include 
  2.     int mul(int a, int b, int c)
  3.     {
  4.         return a * b * c;
  5.     }
  6.     void main()
  7.     {
  8.         int (function_pointer)(int, int, int);
  9.         function_pointer = mul;
  10.         printf("The product of three numbers is:%d",
  11.         function_pointer(2, 3, 4));
  12.     }

a) The product of three numbers is:24
b) Compile time error
c) Nothing
d) Varies
Answer: b
Clarification: None.

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

  1.     #include 
  2.     void f(int (*x)(int));
  3.     int myfoo(int);
  4.     int (*fooptr)(int);
  5.     int ((*foo(int)))(int);
  6.     int main()
  7.     {
  8.         fooptr = foo(0);
  9.         fooptr(10);
  10.     }
  11.     int ((*foo(int i)))(int)
  12.     {
  13.         return myfoo;
  14.     }
  15.     int myfoo(int i)
  16.     {
  17.         printf("%dn", i + 1);
  18.     }

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

250+ TOP MCQs on Typedefs and Answers

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

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

  1.     #include 
  2.     typedef struct student
  3.     {
  4.         char *a;
  5.     }stu;
  6.     void main()
  7.     {
  8.         struct stu s;
  9.         s.a = "hi";
  10.         printf("%s", s.a);
  11.     }

a) Compile time error
b) Varies
c) hi
d) h
Answer: a
Clarification: None.

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

  1.     #include 
  2.     typedef struct student
  3.     {
  4.         char *a;
  5.     }stu;
  6.     void main()
  7.     {
  8.         struct student s;
  9.         s.a = "hey";
  10.         printf("%s", s.a);
  11.     }

a) Compile time error
b) Varies
c) he
d) hey
Answer: d
Clarification: None.

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

  1.     #include 
  2.     typedef int integer;
  3.     int main()
  4.     {
  5.         int i = 10, *ptr;
  6.         float f = 20;
  7.         integer j = i;
  8.         ptr = &j;
  9.         printf("%dn", *ptr);
  10.         return 0;
  11.     }

a) Compile time error
b) Undefined behaviour
c) Depends on the standard
d) 10
Answer: d
Clarification: None.

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

  1.     #include 
  2.     int (*(x()))[2];
  3.     typedef int (*(*ptr)())[2] ptrfoo;
  4.     int main()
  5.     {
  6.         ptrfoo ptr1;
  7.         ptr1 = x;
  8.         ptr1();
  9.         return 0;
  10.     }
  11.     int (*(x()))[2]
  12.     {
  13.         int (*ary)[2] = malloc(sizeof*ary);
  14.         return &ary;
  15.     }

a) Compile time error
b) Nothing
c) Undefined behaviour
d) Depends on the standard
Answer: a
Clarification: None.

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

  1.     #include 
  2.     int *(*(x()))[2];
  3.     typedef int **(*ptrfoo)())[2];
  4.     int main()
  5.     {
  6.         ptrfoo ptr1;
  7.         ptr1 = x;
  8.         ptr1();
  9.         return 0;
  10.     }
  11.     int *(*(x()))[2]
  12.     {
  13.         int (*ary)[2] = malloc(sizeof * ary);
  14.         return &ary;
  15.     }

a) Compile time error
b) Nothing
c) Undefined behaviour
d) Depends on the standard
Answer: b
Clarification: None.

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

  1.     #include 
  2.     typedef struct p
  3.     {
  4.         int x, y;
  5.     };
  6.     int main()
  7.     {
  8.         p k1 = {1, 2};
  9.         printf("%dn", k1.x);
  10.     }

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

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

  1.     #include 
  2.     typedef struct p
  3.     {
  4.         int x, y;
  5.     }k = {1, 2};
  6.     int main()
  7.     {
  8.         p k1 = k;
  9.         printf("%dn", k1.x);
  10.     }

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

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

  1.     #include 
  2.     typedef struct p
  3.     {
  4.         int x, y;
  5.     }k;
  6.     int main()
  7.     {
  8.         struct p p = {1, 2};
  9.         k k1 = p;
  10.         printf("%dn", k1.x);
  11.     }

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

250+ TOP MCQs on Error Handling and Answers

’s Objective Questions and Answers on C helps Freshers preparing for Job Interviews. Freshers’s should practice these questions continuously for 2-3 months, thereby ensuring a top position in campus interviews or fresher’s hiring programs.

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

1. Which of the following causes an error?
a) Trying to read a file that doesn’t exist
b) Inability to write data in a file
c) Failure to allocate memory with the help of malloc
d) All of the mentioned
Answer: d
Clarification: None.

2. What is the purpose of the C function?

a) They check for input errors
b) They check for output errors
c) They check for all types of errors
d) They check for error in accessing the file
Answer: b
Clarification: None.

3. stderr is similar to?
a) stdin
b) stdout
c) Both stdout and stdin
d) None of the mentioned
Answer: b
Clarification: stderr is not exactly the same as stdout, but similar in the sense that both puts the output or error to the monitor.

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

fprintf(stderr, "error: could not open filen");

a) The diagnostic output is directly displayed in the output
b) The diagnostic output is pipelined to the output file
c) The line which caused error is compiled again
d) The program is immediately aborted
Answer: a
Clarification: None.

5. Which of the following function can be used to terminate the main() function from another function safely?
a) return(expr);
b) exit(expr);
c) abort();
d) both exit(expr); and abort();
Answer: b
Clarification: None.

6. Which of the following causes an error?
a) Trying to read a file that doesn’t exist
b) Inability to write data in a file
c) Failure to allocate memory with the help of malloc
d) All of the mentioned
Answer: d
Clarification: None.

7. What is the purpose of the C function?

a) They check for input errors
b) They check for output errors
c) They check for all types of errors
d) They check for error in accessing the file
Answer: b
Clarification: None.

250+ TOP MCQs on printf and Answers

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

1. If by mistake you specify more number of arguments, the excess arguments will ____________
a) be ignored
b) produce compile error
c) produce run-time error
d) produce logical error
Answer: a
Clarification: The excess arguments will simply be ignored.

2. What happens when zero flag is used with left justification?
a) data is padded with zeros
b) zero flag is ignored
c) data is padded with blank spaces
d) will give error
Answer: b
Clarification: Zero flag is not considered when used with left justification because adding zeros after a number changes its value.

3. For floating point numbers, the precision flag specifies the number of decimal places to be printed. When no precision modifier is specified, printf() prints _______
a) six decimal positions
b) five decimal positions
c) four decimal positions
d) three decimal positions
Answer: a
Clarification: Its format can be given as “. m”, where m specifies the number of decimal digits when no precision modifier is specified, printf prints six decimal positions.

4. What will the given code result in printf(“n you are”awesome ” “);?
a) compile error
b) run-time error
c) you are “awesome”
d) you are awesome
Answer: c
Clarification: The above given code uses ”” to display the word within double inverted commas on standard output screen.

5. What will be the output for the given code printf(“n The number is %07d”,1212);
a) The number is 0001212
b) The number is 1212
c) The number is 1212
d) The number is 1212000
Answer: a
Clarification: 0 in the above code is Flags. The number is left-padded with zeros(0) instead of spaces.

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

char t=’N’;
printf(“n %c n %3c n %5c”,t,t,t);

a)              N
          N
    N
b)    N
            N
                 N
c)  N
      N
      N
d)   N  N  N  
Answer: b
Clarification: In the given code each argument is printed on a new line due to control character n. Width mentioned in the above code is 1,3,5 hence the character is printed on a new line after being padded with blank spaces.

7. Select the right explanation to the given code.

printf(%*. *f”, 5,4,5700);

a) the minimum field width has to be 4, the precision is given to be 5, and the value to be displayed is 5700
b) the minimum field width is 5, the precision is 4, and the value to be displayed is 5700
c) compile error
d) run-time error
Answer: b
Clarification: The minimum field width and precision specifiers are usually constants. They can also be provided by arguments to printf(). This is done by using * modifier as shown in the given code.

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

char str[] = "Hello Nancy“;
printf(“n %.7s”, str) ;

a) Hello Nan
b) Hello
c) Hello N
d) Hello Nancy
Answer: c
Clarification: The output for the code must be 7 characters including white spaces.

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

char str[] =”Too Good”;
printf(“n %7s”,str);

a) Too Good
b) Too G
c) Too Go
d) Too
Answer: a
Clarification: The complete string “Too Good” is printed. This is because if data needs more space than specified, then printf overrides the width specified by the user.

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

printf(“n Output: %5d t %x t %#x”, 234,234,234);

a) Output:234EA0xEA
b) Output:00234 EA 0xEA
c) Output:    234 EA 0xEA
d) ERROR
Answer: c
Clarification: The control character t is used to provide gap between the words. %5d – the width of the string is set to 5, characters are printed after being padded with blank spaces.%x, %#x is additional specifiers for octal and hexadecimal values.

250+ TOP MCQs on Error Handling and Answers

C MCQs on “Error Handling”.

1. _______ occurs when a result is too large in magnitude to represent errors as a floating-point value of the required type.
a) underflow
b) significance loss
c) domain
d) overflow
Answer: d
Clarification: An overflow occurs when a result is too large in magnitude to represent errors as a floating-point value of the required type.

2. What occurs when a result has nowhere near the number of significant digits indicated by its type.
a) domain
b) underflow
c) overflow
d) significance loss
Answer: d
Clarification: A significance loss occurs when a result has nowhere near the number of significant digits indicated by its type.

3. What error occurs when a result is undefined for a given argument value?
a) significance loss
b) underflow
c) overflow
d) domain
Answer: d
Clarification: A domain error occurs when a result is undefined for a given argument value.

4.______ is reported on a domain error.
a) EDOM
b) ERANGE
c) Significance loss
d) Underflow
Answer: a
Clarification: EDOM is reported on a domain error.

5. ERANGE is reported on an overflow or an underflow.
a) true
b) false
Answer: a
Clarification: This macro represents a range error, which occurs if an input argument is outside the range, over which the mathematical function is defined and errno is set to ERANGE.

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

errno = 0;
y = sqrt(2);
if(errno == EDOM)
printf("Invalid valuen");
else
printf("Valid valuen");

a) Invalid value
b) Valid value
c) No output
d) Compile error
Answer: b
Clarification: The C library macro EDOM represents a domain error, which occurs if an input argument is outside the domain, over which the mathematical function is defined and errno is set to EDOM.

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

errno = 0;
y = sqrt(-10);
if(errno == EDOM)
printf("Invalid value n");
else
printf("Valid valuen");

a) Invalid value
b) Valid value
c) No output
d) Compile error
Answer: a
Clarification: The C library macro EDOM represents a domain error, which occurs if an input argument is outside the domain, over which the mathematical function is defined and errno is set to EDOM.

8. errno causes trouble in two subtler ways(vague and explicit).
a) true
b) false
Answer: a
Clarification: errno causes trouble in two subtler ways – sometimes its specification is too vague and sometimes it is too explicit.

9. No library function will store a zero in errno.
a) true
b) false
Answer: a
Clarification: Any library function can store nonzero values in errno.

10. __________ tells the compiler that this data is defined somewhere and will be connected with the linker.
a) errno
b) extern
c) variable
d) yvals
Answer: b
Clarification: The C library macro extern int errno is set by system calls and some library functions in the event of an error to indicate if anything went wrong.

250+ TOP MCQs on Date and Time Functions and Answers

C MCQs on “Date and Time Functions – 1”.

1. Which of the following library functions returns the time in UTC (Greenwich mean time) format?
a) localtime()
b) gettime()
c) gmtime()
d) settime()
Answer: c
Clarification: The library function gmtime() returns the time in UTC format. To find the current time, we use the function localtime().

2. What will be the output of the following C code? (By date we mean: date, month and year)

#include
#include
#include
int main()
{
    time_t ct;
    time(&ct);
    printf("%sn",ctime(&ct));
}

a) only current date
b) only current date and current time
c) current date, current time and the day of the week
d) only current time
Answer: c
Clarification: The code shown above will print the current date, current time and the day of the week as output.

3. What will be the output of the following C code if the current system date is 6/22/2017?

#include
#include
#include
int main()
{
    time_t ct;
    time(&ct);
    struct tm *mt=localtime(&ct);
    printf("%dn",mt-> tm_mon+2);
}

a) 8
b) 7
c) 5
d) 6
Answer: b
Clarification: Since the given date is 22nd of June, 2017. Since June is the sixth month of the year, the output will be 5. (0-Jan, 1-Feb, 2-March, 4-April, 5-May, 6-June……..)

4. What will be the output of the following C code if the current system date is 6/22/2017?

#include
#include
#include
typedef struct tm tm;
int main()
{
    time_t ct;
    time(&ct);
    tm *mt=localtime(&ct);
    printf("%dn",mt-> tm_year);
}

a) 17
b) 2017
c) error
d) 117
Answer: d
Clarification: The output of the code shown above is the number of the current year, counted from the year 1900. Hence, since the current year is 2017, the output will be: 2017-1900 = 117.

5. What will be the output of the following C code if the current system date is 6/22/2017?

#include
#include
#include
int main()
{
    time_t ct;
    time(&ct);
    struct tm *mt=localtime(&ct);
    printf("%dn",mt-> tm_date);
}

a) 22
b) 6
c) 22/6
d) error
Answer: d
Clarification: The code shown above results in an error. This is because tm_date is not a specified function under time.h.

6. The purpose of the function ctime() is that ___________
a) it returns a string representing the local time
b) it returns a void string
c) it returns a string representing the time in UTC format
d) it returns a string representing the time stored in a structure
Answer: a
Clarification: The library function ctime() returns a string representation of the local time. The function asctime() returns a string representing the time stored in a structure.

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

#include
int main (void)
{
    float n = time(NULL);
    printf("%.2fn" , n);
}

a) time in seconds from 1 January, 1970
b) time in minutes from 1 January, 1970
c) time in seconds from 1 January, 1980
d) time in minutes from 1 January, 1980
Answer: a
Clarification: The output of the code shown above will be the time in seconds from 1 January, 1970.

8. What will be the output of the following C code if the system date is 6/2/2017(Friday)?

#include
#include
int main()
{
    struct tm *local, *gm;
    time_t t;
    t=time(NULL);
    local=localtime(&t);
    printf("%d",local->tm_wday);
    return 0;
}

a) 6
b) 5
c) error
d) 0
Answer: b
Clarification: Since the given question has clearly specified that the current weekday at the time of execution of the code is Friday, the output of the code shown above is 5. (0-Sunday, 1-Mon, 2-Tue, 3-Wed, 4 – Thurs, 5-fri, 6-Sat)

9. Which of the following functions returns a pointer to a string representing the date and time stored in a structure?
a) ctime()
b) time()
c) asctime()
d) localtime()
Answer: c
Clarification: The function asctime() returns a pointer to a string representing the date and time stored in a structure.

10. What will be the output of the following C code if it is executed on 2nd January, 2017 (system date)?

#include
#include
int main()
{
    struct tm *local, *gm;
    time_t t;
    t=time(NULL);	
    local=localtime(&t);
    printf("%d",local->tm_yday);
    return 0;
}

a) 0
b) 1
c) 2
d) Error
Answer: b
Clarification: The output of the code shown above is 1. In the question, it is given that the current date is 2nd January, 2017. (1st Jan – 0, 2nd Jan – 1, 3rd Jan – 2)