0% found this document useful (0 votes)
47 views

C++ Questions

The document contains questions and answers related to C++ concepts like operator overloading, class and objects, access specifiers etc. Some key points covered are - operators that can and cannot be overloaded, how objects are created and accessed in a class, difference between private, protected and public access specifiers.

Uploaded by

Bhavya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

C++ Questions

The document contains questions and answers related to C++ concepts like operator overloading, class and objects, access specifiers etc. Some key points covered are - operators that can and cannot be overloaded, how objects are created and accessed in a class, difference between private, protected and public access specifiers.

Uploaded by

Bhavya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Q 1 - Choose the operator which cannot be overloaded.

A-/

B - ()

C - ::

D-%

Answer : C

Explaination

Scope resolution (::) is not permitted to be overloaded.

Q 2 - What is the output of the following program?

#include<iostream>

using namespace std;


class abc {

public:
int i;

abc(int i) {
i = i;
}
};

main() {
abc m(5);

cout<<m.i;
}

A-5

B - Garbage

C - Error at the statement i=i;

D - Compile error: ‘i’ declared twice.

Answer : B

Explaination

i=i, is assigning member variable to itself.

#include<iostream>
using namespace std;
class abc {

public:
int i;

abc(int i) {
i = i;
}
};

main() {
abc m(5);

cout<<m.i;
}

Q 3 - A protected member of the class in accessible in

A - Only same class

B - Same class and derived class

C - Outside the class

D - None of the above.

Answer : B

Explaination

Only members of the derived class and the same class can access a protected member.

Q 4 - How can we make an class act as an interface in C++?

A - By only providing all the functions as virtual functions in the class.

B - Defining the class following with the keyword virtual

C - Defining the class following with the keyword interface

D - Defining the class following with the keyword abstract

Answer : A

Explaination

There are no keywords in C++ such as abstract and interface.

Q 5 - What is the output of the following program?


#include<iostream>

using namespace std;


main() {
int *p = new int;
delete p;
delete p;
cout<<"Done";
}

A - Done

B - Compile error

C - Runtime error

D - None of the above

Answer : C

Explaination

It is invalid to release memory more than once.

#include<iostream>

using namespace std;


main() {
int *p = new int;
delete p;
delete p;
cout<<"Done";
}

Oues 6: What is the output of the following program?

#include<iostream>

using namespace std;


main() {
short unsigned int i = 0;

cout<<i--;
}

A-0

B - Compile error

C - 65535

D - 32767
Answer : A

Explaination

0, with post decrement operator value of the variable will be considered as the expression’s
value and later gets decremented

#include<iostream>

using namespace std;


main() {
short unsigned int i = 0;

cout<<i--;
}

Ques 7: What is the output of the following program?

#include<iostream>

using namespace std;


main() {
int r, x = 2;

float y = 5;

r = y%x;
cout<<r;
}

A-1

B-0

C-2

D - Compile error

Answer : D

Explaination

Answer Compile Error, It is invalid that either of the operands for the modulus operator (%) is a real
number.

Ques 8. Where does the object is created?


a) class
b) constructor
c) destructor
d) attributes
Answer: a
Explanation: In class, only all the listed items except class will be declared.

Ques 9. How to access the object in the class?


a) scope resolution operator
b) ternary operator
c) direct member access operator
d) resolution operator

Answer: c
Explanation: Objects in the method can be accessed using direct member access operator which
is (.).

Ques 10. Which of these following members are not accessed by using direct member access
operator?
a) public
b) private
c) protected
d) both private & protected

Answer: d
Explanation: Because of the access is given to the private and protected, We can’t access them
by using direct member access operator.

Ques 11 Pick out the other definition of objects.


a) member of the class
b) associate of the class
c) attribute of the class
d) instance of the class

Answer: d
Explanation: An Object represents an instance of a class i.e. a variable of that class type having
access to its data members and member functions from outside if allowed.

Ques 12 How many objects can present in a single class?


a) 1
b) 2
c) 3
d) as many as possible

Answer: d
Explanation: Because a class may contain any number of objects according to its compliance.

Ques 13 Pick the other name of operator function.


a) function overloading
b) operator overloading
c) member overloading
d) object overloading

Answer: b
Explanation: Operator function means operation defined for that operator so if user defines a
function for an operator then that is called operator overloading i.e. overloading already present
operator function.
Ques 14 Which of the following operators can’t be overloaded?
a) ::
b) +
c) –
d) []

Answer: a
Explanation: :: operator cannot be overloaded because this operator operates on names rather
than values and C++ has no syntax for writing codes that works on names than values so using
syntax these operators cannot be overloaded.

Ques 15 How to declare operator function?


a) operator sign
b) operator
c) name of the operator
d) name of the class

Answer: a
Explanation: We have to declare the operator function by using the operator, operator sign.
Example “operator +” where the operator is a keyword and + is the symbol need to be
overloaded.

Ques 16. Which of the following statements is NOT valid about operator overloading?
a) Only existing operators can be overloaded
b) The overloaded operator must have at least one operand of its class type
c) The overloaded operators follow the syntax rules of the original operator
d) None of the mentioned

Answer: d
Explanation: The overloaded operator must not have at least one operand of its class type.

You might also like