Objects and Classes Questions
Practice Objects and Classes MCQs with answers and explanations. Page 2 of 3.
Category
C++ Programming
Topic
Objects and Classes
Page
2 / 3
Mode
Practice
Questions
Open any question to view the answer and explanation.
In C++ classes, which statement about the accessibility of data members and member functions is correct (considering typical design, not a special case)?
Open
View answer
C++ friendship: which of the following statements about the friend keyword is incorrect?
Open
View answer
C++ object lifecycle: a constructor is executed when which of the following events occurs?
Open
View answer
C++ private inheritance: under private inheritance, how are the base class’s public members treated in the derived class interface?
Open
View answer
Abstract classes in C++: how many objects (instances) can you directly create from an abstract class?
Open
View answer
C++ static member functions: does using this-> inside a static method compile? Examine and predict the program's behavior.
#include<iostream.h>
class CuriousTab
{
static int x;
public:
static void SetData(int xx)
{
this->x = xx; // attempt to use this in a static function
}
static void Display()
{
cout << x;
}
};
int CuriousTab::x = 0;
int main()
{
CuriousTab::SetData(22);
CuriousTab::Display();
return 0;
}
Open
View answer
C++ nested struct as a global member: if a function that initializes members is never called, what does Display() print?
#include<iostream.h>
class India
{
public:
struct CuriousTab
{
int x;
float y;
void Function()
{
y = x = (x = 4 * 4);
y = --y * y;
}
void Display()
{
cout << y << endl;
}
} B;
} I;
int main()
{
I.B.Display();
return 0;
}
Open
View answer
C library strcspn in C++ (legacy headers): for the given strings, what index does strcspn("India", "CuriousTab") return?
#include<iostream.h>
#include<string.h>
class CuriousTab
{
int val;
public:
void SetValue(char str1, char str2)
{
val = strcspn(str1, str2);
}
void ShowValue()
{
cout << val;
}
};
int main()
{
CuriousTab objCuriousTab;
objCuriousTab.SetValue((char)"India", (char)"CuriousTab");
objCuriousTab.ShowValue();
return 0;
}
Open
View answer
C++ operator overloading for a 2D point: compute the result of Point operator+(Point) with default and explicit constructors, then display the sum.
#include<iostream.h>
class Point
{
int x, y;
public:
Point(int xx = 10, int yy = 20) { x = xx; y = yy; }
Point operator+(Point objPoint)
{
Point objTmp;
objTmp.x = objPoint.x + this->x;
objTmp.y = objPoint.y + this->y;
return objTmp;
}
void Display() { cout << x << " " << y; }
};
int main()
{
Point objP1; // 10,20
Point objP2(1, 2); // 1,2
Point objP3 = objP1 + objP2;
objP3.Display();
return 0;
}
Open
View answer
C++ composition (has-a): given a contained base-like object constructed with (10,20), what does Show() print?
#include<iostream.h>
class CuriousTabBase
{
int x, y;
public:
CuriousTabBase(int xx = 10, int yy = 10) { x = xx; y = yy; }
void Show() { cout << x * y << endl; }
};
class CuriousTabDerived
{
private:
CuriousTabBase objBase;
public:
CuriousTabDerived(int xx, int yy) : objBase(xx, yy)
{
objBase.Show();
}
};
int main()
{
CuriousTabDerived objDev(10, 20);
return 0;
}
Open
View answer
C++ static data member and constructor side effects: given exit(0) when x == 1, what does the program actually print?
#include<iostream.h>
#include<process.h>
class CuriousTab
{
static int x;
public:
CuriousTab()
{
if (x == 1)
exit(0);
else
x++;
}
void Display()
{
cout << x << " ";
}
};
int CuriousTab::x = 0;
int main()
{
CuriousTab objCuriousTab1;
objCuriousTab1.Display();
CuriousTab objCuriousTab2;
objCuriousTab2.Display();
return 0;
}
Open
View answer
C++ inheritance with float and char: what does the following print after casting a computed float to char and then to int?
#include<iostream.h>
class CuriousTabBase { public: float x; };
class CuriousTabDerived : public CuriousTabBase
{
public:
char ch;
void Process() { ch = (int)((x = 12.0) / 3.0); }
void Display() { cout << (int)ch; }
};
int main()
{
CuriousTabDerived *objDev = new CuriousTabDerived;
objDev->Process();
objDev->Display();
return 0;
}
Open
View answer
C++ constructor pre-increment parameters and post-increment in output: what does Show() print?
#include<iostream.h>
class CuriousTabData
{
int x, y, z;
public:
CuriousTabData(int xx, int yy, int zz)
{
x = ++xx; y = ++yy; z = ++zz;
}
void Show()
{
cout << "" << x++ << " " << y++ << " " << z++;
}
};
int main()
{
CuriousTabData objData(1, 2, 3);
objData.Show();
return 0;
}
Open
View answer
C++ substring printing with index and count: what is printed by GetData("Welcome!", 1, 3) given the loop logic?
#include<iostream.h>
#include<string.h>
class CuriousTab
{
public:
void GetData(char s, int x, int y)
{
int i = 0;
for (i = x - 1; y > 0; i++)
{
cout << s[i];
y--;
}
}
};
int main()
{
CuriousTab objCuriousTab;
objCuriousTab.GetData((char)"Welcome!", 1, 3);
return 0;
}
Open
View answer
C++ pointers and member access: what exactly is printed, and why do (*p).x and p->x show the same value?
#include<iostream.h>
class Tab
{
public:
int x;
};
int main()
{
Tab *p = new Tab();
(*p).x = 10;
cout<< (*p).x << " " << p->x << " " ;
p->x = 20;
cout<< (*p).x << " " << p->x ;
return 0;
}
Open
View answer
C++ static data and static member functions: after setting x via a class-qualified call, what is printed?
#include<iostream.h>
class CuriousTab
{
static int x;
public:
static void SetData(int xx)
{
x = xx;
}
static void Display()
{
cout<< x;
}
};
int CuriousTab::x = 0;
int main()
{
CuriousTab::SetData(44);
CuriousTab::Display();
return 0;
}
Open
View answer
C++ static counter across class functions: after First() then Second(5), what does Display() print?
#include<iostream.h>
class CuriousTab
{
static int count;
public:
static void First(void)
{
count = 10;
}
static void Second(int x)
{
count = count + x;
}
static void Display(void)
{
cout<< count << endl;
}
};
int CuriousTab::count = 0;
int main()
{
CuriousTab::First();
CuriousTab::Second(5);
CuriousTab::Display();
return 0;
}
Open
View answer
C++ mixing static and non-static access: does calling a non-static member via class name compile?
#include<iostream.h>
class CuriousTab
{
static int x;
public:
static void SetData(int xx)
{
x = xx;
}
void Display()
{
cout<< x;
}
};
int CuriousTab::x = 0;
int main()
{
CuriousTab::SetData(33);
CuriousTab::Display();
return 0;
}
Open
View answer
C++ composition vs. base construction: what does this derived constructor print when it calls Show() on its own member?
#include<iostream.h>
class CuriousTabBase
{
int x, y;
public:
CuriousTabBase(int xx = 10, int yy = 10)
{
x = xx;
y = yy;
}
void Show()
{
cout<< x * y << endl;
}
};
class CuriousTabDerived : public CuriousTabBase
{
private:
CuriousTabBase objBase;
public:
CuriousTabDerived(int xx, int yy) : CuriousTabBase(xx, yy)
{
objBase.Show();
}
};
int main()
{
CuriousTabDerived objDev(10, 20);
return 0;
}
Open
View answer
C++ object state tampering via pointer cast: trace the prints before and after writing into the object memory.
#include<iostream.h>
class CuriousTabTeam
{
int x, y;
public:
CuriousTabTeam(int xx)
{
x = ++xx;
}
void Display()
{
cout<< --x << " ";
}
};
int main()
{
CuriousTabTeam objBT(45);
objBT.Display();
int p = (int)&objBT;
p = 23;
objBT.Display();
return 0;
}
Open
View answer
Practice smarter
Solve a few questions daily and revisit weak topics regularly to improve accuracy.