• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C++ const keyword

Question 1

Predict the output of following program CPP
#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

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

Question 3

Predict the output of following program. C
#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();

Question 4

C
#include <stdio.h>
int main()
{
   const int x;
   x = 10;
   printf(\"%d\", x);
   return 0;
}
  • Compiler Error
  • 10
  • 0
  • Runtime Error

Question 5

Output of C++ program? C
#include <iostream>
int const s=9;
int main()
{
    std::cout << s;
    return 0;
}
Contributed by Pravasi Meet
  • 9
  • Compiler Error

There are 5 questions to complete.

Last Updated :
Take a part in the ongoing discussion