C++ Programming Questions and Answers – Static Constant Keyword

«
»

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Static Constant Keyword”.

1. What will be the output of the following C++ code?

advertisement
#include <iostream>
using namespace std;
class Test
{
    static int x;
  public:
    Test() { x++; }
    static int getX() {return x;}
};
int Test::x = 0;
int main()
{
    cout << Test::getX() << " ";
    Test t[5];
    cout << Test::getX();
}

a) 0 0
b) 5 0
c) 0 5
d) 5 5
View Answer

Answer: c
Explanation: Static function can be called without using objects therefore the first call is fine. Next when we are creating 5 objects of the class then value of x is updated each time and as static variables are global to class therefore each updation of x is reflected back to each object hence value of x is 5.

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

#include <iostream>
using namespace std;
class Player
{
  private:
    int id;
    static int next_id;
  public:
    int getID() { return id; }
    Player()  {  id = next_id++; }
};
int Player::next_id = 1;
int main()
{
  Player p1;
  Player p2;
  Player p3;
  cout << p1.getID() << " ";
  cout << p2.getID() << " ";
  cout << p3.getID();
  return 0;
}

a) 1 2 3
b) 2 2 2
c) 1 3 1
d) 1 1 1
View Answer

Answer: a
Explanation: In this as next_id is a static variable so and initialized with 1 therefore the id value for 1st objects is 1 and next_id is updated to 2. In this way next_id is assigned to id in each object creation and updated by 1 so in this way value of each Id is updated.

3. Which of the following is correct about static variables?
a) Static functions do not support polymorphism
b) Static data members cannot be accessed by non-static member functions
c) Static data members functions can access only static data members
d) Static data members functions can access both static and non-static data members
View Answer

Answer: c
Explanation: Static member functions can access static data members only. Static member functions can be overloaded. Static data members can be accessed by non-static member functions.
advertisement

4. What will be the output of the following C++ code?

#include <iostream>
using namespace std;
class A
{
   private:
     int x;
   public:
     A(int _x)  {  x = _x; }
     int get()  { return x; }
};
class B
{
    static A a;
  public:
   static int get()
   {  return a.get(); }
}; 
int main(void)
{
    B b;
    cout << b.get();
    return 0;
}

a) Garbage value
b) Compile-time Error
c) Run-time Error
d) Nothing is printed
View Answer

Answer: b
Explanation: Every static member function of a class must be initialized explicitly before use and a data member, a of class A declared inside class B is used without initializing ‘a’ therefore the program gives an error.

5. What will be the output of the following C++ code?

#include<iostream>
using namespace std;
class Test
{
   private:
     static int count;
   public:
     Test& fun(); 
};
int Test::count = 0;
Test& Test::fun()
{
    Test::count++;
    cout << Test::count << " ";
    return *this;
}
int main()
{
    Test t;
    t.fun().fun().fun().fun();
    return 0;
}

a) 4 4 4 4
b) 1 2 3 4
c) 1 1 1 1
d) 0 1 2 3
View Answer

Answer: b
Explanation: Here we are returning the reference of object by the function call fun() therefore this type of call is allowed. Also as count is static member of the class therefore updation is reflected to the whole class and to every object of the class. Therefore the four function calls to fun() function updates the value of count and prints.

6. What will be the output of the following C++ code?

advertisement
#include <iostream>
class Test
{
   public:
     void fun();
};
static void Test::fun()   
{
    std::cout<<"fun() is static";
}
int main()
{
    Test::fun();   
    return 0;
}

a) fun() is static
b) Compile-time Error
c) Run-time Error
d) Nothing is printed
View Answer

Answer: b
Explanation: The prototype of the functions are not matched. The function declared inside a class does not have static linkage however the class defined outside the class has the static linkage, therefore, the program gives an error.

7. Const qualifier can be applied to which of the following?
i) Functions inside a class
ii) Arguments of a function
iii) Static data members
iv) Reference variables
a) i, ii and iii
b) i, ii, iii, and iv
c) ii, iii and iv
d) i only
View Answer

Answer: b
Explanation: const keyword can be applied to all of the following mentioned above.

8. What will be the output of the following C++ code?

#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;
}

a) 0 0
b) Garbage values
c) Compile erroe
d) Segmentation fault
View Answer

Answer: c
Explanation: C++ does not allows a constant object to access any non constant member functions and as getY() is a non constant function and t is a constant object therefore the program gives the error.

9. What will be the output of the following C++ code?

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

a) 10
b) Garbage value
c) Error
d) Segmentation fault
View Answer

Answer: c
Explanation: In C++, all the constant variables must be initialized while declaration and they cannot be modified later in the program. Now in this program as we have declared the constant variable x in first line and initializing it in the next line therefore the program gives the error.
advertisement

10. What will be the output of the following C++ code?

#include <iostream>
int const s=9;
int main()
{
    std::cout << s;
    return 0;
}

a) 9
b) Garbage value
c) Error
d) Segmentation fault
View Answer

Answer: a
Explanation: The program is syntactically and semantically correct hence the program is compiled and executed successfully.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn