Programming with C MCQs

Programming with C MCQs

Answer these 70+ Programming with C MCQs and see how sharp is your knowledge of Programming with C.
Scroll down and let's start!

1: 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()

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

A.   fputc()

B.   fputs()

C.   fprintf()

D.   fseek()

E.   ftell()  

3: What will be printed on the standard output as a result of the following code snippet?
void main()
{
        int num1 = 30, num2 = 4;
        float result;
        result = (float)(num1/num2);
        printf("%.2f", result);
        return 0;
}

A.   7

B.   7.00

C.   7.000000

D.   7.5

E.   7.50

4: What would be printed on the standard output as a result of the following code snippet?
char *str1 = "Hello World";
strcat(str1, "!");
printf("%s", str1);

A.   Hello World!

B.   Hello World

C.   Hello

D.   The code snippet will throw a compilation error

5: 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 

6: 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

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

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

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

E.   None of the above

7: What will be the output of following code?
int main()
   {
      int i;
      i = 0;
      for (i = 1; i <2; i++)
      {
          i++;
          printf( "%d", i );
          continue;
          printf( "%d", i );
      }
      return 0;
   }

A.   22

B.   2,2

C.   2

D.   none of the above 

8: 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

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

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

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

C.   char str[2][4]={
                  {'a','b','c','\0'},
 &nbs

D.   a and b

E.   a, b and c

10: What will be printed on the standard output as a result of the following code snippet?
int funr(int x, int y)
{
        if(x <= 0)
        {
                   return y;
        }
        else
        {
                return (1+funr(x-1,y));
        }
}

void main()
{
    printf("%d",funr(2,3));
}

A.   2

B.   3

C.   5

D.   6

E.   9

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

A.   w+

B.   a+

C.   r+

D.   r+a

E.   a+r

12: What will be the output of the following program?
#include
main()
{
int n = 5;
assert(n > 3); //statement 1
n = n+2;
assert(n > 7);//statement 2
return 0;
}

A.   Assertion 'n > 3' failed; Program aborts at statement 1

B.   Assertion 'n > 7' failed; Program aborts at statement 2

C.   Program returns 0 with the value of n as 7

D.   Compilation Error

13: 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 

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

A.   atoi()

B.   atof()

C.   atol()

D.   atan()

E.   acos()

15: Which of the following is not a valid mode for opening a file?

A.   r

B.   w

C.   a

D.   r+

E.   i 

16: What is the function to concatenate two strings?

A.   strcmp()

B.   strcpy()

C.   strcat()

D.   strlen()

E.   catstr()  

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

A.   Will result in Compilation Error

B.   0 1 2 3

C.   0 1 0 1

D.   0 1 0 2

E.   Undefined

18: The declaration int (*p[5])() means:

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 integers

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

E.   p is a pointer to a character string 

19: Which of the following statements is valid and correct?

A.   char amessage[] =

B.   char *pmessage =

C.   char amessage[] =

D.   char *pmessage = "abcde"; pmessage++;

20: What would be printed on the standard output as a result of the following code snippet? main() { int i=5; char option = 5; switch(option) { case "5": printf("case : 1 \n"); break; case i: printf("case : 2 \n"); break; default: printf("case : 3 \n"); break; } return 0; }

A.   case : 1

B.   case : 2

C.   case : 3

D.   Result in compilation error

E.   None of the above

21: What will be printed on the standard output as a result of the following code snippet?
void main()
{
        int arr[][2] = {1,2,3,4,5,6};
        printf("%d",arr[2][1]);
}

A.   2

B.   3

C.   4

D.   5

E.   6

22: Which of the following is not a storage type?

A.   auto

B.   global

C.   static

D.   register

E.   extern

23: What is wrong with the following function prototype statement?
int func();

A.   The function definition {...} is missing

B.   While calling a function, the type int is not needed

C.   No parameter has been passed

D.   The semicolon should not be there

E.   There is nothing wrong with the statement 

24: Which of the following declarations of structures is/are valid?
        1)
                struct node {
                int count;
                char *word;
                struct node next;
                }Node;
        2)
                struct node {
                int count;
                char *word;
                struct node *next;
                }Node;
        3)
                struct node {
                int count;
                char *word;
                union u1 {
                        int n1;
                        float f1;
                }U;
                }Node;

A.   1,2,3

B.   1,2

C.   2,3

D.   2

E.   None of the above 

25: Which of the following functions is used to extract formatted input from a file?

A.   fputc()

B.   fputs()

C.   fprintf()

D.   fscanf()

E.   ftell() 

26: What does the following function do?
int fn(unsigned int x)
{
        int count = 0;
        for(; x!=0; x&=(x-1))
                count++;
        return count;
}

A.   Returns the minimum number of bits required to represent the number x

B.   Returns the number of zero bits present in the number x

C.   Returns the number of 1 bits(bits having one) in the number x

D.   Returns the square root of the number

E.   None of the above 

27: What will be printed on the standard output as a result of the following code snippet?
void func()
{
    static int i = 1;
    int j = 1;
    i++;
    j++;
    printf("%d %d ",i,j);
}

void main()
{
    func();
    func();
    func();
}

A.   2 2 2 2 2 2

B.   2 2 3 2 4 2

C.   2 2 2 3 2 4

D.   2 2 3 3 4 4

E.   None of these

28: 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

29: Which of the following standard functions is used to close a file?

A.   fileclose()

B.   closefile()

C.   fclose()

D.   Any of the above

30: Which function allocates memory and initializes elements to 0?

A.   assign()

B.   calloc()

C.   malloc()

D.   swab()

E.   allocate()

31: 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

32: 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, c = 5;
printf("%d ", max(a++, c++));
printf("%d %d\n", a,c);
}

A.   Results in Compilation Error

B.   6 4 5

C.   6 5 7

D.   6 5 6

E.   None of the above

33: What would be printed on the standard output as a result of the following code snippet?
main()
{
    int a[5] = {1,4,5,6,9};
    printf("%d\t", *a);    //Line 1
    printf("%d", *++a);    //Line 2
return 0;
}

A.   1    4

B.   0    1

C.   Undefined value

D.   Compilation Error in "Line 1"

E.   Compilation Error in "Line 2"

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

A.   1,1

B.   1,4

C.   8,8

D.   8,9

E.   9,10

35: What will be printed on the standard output as a result of the following code snippet?
void main()
{
        int arr[5]={1,2,3,4,5};
        printf("%d\n", *(arr+4));
}

A.   1

B.   2

C.   4

D.   5

E.   Cannot be determined

36: Given the following array:
int a[8] = {1,2,3,4,5,6,7,0};
what would be the output of
        printf("%d",a[4]); ?

A.   3

B.   4

C.   5

D.   6

E.   7  

37: What will happen when the following code is executed?
{
int num;
num =0;
do {-- num;
printf("%d\n", num);
num++;
} while (num >=0);
}

A.   The loop will run infinitely

B.   The program will not enter the loop

C.   There will be a compilation error

D.   A runtime error will be reported

38: 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 

39: 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 

40: An array is defined with the following statement in a file, file1.c

        int a[ ] = { 1, 2, 3, 4, 5, 6 };

In another file, file2.c, the following code snippet is written to use the array a:
extern int a[];
int size = sizeof(a);
What is wrong with the above code snippet?

A.   The size of the operator cannot be applied to an array

B.   There is nothing wrong with the code snippet. The value of the size will be 6

C.   There is nothing wrong with the code snippet. The value of the size will be 7

D.   An extern array of unspecified size is an incomplete type. The size of the operator during compile time is unable to learn the size of an array that is defined in another file

E.   None of the above 

41: 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 

42: Which of the following is not a type of operator ?

A.   Rational

B.   Unary

C.   Ternary

D.   Compound assignment

E.   Logical

43: What would be printed on the standard output as a result of the following code snippet?
char i = "A";
char *j;
j = & i;
*j = *j + 32;
printf("%c",i);

A.   An error will occur

B.   a

C.   A

D.   b

E.   c 

44: Which standard function is used to deallocate memory allocated by the malloc() function?

A.   free

B.   calloc

C.   delete

D.   release

E.   destroy

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

A.   0

B.   1

C.   3

D.   4

E.   Cannot be determined 

46: 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

47: Which of the following comparison statements will be true if an integer is 16 bits and a long is 32 bits on a machine?

A.   -1L < 1U

B.   -1L > 1U

C.   -1L < 1UL

D.   -1L > 1UL

48: 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

49: Is the following statement correct? If not, why not? If yes, what is the size of the array?  
int array[][3] = { {1,2}, {2,3}, {3,4,2} }; 

A.   Yes, the size is three columns by two rows

B.   Yes, the size is two columns by two rows

C.   No, the first dimension is omitted

D.   No, one of the three initializer sets contains too many numbers

E.   Yes, the size is three columns by three rows

50: If a two dimensional array arr[4][10](an array with 4 rows and 10 columns) is to be passed in a function, which of the following would be the valid parameters in the function definition?

A.   fn(int arr[4][10])

B.   fn(int arr[][10])

C.   fn(int arr[4][])

D.   fn(int (*fn)[13])

E.   None of the above