My Report

C++ Classes Test – 2


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

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

#include <iostream>
#include <string>
using namespace std;

class A
{
	static int a;

   public:
	void change(int i){
		a = i;
	}
	void value_of_a(){
		cout<<a;
	}
};

int main(int argc, char const *argv[])
{
	A a1 = A();
	a1.change(5);
	a1.value_of_a();
	return 0;
}

2. Pick the incorrect statement about inline functions in C++?

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

#include <iostream>
#include <string>
using namespace std;
class A
{
	static int a;
    public:
	void change(int i){
		a = i;
	}
	void value_of_a(){
		cout<<a;
	}
};

int A::a = 5;

int main(int argc, char const *argv[])
{
	A a1 = A();
	A a2 = A();
	A a3 = A();
	a1.change(10);
	a1.value_of_a();
	a2.value_of_a();
	a3.value_of_a();
	return 0;
}

4. Which functions of a class are called inline functions?

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

#include <iostream>
#include <string>
using namespace std;
class A
{
	int a;

   public:
	int assign(int i) const {
		a = i;
	}
	int return_value() const {
		return a;
	}
};
int main(int argc, char const *argv[])
{
	A obj;
	obj.assign(5);
	cout<<obj.return_value();
}

6. Which operator a pointer object of a class uses to access its data members and member functions?

7. Which category of data type a class belongs to?

8. What is the correct syntax of accessing a static member of a Class?

---------------------------
Example class:
class A
{
	public:
		static int value;
}
---------------------------

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

#include <iostream> 
using namespace std; 
class S 
{ 
	int m; 
   public: 
	#define MAC(S::m)
};

int main(int argc, char const *argv[])
{
	cout<<"Hello World";
	return 0;
}

10. What does a mutable member of a class mean?

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

#include <iostream>
#include <string>
using namespace std;

class A
{
	mutable int a;

        public:
	int assign(int i) const {
		a = i;
	}
	int return_value() const {
		return a;
	}

};

int main(int argc, char const *argv[])
{
	A obj;
	obj.assign(5);
	cout<<obj.return_value();
}

12. Inline functions are avoided when ____________________________

13. How the objects are self-referenced in a member function of that class.

14. Pick the correct statement.

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

#include <iostream>
#include <string>
using namespace std;
class A
{
	int a = 5;
    public:
	void change(int i){
		a = i;
	}
	static void value_of_a(){
		cout<<a;
	}
};

int main(int argc, char const *argv[])
{
	A a1 = A();
	a1.change(10);
	a1.value_of_a();
	return 0;
}

 

Start practicing “1000 MCQs on C++”, and once you are ready, you can take tests on all topics by attempting our “C++ Test Series”.

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.