Open In App

Output of C programs | Set 60 (Constants)

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite : C Constants and Strings
Q.1 What is the output of this program? 
 

CPP




#include<iostream>
using namespace std;
int main()
{
    const int i=0;
    printf("%d\n", i++);
    return 0;
}
    


Options 
a) Error 
b) H 
c) Hello 
d) Hel 
 

ans:- c 

Explanation : 

 const char *s = "";

The constant variable s is declared as an pointer to an array of characters type and initialized with an empty string. 
 

char str[] = "Hello"; 

The variable str is declared as an array of characters type and initialized with a string “Hello”. 
 

s = str;

The value of the variable str is assigned to the variable s. Therefore str contains the text “Hello”.
 

while(*s)
{ printf("%c", *s++); } 

Here the while loop got executed until 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”.
Q.2 What is the output of this program? 
 

CPP




#include<iostream>
using namespace std;
int main()
{
    const int c = -11;
    const int d = 34;
    printf("%d, %d\n", c, d);
    return 0;
}


Options 
a) Garbage value 
b) Error 
c) 20 
d) 0 
 

ans:- c

Explanation : 

int get(); 

This is the function prototype for the function get(), it tells the compiler returns an integer value and accept no parameters. 
 

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”.
Q.3 What is the output of this program? 
 

CPP





Options 
a) Address of i 
Address of j 
b) 10 
223 
c) Error: cannot convert parameter 1 from ‘const int **’ to ‘int **’ 
d) Garbage value
 

ans:- c

Explanation : Here ptr is declared as constant integer pointer to which address of i is assigned but in function fun double pointer is passed as an argument, so it can’t convert from ‘const int **’ to ‘int **’.
Q.4 What is the output of this program? 
 

CPP




#include<iostream>
using namespace std;
int main()
{
    const int i=0;
    printf("%d\n", i++);
    return 0;
}
    


Options 
a) 10 
b) 11 
c) No output 
d) Error: ++needs a value 
 

ans:- d

Explanation : 
 

const int i = 0; 

The constant variable ‘i’ is declared as an integer and initialized with value of ‘0’(zero). 
 

printf("%d\n", i++); 

Here the variable ‘i’ is incremented by 1(one). This will create an error “Cannot modify a const object”. 
Because, we cannot modify a const variable.
Q.5 What is the output of this program? 
 

CPP




#include<iostream>
using namespace std;
int main()
{
    const int c = -11;
    const int d = 34;
    printf("%d, %d\n", c, d);
    return 0;
}


Options 
a) Error 
b) -11, 34 
c) 11, 34 
d) None of these 
 

ans:- b

Explanation :- 

const c = -11;

The constant variable ‘c’ is declared and initialized to value “-11”. 
 

const int d = 34;

The constant variable ‘d’ is declared as an integer and initialized to value ’34’. 
 

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

 



Last Updated : 29 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads