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 -
int main()
-
{ -
enum {ORANGE = 5, MANGO, BANANA = 4, PEACH};
-
printf("PEACH = %dn", PEACH);
-
}
a) PEACH = 3
b) PEACH = 4
c) PEACH = 5
d) PEACH = 6
Answer: c
Clarification: 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 -
int main()
-
{ -
printf("C programming %s", "Class byn%s ", "WOW");
-
}
a)
C programming Class by WOW
b) C programming Class byn%s
c)
C programming Class by %s
d) Compilation error
Answer: c
Clarification: This program has only one %s within first double quotes, so it does not read the string “WOW”.
The %s along with the 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
3. In the following code snippet, character pointer str holds a reference to the string ___________
char *str = ".com " "training classes";
a) .com
b) .com training classes
c) .comtraining classes
d) Invalid declaration
Answer: b
Clarification: ‘ ’ is accepted as a char in the string. Even though strlen will give length of string “.com”, in memory str is pointing to entire string including training classes.
4. What will be the output of the following C code?
-
#include -
#define a 10 -
int main()
-
{ -
const int a = 5;
-
printf("a = %dn", a);
-
}
a) a = 5
b) a = 10
c) Compilation error
d) Runtime error
Answer: c
Clarification: 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 -
int main()
-
{ -
int var = 010;
-
printf("%d", var);
-
}
a) 2
b) 8
c) 9
d) 10
Answer: b
Clarification: 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 -
enum birds {SPARROW, PEACOCK, PARROT};
-
enum animals {TIGER = 8, LION, RABBIT, ZEBRA};
-
int main()
-
{ -
enum birds m = TIGER;
-
int k;
-
k = m;
-
printf("%dn", k);
-
return 0;
-
}
a) 0
b) Compile time error
c) 1
d) 8
Answer: d
Clarification: 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 -
#define MAX 2 -
enum bird {SPARROW = MAX + 1, PARROT = SPARROW + MAX};
-
int main()
-
{ -
enum bird b = PARROT;
-
printf("%dn", b);
-
return 0;
-
}
a) Compilation error
b) 5
c) Undefined value
d) 2
Answer: b
Clarification: 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 -
#include -
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
Clarification: str is null terminated, but ary is not null terminated.
Output:
$ cc pgm7.c
$ a.out
1 5
