250+ TOP MCQs on C-Preprocessor and Answers

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

1. Property which allows to produce different executable for different platforms in C is called?
a) File inclusion
b) Selective inclusion
c) Conditional compilation
d) Recursive macros
Answer: c
Clarification: Conditional compilation is the preprocessor facility to produce a different executable.

2. What is #include ?
a) Preprocessor directive
b) Inclusion directive
c) File inclusion directive
d) None of the mentioned
Answer: a
Clarification: None.

3. C preprocessors can have compiler specific features.
a) True
b) False
c) Depends on the standard
d) Depends on the platform
Answer: a
Clarification: #pragma is compiler specific feature.

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

  1. #include 
  2. #define foo(m, n) m * n = 10
  3. int main()
  4. {
  5.     printf("in mainn");
  6. }

a) In main
b) Compilation error as lvalue is required for the expression m*n=10
c) Preprocessor error as lvalue is required for the expression m*n=10
d) None of the mentioned
Answer: a
Clarification: Preprocessor just replaces whatever is given compiler then checks for error at the replaced part of the code. Here it is not replaced anywhere.
Output:
$ cc pgm1.c
$ a.out
in main

5. C preprocessor is conceptually the first step during compilation.
a) True
b) False
c) Depends on the compiler
d) Depends on the standard
Answer: a
Clarification: None.

6. Preprocessor feature that supply line numbers and filenames to compiler is called?
a) Selective inclusion
b) macro substitution
c) Concatenation
d) Line control
Answer: d
Clarification: None.

7. #include are _______ files and #include “somefile.h” ________ files.
a) Library, Library
b) Library, user-created header
c) User-created header, library
d) They can include all types of file
Answer: d
Clarification: Both of these statement can be used to select any file.

8. What is a preprocessor?
a) That processes its input data to produce output that is used as input to another program
b) That is nothing but a loader
c) That links various source files
d) All of the mentioned
Answer: a
Clarification: A preprocessor is a program that processes its input data to produce output that is used as input to another program.

250+ TOP MCQs on Character Pointers and Functions and Answers

C Objective Questions on “Character Pointers and Functions”. 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 “Character Pointers and Functions” 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.         char *str = "hello, worldn";
  5.         char *strc = "good morningn";
  6.         strcpy(strc, str);
  7.         printf("%sn", strc);
  8.         return 0;
  9.     }

a) hello, world
b) Crash/segmentation fault
c) Undefined behaviour
d) Run time error
Answer: b
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello world";
  5.         char strc[] = "good morning indian";
  6.         strcpy(strc, str);
  7.         printf("%sn", strc);
  8.         return 0;
  9.     }

a) hello world
b) hello worldg india
c) Compile time error
d) Undefined behaviour
Answer: a
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello, world!!n";
  5.         char strc[] = "good morningn";
  6.         strcpy(strc, str);
  7.         printf("%sn", strc);
  8.         return 0;
  9.     }

a) hello, world!!
b) Compile time error
c) Undefined behaviour
d) Segmenation fault
Answer: c
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello, worldn";
  5.         str[5] = '.';
  6.         printf("%sn", str);
  7.         return 0;
  8.     }

a) hello. world
b) hello, world
c) Compile error
d) Segmentation fault
Answer: d
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char str[] = "hello, world";
  5.         str[5] = '.';
  6.         printf("%sn", str);
  7.         return 0;
  8.     }

a) hello. world
b) hello, world
c) Compile error
d) Segmentation fault
Answer: a
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello world";
  5.         char strary[] = "hello world";
  6.         printf("%d %dn", sizeof(str), sizeof(strary));
  7.         return 0;
  8.     }

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

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello world";
  5.         char strary[] = "hello world";
  6.         printf("%d %dn", strlen(str), strlen(strary));
  7.         return 0;
  8.     }

a) 11 11
b) 12 11
c) 11 12
d) x 11 where x can be any positive integer.
Answer: a
Clarification: None.

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

  1.     #include 
  2.     void f(char *k)
  3.     {
  4.         k++;
  5.         k[2] = 'm';
  6.         printf("%cn", *k);
  7.     }
  8.     void main()
  9.     {
  10.         char s[] = "hello";
  11.         f(s);
  12.     }

a) l
b) e
c) h
d) o
Answer: b
Clarification: None.

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

  1.     #include 
  2.     void fun(char *k)
  3.     {
  4.         printf("%s", k);
  5.     }
  6.     void main()
  7.     {
  8.         char s[] = "hello";
  9.         fun(s);
  10.     }

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

250+ TOP MCQs on Basics of Structures and Answers

online C quiz on “Basics of Structures”. One shall practice these online 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 online C quiz on “Basics of Structures” along with answers, explanations and/or solutions:

1. Which of the following are themselves a collection of different data types?
a) string
b) structures
c) char
d) all of the mentioned
Answer: b
Clarification: None.

2. User-defined data type can be derived by___________
a) struct
b) enum
c) typedef
d) all of the mentioned
Answer: d
Clarification: None.

3. Which operator connects the structure name to its member name?
a) –
b) <-
c) .
d) Both <- and .
Answer: c
Clarification: None.

4. Which of the following cannot be a structure member?
a) Another structure
b) Function
c) Array
d) None of the mentioned
Answer: b
Clarification: None.

5. Which of the following structure declaration will throw an error?
a)

b)

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

c)

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

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

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

  1.     #include 
  2.     struct student
  3.     {
  4.         int no;
  5.         char name[20];
  6.     }
  7.     void main()
  8.     {
  9.         struct student s;
  10.         s.no = 8;
  11.         printf("hello");
  12.     }

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

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

  1.     #include 
  2.     struct student
  3.     {
  4.         int no = 5;
  5.         char name[20];
  6.     };
  7.     void main()
  8.     {
  9.         struct student s;
  10.         s.no = 8;
  11.         printf("hello");
  12.     }

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

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

  1.     #include 
  2.     struct student
  3.     {
  4.         int no;
  5.         char name[20];
  6.     };
  7.     void main()
  8.     {
  9.         student s;
  10.         s.no = 8;
  11.         printf("hello");
  12.     }

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

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

  1.     #include 
  2.     void main()
  3.     {
  4.         struct student
  5.         {
  6.             int no;
  7.             char name[20];
  8.         };
  9.         struct student s;
  10.         s.no = 8;
  11.         printf("%d", s.no);
  12.     }

a) Nothing
b) Compile time error
c) Junk
d) 8
Answer: d
Clarification: None.

10. Can the following C code be compiled successfully?

  1.     #include 
  2.     struct p
  3.     {
  4.         int k;
  5.         char c;
  6.         float f;
  7.     };
  8.     int main()
  9.     {
  10.         struct p x = {.c = 97, .f = 3, .k = 1};
  11.         printf("%fn", x.f);
  12.     }

a) Yes
b) No
c) Depends on the standard
d) Depends on the platform
Answer: c
Clarification: None.

250+ TOP MCQs on Bit-fields and Answers

C programming Objective Questions on “Bit-fields”. 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 programming Objective Questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C programming Objective Questions on “Bit-fields” along with answers, explanations and/or solutions:

1. What is the correct syntax to initialize bit-fields in an structure?
a)

    struct temp
    {
        unsigned int a : 1;
    }s;

b)

    struct temp
    {
        unsigned int a = 1;
    }s;

c)

    struct temp
    {
        unsigned float a : 1;
    }s;

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

2. Which of the following data types are accepted while declaring bit-fields?
a) char
b) float
c) double
d) none of the mentioned
Answer: a
Clarification: None.

3. Which of the following reduces the size of a structure?
a) union
b) bit-fields
c) malloc
d) none of the mentioned
Answer: b
Clarification: None.

4. For what minimum value of x in a 32-bit Linux OS would make the size of s equal to 8 bytes?

  1.     struct temp
  2.     {
  3.         int a : 13;
  4.         int b : 8;
  5.         int c : x;
  6.     }s;

a) 4
b) 8
c) 12
d) 32
Answer: c
Clarification: None.

5. Calculate the % of memory saved when bit-fields are used for the following C structure as compared to with-out use of bit-fields for the same structure? (Assuming size of int = 4)

  1.     struct temp
  2.     {
  3.         int a : 1;
  4.         int b : 2;
  5.         int c : 4;
  6.         int d : 4;
  7.     }s;

a) 25%
b) 33.3%
c) 50%
d) 75%
Answer: d
Clarification: None.

6. In the following declaration of bit-fields, the constant-expression specifies __________

    struct-declarator:
    declarator
    type-specifier declarator opt : constant-expression

a) The width of the field in bits
b) Nothing
c) The width of the field in bytes
d) Error
Answer: a
Clarification: None.

7. In the following declaration of bit-fields, the constant-expression must be __________

    struct-declarator:
    declarator
    type-specifier declarator opt : constant-expression

a) Any type
b) Nothing
c) Integer value
d) Nonnegative integer value
Answer: d
Clarification: None.

8. Which of the following is not allowed?
a) Arrays of bit fields
b) Pointers to bit fields
c) Functions returning bit fields
d) None of the mentioned
Answer: d
Clarification: None.

9. Bit fields can only be declared as part of a structure.
a) false
b) true
c) Nothing
d) Varies
Answer: b
Clarification: None.

10. What is the order for the following C declarations?

    short a : 17;
    int long y : 33;

a) Legal, legal
b) Legal, illegal
c) Illegal, illegal
d) Illegal, legal
Answer: c
Clarification: None.

250+ TOP MCQs on String Operations and Answers

’s Objective Questions and Answers on C helps Experienced people preparing for Job Interviews. Experienced IT profession should practice these questions continuously for 2-3 months, thereby ensuring a top position in lateral or walk-in interviews.

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

1. What type of return-type used in String operations?
a) void only
b) void and (char *) only
c) void and int only
d) void, int and (char *) only
Answer: d
Clarification: None.

2. String operation such as strcat(s, t), strcmp(s, t), strcpy(s, t) and strlen(s) heavily rely upon.
a) Presence of NULL character
b) Presence of new-line character
c) Presence of any escape sequence
d) None of the mentioned
Answer: a
Clarification: None.

3. Which pre-defined function returns a pointer to the last occurence of a character in a string?
a) strchr(s, c);
b) strrchr(s, c);
c) strlchr(s, c);
d) strfchr(s, c);
Answer: b
Clarification: None.

4. Which of the following function compares 2 strings with case-insensitively?
a) strcmp(s, t)
b) strcmpcase(s, t)
c) strcasecmp(s, t)
d) strchr(s, t)
Answer: c
Clarification: None.

5. What will be the value of var for the following C statement?

var = strcmp("Hello", "World");

a) -1
b) 0
c) 1
d) strcmp has void return-type
Answer: a
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         char str[10] = "hello";
  5.         char *p = strrchr(str, 'l');
  6.         printf("%cn", *(++p));
  7.     }

a) l
b) o
c) e
d) Compilation error
Answer: b
Clarification: None.

250+ TOP MCQs on File Operations and Answers

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

1. what is the function of fputs()?
a) read a line from a file
b) read a character from a file
c) write a character to a file
d) write a line to a file
Answer: d
Clarification: The fputs() is used to write a line to a file. fputs() syntax can be written as
int fputs(const char *str, FILE *stream);

2. What does the following C code snippet mean?

a) reads the next input line into the array s
b) writes the line into the array s
c) reads the next input character into the array s
d) write a character into the array
Answer: a
Clarification: gets() reads the next input line into the array s, terminating newline is replaced with ‘ ’.It returns s, or NULL if end of file or error occurs.

3. Which function will return the current file position for stream?
a) fgetpos()
b) fseek()
c) ftell()
d) fsetpos()
Answer: c
Clarification: The current file position is returned by ftell() function for stream, or -1L on error.

4. Select the right explanation for the following C code snippet.

int fgetpos(FILE *stream, fpos_t *s)

a) records the current position in stream in *s
b) sets the file position for stream in *s
c) positions stream at the position recorded in *s
d) reads from stream into the array ptr
Answer: a
Clarification:fgetpos() records the current position in stream in *s, for subsequent use by fsetpos() . The type fpost_t is suitable for recording such values.

5. Which functions is declared in ?
a) fseek()
b) ftell()
c) ferror()
d) fsetpos()
Answer: c
Clarification: ferror() is declared under . ferror() returns non-zero if the error indicator for stream is set.

6. setvbuf() and setbuf() function controls buffering for the stream.
a) true
b) false
Answer: a
Clarification: setvbuf() and setbuf() controls buffering for the stream. If buff is NULL, buffering is turned off for the stream.

7. The functions vprintf(), vfprintf(), and vsprintf() are not equivalent to the corresponding printf() functions except the variable argument list.
a) true
b) false
Answer: b
Clarification: The functions vprintf() , vfprintf() , and vsprintf() are similar to the corresponding printf() functions except that the variable argument list is replaced by arg.

8. The______function reads atmost one less than the number of characters specified by size from the given stream and it is stored in the string str.
a) fget()
b) fgets()
c) fput()
d) fputs()
Answer: b
Clarification: The fgets() function reads one less than the number of characters indicated by the size from the given stream and it is stored in the string str. The fgets() terminates as soon as it encounters either a newline character, EOF, or other error.

9. What does the following C code snippet mean?

int ungetc(int c, FILE *stream)

a) pushes c back onto a stream
b) deletes c from the stream
c) reads frequency of c in stream
d) no action is taken by the command
Answer: a
Clarification: ungetc() pushes c back onto stream, where it will be returned on the next read. Only one character of pushback per stream is Guaranteed.

10. Choose the correct difference between getc() and fgetc().
a) If it is not a macro, it may evaluate stream more than once
b) if it is amacro, it may not evaluate stream more than once
c) if it is a macro, it may evaluate stream more than once
d) no difference between fgetc() and getc()
Answer: c
Clarification: getc() is equivalent to fgetc() except that if it is a macro, it may evaluate more than once.