C Programming MCQs

C Programming MCQs

These C Programming multiple-choice questions and their answers will help you strengthen your grip on the subject of C Programming. You can prepare for an upcoming exam or job interview with these C Programming MCQs.
So scroll down and start answering.

1: What is the only function all C programs must contain?

A.   main()

B.   program()

C.   start()

2: char* buf[100]; strcpy(buf, argv[1]); Which security risk is this code vulnerable to?

A.   Format string

B.   Stack overflow

C.   Integer overflow

D.   Race condition

E.   Heap overflow

3: Which one is NOT a reserved keyword?

A.   switch

B.   static

C.   extern

D.   intern

E.   struct

4: A C variable can start with a digit as well a letter.

A.   True

B.   False

5: Function Overloading is not supported in C.

A.   True

B.   False

6: How can you make an infinite loop in C?

A.   for(;;) { }

B.   while(1) { }

C.   loop: ... goto loop;

D.   All answers are right.

7: #ifdef __APPLE__ # include <dir/x.h> #else # include <other_dir/x.h> #endif What does it mean?

A.   It will include dir/x.h if __APPLE__ is defined, or other_dir/x.h, otherwise.

B.   It will define __APPLE__, include dir/x.h and next time will include other_dir/x.h

C.   It will define __APPLE__ and include dir/x.h

D.   It will include dir/x.h if __APPLE__ is not defined, or other_dir/x.h, otherwise.

8: What is i after the following block of code is executed : int i; i = 10/5/2/1;

A.   0

B.   1

C.   5

D.   4

9: What will be the output of: #include void main() { char a[6] = "Hello"; printf("%d", sizeof(a)); }

A.   Program will not execute.

B.   Array not initialized correctly

C.   6

D.   Compile time error

10: What is the value of the variable x? int x; x = 32 / 64;

A.   0.5

B.   0

C.   Undefined

11: What does "int *p = malloc(2);" do?

A.   It will crash your program (an int is four bytes long, not two).

B.   Nothing, it will yield a type mismatch compiler error.

C.   It will make p point to an uninitialized two-byte piece of memory allocated from the heap.

D.   It will make p point to the number 2.

12: If we pass an array as an argument of a function, what exactly get passed?

A.   a[0]th value of array

B.   a[last]th value of array

C.   All elements of an array

D.   Address of array

13: In C, a block is defined by...

A.   tabulations

B.   tags

C.   angle brackets

D.   curly braces

E.   indentation

14: int tab[3] = {0,1,2}; int i = 0; tab[++i] == ?

A.   0

B.   2

C.   1

15: In C language, && is a

A.   None of them

B.   Logical operator

C.   Arithmetic Operator

D.   Relational Operator

16: int i = 17 / 3; what is the value of i ?

A.   5

B.   6

C.   6.0

D.   5.60

E.   5.666666

17: Is C Object Oriented?

A.   No

B.   Yes

18: Which of the following special symbols are allowed in a variable name?

A.   - (hyphen)

B.   * (asterisk)

C.   _ (underscore)

D.   | (pipeline)

19: int *a, b; What is b ?

A.   An int *

B.   An int

C.   It does not compile

20: Which of the following is the correct operator to compare two integer variables?

A.   =

B.   equal

C.   ==

D.   :=

21: which of these is not valid keyword?

A.   char

B.   float

C.   var

D.   double

E.   int

22: Which of the following is not a predefined variable type?

A.   float

B.   real

C.   int

23: The end of a C statement is indicated by this character.

A.   .

B.   +

C.   :

D.   ;

24: The system function longjmp() can be used to return execution control to any user-specified point in the active function call tree.

A.   False

B.   True

25: What will be the output of this Program? #include struct Data{ char a; char *data; int value; }; main() { printf("%d\n",sizeof(struct Data)); }

A.   6

B.   3

C.   9

D.   It depends on the compiler and the hardware architecture.

E.   12

26: What is the value of p in int a,b,*p; p=&a; b=**p; printf("%d",p);

A.   value of variable b

B.   address of variable a

C.   value of variable a

D.   address of variable b

27: Will this loop terminate? int x=10; while( x-- > 0 );

A.   It will cause segfault

