Solved MCQs on C Programming



DATA TYPES

1. Comment on the output of this C code?
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int a[5] = {1, 2, 3, 4, 5};
  5.         int i;
  6.         for (i = 0; i < 5; i++)
  7.             if ((char)a[i] == '5')
  8.                 printf("%d\n", a[i]);
  9.             else
  10.                 printf("FAIL\n");
  11.     }
a) The compiler will flag an error
b) Program will compile and print the output 5
c) Program will compile and print the ASCII value of 5
d) Program will compile and print FAIL for 5 times
View Answer
Answer:d
Explanation:The ASCII value of 5 is 53, the char type-casted integral value 5 is 5 only.
Output:
$ cc pgm1.c
$ a.out
FAILED
FAILED
FAILED
FAILED
FAILED
2. The format identifier ‘%i’ is also used for _____ data type?
a) char
b) int
c) float
d) double
View Answer
Answer:b
Explanation:Both %d and %i can be used as a format identifier for int data type.
3. Which data type is most suitable for storing a number 65000 in a 32-bit system?
a) signed short
b) unsigned short
c) long
d) int
View Answer
Answer:b
Explanation:65000 comes in the range of short (16-bit) which occupies the least memory. Signed short ranges from -32768 to 32767 and hence we should use unsigned short.
4. Which of the following is a User-defined data type?
a) typedef int Boolean;
b) typedef enum {Mon, Tue, Wed, Thu, Fri} Workdays;
c) struct {char name[10], int age};
d) all of the mentioned
View Answer
Answer:d
Explanation:typedef and struct are used to define user-defined data types.
5. What is the size of an int data type?
a) 4 Bytes
b) 8 Bytes
c) Depends on the system/compiler
d) Cannot be determined
View Answer
Answer:c
Explanation:The size of the data types depend on the system.
6. What is the output of this 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
View Answer
Answer:b
Explanation:signed char will be a negative number.
Output:
$ cc pgm2.c
$ a.out
-128
7. Comment on the output of this C code?
  1.     #include  <stdio.h>
  2.     int main()
  3.     {
  4.         char c;
  5.         int i = 0;
  6.         FILE *file;
  7.         file = fopen("test.txt", "w+");
  8.         fprintf(file, "%c", 'a');
  9.         fprintf(file, "%c", -1);
  10.         fprintf(file, "%c", 'b');
  11.         fclose(file);
  12.         file = fopen("test.txt", "r");
  13.         while ((c = fgetc(file)) !=  -1)
  14.             printf("%c", c);
  15.         return 0;
  16.     }
a) a
b) Infinite loop
c) Depends on what fgetc returns
d) Depends on the compiler
View Answer
Answer:a
Explanation:None.
Output:
$ cc pgm3.c
$ a.out
a
8. What is short int in C programming?
a) Basic datatype of C
b) Qualifier
c) short is the qualifier and int is the basic datatype
d) All of the mentioned
View Answer
Answer:c
Explanation:None.
SET 2
1. Comment on the output of this C code?
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         float f1 = 0.1;
  5.         if (f1 == 0.1)
  6.             printf("equal\n");
  7.         else
  8.             printf("not equal\n");
  9.     }
a) equal
b) not equal
c) Output depends on compiler
d) None of the mentioned
View Answer
Answer:b
Explanation:0.1 by default is of type double which has different representation than float resulting in inequality even after conversion.
Output:
$ cc pgm4.c
$ a.out
not equal
2. Comment on the output of this C code?
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         float f1 = 0.1;
  5.         if (f1 == 0.1f)
  6.             printf("equal\n");
  7.         else
  8.             printf("not equal\n");
  9.     }
a) equal
b) not equal
c) Output depends on compiler
d) None of the mentioned
View Answer
Answer:a
Explanation:0.1f results in 0.1 to be stored in floating point representations.
Output:
$ cc pgm5.c
$ a.out
equal
3. What is the output of this C code (on a 32-bit machine)?
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int x = 10000;
  5.         double y = 56;
  6.         int *p = &x;
  7.         double *q = &y;
  8.         printf("p and q are %d and %d", sizeof(p), sizeof(q));
  9.         return 0;
  10.     }
a) p and q are 4 and 4
b) p and q are 4 and 8
c) Compiler error
d) p and q are 2 and 8
View Answer
Answer:a
Explanation:Size of any type of pointer is 4 on a 32-bit machine.
Output:
$ cc pgm6.c
$ a.out
p and q are 4 and 4
4. Which is correct with respect to size of the datatypes?
a) char > int > float
b) int > char > float
c) char < int < double
d) double > char > int
View Answer
Answer:c
Explanation:char has lesser bytes than int and int has lesser bytes than double in any system
5. What is 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
View Answer
Answer:d
Explanation:Since the size of a union is the size of its maximum datatype, here int is the largest hence 4.
Output:
$ cc pgm7.c
$ a.out
4
6. What is the output of this C code?
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         float x = 'a';
  5.         printf("%f", x);
  6.         return 0;
  7.     }
