C Programming MCQ (Multiple Choice Questions)
Posted by Superadmin on July 31 2024 17:08:31

C Programming MCQ (Multiple Choice Questions)

Here are 1000 MCQs on C Programming (Chapterwise).

1. Who is the father of C language?
a) Steve Jobs
b) James Gosling
c) Dennis Ritchie
d) Rasmus Lerdorf

 


Answer: c
Explanation: Dennis Ritchie is the father of C Programming Language. C programming language was developed in 1972 at American Telephone & Telegraph Bell Laboratories of USA.

 


2. Which of the following is not a valid C variable name?
a) int number;
b) float rate;
c) int variable_count;
d) int $main;

 


Answer: c
Explanation: Dennis Ritchie is the father of C Programming Language. C programming language was developed in 1972 at American Telephone & Telegraph Bell Laboratories of USA.

 

 


3. All keywords in C are in ____________
a) LowerCase letters
b) UpperCase letters
c) CamelCase letters
d) None of the mentioned

 


Answer: a
Explanation: None.

 


4. Which of the following is true for variable names in C?
a) They can contain alphanumeric characters as well as special characters
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
c) Variable names cannot start with a digit
d) Variable can be of any length

 


Answer: c
Explanation: According to the syntax for C variable name, it cannot start with a digit.

 


5. Which is valid C expression?
a) int my_num = 100,000;
b) int my_num = 100000;
c) int my num = 1000;
d) int $my_num = 10000;

 


Answer: b
Explanation: Space, comma and $ cannot be used in a variable name.

 

 


6. Which of the following cannot be a variable name in C?
a) volatile
b) true
c) friend
d) export

 


Answer: a
Explanation: volatile is C keyword.

 


7. What is short int in C programming?
a) The basic data type of C
b) Qualifier
c) Short is the qualifier and int is the basic data type
d) All of the mentioned

 


Answer: c
Explanation: None.

 

 


8. Which of the following declaration is not supported by C language?
a) String str;
b) char *str;
c) float str = 3e2;
d) Both “String str;” and “float str = 3e2;”

 


Answer: a
Explanation: It is legal in Java, but not in C language.

 


9. 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
Explanation: const is a keyword constant in C program.

 


10. What is the result of logical or relational expression in C?
a) True or False
b) 0 or 1
c) 0 if an expression is false and any positive number if an expression is true
d) None of the mentioned

 


Answer: b
Explanation: None.

 


11. Which of the following typecasting is accepted by C language?
a) Widening conversions
b) Narrowing conversions
c) Widening & Narrowing conversions
d) None of the mentioned

 

Answer: c
Explanation: None.


12. Where in C the order of precedence of operators do not exist?
a) Within conditional statements, if, else
b) Within while, do-while
c) Within a macro definition
d) None of the mentioned

 


Answer: d
Explanation: None.

 


13. Which of the following is NOT possible with any 2 operators in C?
a) Different precedence, same associativity
b) Different precedence, different associativity
c) Same precedence, different associativity
d) All of the mentioned

 

 

Answer: c
Explanation: None.

 


14. What is an example of iteration in C?
a) for
b) while
c) do-while
d) all of the mentioned


Answer: d
Explanation: None.

 


15. Functions can return enumeration constants in C?
a) true
b) false
c) depends on the compiler
d) depends on the standard

 


Answer: a
Explanation: None.

 


16. Functions in C Language are always _________
a) Internal
b) External
c) Both Internal and External
d) External and Internal are not valid terms for functions

 


Answer: b
Explanation: None.

 


17. Which of following is not accepted in C?
a) static a = 10; //static as
b) static int func (int); //parameter as static
c) static static int a; //a static variable prefixed with static
d) all of the mentioned

 


Answer: c
Explanation: None.

 


18. 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
Explanation: Conditional compilation is the preprocessor facility to produce a different executable.


19. What is #include <stdio.h>?
a) Preprocessor directive
b) Inclusion directive
c) File inclusion directive
d) None of the mentioned

 


Answer: a
Explanation: None.

 


20. C preprocessors can have compiler specific features.
a) True
b) False
c) Depends on the standard
d) Depends on the platform

 


Answer: a
Explanation: #pragma is compiler specific feature.

 


21. Which of the following are C preprocessors?
a) #ifdef
b) #define
c) #endif
d) all of the mentioned

 

Answer: d
Explanation: None.


22. The C-preprocessors are specified with _________ symbol.
a) #
b) $
c) ” ”
d) &

 

Answer: a
Explanation: The C-preprocessors are specified with # symbol.


23. How is search done in #include and #include “somelibrary.h” according to C standard?
a) When former is used, current directory is searched and when latter is used, standard directory is searched
b) When former is used, standard directory is searched and when latter is used, current directory is searched
c) When former is used, search is done in implementation defined manner and when latter is used, current directory is searched
d) For both, search for ‘somelibrary’ is done in implementation-defined places


Answer: b
Explanation: None.

 


24. How many number of pointer (*) does C have against a pointer variable declaration?
a) 7
b) 127
c) 255
d) No limits


Answer: d
Explanation: None.

 


25. Which of the following is not possible statically in C language?
a) Jagged Array
b) Rectangular Array
c) Cuboidal Array
d) Multidimensional Array

 


Answer: a
Explanation: None.

 


26. Which of the following return-type cannot be used for a function in C?
a) char *
b) struct
c) void
d) none of the mentioned

 

Answer: d
Explanation: None.

 

 

27. The standard header _______ is used for variable list arguments (…) in C.
a) <stdio.h >
b) <stdlib.h>
c) <math.h>
d) <stdarg.h>


Answer: d
Explanation: None.

 


28. When a C program is started, O.S environment is responsible for opening file and providing pointer for that file?
a) Standard input
b) Standard output
c) Standard error
d) All of the mentioned

 


Answer: d
Explanation: None.

 


29. In C language, FILE is of which data type?
a) int
b) char *
c) struct
d) None of the mentioned

 


Answer: c
Explanation: None.

 


30. What is the sizeof(char) in a 32-bit C compiler?
a) 1 bit
b) 2 bits
c) 1 Byte
d) 2 Bytes

 


Answer: c
Explanation: None.

 


31. Which of the following is not an operator in C?
a) ,
b) sizeof()
c) ~
d) None of the mentioned

 


Answer: d
Explanation: None.

 


32. scanf() is a predefined function in______header file.
a) stdlib. h
b) ctype. h
c) stdio. h
d) stdarg. h

 


Answer: c
Explanation: scanf() is a predefined function in "stdio.h" header file.printf and scanf() carry out input and output functions in C. These functions statements are present in the header file stdio.h.

 


33. What is meant by ‘a’ in the following C operation?

fp = fopen("Random.txt", "a");

a) Attach
b) Append
c) Apprehend
d) Add

 

Answer: b
Explanation: None.


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

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int y = 10000;
  5.         int y = 34;
  6.         printf("Hello World! %d\n", y);
  7.         return 0;
  8.     }

a) Compile time error
b) Hello World! 34
c) Hello World! 1000
d) Hello World! followed by a junk value

 

Answer: a
Explanation: Since y is already defined, redefining it results in an error.
Output:
$ cc pgm2.c
pgm2.c: In function ‘main’:
pgm2.c:5: error: redefinition of ‘y’
pgm2.c:4: note: previous definition of ‘y’ was here


35. What will happen if the following C code is executed?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int main = 3;
  5.         printf("%d", main);
  6.         return 0;
  7.     }

a) It will cause a compile-time error
b) It will cause a run-time error
c) It will run without any error and prints 3
d) It will experience infinite looping

 


Answer: c
Explanation: A C program can have same function name and same variable name.
$ cc pgm3.c
$ a.out
3

 


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

  1.     #include  <stdio.h>
  2.     int main()
  3.     {
  4.        signed char chr;
  5.        chr = 128;
  6.        printf("%d\n", chr);
  7.        return 0;
  8.     }

a) 128
b) -128
c) Depends on the compiler
d) None of the mentioned

 


Answer: b
Explanation: The range of signed character is from -128 to +127. Since we are assigning a value of 128 to the variable ‘chr’, the result will be negative. 128 in binary is represented as “1000 0000” for character datatype. As you can see that the sign bit is set to 1, followed by 7 zeros (0), its final decimal value will be -128 (negative 128).
Output:
$ cc pgm2.c
$ a.out
-128

 


37. What will be the output of the following C code on a 64 bit machine?

  1.     #include <stdio.h>
  2.     union Sti
  3.     {
  4.         int nu;
  5.         char m;
  6.     };
  7.     int main()
  8.     {
  9.         union Sti s;
  10.         printf("%d", sizeof(s));
  11.         return 0;
  12.     }

a) 8
b) 5
c) 9
d) 4

 


Answer: d
Explanation: Since the size of a union is the size of its maximum data type, here int is the largest data type. Hence the size of the union is 4.
Output:
$ cc pgm7.c
$ a.out
4

 


38. What will be the output of the following C function?

  1.     #include <stdio.h>
  2.     enum birds {SPARROW, PEACOCK, PARROT};
  3.     enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
  4.     int main()
  5.     {
  6.         enum birds m = TIGER;
  7.         int k;
  8.         k = m;
  9.         printf("%d\n", k);
  10.         return 0;
  11.     }

a) 0
b) Compile time error
c) 1
d) 8

 


Answer: d
Explanation: m is an integer constant, hence it is compatible.
Output:
$ cc pgm5.c
$ a.out
8

 


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

  1.     #include <stdio.h>
  2.     int const print()
  3.     {
  4.         printf("Sanfoundry.com");
  5.         return 0;
  6.     }
  7.     void main()
  8.     {
  9.         print();
  10.     }

a) Error because function name cannot be preceded by const
b) Sanfoundry.com
c) Sanfoundry.com is printed infinite times
d) Blank screen, no output

 


Answer: b
Explanation: None.
Output:
$ cc pgm13.c
$ a.out
Sanfoundry.com

 


40. Will the following C code compile without any error?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         for (int k = 0; k < 10; k++);
  5.             return 0;
  6.     }

a) Yes
b) No
c) Depends on the C standard implemented by compilers
d) Error

 

Answer: c
Explanation: Compilers implementing C90 do not allow this, but compilers implementing C99 allow it.
Output:
$ cc pgm4.c
pgm4.c: In function ‘main’:
pgm4.c:4: error: ‘for’ loop initial declarations are only allowed in C99 mode
pgm4.c:4: note: use option -std=c99 or -std=gnu99 to compile your code


41. What will be the final value of x in the following C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int x = 5 * 9 / 3 + 9;
  5.     }

a) 3.75
b) Depends on compiler
c) 24
d) 3

 

Answer: c
Explanation: None.

 


42. What will be the output of the following C code? (Initial values: x= 7, y = 8)

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         float x;
  5.         int y;
  6.         printf("enter two numbers \n");
  7.         scanf("%f %f", &x, &y);
  8.         printf("%f, %d", x, y);
  9.     }

a) 7.000000, 7
b) Run time error
c) 7.000000, junk
d) Varies

 


Answer: c
Explanation: None.

 


43. What will be the output of the following C code considering the size of a short int is 2, char is 1 and int is 4 bytes?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         short int i = 20;
  5.         char c = 97;
  6.         printf("%d, %d, %d\n", sizeof(i), sizeof(c), sizeof(c + i));
  7.         return 0;
  8.     }

a) 2, 1, 2
b) 2, 1, 1
c) 2, 1, 4
d) 2, 2, 8

 


Answer: c
Explanation: None.

 


44. What is the difference between the following 2 C codes?

  1.     #include <stdio.h> //Program 1
  2.     int main()
  3.     {
  4.         int d, a = 1, b = 2;
  5.         d =  a++ + ++b;
  6.         printf("%d %d %d", d, a, b);
  7.     }
  1.     #include <stdio.h> //Program 2
  2.     int main()
  3.     {
  4.         int d, a = 1, b = 2;
  5.         d =  a++ +++b;
  6.         printf("%d %d %d", d, a, b);
  7.     }

a) No difference as space doesn’t make any difference, values of a, b, d are same in both the case
b) Space does make a difference, values of a, b, d are different
c) Program 1 has syntax error, program 2 is not
d) Program 2 has syntax error, program 1 is not

 


Answer: d
Explanation: None.

 


45. What will be the output of the following C code snippet?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         1 < 2 ? return 1: return 2;
  5.     }

a) returns 1
b) returns 2
c) Varies
d) Compile time error

 


Answer: d
Explanation: None.

 


46. What will be the value of the following assignment expression?

 (x = foo())!= 1 considering foo() returns 2

a) 2
b) True
c) 1
d) 0

 


Answer: c
Explanation: None.

 

 


47. What will be the output of the following C function?

  1.     #include <stdio.h>
  2.     void reverse(int i);
  3.     int main()
  4.     {
  5.         reverse(1);
  6.     }
  7.     void reverse(int i)
  8.     {
  9.         if (i > 5)
  10.             return ;
  11.         printf("%d ", i);
  12.         return reverse((i++, i));
  13.     }

a) 1 2 3 4 5
b) Segmentation fault
c) Compilation error
d) Undefined behaviour

 


Answer: a
Explanation: None.

 


48. What will be the final values of i and j in the following C code?

  1. #include <stdio.h>
  2. int x = 0;
  3. int f()
  4. {
  5.     if (x == 0)
  6.         return x + 1;
  7.     else
  8.         return x - 1;
  9. }
  10. int g()
  11. {
  12.     return x++;
  13. }
  14. int main()
  15. {
  16.     int i = (f() + g()) | g(); //bitwise or
  17.     int j = g() | (f() + g()); //bitwise or
  18. }

a) i value is 1 and j value is 1
b) i value is 0 and j value is 0
c) i value is 1 and j value is undefined
d) i and j value are undefined

 


Answer: c
Explanation: None.

 


