Multiple Choice Questions and Answers for Constants in C Language



Here is a listing of online C test questions on “Constants” along with answers, explanations and/or solutions:

1. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};
  5.         printf("PEACH = %d\n", PEACH);
  6.     }
a) PEACH = 3
b) PEACH = 4
c) PEACH = 5
d) PEACH = 6

Answer:c

Explanation:In enum, the value of constant is defined to the recent assignment from left.

Output:
$ cc pgm1.c
$ a.out
PEACH = 5
2. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         printf("C programming %s", "Class by\n%s Sanfoundry", "WOW");
  5.     }
a) C programming Class by
    WOW Sanfoundry
b) C programming Class by\n%s Sanfoundry
c) C programming Class by
    %s Sanfoundry
d) Compilation error

Answer:c

Explanation:This program has only one %s within first double quotes, so it does not read the string “WOW”.

The %s along with the Sanfoundry is not read as a format modifier while new line character prints the new line.

Output:
$ cc pgm2.c
$ a.out
C programming Class by
%s Sanfoundry
3. For the following code snippet:

       char *str = “Sanfoundry.com\0” “training classes”;
       The character pointer str holds reference to string:

a) Sanfoundry.com
b) Sanfoundry.com\0training classes
c) Sanfoundry.comtraining classes
d) Invalid declaration

Answer:b

Explanation:’\0′ is accepted as a char in the string. Even though strlen will give length of string “Sanfoundry.com”, in memory str is pointing to entire string including training classes”
4. What is the output of this C code?
  1.     #include 
  2.     #define a 10
  3.     int main()
  4.     {
  5.         const int a = 5;
  6.         printf("a = %d\n", a);
  7.     }
a) a = 5
b) a = 10
c) Compilation error
d) Runtime error

Answer:c

Explanation:The #define substitutes a with 10 leaving no identifier and hence compilation error.

Output:
$ cc pgm3.c
pgm3.c: In function ‘main’:
pgm3.c:5: error: expected identifier or ‘(’ before numeric constant
5. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         int var = 010;
  5.         printf("%d", var);
  6.     }
a) 2
b) 8
c) 9
d) 10

Answer:b

Explanation:010 is octal representation of 8.

Output:
$ cc pgm4.c
$ a.out
8
6. What is the output of this C code?
  1.     #include 
  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 compatible.

Output:
$ cc pgm5.c
$ a.out
8
7. What is the output of this C code?
  1.     #include 
  2.     #define MAX 2
  3.     enum bird {SPARROW = MAX + 1, PARROT = SPARROW + MAX};
  4.     int main()
  5.     {
  6.         enum bird b = PARROT;
  7.         printf("%d\n", b);
  8.         return 0;
  9.     }
a) Compilation error
b) 5
c) Undefined value
d) 2

Answer:b

Explanation:MAX value is 2 and hence PARROT will have value 3 + 2.

Output:
$ cc pgm6.c
$ a.out
5
8. What is the output of this C code?
  1.     #include 
  2.     #include 
  3.     int main()
  4.     {
  5.         char *str = "x";
  6.         char c = 'x';
  7.         char ary[1];
  8.         ary[0] = c;
  9.         printf("%d %d", strlen(str), strlen(ary));
  10.         return 0;
  11.     }
a) 1 1
b) 2 1
c) 2 2
d) 1 (undefined value)

Answer:d

Explanation:str is null terminated but ary is not.

Output:
$ cc pgm7.c
$ a.out
1 5
9. enum types are processed by

a) Compiler
b) Preprocessor
c) Linker
d) Assembler

Answer:a

10. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         printf("sanfoundry\rclass\n");
  5.         return 0;
  6.     }
a) sanfoundryclass
b) sanfoundry
    class
c) classundry
d) sanfoundry

Answer:c

Explanation:r is carriage return and moves the cursor back. sanfo is replaced by class

Output:
$ cc pgm8.c
$ a.out
classundry
11. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         printf("sanfoundry\r\nclass\n");
  5.         return 0;
  6.     }
a) sanfoundryclass
b) sanfoundry
    class
c) classundry
d) sanfoundry

Answer:b

Explanation:rn combination makes cursor move to next line.

Output:
$ cc pgm9.c
$ a.out
sanfoundry
class
12. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         const int p;
  5.         p = 4;
  6.         printf("p is %d", p);
  7.         return 0;
  8.     }
a) p is 4
b) Compile time error
c) Run time error
d) p is followed by a garbage value

Answer:b

Explanation:Since the constant variable has to be declared and defined at the same time, not doing it results in an error.

Output:
$ cc pgm10.c
pgm10.c: In function ‘main’:
pgm10.c:5: error: assignment of read-only variable ‘p’
13. Comment on the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         int k = 4;
  5.         int *const p = &k;
  6.         int r = 3;
  7.         p = &r;
  8.         printf("%d", p);
  9.     }
a) Address of k
b) Address of r
c) Compile time error
d) Adress of k + address of r

Answer:c

Explanation:Since the pointer p is declared to be constant, trying to assign it with a new value results in an error.

Output:
$ cc pgm11.c
pgm11.c: In function ‘main’:
pgm11.c:7: error: assignment of read-only variable ‘p’
pgm11.c:8: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int * const’
14. Which is false?

a) Constant variables need not be defined as they are declared and can be defined later
b) Global constant variables are initialised to zero
c) const keyword is used to define constant values
d) You cannot reassign a value to a constant variable

Answer:a

Explanation:Since the constant variable has to be declared and defined at the same time, not doing it results in an error.

Hence the statement a is false.
15. Comment on the output of this C code?
  1.     #include 
  2.     void main()
  3.     {
  4.         int const k = 5;
  5.         k++;
  6.         printf("k is %d", k);
  7.     }
a) k is 6
b) Error due to const succeeding int
c) Error, because a constant variable can be changed only twice
d) Error, because a constant variable cannot be changed

Answer:d

Explanation:Constant variable has to be declared and defined at the same time. Trying to change it results in an error.

Output:
$ cc pgm12.c
pgm12.c: In function ‘main’:
pgm12.c:5: error: increment of read-only variable ‘k’
16. Comment on the output of this C code?
  1.     #include 
  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

Related

Multiple Choice Questions 939851695680270808

Post a Comment

emo-but-icon

item