Classes And Objects In C Plus Plus
Name: _____________________
Date: _____________________
Instructions: Answer all questions. Write your answers clearly in the space provided.
Operator overloading is . . . . . . . .
What do we need to do to pointer for overloading the subscript operator?
Pick out the compound assignment statement.
Identify the correct statement.
What is the use of function call operator?
Pick the incorrect statement about inline functions in C++?
What is a class in C++?
Which keyword is used to create an object of a class in C++?
What is an object in C++?
What is the purpose of a constructor in C++?
Which access specifier allows members of a class to be accessed from outside the class in C++?
What is the correct syntax to define a member function outside the class in C++?
What is the process of combining data and functions into a single unit called in C++?
What does the 'this' pointer refer to in C++?
Which keyword is used to declare a member function of a class as constant in C++?
What is the purpose of the 'static' keyword in C++ class members?
Which is used to tell the computer that where a pointer is pointing to?
What is the scope of typedef defined data types?
Which is called ternary operator?
How the objects are self-referenced in a member function of that class.
How many objects can present in a single class?
In which form does the function call operator can be overloaded?
Which is the correct example of a binary operator?
Which of the following operator can be used to overload when that function is declared as friend function?
The data members and functions of a class in C++ are by default . . . . . . . .
How many arguments will the subscript operator will take for overloading?
How are types therein user-defined conversion?
What is string objects in C++?
Pick out the other definition of objects.
Where does keyword ‘friend’ should be placed?
What is a binary operator?
Which option is best to eliminate the memory problem?
Pick out the correct statement.
What do we need to use when we have multiple subscripts?
Which of the following operator cannot be used to overload when that function is declared as friend function?
In Linux, how do the heaps and stacks are managed?
Which of the following is not a function of complex values?
subscript operator is used to access which elements?
Which keyword is used to represent a friend function?
What is Character-Array?
Pick the incorrect statements out of the following.
Which keyword is used to define the user defined data types?
Which of the following is correct about friend functions?
Which function is used to get the length of a string object?
How many types of user-defined data type are in c++?
Which is the correct statement about operator overloading?
Which other keywords are also used to declare the class other than class?
Inline functions are avoided when . . . . . . . .
What is a friend function in C++?
Pick the correct statement.
How to declare operator function?
What is the use of functor?
How many types of representation are in the string?
Which header file is used to declare the complex number?
Pick the other name of operator function.
Which header file is used to include the string object functions in C++?
Constructors are used to . . . . . . . .
What is the header file for the string class?
Pick the incorrect statement about Character-Array.
What does the data type defined by union will do?
What is the name of | operator?
In which direction does the assignment operation will take place?
Which category of data type a class belongs to?
Which operator works only with integer variables?
How many approaches are used for operator overloading?
What is the syntax of overloading operator + for class A?
Which of the following operator can be overloaded?
How to store the large objects in c++ if it extends its allocated memory?
How to declare the complex number?
Which container in c++ will take large objects?
How many types of models are available to create the user-defined data type?
Which concepts does the Pre Increment use?
Which of the following is not a modifier function in string class?
In the case of friend operator overloaded functions how many maximum object arguments a unary operator overloaded function can take?
Which of the following operators can't be overloaded?
Which of the following statements is NOT valid about operator overloading?
Where does the object is created?
What does the dereference operator will return?
What is the return type of the conversion operator?
Which functions of a class are called inline functions?
What will happen when the function call operator is overloaded?
How many real types are there in complex numbers?
Which of these following members are not accessed by using direct member access operator?
When struct is used instead of the keyword class means, what will happen in the program?
Pick out the correct option.
What is the identifier given to string class to declare string objects?
Which is used to do the dereferencing?
When we are using heap operations what do we need to do to save the memory?
Which method do we use to append more than one character at a time?
How many specifiers are present in access specifiers in class?
How to access the object in the class?
In case of non-static member functions how many maximum object arguments a unary operator overloaded function can take?
Which keyword is used to make a member function constant in C++?
What is the difference between a constructor and a normal member function in C++?
What is the purpose of the keyword 'explicit' before a constructor in C++?
Which keyword is used to prevent inheritance in C++?
What is the access specifier that allows members of a class to be accessed from anywhere in the program in C++?
What is the difference between a shallow copy and a deep copy in C++?
How many parameters does a conversion operator may take?
What is operator overloading in C++?
Which of the following operator cannot be overloaded?
Which is used to define the member of a class externally?
Which operator a pointer object of a class uses to access its data members and member functions?
Which is the correct example of a unary operator?
In case of non-static member functions how many maximum object arguments a binary operator overloaded function can take?
Which type of function is used to destroy objects in C++?
What is the default access specifier for members of a class in C++?
Which operator is used to access members of an object in C++?
What is the difference between a constructor and a destructor in C++?
What is the purpose of the destructor in C++?
Which keyword is used to declare a class in C++?
What is the purpose of the static member variable in C++?
Which operator is used to access members of a class through a pointer in C++?
What is the purpose of using friend functions in C++?
Which access specifier allows members of a class to be accessed only by the derived classes in C++?
What does a class in C++ holds?
What is the size of the heap?
Pick the correct statement about string objects in C++.
Why we use the "dynamic_cast" type conversion?
Which is used to return the number of characters in the string?
Pick out the correct syntax of operator conversion.
What are the essential operators in c++?
What is the syntax of user-defined data types?
Which is used to pass the large objects in c++?
How to stop your program from eating so much ram?
What does a mutable member of a class mean?
In the case of friend operator overloaded functions how many maximum object arguments a binary operator overloaded function can take?
How many types are there in increment/decrement operator?
Which operator is having the highest precedence in c++?
How to unlimit the size of the stack?
What is the term for a function that is declared inside a class but defined outside of it in C++?
What is the process of wrapping data and functions into a single unit called in C++?
What is the purpose of the 'this' pointer in C++?
What is the purpose of using constructors in C++?
What is a constructor in C++?
Which keyword is used to create a copy constructor in C++?
What is the purpose of a copy constructor in C++?
What happens if a class does not explicitly declare any constructors in C++?
What is a destructor in C++?
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main(int argc, char const *argv[])
{
string str
cin>>str
cout<<str
return 0
}
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()
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main ()
{
string str ("helloworld.")
cout << str.substr(3).substr(4) << endl
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main(int argc, char const *argv[])
{
char str[] = "Hello World"
cout<<str[0]
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int main()
{
int a = 0
int b = 10
if ( a && b )
{
cout << "true"<< endl
}
else
{
cout << "false"<< endl
}
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int main()
{
typedef int num
num a = 10, b = 15
num c = a + b + a - b
cout << c
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class B
{
int b
public:
B(int i){
b = i
}
}
class C
{
B b
public:
C(int i){
b = B(i)
}
friend void show()
}
void show()
{
C c(10)
cout<<"value of b is: "<<c.b.b<<endl
}
int main(int argc, char const *argv[])
{
show()
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main ()
{
string str ("nobody does like this")
string key ("nobody")
size_t f
f = str.rfind(key)
if (f != string::npos)
str.replace (f, key.length(), "everybody")
cout << str << endl
return 0
}
What is the correct syntax of accessing a static member of a Class? ---------------------------
Example class:
class A
{
public:
static int value
}
---------------------------
What will be the output of the following C++ code? #include <iostream>
using namespace std
class myclass
{
public:
int i
myclass *operator->()
{return this
}
}
int main()
{
myclass ob
ob->i = 10
cout << ob.i << " " << ob->i
return 0
}
What happens when objects s1 and s2 are added? string s1 = "Hello"
string s2 = "World"
string s3 = (s1+s2).substr(5)
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class test
{
public:
operator string ()
{
return "Converted"
}
}
int main()
{
test t
string s = t
cout << s << endl
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
#include <cstring>
using namespace std
int main()
{
string s('a')
cout<<s
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class sample
class sample1
{
int width, height
public:
int area ()
{
return (width * height)
}
void convert (sample a)
}
class sample
{
private:
int side
public:
void set_side (int a)
{
side = a
}
friend class sample1
}
void sample1::convert (sample a)
{
width = a.side
height = a.side
}
int main ()
{
sample sqr
sample1 rect
sqr.set_side(6)
rect.convert(sqr)
cout << rect.area()
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class Rect
{
int x, y
public:
void set_values (int,int)
int area ()
{
return (x * y)
}
}
void Rect::set_values (int a, int b)
{
x = a
y = b
}
int main ()
{
Rect recta, rectb
recta.set_values (5, 6)
rectb.set_values (7, 6)
cout << "recta area: " << recta.area()
cout << "rectb area: " << rectb.area()
return 0
}
Which access specifier allows members of a class to be accessed only by member functions of the same class and its derived classes in C++?
What will be the output of the following C++ code? #include <iostream>
using namespace std
class numbers
{
private:
int m_nValues[10]
public:
int& operator[] (const int nValue)
}
int& numbers::operator[](const int nValue)
{
return m_nValues[nValue]
}
int main()
{
numbers N
N[5] = 4
cout << N[5]
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int main()
{
typedef int num
typedef char let
let w = "steve"
num a = 10, b = 15
num c = a + w
cout << c
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main(int argc, char const *argv[])
{
char str[10]
cin>>str
cout<<str
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int main()
{
double arr[] = {5.0, 6.0, 7.0, 8.0}
double *p = (arr+2)
cout << *p << endl
cout << arr << endl
cout << *(arr+3) << endl
cout << *(arr) << endl
cout << *arr+9 << endl
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class B
{
int b
public:
B(){}
B(int i){
b = i
}
int show(){
return b
}
}
class C
{
B b
public:
C(int i){
b = B(i)
}
friend void show(){
C c(10)
cout<<"value of b is: "<<c.b.show()<<endl
}
}
int main(int argc, char const *argv[])
{
C c(1)
c.show()
return 0
}
Which is the correct way of concatenating a character at the end of a string object? way 1:
string s
s = s + 'a'
way 2:
string s
s.push_back('a')
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main(int argc, char const *argv[])
{
string s1 = "Hello"
string s2 = "World"
string s3 = s1 + " " + s2
cout<<s3
return 0
}
Which special character is used to mark the end of class?
What will be the output of the following C++ code? #include <stdio.h>
#include<iostream>
using namespace std
int main()
{
int x = 5, y = 5
cout << ++x << --y << endl
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class base
{
int val1, val2
public:
int get()
{
val1 = 100
val2 = 300
}
friend float mean(base ob)
}
float mean(base ob)
{
return float(ob.val1 + ob.val2) / 2
}
int main()
{
base obj
obj.get()
cout << mean(obj)
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class complex
{
int i
int j
public:
complex(int a, int b)
{
i = a
j = b
}
complex operator+(complex c)
{
complex temp
temp.i = this->i + c.i
temp.j = this->j + c.j
return temp
}
void show(){
cout<<"Complex Number: "<<i<<" + i"<<j<<endl
}
}
int main(int argc, char const *argv[])
{
complex c1(1,2)
complex c2(3,4)
complex c3 = c1 + c2
c3.show()
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class sample1
{
float i, j
}
class sample2
{
int x, y
public:
sample2 (int a, int b)
{
x = a
y = b
}
int result()
{
return x + y
}
}
int main ()
{
sample1 d
sample2 * padd
padd = (sample2*) &d
cout<< padd->result()
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
#include <cstring>
using namespace std
int main(int argc, char const *argv[])
{
const char *a = "Hellox00World"
cout<<a
return 0
}
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
}
What will be the output of the following C++ code? #include <iostream>
#include <complex>
using namespace std
int main ()
{
complex<double> mycomplex (20.0, 2.0)
cout << imag(mycomplex) << endl
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class Integer
{
int i
public:
Integer(int ii) : i(ii) {}
const Integer
operator+(const Integer& rv) const
{
cout << "operator+" << endl
return Integer(i + rv.i)
}
Integer&
operator+=(const Integer& rv)
{
cout << "operator+=" << endl
i += rv.i
return *this
}
}
int main()
{
int i = 1, j = 2, k = 3
k += i + j
Integer ii(1), jj(2), kk(3)
kk += ii + jj
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
ostream & operator<<(ostream & i, int n)
{
return i
}
int main()
{
cout << 5 << endl
cin.get()
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class Box{
int capacity
bool operator<(Box b){
return this->capacity < b.capacity ? true : false
}
public:
Box(){}
Box(double capacity){
this->capacity = capacity
}
}
int main(int argc, char const *argv[])
{
Box b1(10)
Box b2 = Box(14)
if(b1 < b2){
cout<<"Box 2 has large capacity."
}
else{
cout<<"Box 1 has large capacity."
}
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int main ()
{
int a, b
a = 10
b = 4
a = b
b = 7
cout << "a:"
cout << a
cout << " b:"
cout << b
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main ()
{
std::string str ("Example.")
str.back() = '!'
std::cout << str << endl
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class rect
{
int x, y
public:
void val (int, int)
int area ()
{
return (x * y)
}
}
void rect::val (int a, int b)
{
x = a
y = b
}
int main ()
{
rect rect
rect.val (3, 4)
cout << "rect area: " << rect.area()
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class Box
{
double length
double breadth
double height
public:
double getVolume(void)
{
return length * breadth * height
}
void setLength( double len )
{
length = len
}
void setBreadth( double bre )
{
breadth = bre
}
void setHeight( double hei )
{
height = hei
}
Box operator+(const Box& b)
{
Box box
box.length = this->length + b.length
box.breadth = this->breadth + b.breadth
box.height = this->height + b.height
return box
}
}
int main( )
{
Box Box1
Box Box2
Box Box3
double volume = 0.0
Box1.setLength(6.0)
Box1.setBreadth(7.0)
Box1.setHeight(5.0)
Box2.setLength(12.0)
Box2.setBreadth(13.0)
Box2.setHeight(10.0)
volume = Box1.getVolume()
cout << "Volume of Box1 : " << volume <<endl
volume = Box2.getVolume()
cout << "Volume of Box2 : " << volume <<endl
Box3 = Box1 + Box2
volume = Box3.getVolume()
cout << "Volume of Box3 : " << volume <<endl
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main ()
{
string str ("Ubuntu")
cout << str.capacity()
cout << str.max_size()
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
void duplicate (int& a, int& b, int& c)
{
a *= 2
b *= 2
c *= 2
}
int main ()
{
int x = 1, y = 3, z = 7
duplicate (x, y, z)
cout << x << y << z
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int main()
{
int a = 20
int b = 10
int c = 15
int d = 5
int e
e = a + b * c / d
cout << e << endl
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <cstring>
using namespace std
int main ()
{
char str1[10] = "Hello"
char str2[10] = "World"
char str3[10]
int len
strcpy( str3, str1)
strcat( str1, str2)
len = strlen(str1)
cout << len << endl
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class Box{
int capacity
public:
Box(){}
Box(double capacity){
this->capacity = capacity
}
bool operator<(Box b){
return b.capacity < this->capacity? true : false
}
}
int main(int argc, char const *argv[])
{
Box b1(10)
Box b2 = Box(14)
if(b1 < b2){
cout<<"B1's capacity is small"
}
else{
cout<<"B2's capacity is small"
}
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class complex
{
int i
int j
public:
complex(){}
complex(int a, int b)
{
i = a
j = b
}
complex operator+(complex c)
{
complex temp
temp.i = this->i + c.i
temp.j = this->j + c.j
return temp
}
void operator+(complex c)
{
complex temp
temp.i = this->i + c.i
temp.j = this->j + c.j
temp.show_poss()
}
void show(){
cout<<"Complex Number: "<<i<<" + i"<<j<<endl
}
void show_poss(){
cout<<"Your result after addition will be: "<<i<<" + i"<<j<<endl
}
}
int main(int argc, char const *argv[])
{
complex c1(1,2)
complex c2(3,4)
c1 + c2
return 0
}
What will be the output of the following C++ code? #include <stdio.h>
#include<iostream>
using namespace std
int main()
{
int x = 5, y = 5, z
x = ++x
y = --y
z = x++ + y--
cout << z
return 0
}
What will be the output of the following C++ code? #include <stdio.h>
#include<iostream>
using namespace std
int main()
{
int num1 = 5
int num2 = 3
int num3 = 2
num1 = num2++
num2 = --num3
cout << num1 << num2 << num3
return 0
}
Which of the following is correct way of concatenating two string objects in C++? way 1:
string s1 = "hello"
string s2 = "world"
string s3 = s1 + s2
way 2:
string s1 = "hello"
string s2 = "world"
string s3 = s1.append(s2)
way 3:
string s1 = "hello"
string s2 = "world"
string s3 = strcat(s1,s2)
What will be the output of the following C++ code? #include <iostream>
using namespace std
class sample
{
private:
int a, b
public:
void test()
{
a = 100
b = 200
}
friend int compute(sample e1)
}
int compute(sample e1)
{
return int(e1.a + e1.b) - 5
}
int main()
{
sample e
e.test()
cout << compute(e)
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <complex>
using namespace std
int main()
{
complex<double> c1(4.0,3.0)
cout << "c1: " << c1
complex<float> c2(polar(5.0,0.75))
cout << c1 + complex<double>(c2.real(),c2.imag())
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int main()
{
int x
int *p
x = 5
p = &x
cout << *p
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int main()
{
int i
enum month
{
JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,DEC
}
for (i = JAN
i <= DEC
i++)
cout << i
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int main()
{
int a, b
int* c
c = &a
a = 200
b = 200
*c = 100
b = *c
cout << *c << " " << b
return 0
}
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
}
What will be the output of the following C++ code if the string entered by the user is "Hello World"? #include <iostream>
#include <string>
using namespace std
int main(int argc, char const *argv[])
{
string str
cin>>str
cout<<str
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int main()
{
double a = 21.09399
float b = 10.20
int c
c = (int) a
cout << c
c = (int) b
cout << c
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main ()
{
string str ("microsoft")
string::reverse_iterator r
for (r = str.rbegin()
r < str.rend()
r++ )
cout << *r
return 0
}
Given the following C++ code. How would you define the < operator for Box class so that when boxes b1 and b2 are compared in if block the program gives correct result? #include <iostream>
#include <string>
using namespace std
class Box
{
int capacity
public:
Box(){}
Box(double capacity){
this->capacity = capacity
}
}
int main(int argc, char const *argv[])
{
Box b1(10)
Box b2 = Box(14)
if(b1 < b2){
cout<<"Box 2 has large capacity."
}
else{
cout<<"Box 1 has large capacity."
}
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class sample
{
public:
sample(int i) : m_i(i) { }
public:
int operator()(int i = 0) const
{
return m_i + i
}
operator int () const
{
return m_i
}
private:
int m_i
friend int g(const sample&)
}
int f(char c)
{
return c
}
int main()
{
sample f(2)
cout << f(2)
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main ()
{
string str ("example.")
str.front() = 'E'
cout << str << endl
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <complex>
using namespace std
int main()
{
complex<int> i(2, 3)
i = i * 6 / 3
cout << i
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class sample
{
public:
sample()
{
cout << "X::X()" << endl
}
sample( sample const & )
{
cout << "X::X( X const & )" << endl
}
sample& operator=( sample const & )
{
cout << "X::operator=(X const &)" << endl
}
}
sample f()
{
sample tmp
return tmp
}
int main()
{
sample x = f()
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int main ()
{
int a, b, c
a = 2
b = 7
c = (a > b) ? a : b
cout << c
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class Distance
{
private:
int feet
int inches
public:
Distance()
{
feet = 0
inches = 0
}
Distance(int f, int i)
{
feet = f
inches = i
}
Distance operator()(int a, int b, int c)
{
Distance D
D.feet = a + c + 10
D.inches = b + c + 100
return D
}
void displayDistance()
{
cout << feet << inches << endl
}
}
int main()
{
Distance D1(11, 10), D2
cout << "First Distance : "
D1.displayDistance()
D2 = D1(10, 10, 10)
cout << "Second Distance :"
D2.displayDistance()
return 0
}
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
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class Box
{
int capacity
public:
Box(int cap){
capacity = cap
}
friend void show()
}
void Box::show()
{
Box b(10)
cout<<"Value of capacity is: "<<b.capacity<<endl
}
int main(int argc, char const *argv[])
{
show()
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main ()
{
string str ("steve jobs is legend")
string::iterator it
str.erase (str.begin()+ 5, str.end()-7)
cout << str << endl
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class A
{
public:
int x
A(int n = 0) : x(n) {}
int& operator[](int n)
{
cout << "0"
return x
}
int operator[](int n) const
{
cout << "1"
return x
}
}
void foo(const A& a)
{
int z = a[2]
}
int main()
{
A a(7)
a[3] = 8
int z = a[2]
foo(a)
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class Box
{
double width
public:
friend void printWidth( Box box )
void setWidth( double wid )
}
void Box::setWidth( double wid )
{
width = wid
}
void printWidth( Box box )
{
box.width = box.width * 2
cout << "Width of box : " << box.width << endl
}
int main( )
{
Box box
box.setWidth(10.0)
printWidth( box )
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class Complex
{
private:
float real
float imag
public:
Complex():real(0), imag(0){}
Complex operator ()(float re, float im)
{
real += re
imag += im
return *this
}
Complex operator() (float re)
{
real += re
return *this
}
void display()
{
cout << "(" << real << "," << imag << ")" << endl
}
}
int main()
{
Complex c1, c2
c2 = c1(3.2, 5.3)
c1(6.5, 2.7)
c2(1.9)
cout << "c2="
c1.display()
cout << "c2="
c2.display()
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <complex>
using namespace std
int main()
{
complex<double> c1(4.0, 3.0)
complex<float> c2(polar(5.0, 0.75))
cout << (c1 += sqrt(c1)) << endl
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class Box
{
int capacity
Box(){}
Box(double capacity){
this->capacity = capacity
}
}
int main(int argc, char const *argv[])
{
Box b1(10)
Box b2 = Box(14)
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class B
{
int b
public:
B(){}
B(int i){
b = i
}
int show(){
return b
}
}
class C
{
B b
public:
C(int i){
b = B(i)
}
friend void show(){
C c(10)
cout<<"value of b is: "<<c.b.show()<<endl
}
}
int main(int argc, char const *argv[])
{
show()
return 0
}
Give the function prototype of the operator function which we need to define in this program so that the program has no errors. #include <iostream>
#include <string>
using namespace std
class Box{
int capacity
public:
Box(){}
Box(double capacity){
this->capacity = capacity
}
}
int main(int argc, char const *argv[])
{
Box b1(10)
Box b2 = Box(14)
if(b1 == b2){
cout<<"Equal"
}
else{
cout<<"Not Equal"
}
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <cmath>
using namespace std
class Complex
{
private:
double real
double imag
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i)
{}
double mag()
{
return getMag()
}
operator double ()
{
return getMag()
}
private:
double getMag()
{
return sqrt(real * real + imag * imag)
}
}
int main()
{
Complex com(3.0, 4.0)
cout << com.mag()
cout << com
return 0
}
What is the associativity of add(+)
?
What will be the output of the following C++ code? #include <iostream>
using namespace std
class number
{
int i
public:
int geti()
void puti(int j)
}
int number::geti()
{
return i
}
void number::puti(int j)
{
i = j
}
int main()
{
number s
s.puti(10)
cout << s.geti( )
return 0
}
How many member functions are there in this C++ class excluding constructors and destructors? class Box
{
int capacity
public:
void print()
friend void show()
bool compare()
friend bool lost()
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main ()
{
string str = "HelloWorld!"
cout<<str.capacity()
cout<<str.size()
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
#include <cstring>
using namespace std
int main(int argc, char const *argv[])
{
string s("a")
cout<<s
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main(int argc, char const *argv[])
{
char s1[6] = "Hello"
char s2[6] = "World"
char s3[12] = s1 + " " + s2
cout<<s3
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class A
{
static int a
public:
A()
{
cout<<"Object of A is created
"
}
void show()
{
a++
cout<<"a: "<<a<<endl
}
}
class B
{
public:
}
int main(int argc, char const *argv[])
{
A a1, a2
A a3 = a1 + a2
return 0
}
#include <iostream>
using namespace std
class three_d
{
int x, y, z
public:
three_d() { x = y = z = 0
}
three_d(int i, int j, int k) { x = i
y = j
z = k
}
three_d operator()(three_d obj)
three_d operator()(int a, int b, int c)
friend ostream &operator<<(ostream &strm, three_d op)
}
three_d three_d::operator()(three_d obj)
{
three_d temp
temp.x = (x + obj.x) / 2
temp.y = (y + obj.y) / 2
temp.z = (z + obj.z) / 2
return temp
}
three_d three_d::operator()(int a, int b, int c)
{
three_d temp
temp.x = x + a
temp.y = y + b
temp.z = z + c
return temp
}
ostream &operator<<(ostream &strm, three_d op) {
strm << op.x << ", " << op.y << ", " << op.z << endl
return strm
}
int main()
{
three_d objA(1, 2, 3), objB(10, 10, 10), objC
objC = objA(objB(100, 200, 300))
cout << objC
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class sample
{
private:
int* i
int j
public:
sample (int j)
~sample ()
int& operator [] (int n)
}
int& sample::operator [] (int n)
{
return i[n]
}
sample::sample (int j)
{
i = new int [j]
j = j
}
sample::~sample ()
{
delete [] i
}
int main ()
{
sample m (5)
m [0] = 25
m [1] = 20
m [2] = 15
m [3] = 10
m [4] = 5
for (int n = 0
n < 5
++ n)
cout << m [n]
return 0
}
What will be the output of the following C++ code? #include <stdio.h>
#include<iostream>
using namespace std
int main()
{
int x = 5, y = 5, z
x = ++x
y = --y
z = x + ++x
cout << z
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class B
{
int b
public:
B(){}
B(int i){
b = i
}
int show(){
return b
}
}
class C
{
B b
public:
C(int i){
b = B(i)
}
friend void show()
}
void show()
{
C c(10)
cout<<"value of b is: "<<c.b.show()<<endl
}
int main(int argc, char const *argv[])
{
show()
return 0
}
What will be the output of the following C++ code? #include using namespace std
class CDummy
{
public:
int isitme (CDummy& param)
}
int CDummy::isitme (CDummy& param)
{
if (¶m == this)
return true
else
return false
}
int main ()
{
CDummy a
CDummy *b = &a
if (b->isitme(a))
{
cout << "execute"
}
else
{
cout<<"not execute"
}
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class A
{
static int a
public:
void show()
{
a++
cout<<"a: "<<a<<endl
}
}
int A::a = 5
int main(int argc, char const *argv[])
{
A a
return 0
}
Which operator should be overloaded in the following code to make the program error free? #include <iostream>
#include <string>
using namespace std
class Box{
int capacity
public:
Box(){}
Box(double capacity){
this->capacity = capacity
}
}
int main(int argc, char const *argv[])
{
Box b1(10)
Box b2 = Box(14)
if(b1 == b2){
cout<<"Equal"
}
else{
cout<<"Not Equal"
}
return 0
}
Which of the following is a valid class declaration?
What will be the output of the following C++ code? #include <iostream>
#include <complex>
using namespace std
int main()
{
complex<double> c1(4.0, 16.0), c2
c2 = pow(c1, 2.0)
cout << c2
return 0
}
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
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int main ()
{
int a
int * ptr_b
int ** ptr_c
a = 1
ptr_b = &a
ptr_c = &ptr_b
cout << a << "
"
cout << *ptr_b << "
"
cout << *ptr_c << "
"
cout << **ptr_c << "
"
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class sample
{
private:
int var
public:
void input()
{
cout << var
}
void output()
{
cout << "Variable entered is "
cout << var << "
"
}
}
int main()
{
sample object
object.input()
object.output()
object.var()
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class Box
{
int capacity
public:
Box(int cap){
capacity = cap
}
friend void show()
}
void show()
{
Box b(10)
cout<<"Value of capacity is: "<<b.capacity<<endl
}
int main(int argc, char const *argv[])
{
show()
return 0
}
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()
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
int main ()
{
string str ("Microsoft")
for (size_t i = 0
i < str.length()
)
{
cout << str.at(i-1)
}
return 0
}
What will be the output of the following C++ code? #include <stdio.h>
#include<iostream>
using namespace std
int main()
{
int a = 21
int c
c = a++
cout << c
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
const int SIZE = 10
class safe
{
private:
int arr[SIZE]
public:
safe()
{
register int i
for (i = 0
i < SIZE
i++)
{
arr[i] = i
}
}
int &operator[](int i)
{
if (i > SIZE)
{
cout << "Index out of bounds" <<endl
return arr[0]
}
return arr[i]
}
}
int main()
{
safe A
cout << A[5]
cout << A[12]
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class sample
{
public:
int x, y
sample() {}
sample(int, int)
sample operator + (sample)
}
sample::sample (int a, int b)
{
x = a
y = b
}
sample sample::operator+ (sample param)
{
sample temp
temp.x = x + param.x
temp.y = y + param.y
return (temp)
}
int main ()
{
sample a (4,1)
sample b (3,2)
sample c
c = a + b
cout << c.x << "," << c.y
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class A
{
static int a
public:
void show()
{
a++
cout<<"a: "<<a<<endl
}
void operator.()
{
cout<<"Objects are added
"
}
}
class B
{
public:
}
int main(int argc, char const *argv[])
{
A a1, a2
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int main()
{
int x = 9
int* p = &x
cout << sizeof(p)
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
const int limit = 4
class safearray
{
private:
int arr[limit]
public:
int& operator [](int n)
{
if (n == limit - 1)
{
int temp
for (int i = 0
i < limit
i++)
{
if (arr[n + 1] > arr[n])
{
temp = arr[n]
arr[n] = arr[n + 1]
arr[n + 1] = temp
}
}
}
return arr[n]
}
}
int main()
{
safearray sa1
for(int j = 0
j < limit
j++)
sa1[j] = j*10
for(int j = 0
j < limit
j++)
{
int temp = sa1[j]
cout << "Element " << j << " is " << temp
}
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <string>
using namespace std
class complex
{
int i
int j
public:
complex(){}
complex(int a, int b)
{
i = a
j = b
}
complex operator+(complex c)
{
complex temp
temp.i = this->i + c.i
temp.j = this->j + c.j
return temp
}
void show(){
cout<<"Complex Number: "<<i<<" + i"<<j<<endl
}
}
int main(int argc, char const *argv[])
{
complex c1(1,2)
complex c2(3,4)
complex c3 = c1 + c2
c3.show()
return 0
}
What will be the output of the following C++ code? #include <iostream>
#include <complex>
using namespace std
int main()
{
complex<double> c_double(2, 3)
complex<int> c_int(4, 5)
c_double *= 2
c_double = c_int
cout << c_double
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
int operate (int a, int b)
{
return (a * b)
}
float operate (float a, float b)
{
return (a / b)
}
int main ()
{
int x = 5, y = 2
float n = 5.0, m = 2.0
cout << operate (x, y)
cout << operate (n, m)
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class Box
{
public :
double length
double breadth
double height
}
int main( )
{
Box Box1
double volume
Box1.height = 5
Box1.length = 6
Box1.breadth = 7.1
volume = Box1.height * Box1.length * Box1.breadth
cout << "Volume of Box1 : " << volume <<endl
return 0
}
What will be the output of the following C++ code? #include <iostream>
using namespace std
class sample
{
int width, height
public:
void set_values (int, int)
int area () {return (width * height)
}
friend sample duplicate (sample)
}
void sample::set_values (int a, int b)
{
width = a
height = b
}
sample duplicate (sample rectparam)
{
sample rectres
rectres.width = rectparam.width * 2
rectres.height = rectparam.height * 2
return (rectres)
}
int main ()
{
sample rect, rectb
rect.set_values (2, 3)
rectb = duplicate (rect)
cout << rectb.area()
return 0
}
Answer Key
it is a keyword used to allow a specific external function or class to access private or protected members of another class. It doesn’t define a general access level. Conclusion: The protected access specifier is the one that allows class members to be accessed within the same class and its derived classes. Hence, the correct answer is Option A: protected .