C Programming Questions and Answers – Constants – 1

C Programming Questions and Answers – Constants – 1

This section on online C test focuses on “Constants”. One shall practice these test questions to improve their C programming skills needed for various interviews (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our online C test questions come with the detailed explanation of the answers which helps in better understanding of C concepts.Here is a listing of online C test questions on “Constants” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

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

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 will be the output of the following C code?

#include <stdio.h>
int main()
{
printf(“C programming %s”, “Class by\n%s Answers4u”, “WOW”);
}

a)

   C programming Class by
   WOW Answers4u

b) C programming Class by\n%s Answers4u
c)

   C programming Class by
   %s Answers4u

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 Answers4u 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 Answers4u

3. In the following code snippet, character pointer str holds a reference to the string ___________

char *str =  "Answers4u.com\0" "training classes";

a) Answers4u.com
b) Answers4u.com\0training classes
c) Answers4u.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 “Answers4u.com”, in memory str is pointing to entire string including training classes.

4. What will be the output of the following C code?

#include <stdio.h>
#define a 10
int main()
{
const int a = 5;
printf(“a = %d\n”, a);
}

a) a = 5
b) a = 10
c) Compilation error
d) Runtime error

Answer: c
Explanation: The #define substitutes a with 10 without leaving any identifier, which results in Compilation error.
Output:
$ cc pgm3.c
pgm3.c: In function ‘main’:
pgm3.c:5: error: expected identifier or ‘(’ before numeric constant

5. What will be the output of the following C code?

#include <stdio.h>
int main()
{
int var = 010;
printf(“%d”, var);
}

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 will be the output of the following C function?

#include <stdio.h>
enum birds {SPARROW, PEACOCK, PARROT};
enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
int main()
{
enum birds m = TIGER;
int k;
k = m;
printf(“%d\n”, k);
return 0;
}

a) 0
b) Compile time error
c) 1
d) 8

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

7. What will be the output of the following C code?

#include <stdio.h>
#define MAX 2
enum bird {SPARROW = MAX + 1, PARROT = SPARROW + MAX};
int main()
{
enum bird b = PARROT;
printf(“%d\n”, b);
return 0;
}

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 will be the output of the following C code?

#include <stdio.h>
#include <string.h>
int main()
{
char *str = “x”;
char c = ‘x’;
char ary[1];
ary[0] = c;
printf(“%d %d”, strlen(str), strlen(ary));
return 0;
}

a) 1 1
b) 2 1
c) 2 2
d) 1 (undefined value)

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

« Prev – C Programming Questions and Answers – Data Types and Sizes – 2
» Next – C Programming Questions and Answers – Constants – 2

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
%d bloggers like this: