Log In Register

Units


Constants - Questions and Answers

 

Practice these Constants under C Programming walk-in interview Questions/ examination questions with best tricks and short cuts with solution. Student (candidate) who want to crack the walk in interview, competitive exams and want to find short cuts and tricks to solve questions on Constants for following purpose.


– Anyone who is wishing to increase their perfect knowledge of Constants of C Programming.
– Anyone who is preparing for aptitude test and increase his in depth aptitude knowledge C Programming specifically Constants.
– Anyone who is preparing for interviews (competitive examinations, govt examinations and bank examinations, off-campus or on campus interviews, walk-in interview and company interviews) having questions on Constants under C Programming.

– Anyone who is preparing for aptitude test or competitive exam or entrance exam.
– Anyone who is preparing for entrance examinations and other competitive examinations.
– All Experienced, Freshers and Students.

Here is list of Questions and Answers covering all key area of  Constants topic of C Programming:

 

1.

What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         printf("C programming %s", "Class by\n%s testmirror", "WOW");
  5.     }

Answer: Option 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 testmirror 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 testmirror

2.

What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         const int p;
  5.         p = 4;
  6.         printf("p is %d", p);
  7.         return 0;
  8.     }

Answer: Option D

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’

3.

What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         printf("testmirror\rclass\n");
  5.         return 0;
  6.     }

Answer: Option B

Explanation:

r is carriage return and moves the cursor back. sanfo is replaced by class
Output:
$ cc pgm8.c
$ a.out
classundry

4.

What is the output of this C code?

  1.     #include <stdio.h>
  2.     #include <string.h>
  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.     }

Answer: Option A

Explanation:

str is null terminated but ary is not.
Output:
$ cc pgm7.c
$ a.out
1 5

5.

Which is false?

Answer: Option D

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.

6.

What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};
  5.         printf("PEACH = %d\n", PEACH);
  6.     }

Answer: Option B

Explanation:

In enum, the value of constant is defined to the recent assignment from left.
Output:
$ cc pgm1.c
$ a.out
PEACH = 5

7.

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

Answer: Option C

Explanation:

010 is octal representation of 8.
Output:
$ cc pgm4.c
$ a.out
8

8.

What is the output of this C code?

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         printf("testmirror\r\nclass\n");
  5.         return 0;
  6.     }

Answer: Option C

Explanation:

rn combination makes cursor move to nextline.
Output:
$ cc pgm9.c
$ a.out
testmirror
class

9.

Comment on the output of this C code?

  1.     #include <stdio.h>
  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.     }

Answer: Option B

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’

10.

enum types are processed by

Answer: Option D

Explanation:

11.

Comment on the output of this C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         int const k = 5;
  5.         k++;
  6.         printf("k is %d", k);
  7.     }

Answer: Option A

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’

12.

What is the output of this C code?

  1.     #include <stdio.h>
  2.     #define a 10
  3.     int main()
  4.     {
  5.         const int a = 5;
  6.         printf("a = %d\n", a);
  7.     }

Answer: Option B

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

13.

What is the output of this C code?

  1.     #include <stdio.h>
  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.     }

Answer: Option A

Explanation:

m is an integer constant, hence compatible.
Output:
$ cc pgm5.c
$ a.out
8

14.

Comment on the output of this C code?

  1.     #include <stdio.h>
  2.     int const print()
  3.     {
  4.         printf("testmirror.com");
  5.         return 0;
  6.     }
  7.     void main()
  8.     {
  9.         print();
  10.     }

Answer: Option C

Explanation:

Output:
$ cc pgm13.c
$ a.out
testmirror.com

15.

For the following code snippet:
       char *str = “testmirror.com\0” “training classes”;
       The character pointer str holds reference to string:

Answer: Option B

Explanation:

‘\0’ is accepted as a char in the string. Even though strlen will give length of string “testmirror.com”, in memory str is pointing to entire string including training classes