Exercise: Questions on const keyword


Questions on const keyword : Question 1 :
Predict the output of following program
#include <iostream>
using namespace std;
int main()
{
    const char* p = "12345";
    const char **q = &p;
    *q = "abcde";
    const char *s = ++p;
    p = "XYZWVU";
    cout << *++s;
    return 0;
}

Compiler Error
c
b
Garbage Value
Show Answer
Questions on const keyword : Question 2 :
In C++, const qualifier can be applied to 1) Member functions of a class 2) Function arguments 3) To a class data member which is declared as static 4) Reference variables

Only 1, 2 and 3
Only 1, 2 and 4
All
Only 1, 3 and 4
Show Answer
Questions on const keyword : Question 3 :
Predict the output of following program.
#include <iostream>
using namespace std;
class Point
{
    int x, y;
public:
 Point(int i = 0, int j =0)
   { x = i; y = j;  }
   int getX() const { return x; }
   int getY() {return y;}
};

int main()
{
    const Point t;
    cout << t.getX() << " ";
    cout << t.gety();
    return 0;
}

Garbage Values
0 0
Compiler Error in line cout << t.getX() << " ";
Compiler Error in line cout << t.gety();
Show Answer
Questions on const keyword : Question 4 :
#include <stdio.h>
int main()
{
   const int x;
   x = 10;
   printf("%d", x);
   return 0;
}

Compiler Error
10
0
Runtime Error
Show Answer
Questions on const keyword : Question 5 :
Output of C++ program?
#include <iostream>
int const s=9;
int main()
{
    std::cout << s;
    return 0;
}
Contributed by Pravasi Meet

9
Compiler Error
Show Answer