AEC OOPS Study Material

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

Subject: Ability enhancement Course : Programming in C++ Code:

Sem : III Date: 28/12/2022


Practice Questionnaires

1. Which of the following is the default return value of functions in C++?


a) int
b) char
c) float
d) void

Answer: a
Explanation: C++ uses int as the default return values for functions. It also restricts that the
return type of the main function must be int.
2. What happens to a function defined inside a class without any complex operations (like
looping, a large number of lines, etc)?
a) It becomes a virtual function of the class
b) It becomes a default calling function of the class
c) It becomes an inline function of the class
d) The program gives an error

Answer: c
Explanation: Any function which is defined inside a class and has no complex operations like
loops, a large number of lines then it is made inline.
3. What is an inline function?
a) A function that is expanded at each call during execution
b) A function that is called during compile time
c) A function that is not checked for syntax errors
d) A function that is not checked for semantic analysis

Answer: a
Explanation: Inline function is those which are expanded at each call during the execution of the
program to reduce the cost of jumping during execution.
4. An inline function is expanded during ______________
a) compile-time
b) run-time
c) never expanded
d) end of the program
View Answer
Answer: a
Explanation: An inline function is expanded during the compile-time of a program.
5. In which of the following cases inline functions may not word?
i) If the function has static variables.
ii) If the function has global and register variables.
iii) If the function contains loops
iv) If the function is recursive
a) i, iv
b) iii, iv
c) ii, iii, iv
d) i, iii, iv
View Answer
Answer: d
Explanation: A function is not inline if it has static variables, loops or the function is having any
recursive calls.
6. This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on
“Constructors and Destructors – 1”.

1. What is the role of a constructor in classes?


a) To modify the data whenever required
b) To destroy an object
c) To initialize the data members of an object when it is created
d) To call private functions from the outer world

Answer: c
Explanation: A constructor is used in classes to initialize data members of class in order to avoid
errors/segmentation faults.
7. Why constructors are efficient instead of a function init() defined by the user to initialize the
data members of an object?
a) Because user may forget to call init() using that object leading segmentation fault
b) Because user may call init() more than once which leads to overwriting values
c) Because user may forget to define init() function
d) All of the mentioned

Answer: d
Explanation: We cannot use init() because as mentioned in options that user may forget to
initialize the members which will lead to a segmentation fault. Also if the user calls the init()
function more than once it may overwrite the values and may result into disastrous results. Also
if any user forgets to define init() function then no object will be initialized whereas if any
constructor is not defined in any class the class provides a default constructor for initialization.
8. What is a copy constructor?
a) A constructor that allows a user to move data from one object to another
b) A constructor to initialize an object with the values of another object
c) A constructor to check the whether to objects are equal or not
d) A constructor to kill other copies of a given object.

Answer: b
Explanation: Copy constructor allows the user to initialize an object with the values of another
object instead of supplying the same set of values again to initialize the object.
9. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class A{
int a;
public:
A(int i){
a = i;
}
void assign(int i){
a = i;
}
int return_value(){
return a;
}
};
int main(int argc, char const *argv[])
{
A obj;
obj.assign(5);
cout<<obj.return_value();
}
a) 5
b) 55
c) Error
d) Segmentation Fault

Answer: c
Explanation: As we have defined a constructor which takes an int parameter, so when we are
trying to declare an object obj of class A without supplying any parameter then as a constructor
is overwritten it will give an error saying that no matching function found. So whenever one
writes a constructor then the default constructor is overwritten hence if you want to declare an
object without parameter then you also have to define that constructor.
10. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class A{
int a;
A(){
a = 5;
}
 
public:
void assign(int i){
a = i;
}
int return_value(){
return a;
}
};
int main(int argc, char const *argv[])
{
A obj;
obj.assign(10);
cout<<obj.return_value();
}
a) 5
b) 10
c) Error
d) Segmentation fault

Answer: c
Explanation: Here the constructor is made private and as no object can access any private object
directly therefore the program will give error. One should always define a constructor as public.

11. In the following C++ code how many times the string “A’s constructor called” will be
printed?

#include <iostream>
#include <string>
using namespace std;
class A{
int a;
public:
A(){
cout<<"A's constructor called";
}
};
class B{
static A a;
public:
B(){
cout<<"B's constructor called";
}
static A get(){
return a;
}
};
A B::a;
int main(int argc, char const *argv[])
{
B b;
A a1 = b.get();
A a2 = b.get();
A a3 = b.get();
}
a) 3
b) 4
c) 2
d) 1

Answer: d
Explanation: As the object is defined ony once in the program at line A B::a, so the constructor
of A is called only once. For objects a1, a2 and a3 copy constructor is called so the string will not
be printed for them.
11. What happens if a user forgets to define a constructor inside a class?
a) Error occurs
b) Segmentation fault
c) Objects are not created properly
d) Compiler provides a default constructor to avoid faults/errors

Answer: d
Explanation: The C++ compiler always provides a default constructor if one forgets to define a
constructor inside a class.
12. How many parameters does a default constructor require?
a) 1
b) 2
c) 0
d) 3
View Answer

c) 0

13. How constructors are different from other member functions of the class?
a) Constructor has the same name as the class itself
b) Constructors do not return anything
c) Constructors are automatically called when an object is created
d) All of the mentioned

Answer: d
Explanation: All the above mention are the reasons where constructor differs from other normal
member functions of a class.
14. How many types of constructors are there in C++?
a) 1
b) 2
c) 3
d) 4

Answer: c
Explanation: There are three types of constructors in C++ namely default, parameterized and
copy constructor.
15. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class A{
mutable int a;
public:
A(){
cout<<"Default constructor called\n";
}
A(const A& a){
cout<<"Copy Constructor called\n";
}
};
int main(int argc, char const *argv[])
{
A obj;
A a1 = obj;
A a2(obj);
}
a)

Default constructor called

Copy Constructor called

b)

Default constructor called

Copy Constructor called

Copy Constructor called

c)

Default constructor called

Default constructor called

Copy Constructor called

d)

Copy Constructor called

Default constructor called

Copy Constructor called

Answer: b
Explanation: When object obj is declared then the default constructor is called. When we are
declaring the a1 object as we are equating obj to a1 object obj will be copied to a1 hence copy
constructor is called, similarly when object a2 is created obj is passed as a parameter to function
which is available as copy constructor function, hence copy constructor will be called. So one
time Default constructor and two times copy constructor.
 
 
16. What will be the output of the following C++ code?

#include <iostream>
#include <string>
using namespace std;
class A{
mutable int a;
public:
A(){
cout<<"A's default constructor called\n";
}
A(const A& a){
cout<<"A's copy Constructor called\n";
}
};
class B{
A obj;
public:
B(){
cout<<"B's Constructor called\n";
}
};
int main(int argc, char const *argv[])
{
B b1;
B b2;
}
a)

B's Constructor called

B's Constructor called

b)

B's Constructor called

A's default constructor called

B's Constructor called

A's default constructor called


c)

A's default constructor called

B's Constructor called

A's default constructor called

B's Constructor called

d)

A's default constructor called

B's Constructor called

A's copy Constructor called

Answer: c
Explanation: Here when we are declaring the object b1 of class B then first the constructor of
class B will be called, in which first it will initialize all the members of class B and as obj from
class A is member of class B and it should be initialized so the A’s default constructor will be
called and terminates after that B’s constructor terminates hence A’s default constructor called is
printed before B’s constructor called.
 
 
17. What is the role of destructors in Classes?
a) To modify the data whenever required
b) To destroy an object when the lifetime of an object ends
c) To initialize the data members of an object when it is created
d) To call private functions from the outer world

Answer: b
Explanation: Destructors are used in Classes to destroy an object after its lifetime is over i.e. to
free resources occupied by that object.
18. What is syntax of defining a destructor of class A?
a) A(){}
b) ~A(){}
c) A::A(){}
d) ~A(){};
Answer: b
Explanation: A destructor starts with a ~(tilde) symbol, has the same name as the class.
19. When destructors are called?
a) When a program ends
b) When a function ends
c) When a delete operator is used
d) All of the mentioned

Answer: d
Explanation: Destructors are called at the following time:
i) at the end of the program to destroy objects declared in the main() or global scope.
ii) at the end of a function to destroy objects declared at that function scope.
iii) when user by himself tries to delete an object using the delete operator.
iv) at the end of a block to destroy objects declared at that block scope.
20. Which of the following cannot be overloaded in C++?

1. Increment operator
2. Constructor
3. Destructor
4. New and delete operator

Answer: 3

21. Compile time polymorphism in C++ language are

1. Operator overloading
2. Function overloading
3. Function overriding
4. B Only
5. A&B

Answer: 5

22. C++ abstract class can contain

1. Pure virtual function


2. Non-virtual function
3. Only pure virtual function
4. Both pure virtual and non-virtual function

Answer: 4
23. False statements about function overloading is

1. Defining multiple functions with same name in a class is called function overloading
2. Overloaded function must differ in their order and types of arguments.
3. Overloaded functions should be preceded with virtual keyword
4. No statement is false

Answer: 3

24. Following keyword is used before a function in a base class to be overridden in derived class
in C++

1. override
2. virtual
3. void
4. none

Answer: 2

25. Which public member of a base class cannot be inherited?

1. Constructor
2. Destructor
3. Both A & B
4. Only B

Answer: 3

26. In C++ constructor and destructor both cannot be inherited to child class.

1) We can overload which of the following C++ operators.


A) Arithmetic operator (+, -, *, /)
B) Class Member Access Operators (., .*)
C) Size operator(sizeof)
D) Conditional operator(?:)
2) ……………… must be either non-static member function or friend functions.
A) member functions
B) Operator functions
C) non-static functions
D) friend functions
Answer: A) Arithmetic operator (+, -, *, /)

27. Operator overloading is also called …………….. polymorphism.


A) run time
B) initial time
C) compile time
D) completion time

Answer: B) Operator functions

28. We can overload almost all the C++ operators except the following.
i) Class member operator (.,.*)   ii) Assignment operator (=)
iii) Scope resolution operator (::) iv) Conditional operator (?:)
A) i, ii and iii only
B) ii, iii and iv only
C) i, iii and iv only
D) All i, ii, iii and iv

Answer: C) i, iii and iv only

29. Which of the following is the correct order involves in the process of operator overloading.
i) Define the operator function to implement the required operations.
ii) Create a class that defines the data type that is to be used in the overloading operation.
iii) Declare the operator function op() in the public part of the class.
A) 1-i, 2-ii, 3-iii
B) 1-ii, 2-iii, 3-i
C) 1-ii, 2-i, 2-iii
D) 1-iii, 2-ii, 3-i

Answer: B) 1-ii, 2-iii, 3-i

30. State whether the following statements are True or False for overloading operators.
i) Only existing operators can be overloaded.
ii) We can change the basic meaning of an operator
A) True, True
B) True, False
C) False, True
D) False, False
Answer: B) True, False
31. We cannot use friend functions to overload which of the following operators.
i) membership operator(.)    ii) Assignment operator(=)
iii) class member access operator(_>)  iv) conditional operator(?:)
A) i and ii only
B) ii and iii only
C) iii and iv only
D) i and iv only

Answer: B) ii and iii only

32. …………… overloaded by means of a member function, take no explicit arguments and return
no explicit values.
A) Unary operators
B) Binary operators
C) Arithmetic operators
D) Function operator

Answer: A) Unary operators

33. ……………. overloaded through a member function take one explicit argument and those which
are overloaded through a friend function take two explicit arguments.
A) Unary operators
B) Binary operators
C) Arithmetic operators
D) Function operator

Answer: B) Binary operators

34. When using ………………., overloaded through a member function, the left-hand operand must
be an object of the relevant class.
A) Unary operators
B) Binary operators
C) Arithmetic operators
D) Function operator

Answer: B) Binary operators

35. Operator overloading is done with the help of a special function called ……………, which
describes the special task of an operator.
A) overloading function
B) special task function
C) detail function
D) operator function

Answer: D) operator function


36. The compiler does not support automatic type conversions for the ………….. data type.
A) basic
B) user-defined
C) class
D) automatic

Answer: B) user-defined

37. The casting operator function should satisfy which of the following conditions.
i) It must be a class member    ii) It must not specify the return type
iii) It must not have any arguments
A) i and ii only
B) ii and iii only
C) i, iii only
D) All i, ii and iii

Answer: D) All i, ii and iii

38. The conversion from a class to any other type or any other class should make use of a …………..
in the source class.
A) casting operator
B) constructor
C) not applicable
D) operator function

Answer: A) casting operator

39. To perform the conversion from any other data type or class to a class type, a ………….. should
be used in the destination class.
A) casting operator
B) constructor
C) not applicable
D) operator function

Answer: B) constructor

40. The general form of an overloaded casting operator function usually referred to as a
……………..
A) casting function
B) operator function
C) conversion function
D) overloaded function
Answer:  C) conversion function

41. Operator overloading provides a flexible option for the creation of new …………….. for most of
the C++ operations.
A) class
B) function
C) object
D) definitions

Answer: D) definitions

42. In the case of …………….. function, arguments may be passed either by value or by reference.
A) private
B) friend
C) member
D) public

Answers : B) friend

43. Polymorphism is achieved through ___

(A) Heritance

(B) Poly programming

(C) Encapsulation

(D) Overloading

Ans: D

Overloading

44. The word polymorphism means ____

(A) Many programs

(B) Two forms

(C) Single form

(D) Many shapes

Ans: D
Many shapes

45. The mechanism of giving special meaning to an operator is called ____

(A) Object

(B) Inheritance

(C) Function overloading

(D) Operator Overloading

Ans: D

Operator Overloading

46. In function overloading do not use the ___ function name for two unrelated functions.

(A) Same

(B) Different

(C) Similar

(D) Complement

Ans: A

Same

47. The functionality of operator like ‘+’ can be extended using ___

(A) Operator precedence

(B) Operator overloading

(C) Operator definition

(D) None of the given

Ans: B

Operator overloading
48. Binary operators overloaded through a member function take one ____ argument.

(A) Default

(B) Complete

(C) Implicit

(D) Explicit

Ans: D

Explicit

49. The operator function must be ____

(A) A member function

(B) A friend function

(C) Either member or friend function

(D) None of the given

Ans: C

Either member or friend function

Following are the situations where ‘this’ pointer is used:


50) When local variable’s name is same as member’s name

#include<iostream>

using namespace std;

  

/* local variable is same as a member's name */

class Test
{

private:

   int x;

public:

   void setX (int x)

   {

       // The 'this' pointer is used to retrieve the object's x

       // hidden by the local variable 'x'

       this->x = x;

   }

   void print() { cout << "x = " << x << endl; }

};

  

int main()

   Test obj;

   int x = 20;

   obj.setX(x);

   obj.print();
   return 0;

Output:
x = 20
********************* end of session *******************************************

You might also like