a) a
b) run time error
c) a.0000000
d) 97.000000
View Answer
Answer:d
Explanation:Since the ASCII value of a is 97, the same is assigned to the float variable and printed.
Output:
$ cc pgm8.c
$ a.out
97.000000
7. Which of the datatypes have size that is variable?
a) int
b) struct
c) float
d) double
View Answer
Answer:b
Explanation:Since the size of the structure depends on its fields, it has a variable size.


VARIABLES

1. C99 standard guarantees uniqueness of ____ characters for internal names.
a) 31
b) 63
c) 12
d) 14

Answer:b
Explanation:ISO C99 compiler may consider only first 63 characters for internal.

2. C99 standard guarantess uniqueness of _____ characters for external names.
a) 31
b) 6
c) 12
d) 14

Answer:a
Explanation:ISO C99 compiler may consider only first 31 characters for external
variables having 31 characters due to which it may not be unique.

3. Which of the following is not a valid variable name declaration?
a) int __a3;
b) int __3a;
c) int __A3;
d) None of the mentioned

Answer:d
Explanation:None.

4. Which of the following is not a valid variable name declaration?
a) int _a3;
b) int a_3;
c) int 3_a;
d) int _3a

Answer:c
Explanation:Variable name cannot start with a digit.

5. Variable names beginning with underscore is not encouraged. Why?
a) It is not standardized
b) To avoid conflicts since assemblers and loaders use such names
c) To avoid conflicts since library routines use such names
d) To avoid conflicts with environment variables of an operating system

Answer:c
Explanation:None.

6. All keywords in C are in
a) LowerCase letters
b) UpperCase letters
c) CamelCase letters
d) None

Answer:a
Explanation:None.

7. Variable name resolving (number of significant characters for uniqueness of variable) depends on
a) Compiler and linker implementations
b) Assemblers and loaders implementations
c) C language
d) None

Answer:a
Explanation:It depends on the standard to which compiler and linkers are adhering to.

8. 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:d
Explanation:Since only underscore and no other special character is allowed in a variable name, it results in an error.

9. 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.

SET 2

1. 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.

2. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        printf("Hello World! %d \n", x);
        return 0;
    }
a) Hello World! x;
b) Hello World! followed by a junk value
c) Compile time error
d) Hello World!


Answer:c
Explanation:It results in an error since x is used without declaring the variable x.
Output:
$ cc pgm1.c
pgm1.c: In function ‘main’:
pgm1.c:4: error: ‘x’ undeclared (first use in this function)
pgm1.c:4: error: (Each undeclared identifier is reported only once
pgm1.c:4: error: for each function it appears in.)

3. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int y = 10000;
        int y = 34;
        printf("Hello World! %d\n", y);
        return 0;
    }
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

4. Which of the following is not a valid variable name declaration?
a) float PI = 3.14;
b) double PI = 3.14;
c) int PI = 3.14;
d) #define PI 3.14

Answer:d
Explanation:#define PI 3.14 is a macro preprocessor, it is a textual substitution.

5. What will happen if the below program is executed?

    #include <stdio.h>
    int main()
    {
        int main = 3;
        printf("%d", main);
        return 0;
    }
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
6. What is the problem in following variable declaration?
float 3Bedroom-Hall-Kitchen?;
a) The variable name begins with an integer
b) The special character ‘-‘
c) The special character ‘?’
d) All of the mentioned


Answer:d
Explanation:A variable name cannot start with an integer, along with that the C compiler
interprets the ‘-‘ and ‘?’ as a minus operator and a question mark operator respectively.

7. Comment on the output of this C code?

    #include <stdio.h>
    int main()
    {
        int ThisIsVariableName = 12;
        int ThisIsVariablename = 14;
        printf("%d", ThisIsVariablename);
        return 0;
    }
a) The program will print 12
b) The program will print 14
c) The program will have a runtime error
d) The program will cause a compile-time error due to redeclaration

View Answer
Answer:b
Explanation:Variable names ThisIsVariablename and ThisIsVariableName are both distinct as C is case sensitive.
Output:
$ cc pgm4.c
$ a.out
14

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

Answer: a
Explanation:volatile is C keyword.

---------------------------------------------------------------------------------------------------------------------

Previous Post Next Post