B.   no

C.   yes

D.   It will not compile

28: Which statement is true about double?

A.   its size depends on the implementation

B.   its size is 128 bits

C.   it's an alias of float

D.   it uses the GPU

29: How can you access the first element of an array called 'arr'?

A.   arr[0]

B.   (both of these)

C.   *arr

30: What are the different types of floating-point data in C ?

A.   float, double, long double

B.   double, long int, float

C.   short int, double, long int

D.   float, double

31: With: sizeof(char *) == 4 sizeof(char) == 1 What will sizeof(plop) for char plop[2][3] be?

A.   6

B.   14

C.   18

D.   10

32: foo[4] is equivalent of :

A.   *(&foo + 4)

B.   (*foo + 4)

C.   There is no equivalent using those notations

D.   &(foo + 4)

E.   *(foo + 4)

33: What is the output of the following code? char * str1 = "abcd"; char * str2 = "xyz"; if( str1 < str2 ) printf( "1" ); else printf( "2" );

A.   Undefined

B.   2

C.   1

34: What will the following code print? void *p = malloc(0); printf ("%d\n", p);

A.   Nothing, it will give a runtime error.

B.   Nothing, it won't compile.

C.   Unknown, it depends on what malloc will return.

D.   0

35: What is the value of 1 & 2?

A.   2

B.   0

C.   1

D.   3

36: stdarg.h defines?

A.   actual arguments

B.   formal arguments

C.   array definitions

D.   arguments with data types

E.   macros used with variable argument functions

37: What will be the output of the following? (int)b * (float)a / (double)d * (long)c

A.   double

B.   long

C.   float

D.   int

38: To send an array as a parameter to function, which one is a right way:

A.   doThis(*array)

B.   doThis(array[size])

C.   doThis(&array)

D.   doThis(array)

39: What is the output of printf("%d\n", sizeof(long) / sizeof(int))?

A.   2

B.   Depends on the implementation, but always some number > 1.

C.   1

D.   4

E.   Depends on the implementation, but always some number >= 1.

40: The main() function can be called recursively.

A.   True

B.   False

41: Which one is not a bitwise operator ?

A.   ~

B.   ^

C.   <<

D.   !

E.   |

42: What will this code print out? #include void function(char *name) { name=NULL; } main() { char *name="ELANCE"; function(name); printf("%s",name); }

A.   ELANCE

B.   NULL

C.   Sengmentation Fault

D.   It Won't Compile

43: What does malloc(0) return?

A.   NULL

B.   The program segfault

C.   The behavior is implementation-defined

D.   A unique pointer

44: The operator used to get value at address stored in a pointer "p" is :

A.   *p

B.   &p

C.   &(p)

D.   **(p)

E.   *(*p)

45: #include int main(){ int a=0; #if (a==0) printf("Equal"); #else if printf("Not equal"); #endif return 0; } Output of above program is:

A.   Garbage

B.   Null

C.   Not equal

D.   Equal

E.   Compilation error

46: For a pointer to a 3 dimensional array (*foo)[2][3][4], how many calls to malloc do you need at least to allocate its contents?

A.   24

B.   2

C.   4

D.   1

E.   3

47: What is the output of the Program : int main() { int i,j; i=1,2,3; j=(1,2,3); printf("%d %d",i,j); return 0; }

A.   0 0

B.   1 1

C.   1 3

D.   Garbage Values of i & j

E.   Compile Time Error

48: memmove() is safer than memcpy() when it comes to the location of its arguments.

A.   False

B.   True

49: What will be output if you will execute following code? #include int main(){ int x=25; if(!!x) printf("%d",!x); else printf("%d",x); return 0; }

A.   0

B.   1

C.   25

D.   -1

E.   2

50: Which function is a System Call?

A.   fopen

B.   close

C.   printf

51: What will be output if you will execute following code? #include int main(){ float a=0.5, b=0.9; if(a&&b>0.9) printf("Sachin"); else printf("Rahul"); return 0; }

A.   Compiler error

B.   Sachin

C.   Rahul

D.   null

E.   Runtime error

52: What will be the output of the following program: #include int main(){ int a,b; a= -3 - - 25; b= -5 - (- 29); printf("a= %d b=%d", a, b); return 0; }

