Programming with C Test Answers



1. Which of the following statements will result in a compilation error?
Answers:
• int n=5, x; x=n++;
• int n=5, x; x= ++n++;
• int n=5, x; x= (n+1)++;
• int n=5, x=6; x= (n+x)++;
• None of the above

2. Read the following two declaration statements.

1. #include <stdio.h>
2. #include "stdio.h"

Which of the following statements pertaining to the above two statements are correct?
Answers:
• 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"
• For statement 1, the header file will be searched in the standard system directories such as "/usr/include"
• 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"
• For statement 2, the header file will be searched in the standard system directories such as "/usr/include"
• None of the above
3. Which of the following statements are correct for the keyword register?
Answers:
• It is a storage-class-specifier
• It guarantees that the variable is kept in the CPU register for maximum speed
• It requests that the variable be kept in the CPU register for maximum speed
• It does not guarantee that the variable value is kept in CPU register for maximum speed
4. Which is/are the type/s of memory allocation that needs/need the programmer to take care of memory management?
Answers:
• Static memory allocation
• Dynamic memory allocation
• Automatic memory allocation
• Memory allocation on stack
• Memory allocation on heap
5. Which of the following comparison statements will be true if an integer is 16 bits and a long is 32 bits on a machine?
Answers:
• -1L < 1U
• -1L > 1U
• -1L < 1UL
• -1L > 1UL
6. Which of the following is the correct way of initializing a two-dimensional array?
Answers:
• char str[2][4]={ "abc", "def" };
• char str[2][4]={ {"abc"}, {"def"} };
• char str[2][4]={ {'a','b','c','\0'}, {'d','e','f','\0'} };
• a and b
• a, b and c
7. 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?
Answers:
• 3
• 15
• 26
• 66
• 77
8. Which function will you use to write a formatted output to the file?
Answers:
• fputc()
• fputs()
• fprintf()
• fseek()
• ftell()
9. 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))?
Answers:
• 3
• 15
• 66
• 6
• 25
10. What would be printed on the standard output as a result of the following code snippet?

main()
{
        char option = 5;
        switch(option)
        {
                case '5':
                                printf("case : 1 \n");
                                break;
                case 5:
                                printf("case : 2 \n");
                                break;
                default:
                                printf("case : 3 \n");
                                break;
        }
        return 0;
}
Answers:
• case : 1
• case : 2
• case : 3
• Result in compilation error
• None of the above
11. 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;
}
Answers:
• case : 1
• case : 2
• case : 3
• Result in compilation error
• None of the above
12. What would be printed on the standard output as a result of the following code snippet?

#define func(t, a, b) { t temp; temp=a; a=b; b=temp; }
main()
{
        int a=3, b=4;
        float c=4.5, d = 5.99;
        func(int, a, b);
        func(float, c, d);
        printf("%d %d ", a, b);
        printf("%.2f %.2f\n", c, d);
}
Answers:
• Results in Compilation Error
• 3 4 5.99 4.50
• 3 4 4.50 5.99
• 4 3 5.99 4.50
• None of the above
13. 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);
}
Answers:
• Will result in Compilation Error
• 5
• 0
• Undefined value
14. Given the following array declaration:

       int a[2][3][4];
what would be the number of elements in array a?
Answers:
• 24
• 22
• 20
• 12
• 36
15. Which file header is to be included for file handling in a C program?
Answers:
• string.h
• file.h
• stdio.h
• stdlib.h
• ctype.h
16. What is the return type of the following function declaration?

func(char c);
Answers:
• void
• char
• int
• undefined
17. What is the return value in case a file is not opened successfully by using fopen()?
Answers:
• 0
• 1
• 100
• NULL
18. What is the function to concatenate two strings?
Answers:
• strcmp()
• strcpy()
• strcat()
• strlen()
• catstr()
19. Which of the following statements is valid and correct?
Answers:
• char amessage[] = "lmnop"; amessage++;
• char *pmessage = "abcde"; (*pmessage)++;
• char amessage[] = "lmnop"; (*amessage)++;
• char *pmessage = "abcde"; pmessage++;
20. 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;
   }
Answers:
• 22
• 2,2
• 2
• none of the above
21. Which of the following is not a valid mode for opening a file?
Answers:
• r
• w
• a
• r+
• i
22. 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("%c",books[2][5]);?
Answers:
• m
• M
• F
• i
• L
23. 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);
Answers:
• Hello World!
• Hello World
• Hello
• The code snippet will throw a compilation error
24. 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));
}
Answers:
• 0
• 1
• 3
• 4
• Cannot be determined
25. 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]); ?
Answers:
• 3
• 4
• 5
• 6
• 7
26. In order to read structures/records from a file, which function will you use?
Answers:
• fscanf()
• fread()
• fgets()
• fgetc()
• fseek
27. 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?
Answers:
• The size of the operator cannot be applied to an array
• There is nothing wrong with the code snippet. The value of the size will be 6
• There is nothing wrong with the code snippet. The value of the size will be 7
• 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
• None of the above
28. What would be printed on the standard output as a result of the following code snippet?

main()
{
        char *s="Hello World";
        char s1[20], s2[20];
        int len = sscanf(s,"%s",s1);
        printf("%s : %d", s1, len);
}
Answers:
• Compilation Error
• Hello World : 11
• Hello World : 1
• Hello : 5
• Hello : 1
29. 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?
Answers:
• ptr1 = open("a.dat");
• ptr1 = fileopen("a.dat");
• ptr1 = fopen("a.dat","r");
• ptr1 = open("a.dat","r");
• ptr1 = fileopen("a.dat","r");
30. 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]);?
Answers:
• Birds
• B
• Birds, Beasts and Relatives
• My Family and Other Animals
• M
31. 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);
}
Answers:
• 0 0
• 230 230
• 230 -26
• 230 -103
• None of the above
32. Which function will convert a string into a double precision quantity?
Answers:
• atoi()
• atof()
• atol()
• atan()
• acos()
33. Which of the following is not a type of operator ?
Answers:
• Rational
• Unary
• Ternary
• Compound assignment
• Logical
34. The declaration int *(*p)[10] indicates:
Answers:
• p is an array of pointers to functions the return type of which is an integer
• p is a pointer to a function that returns a pointer to an integer
• p is a pointer to an array of integer pointers
• p is a pointer to a character string
35. 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;
}
Answers:
• 7
• 7.00
• 7.000000
• 7.5
• 7.50
36. What is the output of the following program ?

main()
{

int u = 1, v = 3;
printf("%d %d",u,v);
funct1(&u,&v);
printf(" %d %d\n",u,v);
}

void funct1(int *pu, int *pv)
{
*pu=0;
*pv=0;
return;
}
Answers:
• 1 3 1 3
• 1 3 1 1
• 1 3 0 0
• 1 1 1 1
• 3 1 3 1
37. Which of the following is/are the correct signature/s of main with command line arguments?
Answers:
• int main(int argc, char **argv)
• int main(int argc, char *argv[])
• int main(int argc, char *argv)
• int main(int argc, char argv[])
• All of the above
38. From which of the following loop or conditional constructs, is "break" used for an early exit?
Answers:
• switch
• for
• while
• do-while
• All of the above
39. Given the operators:

1) *
2) /
3) %

What would be the order of precedence?
Answers:
• 1,2,3
• 1,3,2
• 3,2,1
• All have the same precedence
• 1 and 2 have the same precedence, 3 is of lower precedence
40. 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;
Answers:
• 1,2,3
• 1,2
• 2,3
• 2
• None of the above
41. 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] );
}
Answers:
• string1 = Manish string2 = Kumar string3 = Choudhary
• string1 = Manish string2 = Manish string3 = Manish
• string1 = ManishKumarChoudhary string2 = (null) string3 = (null)
• You will get an error message from the compiler
42. 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;
}
Answers:
• 6 7 8
• 5 7 8
• 6 8 8
• 5 8 8
• None of the above
43. In which area of memory are static variables allocated?
Answers:
• stack
• heap
• With the code binary
• None of the above
44. Which standard function is used to deallocate memory allocated by the malloc() function?
Answers:
• free
• calloc
• delete
• release
• destroy
45. Read the statement below:

extern int a;

Which of the following statement/s pertaining to the above statement is/are correct?
Answers:
• Declares an integer variable a; Allocates storage for the variable
• Declares an integer variable a; Does not allocate the storage for the variable
• Indicates that the variable is defined outside the current file
• Brings the scope of the variable defined outside the file to this file
• All of the above
• None of the above
46. The declaration int (*p[5])() means:
Answers:
• p is an array of pointers to functions the return type of which is an integer
• p is a pointer to a function that returns a pointer to an integer
• p is a pointer to an array of integers
• p is a pointer to an array of integer pointers
• p is a pointer to a character string
47. 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));
}
Answers:
• 1
• 2
• 4
• 5
• Cannot be determined
48. 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;
}
Answers:
• Will result in Compilation Error
• Undefined string
• sdfgh
• asdfgh
49. Which function returns the current pointer position within a file?
Answers:
• ftell()
• fseek()
• fgetc()
• fread()
• fscanf()
50. Which of the following is not a relational operator?
Answers:
• ==
• !=
• <>
• >=
• <=
51. What does the argv[0] represent?
Answers:
• The first command line parameter has been passed to the program
• The program name
• The number of command line arguments
• None of the above
52. 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));
}
Answers:
• 1,1
• 1,4
• 8,8
• 8,9
• 9,10
53. What will be the output of the following program?

#include <assert.h>
main()
{
int n = 5;
assert(n > 3); //statement 1
n = n+2;
assert(n > 7);//statement 2
return 0;
}
Answers:
• Assertion 'n > 3' failed; Program aborts at statement 1
• Assertion 'n > 7' failed; Program aborts at statement 2
• Program returns 0 with the value of n as 7
• Compilation Error
54. Which of the following is a function for formatting data in memory?
Answers:
• sprintf()
• printf()
• scanf()
• free()
• atol()
55. Which function allocates memory and initializes elements to 0?
Answers:
• assign()
• calloc()
• malloc()
• swab()
• allocate()
56. 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} };
Answers:
• Yes, the size is three columns by two rows
• Yes, the size is two columns by two rows
• No, the first dimension is omitted
• No, one of the three initializer sets contains too many numbers
• Yes, the size is three columns by three rows
57. Which of the following sets of conversion statements may result in the loss of data?
Answers:
• int i; char c; i=c; c=i;
• int i; char c; c=i; i=c;
• int i; float f; i=f; f=i;
• None of the above
58. Consider the following code.

int i = 4, *j, *k;

Which one of the following statements will result in Compilation error?
Answers:
• j = &i;
• j = j + 4;
• j = j - 2;
• k = j + 3;
• j = j * 2;
59. 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;
}
Answers:
• Will result in Compilation Error
• 0 1 2 3
• 0 1 0 1
• 0 1 0 2
• Undefined
60. By which file function you can position the file pointer in accordance with the current position?
Answers:
• ftell()
• fseek()
• fgetc()
• fread()
• fscanf()
61. What is wrong with the following function prototype statement?

int func();
Answers:
• The function definition {...} is missing
• While calling a function, the type int is not needed
• No parameter has been passed
• The semicolon should not be there
• There is nothing wrong with the statement
62. 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));
}
Answers:
• 0
• 1
• 3
• 4
• Cannot be determined
63. 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]);
}
Answers:
• 2
• 3
• 4
• 5
• 6
64. What would be printed on the standard output as a result of the following code snippet?

#include<stdio.h>
main()
{
        unsigned char a=255;
        a = a+1;
        printf("%d",a);
        return 0;
}
Answers:
• Undefined value
• 256
• 1
• 0
• -1
65. Which of the following is not a storage type?
Answers:
• auto
• global
• static
• register
• extern
66. 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?
Answers:
• fn(int arr[4][10])
• fn(int arr[][10])
• fn(int arr[4][])
• fn(int (*fn)[13])
• None of the above
67. Which function will you use to position the file pointer at the beginning of the file?
Answers:
• rewind()
• fseek()
• fscanf()
• a or b
• b or c
68. Which of the following is not a string function?
Answers:
• strlen()
• strcmp()
• strcpy()
• strcomp()
69. 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);
Answers:
• An error will occur
• a
• A
• b
• c
70. 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));
}
Answers:
• 1,1
• 8,4
• 8,8
• 8,9
• 9,8
71. 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));
}
Answers:
• 2
• 3
• 5
• 6
• 9
72. Which header file are methods(or macros) isalpha(), islower() a part of?
Answers:
• stdio.h
• ctype.h
• string.h
• math.h
• None of the above
73. What would be printed on the standard output as a result of the following code snippet?

#define Name Manish
main()
{
printf("My name""Name");
}
Answers:
• My name Manish
• My nameName
• Results in Compilation Error
• None of the above
74. 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);
}
Answers:
• 5 31 8
• 5 31 7
• 5 31 6
• 4 30 7
75. What will be printed on the standard output as a result of the following code snippet?

main()
{
        int num = 425;
        printf("%d", printf("%d", num));
}
Answers:
• Will result in Compilation Error
• 4425
• 4253
• 3435
• None of the above
76. What would be printed on the standard output as a result of the following code snippet?

main()

{

        signed char i = 1;

        for (; i<=255; i++)

        printf ("%d ",i);

        return 0;

}
Answers:
• Compilation Error
• 1 2 3 ... 255
• 1 2 3 . . . 127
• 1 2 3 . . . 127 -128 -127 ... 0 1 2 3 . . . (infinite times)
77. 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));
}
Answers:
• Results in Compilation Error
• Undefined value
• 4.50
• 4.0
• None of the above
78. Which of the following is not a file related function?
Answers:
• fgetc()
• puts()
• fputc()
• fscanf()
• fprintf()
79. 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;
}
Answers:
• 0 1 6 2
• 0 1 6 7
• Will result in Compilation Error
• None of the above
80. 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;
}
Answers:
• 1 4
• 0 1
• Undefined value
• Compilation Error in "Line 1"
• Compilation Error in "Line 2"
81. 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);
}
Answers:
• Results in Compilation Error
• 6 4 5
• 6 5 7
• 6 5 6
• None of the above
82. Which of the following file modes would mean read + append?
Answers:
• w+
• a+
• r+
• r+a
• a+r
83. Which of the following standard functions is used to close a file?
Answers:
• fileclose()
• closefile()
• fclose()
• Any of the above
84. 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;
}
Answers:
• 10
• 9
• 11
• 8
• None of the above
85. Identify the incorrect statement.
Answers:
• Records can be defined in C by using structures
• Structure members can be of the same/different data types
• Memory is reserved when a structure label is defined
• A pointer to a structure can be used to pass a structure to a function
• Arrays of structures can be defined and initialized
86. What will happen when the following code is executed?

{
int num;
num =0;
do {-- num;
printf("%d\n", num);
num++;
} while (num >=0);
}
Answers:
• The loop will run infinitely
• The program will not enter the loop
• There will be a compilation error
• A runtime error will be reported
87. Which of the following functions is used to extract formatted input from a file?
Answers:
• fputc()
• fputs()
• fprintf()
• fscanf()
• ftell()
88. What does the following function do?

int fn(unsigned int x)
{
        int count = 0;
        for(; x!=0; x&=(x-1))
                count++;
        return count;
}
Answers:
• Returns the minimum number of bits required to represent the number x
• Returns the number of zero bits present in the number x
• Returns the number of 1 bits(bits having one) in the number x
• Returns the square root of the number
• None of the above