This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Constants – 2”.
Pre-requisite for C Constants MCQ set: Video Tutorial on C Constants.
1. enum types are processed by _________
a) Compiler
b) Preprocessor
c) Linker
d) Assembler
View Answer
Explanation: None.
2. What will be the output of the following C code?
#include <stdio.h>
int main()
{
printf("sanfoundry\rclass\n");
return 0;
}
a) sanfoundryclass
b)
sanfoundry class
c) classundry
d) sanfoundry
View Answer
Explanation: r is carriage return and moves the cursor back. sanfo is replaced by class.
Output:
$ cc pgm8.c
$ a.out
classundry
3. What will be the output of the following C code?
#include <stdio.h>
int main()
{
printf("sanfoundry\r\nclass\n");
return 0;
}
a) sanfoundryclass
b)
sanfoundry class
c) classundry
d) sanfoundry
View Answer
Explanation: rn combination makes the cursor move to the next line.
Output:
$ cc pgm9.c
$ a.out
sanfoundry
class
4. What will be the output of the following C code?
#include <stdio.h>
int main()
{
const int p;
p = 4;
printf("p is %d", p);
return 0;
}
a) p is 4
b) Compile time error
c) Run time error
d) p is followed by a garbage value
View Answer
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’
5. What will be the output of the following C code?
#include <stdio.h>
void main()
{
int k = 4;
int *const p = &k;
int r = 3;
p = &r;
printf("%d", p);
}
a) Address of k
b) Address of r
c) Compile time error
d) Address of k + address of r
View Answer
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’
6. Which of the following statement is false?
a) Constant variables need not be defined as they are declared and can be defined later
b) Global constant variables are initialized to zero
c) const keyword is used to define constant values
d) You cannot reassign a value to a constant variable
View Answer
Explanation: Since the constant variable has to be declared and defined at the same time, not doing it results in an error.
7. What will be the output of the following C code?
#include <stdio.h>
void main()
{
int const k = 5;
k++;
printf("k is %d", k);
}
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
View Answer
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’
8. What will be the output of the following C code?
#include <stdio.h>
int const print()
{
printf("Sanfoundry.com");
return 0;
}
void main()
{
print();
}
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
View Answer
Explanation: None.
Output:
$ cc pgm13.c
$ a.out
Sanfoundry.com
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.
- Check C Books
- Practice BCA MCQs
- Apply for Computer Science Internship
- Check Computer Science Books
- Practice Computer Science MCQs