Const - Find Output of Program Multiple Questions and Answers.
Exercise Questions ::
Const
6. What will be the output of the program?
#include<stdio.h>
int main()
{
const char *s = "" ;
char str[] = "Hello" ;
s = str;
while (*s)
printf("%c" , *s++);
return 0 ;
}
A. Error B. H C. Hello D. He
View Answer
Discuss forum
Workplace
Report
Answer: Option C
Explanation:
Step 1 : const char *s = ""; The constant variable s is declared as an pointer to an array of characters type and initialized with an empty string.
Step 2 : char str[] = "Hello"; The variable str is declared as an array of charactrers type and initialized with a string "Hello".
Step 3 : s = str; The value of the variable str is assigned to the variable s . Therefore str contains the text "Hello".
Step 4 : while(*s){ printf("%c", *s++); } Here the while loop got executed untill the value of the variable s is available and it prints the each character of the variable s .
Hence the output of the program is "Hello".
See More Information
Tutorial Link:
Published by:Michael Daani
7. What will be the output of the program?
#include<stdio.h>
int get();
int main()
{
const int x = get();
printf("%d" , x);
return 0 ;
}
int get()
{
return 20 ;
}
A. 20 B. 0 C. Garbage Value D. Error
View Answer
Discuss forum
Workplace
Report
Answer: Option A
Explanation:
Step 1 : int get(); This is the function prototype for the funtion get() , it tells the compiler returns an integer value and accept no parameters.
Step 2 : const int x = get(); The constant variable x is declared as an integer data type and initialized with the value "20".
The function get() returns the value "20".
Step 3 : printf("%d", x); It prints the value of the variable x .
Hence the output of the program is "20".
See More Information
Tutorial Link:
Published by:Michael Daani
8. What will be the output of the program?
#include<stdio.h>
int main()
{
const int i=0 ;
printf("%d\n" , i++);
return 0 ;
}
A. 10 B. 11 C. No output D. Error: ++needs a value
View Answer
Discuss forum
Workplace
Report
Answer: Option D
Explanation:
This program will show an error "Cannot modify a const object".
Step 1 : const int i=0; The constant variable 'i' is declared as an integer and initialized with value of '0'(zero).
Step 2 : printf("%d\n", i++); Here the variable 'i' is increemented by 1(one). This will create an error "Cannot modify a const object".
Because, we cannot modify a const variable.
See More Information
Tutorial Link:
Published by:Michael Daani
9. What will be the output of the program?
#include<stdio.h>
int main()
{
const c = -11 ;
const int d = 34 ;
printf("%d, %d\n" , c, d);
return 0 ;
}
A. Error B. -11, 34 C. 11, 34 D. None of above
View Answer
Discuss forum
Workplace
Report
Answer: Option B
Explanation:
Step 1 : const c = -11; The constant variable 'c' is declared and initialized to value "-11".
Step 2 : const int d = 34; The constant variable 'd' is declared as an integer and initialized to value '34'.
Step 3 : printf("%d, %d\n", c, d); The value of the variable 'c' and 'd' are printed.
Hence the output of the program is -11, 34
See More Information
Tutorial Link:
Published by:Michael Daani
«