A.   a=22 b=34

B.   a=22 b=24

C.   a=28 b=34

D.   a=28 b=24

53: In C ....

A.   Strings are surrounded with double quotes, and Character with single-quotes.

B.   Strings and chars can be surrounded with double quotes or single-quotes.

C.   Strings does not exists in C.

54: C99 standard guarantees uniqueness of ____ characters for internal names.

A.   63

B.   14

C.   12

D.   31

55:

Which of the following is the correct way of initializing a two-dimensional array?

A.   char str[2][4]={ "abc", "def" };

B.   char str[2][4]={ {"abc"}, {"def"} };

C.   char str[2][4]={ {'a','b','c','\0'}, {'d','e','f','\0'} };

D.   a and b

E.   a, b and c

56:

Which of the following statements are correct for the keyword register?

A.   It is a storage-class-specifier

B.   It guarantees that the variable is kept in the CPU register for maximum speed

C.   It requests that the variable be kept in the CPU register for maximum speed

D.   It does not guarantee that the variable value is kept in CPU register for maximum speed

57:

What would be printed on the standard output as a result of the
following code snippet?
main( )
{
char *str[ ] = {
"Manish"
"Kumar"
"Choudhary"
};

printf ( "\nstring1 = %s", str[0] );
printf ( "\nstring2 = %s", str[1] );
printf ( "\nstring3 = %s", str[2] );
}

A.   string1 = Manish string2 = Kumar string3 = Choudhary

B.   string1 = Manish string2 = Manish string3 = Manish

C.   string1 = ManishKumarChoudhary string2 = (null) string3 = (null)

D.   You will get an error message from the compiler

58:

What would be printed on the standard output as a result of the
following code snippet?
int Recur(int num)
{
if(num==1 || num==0)


return 1;
if(num%2==0)
return Recur(num/2)+2;
else
return Recur(num-1)+3;
}
int main()
{
int a=9;
printf("%d\n", Recur(a));
return 0;
}

A.   10

B.   9

C.   11

D.   8

E.   None of the above

59:

Read the following two declaration statements.
1. #include
 
2. #include "stdio.h"
Which of the following statements pertaining to the above two
statements are correct?

A.   For statement 1, the header file will be searched first in the local directory and then in the standard system directories such as "/usr/include"

B.   For statement 1, the header file will be searched in the standard system directories such as "/usr/include"

C.   For statement 2, the header file will be searched first in the local directory and then in the standard system directories such as "/usr/include"

D.   For statement 2, the header file will be searched in the standard system directories such as "/usr/include"

E.   None of the above

60:

Study the following code where num is an integer array and n is the
length of the array:
for(i=0;i<n-1;i++)
{
 
for(j=i+1;j<n;j++)
{
if(num[i] > num[j])
{
var=num[i];
num[i]=num[j];
num[j]=var;
}
}
}
What does the above code do?

A.   It prints the elements of the array in the ascending order

B.   It calculates the sum of the elements of the array

C.   It sorts the array in the ascending order

D.   It sorts the array in the descending order

E.   It calculates the average of the elements of the array

61:

Given the following array declaration:
int a[2][3][4]
 
what would be the number of elements in array a?

A.   24

B.   22

C.   20

D.   12

E.   36

62:

Which of the following file modes would mean read + append?

A.   w+

B.   a+

C.   r+

D.   r+a

E.   a+r

63:

Which header file are methods(or macros) isalpha(), islower() a part
of?

A.   stdio.h

B.   ctype.h

C.   string.h

D.   math.h

E.   None of the above

64:

Suppose there is a file a.dat which has to be opened in the read
mode using the FILE pointer ptr1, what will be the correct syntax?

A.   ptr1 = open("a.dat");

B.   ptr1 = fileopen("a.dat");

C.   ptr1 = fopen("a.dat","r");

D.   ptr1 = open("a.dat","r");

E.   ptr1 = fileopen("a.dat","r");

65:

Which of the following is not a storage type?

A.   auto

B.   global

C.   static

D.   register

E.   extern

66:

Which of the following sets of conversion statements may result in the
loss of data?

A.   int i; char c; i=c; c=i;

B.   int i; char c; c=i; i=c;

C.   int i; float f; i=f; f=i;

D.   None of the above

67:

Which function allocates memory and initializes elements to 0?

A.   assign()

B.   calloc()

C.   malloc()

D.   swab()

E.   allocate()

68:

Which function will you use to position the file pointer at the beginning
of the file?

A.   rewind()

B.   fseek()

C.   fscanf()

D.   a or b

E.   b or c

69:

Which function will convert a string into an integer?

A.   int()

B.   number()

C.   atoi()

D.   val()

E.   tonum()

70:

What will be printed on the standard output as a result of the following
code snippet?
void main()
{
int i,j,k;
i=4;

j=30;
k=0;
k=j++/i++;
++k;
printf("%d %d %d",i,j,k);
}

A.   5 31 8

B.   5 31 7

C.   5 31 6

D.   4 30 7

71:

What does the argv[0] represent?

A.   The first command line parameter has been passed to the program

B.   The program name

C.   The number of command line arguments

D.   None of the above

72:

Which of the following is a function for formatting data in memory?

A.   sprintf()

B.   printf()

C.   scanf()

D.   free()

E.   atol()

73:

What would be printed on the standard output as a result of the
following code snippet?
main()
{
int n=5, x;
x = n++;
printf("%d ", x);
x = ++n;
printf("%d ", x++);
printf("%d", x);
 
return 0;
}

A.   6 7 8

B.   5 7 8

C.   6 8 8

D.   5 8 8

E.   None of the above

74:

What will be the output of the following program, assuming that data
type short takes 2 bytes for storage?
struct node
{
unsigned char bit1 : 1;
unsigned char bit2 : 1;
unsigned short bit3 : 7;
} node1;
main()
{
int size = sizeof(node1);
 
printf("%d", size);
}

A.   4

B.   3

C.   2

D.   None of the above

75:

What would be printed on the standard output as a result of the
following code snippet?
main()
{
enum {red, green, blue = 6, white};
printf("%d %d %d %d", red, green, blue, white);
return 0;
}

A.   0 1 6 2

B.   0 1 6 7

C.   Will result in Compilation Error

D.   None of the above

76:

Read the statement below:
extern int a;
 
Which of the following statement/s pertaining to the above statement
is/are correct?

A.   Declares an integer variable a; Allocates storage for the variable

B.   Declares an integer variable a; Does not allocate the storage for the variable

C.   Indicates that the variable is defined outside the current file

D.   Brings the scope of the variable defined outside the file to this file

E.   All of the above

F.   None of the above

77:

What is the return type of the following function declaration?
func(char c);

A.   void

B.   char

C.   int

D.   undefined

78:

The declaration int *(*p)[10] indicates:

A.   p is an array of pointers to functions the return type of which is an integer

B.   p is a pointer to a function that returns a pointer to an integer

C.   p is a pointer to an array of integer pointers

D.   p is a pointer to a character string

79:

What would be printed on the standard output as a result of the
following code snippet?
main()
{
void addup (int b);
addup(b);
return 0;
}
int b = 5;
 
void addup (int b)
{
static int v1;
v1 = v1+b;
printf("%d ", v1);
}

A.   Will result in Compilation Error

B.   5

C.   0

D.   Undefined value

80:

What would be printed on the standard output as a result of the
following code snippet?
main()
{
char *pmessage = "asdfgh";
*pmessage++;
printf("%s", pmessage);
return 0;
}

A.   Will result in Compilation Error

B.   Undefined string

C.   sdfgh

D.   asdfgh

81:

Select the correct statement about arrays.

A.   Automatic arrays cannot be initialized

B.   An array declared as A[100][100] can hold a maximum of 10000 elements

C.   An array can hold elements of different data types

82:

What will be printed on the standard output as a result of the following
code snippet?
void main()
{
char arr[] = {'R','A','M','\0'};
 
printf("%d",strlen(arr));
}

A.   0

B.   1

C.   3

D.   4

E.   Cannot be determined

83:

What happens when the continue keyword is encountered in the 'for
loop'?

A.   Control passes to the initialization of the loop

B.   Control passes to the condition of the loop

C.   Control passes to the beginning of the loop

D.   Control passes to the first statement of the loop

E.   Control passes to the statement preceding the end of the loop

84:

Given the following array:
char books[][40]={
"The Little World of Don Camillo",
"To Kill a Mockingbird",
"My Family and Other Animals",
"Birds, Beasts and Relatives"
};
what would be the output of printf("%s",books[3]);?

A.   Birds

B.   B

C.   Birds, Beasts and Relatives

D.   My Family and Other Animals

E.   M

85:

What will happen when the following code is executed?
void main()
{
char arr1[] = "REGALINT";
char *arr2;
 
arr2 = arr1;
printf("%d,",sizeof(arr1));
printf("%d",sizeof(arr2));
}

A.   1,1

B.   1,4

C.   8,8

D.   8,9

E.   9,4

86:

What will be printed on the standard output as a result of the following
code snippet?
void main()
{
char arr1[] = "REGALINT";
printf("%d,",strlen(arr1));
printf("%d",sizeof(arr1));
}

A.   1,1

B.   8,4

C.   8,8

D.   8,9

E.   9,8

87:

Which function will convert a string into a double precision quantity?

A.   atoi()

B.   atof()

C.   atol()

D.   atan()

E.   acos()

88:

By which file function you can position the file pointer in accordance
with the current position?

A.   ftell()

B.   fseek()

C.   fgetc()

D.   fread()

E.   fscanf()

89:

What would be printed on the standard output as a result of the
following code snippet?
#define Name Manish
main()
{
printf("My name""Name");
}

A.   My name Manish

B.   My nameName

C.   Results in Compilation Error

D.   None of the above

90:

Which of the following is/are the correct signature/s of main with
command line arguments?

A.   int main(int argc, char **argv)

B.   int main(int argc, char *argv[])

C.   int main(int argc, char *argv)

D.   int main(int argc, char argv[])

E.   All of the above

91:

Which of the following statements will result in a compilation error?

A.   int n=5, x; x=n++;

B.   int n=5, x; x= ++n++;

C.   int n=5, x; x= (n+1)++;

D.   int n=5, x=6; x= (n+x)++;

E.   None of the above

92:

Which is/are the type/s of memory allocation that needs/need the
programmer to take care of memory management?

A.   Static memory allocation

B.   Dynamic memory allocation

C.   Automatic memory allocation

D.   Memory allocation on stack

E.   Memory allocation on heap

93:

What is the function to concatenate two strings?

A.   strcmp()

B.   strcpy()

C.   strcat()

D.   strlen()

E.   catstr()

94:

What would be printed on the standard output as a result of the
following code snippet?
#define max(a, b) ((a) > (b)?(a):(b))
main()
 
{
int a=4;
float b=4.5;
printf("%.2f\n",max(a, b));
}

A.   Results in Compilation Error

B.   Undefined value

C.   4.50

D.   4.0

E.   None of the above

95:

Given the operators:
1) *
2) /
3) %
What would be the order of precedence?

A.   1,2,3

B.   1,3,2

C.   3,2,1

D.   All have the same precedence

E.   1 and 2 have the same precedence, 3 is of lower precedence

96:

What will happen if you assign a value to an element of an array the
subscript of which exceeds the size of the array?

A.   The element will be set to 0

B.   Nothing; it is commonly done

C.   It is undefined behavior

D.   You will get an error message from the compiler

97:

What would be printed on the standard output as a result of the
following code snippet?
void main()
{
unsigned char a=25;
 
a = ~a;
signed char b = 25;
b = ~b;
printf("%d %d ", a, b);
}

A.   0 0

B.   230 230

C.   230 -26

D.   230 -103

E.   None of the above

A.   fgetc()

B.   puts()

C.   fputc()

D.   fscanf()

E.   fprintf()

99:

Which function will you use to write a formatted output to the file?

A.   fputc()

B.   fputs()

C.   fprintf()

D.   fseek()

E.   ftell()

100:

Given the array:
int num[3][4]= {
{3,6,9,12},
{15,25,30,35},
{66,77,88,99}
};
what would be the output of *(*(num+1)+1)+1?

A.   3

B.   15

C.   26

D.   66

E.   77