49. Comment on the following C statement.

   n = 1;
   printf("%d, %dn", 3*n, n++);

a) Output will be 3, 2
b) Output will be 3, 1
c) Output will be 6, 1
d) Output is compiler dependent

 


Answer: d
Explanation: None.

 


50. How many times i value is checked in the following C program?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         while (i < 3)
  6.             i++;
  7.         printf("In while loop\n");
  8.     }

a) 2
b) 3
c) 4
d) 1

 

Answer: c
Explanation: None.


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

  1.      #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 0;
  5.         do
  6.         {
  7.             i++;
  8.             if (i == 2)
  9.                 continue;
  10.                 printf("In while loop ");
  11.         } while (i < 2);
  12.         printf("%d\n", i);
  13.     }

a) In while loop 2
b) In while loop in while loop 3
c) In while loop 3
d) Infinite loop

 


Answer: a
Explanation: None.

 

 

 


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

  1.     #include <stdio.h>
  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
Explanation: None.


53. 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
Explanation: None.

 


54. Which option should be selected to work the following C expression?

string p = "HELLO";

a) typedef char [] string;
b) typedef char *string;
c) typedef char [] string; and typedef char *string;
d) Such expression cannot be generated in C

 

 

Answer: b
Explanation: None.


55. What is the meaning of the following C statement?

 printf(“%10s”, state); 

a) 10 spaces before the string state is printed
b) Print empty spaces if the string state is less than 10 characters
c) Print the last 10 characters of the string
d) None of the mentioned

 


Answer: b
Explanation: None.

 


56. What are the elements present in the array of the following C code?

int array[5] = {5};

a) 5, 5, 5, 5, 5
b) 5, 0, 0, 0, 0
c) 5, (garbage), (garbage), (garbage), (garbage)
d) (garbage), (garbage), (garbage), (garbage), 5

 

Answer: b
Explanation: None.


57. What will be the output of the following C function when EOF returns?

   int fputs(char *line, FILE *fp)

a) ‘�’ character of array line is encountered
b) ‘n’ character in array line is encountered
c) ‘t’ character in array line is encountered
d) When an error occurs

 


Answer: d
Explanation: None.

 


58. Which part of the program address space is p stored in the following C code?

  1.     #include <stdio.h>
  2.     int *p;
  3.     int main()
  4.     {
  5.         int i = 0;
  6.         p = &i;
  7.         return 0;
  8.     }

a) Code/text segment
b) Data segment
c) Bss segment
d) Stack

 

Answer: c
Explanation: None.


59. Which of the following sequences are unaccepted in C language?
a)

   #if
   #else
   #endif

b)

   #if
   #elif
   #endif

c)

   #if
   #if
   #endif

d)

   #if
   #undef
   #endif


Answer: c
Explanation: None.
 

 

59. Comment on the output of following C code.

  1.     #include <stdio.h>
  2.     main()
  3.     {
  4.         char *p = 0;
  5.         *p = 'a';
  6.         printf("value in pointer p is %c\n", *p);
  7.     }

a) It will print a
b) It will print 0
c) Compile time error
d) Run time error

 


Answer:d
Output:
$ cc pgm.c
$ a.out
Segmentation fault (core dumped)

 


60. What is the output of this C code?

  1.    #include <stdio.h>
  2.    main()
  3.    {
  4.        if (sizeof(int) > -1)
  5.            printf("True");
  6.        else
  7.            printf("False");
  8.    }

a) True
b) False

 


Answer:b
Output:
$ cc pgm.c
$ a.out
False

 


61. What is the output of this C code?

  1.     #include <stdio.h>
  2.     main()
  3.     {
  4.         char *p = "Sanfoundry C-Test";
  5.         p[0] = 'a';
  6.         p[1] = 'b';
  7.         printf("%s", p);
  8.     }

a) abnfoundry C-Test
b) Sanfoundry C-Test
c) Compile time error
d) Run time error

 


Answer:d
Output:
$ cc pgm.c
$ a.out
Segmentation fault (core dumped)

 


62. What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         float f = 0.1;
  5.         if (f == 0.1)
  6.             printf("True");
  7.         else
  8.             printf("False");
  9.     }

a) True
b) False

 


Answer: b
Output:
$ cc pgm.c
$ a.out
False

 


63. What is the output of this C code?

  1.     #include <stdio.h>
  2.     main()
  3.     {
  4.         int n = 0, m = 0;
  5.         if (n > 0)
  6.             if (m > 0)
  7.                 printf("True");
  8.         else 
  9.             printf("False");
  10.     }

a) True
b) False
c) No Output will be printed
d) Run Time Error

 


Answer:c
Output:
$ cc pgm.c
$ a.out
$