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.






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.