Scroll to Top

C Programming Questions and Answers – Constants – 2

C Programming Questions and Answers – Constants – 2

 

Here is a listing of C problems on “Constants” along with answers, explanations and/or solutions:

1. enum types are processed by _________
a) Compiler
b) Preprocessor
c) Linker
d) Assembler

Answer: a
Explanation: None.

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

#include <stdio.h>
int main()
{
printf(“answers4u\rclass\n”);
return 0;
}

a) answers4uclass
b)

   answers4u
   class

c) classrs4u
d)answers4u

Answer: c
Explanation: r is carriage return and moves the cursor back. answe is replaced by class.
Output:
$ cc pgm8.c
$ a.out
classrs4u

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

#include <stdio.h>
int main()
{
printf(“answers4u\r\nclass\n”);
return 0;
}

a) answers4uclass
b)

   answers4u
   class

c) classrs4u
d)answers4u

Answer: b
Explanation: rn combination makes the cursor move to the next line.
Output:
$ cc pgm9.c
$ a.out
answers4u
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

Answer: b
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

Answer: c
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

Answer: a
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

Answer: d
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(“Answers4u.com”);
return 0;
}
void main()
{
print();
}

a) Error because function name cannot be preceded by const
b) Answers4u.com
c) Answers4u.com is printed infinite times
d) Blank screen, no output

Answer: b
Explanation: None.
Output:
$ cc pgm13.c
$ a.out
Answers4u.com

« Prev – C Programming Questions and Answers – Constants – 1
» Next – C Programming Questions and Answers – Declarations – 1

 

Related posts

1 thought on “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: