This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Constants – 1”.
Pre-requisite for C Constants MCQ set: Video Tutorial on C Constants.
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
View Answer
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 Sanfoundry", "WOW");
}
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
View Answer
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. In the following code snippet, character pointer str holds a reference to the string ___________
char *str = "Sanfoundry.com\0" "training classes";
a) Sanfoundry.com
b) Sanfoundry.com\0training classes
c) Sanfoundry.comtraining classes
d) Invalid declaration
View Answer
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 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
View Answer
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
View Answer
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
View Answer
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
View Answer
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)
View Answer
Explanation: str is null terminated, but ary is not null terminated.
Output:
$ cc pgm7.c
$ a.out
1 5
Sanfoundry Global Education & Learning Series – C Programming Language.
To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship
- Practice BCA MCQs
- Check Computer Science Books