Home » Aptitude Questions and Answers » C++ Aptitude Questions and Answers

C++ programming Class, Objects Aptitude Questions and Answers



This section contains Aptitude Questions and Answers on class, object and related topics in C++ programming language.

List of C++ Classes and Objects Aptitude Questions and Answers

1) Which type of Access Specifier class data member number is?
//a simple class
class test_example
{
	int number;
};
    
  1. private
  2. public
  3. protected
  4. default

2) What will be the output of this program?
#include <iostream>
using namespace std;

class sample
{
	int x;
}

int main()
{
	sample obj;
	obj.x=100;
	cout<<"x="<<obj.x<<endl;
}
    
  1. x=10
  2. Error

3) Which variable(s) is/are accessible in main() function?
class sample
{
	private:
		int x;
	protected:
		int y;
	public:
		int z;
}
    
  1. x
  2. y
  3. z
  4. y and z

4) Write statement to print value of var ?
int var=100;
class sample
{
private:
	void showVal(void)
	{
		...
	}
}
    
  1. cout<<var;
  2. cout<<::var;
  3. Cannot access var inside class member function.
  4. Both 1 and 2



5) Where a protected member can be accessed?
  1. Within the same class
  2. Outside of the class
  3. Within the Derived class
  4. Both 1 and 3

6) A C+++ class contains...
  1. Member Functions
  2. Data Members
  3. Both 1 and 2
  4. Customize main () function

7) Where a class member function can be defined?
  1. Inside the class definition
  2. Outside of the class definition
  3. Both 1 and 2
  4. Don’t know

8) Can we declare a member function private?
  1. Yes
  2. No

9) What will be the output of this program?
#include <iostream>
using namespace std;
//Empty class
class test
{
};

int main()
{
   test testObj;
   cout<<"size ="<<sizeof(testObj);
   return 0;
}
    
  1. Error
  2. size =Garbage
  3. size =1

10) Which is correct sentence regarding structure and class in c?
  1. Both are same in C++.
  2. By default structure’s members are public and class’s members are private.
  3. Structure does not support function declaration within its declaration while class supports.
  4. Both 2 and 3.






Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.