A. restricting access to members.
B. ignoring background details.
C. bundling data and functions together.
D. allowing different behavior for the same function.
Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings or functions to the operators or functions. Poly, referring to many, signifies the many uses of these operators and functions.
A. it can be used anywhere in the program by any function.
B. It can be used only within the function that declares it.
C. the member can be accessed only by the member functions of the class.
D. the scope of public members depends upon the referencing object.
If an object has a global scope, then the objects can be used anywhere in the program by any function. If an object has a local scope, then the objects can be used only within the function that declares it.
A. object.
B. class.
C. method.
D. data hiding.
A class is a way to bind the data describing an entity and its associated functions together. In C++, class makes a data type that is used to create objects of this type.
A. method.
B. data member.
C. object.
D. function.
Member functions are the set of operations that may be applied to objects of that class. There may be zero or more member functions for a class. They are referred to as the class interface.
A. members.
B. functions.
C. class.
D. methods.
A class is a way to bind the data describing an entity and its associated functions together.
A. is a non member function.
B. is a member function declared as a friend.
C. data members.
D. is called using the object.
A friend function is used in object-oriented programming to allow access to private or protected data in a class from outside the class.
A. defined inside a member function.
B. defined under public label.
C. defined inside the class.
D. defined inside every object.
In computer science, an inline function is a programming language construct used to tell a compiler it should perform in-line expansion on a particular function.These functions are defined inside the class.
A. X :: Count.
B. :: Count.
C. Count :: X.
D. X :: int Count .
:: is scope resolution operator. If the Count integer belongs to class X, then it would have been declared as X :: Count. But, since it is global variable, it will be declared as ::Count outside class.
A. an accessor function.
B. a mutator function.
C. setters function.
D. getmember() function.
Accessor functions allow us to access the data number/ field of an object. Example getRollNo()can be an accessor function to roll no. of students.
A. public members.
B. private members.
C. protected members.
D. hidden members.
Protected means data members and member functions can be used in same class and its derived class.
A. the class name must precede the function name.
B. function call is replaced by the function code.
C. there must be at least one static member.
D. program size decreases as there is no function call.
With inline code, the compiler replaces the function call statement with the function code itself and then compiles the entire code. This speeds up the process as the compiler doesnot have to jump to another location to execute the function.
A. Passing an object by reference may change the original value.
B. Passing an object by value does not change the original value.
C. Objects can be passed only to member functions.
D. Objects can directly call public members of the class.
An object may be passed to a member function, a non-member function, and a friend function.
A. an overloaded assignment operator
B. a destructor.
C. a copy constructor.
D. no member functions provided.
In dynamic memory allocation, whenever the object is out of scope, its memory would be freed and deallocated. So, a destructor is automatically invoked by the class.
A. is a non member function
B. is a member function declared as a friend
C. data members
D. is called using the object
A friend function is a function that can access the private members of a class as though it were a member of that class. In all other regards, the friend function is just like a normal function. A friend function may or may not be a member of another class.
To declare a friend function, use the friend keyword in front of the prototype of the function you wish to be a friend of the class. It does not matter whether you declare the friend function in the private or public section of the class.
A. directly accessed by any function.
B. hidden from outside world.
C. are inheritable.
D. are accessed by classes in which they are absent.
Private Members are the class members that are hidden from outside world. The private members implement the OOP concept of data hiding. The private members of a class can be used only by member functions (and friends) of the class in which it is declared.
A. the class name must precede the function name
B. function call is replaced by the function code
C. there must be at least one static member
D. program size decreases as there is no function call
With inline code, the compiler code replaces the function call statement with the function code itself and then compiles the entire code. Thus, with inline functions, the compiler does not have to jump to another location to execute the function and then jumps back as the code of the called function is already available to the calling program.
A. it speeds up the program.
B. it improves performance.
C. there is a memory penalty.
D. it removes too many function calls.
Inline functions run a little faster than the normal functions as function-calling overheads are saved, however, there is a memory penalty. If 10 times an inline function is called, there will be 10 copies of the function inserted into the code.
A. encapsulation.
B. inheritance.
C. polymorphism.
D. data hiding.
Polymorphism : An object is in different forms and in each form it exhibits the same functionality but the implementation is different.
A. visible globally.
B. visible only in two classes.
C. visible only in three classes.
D. visible only within the class.
A static data member is different from ordinary data members of a class in the following aspects :
1. There is only one copy of this data member maintained for the entire class which is shared by all the objects of that class.
2. It is visible only within the class, however, its lifetime is the entire program.
A. only static members
B. all data members
C. other member functions
D. only public members
A member function that accesses only the static members of a class may be declared as static. A static member function can access only static members of the same class. A static member function is invoked by using the class name instead of its objects as it is shown below:
classname::function-name;
A. polymorphism.
B. data hiding.
C. abstraction.
D. encapsulation.
Private Members are the class members that are hidden from the outside world. The private members implement the OOP concept of data hiding.
A. only 3.
B. only 2.
C. only 1.
D. 0 or more.
Data members are the data type properties that describe the characteristics of a class. There may be zero or more data members of any type in a class.
A. an instance of a class.
B. a hardware tool.
C. also known as class.
D. is the way in which a class is defined.
Object is an identifiable entity with some characteristics and behaviour. In OOP, object represents an entity that can store data and has its interface through functions.
A. class X{ private: int a=2; char b = }:
B. Class X (int a=2; public: char b= )
C. -class X{int a=2; char b= }
D. class X
{
int a;
public:
void add();
};
The general form of a class definition looks like :
class class-name
{
private:
[variable declarations;]
[function declarations;]
protected:
[variable declarations;]
[function declarations;]
public:
[variable declarations;]
[function declarations;]
};
For example,
class Sample
{
int a;
double b;
public:
void add();
void print();
...
};
A. restricting access to members
B. ignoring background details
C. bundling data and functions together
D. allowing different behavior for the same function
Polymorphisms is the ability to take more than one forms. For example, say a door. A door to a temple, a door to a house, a door to a kitty house,
all are doors, but all look different.
A. restricting access to members
B. storing essential features
C. bundling data and functions together
D. allowing different behavior for the same function
Abstraction refers to the act of representing essential features without including the background details or explanations.
A. restricting access to members
B. ignoring background details
C. bundling data and functions together
D. allowing different behavior for the same function
Encapsulation refers to wrapping up data and associated functions into one single unit.
A. data
B. functions
C. private
D. public
There are generally two types of members in a class : private and public. If no label (public or private) is specified then by default, members are private.
A. keyword Class, followed by class-name, followed by members within braces
B. keyword Class, followed by private members and functions, within braces
C. keyword Class, followed by data members and functions within braces
D. keyword class, followed by class-name, followed by members within braces
The keyword class specifies that it is a class; class-name is the tagname of the class that acts as a type specifier for it, using which objects of this class type can be created. The braces { and } surround the body of the class. The class body contains the declaration of its members ( data and functions). These are generally two types of members in a class : private and public.
The class declaration looks like :
class class-name
{
private:
[variable declarations;]
[function declarations;]
protected:
[variable declarations;]
[function declarations;]
public:
[variable declarations;]
[function declarations;]
};
A. protected, personal, public
B. private, public, preserved
C. public, private, protected
D. public, protected, personal
Program access levels control access to members from within the program. These access levels are private, protected or public. Depending upon the access level of a class member, access to it is allowed or denied.
A. data and structures
B. data and functions
C. objects and structures
D. functions and structures
A class is a way to bind the data describing an entity and its associated functions together. In C++, class makes a data type that is used to create objects of this type.
A. restricting access to members.
B. storing essential features.
C. bundling data and functions together.
D. allowing different behavior for the same function.
Abstraction is the process or result of generalization by reducing the information content of a concept or an observable phenomenon, typically to retain only information which is relevant for a particular purpose.
A. restricting access to members.
B. ignoring background details.
C. bundling data and functions together.
D. allowing different behavior for the same function.
Encapsulation is the inclusion of one thing within another thing so that the included thing is not apparent. A class implements encapsulation by bundling data and functions together.
A. data members.
B. mutator functions.
C. manager functions.
D. member functions.
The member functions are the set of operations that may be applied to objects of that class. There may be zero or more member functions for a class. They are referred to as the class interface.
A. class X{ private: int a=2; char b= };
B. Class X{int a=2; public: char b= }
C. -class X{int a=2; char b= }
D. class X{int a; public: char b; )
The correct syntax for class definition is class classname { data members }
A. A member function can call another member function.
B. Member functions are unable to call non-member functions.
C. A class with one static data must have one static member function.
D. A friend function can access private data without the object.
Member functions cannot access non-member functions of a class and private data can be accessed only by member functions of class.
A. :: .
B. ; .
C. : .
D. << .
The scope resolution operator ::, when used with classname depicts the class member as in classname :: functionname and when used only with the variable name as in ::svariablename, depicts the global variable (the one with the file scope).
A. A reference can be used without proper initialization.
B. Array of references is legal in C++.
C. Char , int pointers can refer to null pointers but vice versa not true.
D. Array of pointers is not legal in C++.
A reference has to be properly initialized, otherwise it will result in an error. In C++, special care is taken for assignment of void pointers to other pointer types. While we can assign a pointer of any type to a void pointer the reverse is not true until we do explicit typecasting.
A. has more than a name.
B. has no member functions.
C. is just a name.
D. has no common properties.
An empty class has no variable declarations, no functions or methods but the objects of these classes can have non–zero size.
A. is same as class declaration.
B. does not create objects.
C. creates objects.
D. creates objects and initializes them.
Objects are created separately using new operator. A class definition just defines a class and its data members.
A. public access specifier.
B. private access specifier.
C. protected access specifier.
D. friend access specifier.
Public member functions can be accessed by any other member or non-member functions of the class as well as outside the class.
Book Bk[15];
Public members, that can be called in any function (using the object) constitute the public interface of the class.
Make only small functions inline. It must fulfill certain other conditions - it must not have a loop, switch or goto if it returns a value, it must not have static variables and must not be recursive.
Containership refers to an object of a class being a member of another class.
In object-oriented programming (OOP), an inner class or nested class is a class declared entirely within the body of another class or interface.
Public members can be called outside the class but Private members can be accessed only by other members of the class. Also, public members can be inherited while private members cannot be.
When a member function of a class calls another member function, it is known as nesting of member functions.
The only difference is that private members cannot be inherited outside the class while protected members can be inherited by its derived classes.
An object is an instance of a class. For a class that has been defined, objects can be created as variables of that class. Each object will have a set of data members defined in the class.
A friend function is used in object-oriented programming to allow access to private or protected data in a class from the outside. Normally, a function that is not a member of a class cannot access such information; neither can an external class. Occasionally, such access will be advantageous for the programmer. Under these circumstances, the function or external class can be declared as a friend of the class using the friend keyword.
Members of a class are private by default whereas members of a structure are public by default.
A static member function is a member function that can access only static data members. It is called using the class name and not with the object as is the case with other member functions.
A. constructor should not be defined publicly.
B. constructor should have arguments.
C. constructor should not have any return type.
D. constructor should be hidden.
char file(); is incorrect as constructor does not have any return type.
A. line 3 and line 10.
B. line 4 and line 5.
C. line 3 and line 5.
D. line 4 and line 10.
In line 3, the class member cannot be initialized inside a class. In line 10, a1 is assigned a value that is never used.
A. exam In;
B. exam In();
C. exam (10);
D. exam In(10);
exam is the name of the class and In is the name of the object. Inside the brackets, integer value is passed.
A. constructor.
B. private member.
C. destructor.
D. public member.
A member function with the same name as its class is called constructor and it is used to initialize the objects of that class type with a legal initial value.
A. explicitly.
B. automatically.
C. implicitly.
D. locally.
The object creation is a two-step process. At first, the memory is allocated and then its data members are initialised. For initialisation, constructors are provided which gets called automatically.
A. constructors cannot be overloaded.
B. constructors have a return type.
C. constructors do not return any value.
D. constructors are called explicitly.
Constructors are like other member functions of the class and hence, can be overloaded. The only difference is that the constructors do not return any value.
A. execution.
B. compilation.
C. linking.
D. initialization.
Static memory allocation takes place during compilation whereas dynamic memory allocation takes place during execution.
A. execution.
B. compilation.
C. linking.
D. initialization.
Static memory allocation takes place during compilation, whereas, dynamic memory allocation takes place during execution.
A. hash.
B. sigma.
C. tilde.
D. dash.
The destructor is a member function that has the same name as the class. It is used to deallocate the memory space and always preceded with a tilde sign(~).
A. an object is passed by reference.
B. a function returns no argument.
C. an object is passed by value.
D. no object is passed.
The copy constructor is invoked when an object is passed by value to a function. The pass-by-value method requires a copy of the passed argument to be created for the function to operate upon.
A. parameterized constructor.
B. copy constructor.
C. default constructor.
D. mutual constructor.
A copy constructor is a special constructor in the C++ programming language used to create a new object as a copy of an existing object. This constructor takes a single argument: a reference to the object to be copied.
A. value
B. address.
C. value-result.
D. reference.
A copy constructor is a constructor of the form classname(classname &). The compiler will use the copy constructor whenever you initialize an instance using values of another instance of same type. In copy constructor, if the argument is passed by value instead of reference, the compiler runs out of memory.
A. explicitly.
B. automatically.
C. implicitly.
D. locally.
Member functions are used to modify, retrieve or print data whenever required and hence are called explicitly.
A. protected
B. private
C. static
D. dynamic
One of the main characteristic of constructor is that it cannot be static.
A. copy constructor.
B. special member function.
C. zero-argument constructor.
D. new operator.
Default constructor does not accept any argument. Thats why it is also called as zero-argument constructor.
A. takes one argument.
B. uses different name from the class name.
C. must be a private member.
D. does not allow overloading.
Copy constructor takes one argument. Also called as one argument constructor. The main use of copy constructor is to initialize the objects while in creation. Also it is used to copy values of one object into another object.
A. initializing references.
B. initializing constants.
C. compile time initialization.
D. dynamic initialization.
In dynamic initialization, values are assigned to the variables during run-time.
A. 44 33
B. 22 33
C. 11 33
D. 11 12
when disp1() function will be called, it will print multiplication of a and b, i.e., 11 and 4 respectively. So 44 will be printed. when disp2() function will be called, it will print addition of a and b, i.e. , 12 and 21 respectively. So 33 will be displayed.
A. 1122
B. 484
C. 55
D. 2211
The variables a and b are initialized with 11 and 44 respectively. When object obj called the function disp(), it will display the multiplication of a and b, i.e., 484
A. Destructor Overloading
B. Destructor
C. Constructor
D. Constructor Overloading
Function 3 : Destructor
A destructor is called or invoked when an object of that class is destroyed. When a variable goes out of scope or a dynamically allocated variable is explicitly deleted using the delete keyword, the class destructor is called to help clean up the class before it is removed from the memory.
Static data members are class variables. Only one copy of the static data member is maintained and this is shared by all the objects. They are visible only within the class but their lifetime is the entire program. They can be accessed in all member functions.
Function f2( ) is a static member function. It is accessing a, which is a non-static data member.
class AC{ int acc_no;
char name[20];
char type;
float balance;
public:
void init(){
cout<<"nEnter Account No.";
cin>>acc_no;
cout<<"nName of Account Holder : ";
cin.getline(name,20);
cout<<"nType = S or C ";
cin>>type;
type=toupper(type);
cout<<"nBalance : ";
cin>>balance;
}
void disp(){
cout<<"nAccount No."<< acc_no;
cout<<"nName of Account Holder : "<< name;
cout<<"nType = "<< type;
cout<<"nBalance : "<< balance;
}
void deposit(float d){
balance = balance + d;
cout<<"nBalance Updated";
}
void draw(float d){
if(balance - d>=1000)
{ balance=balance-d;
cout<<"nBalance Updated";
}
else
cout<<"Leaves insufficient balance";
}
sums( ) can access A::x, A::y, A::z, A::a, A::b, ::a, ::b
fn( ) can access A::x, A::y, A::z, A::a, A::b, ::a, ::b
class Book{
int book_code;
char title[20];
char author[20];
int Price;
public:
void getbook(){
cout << "nEnter Book Code : ";
cin >> book_code;
cout << "nBook Title : ";
cin.getline(title,20);
cout << "nBook Author : ";
cin.getline(author,20);
cout << "nPrice : ";
cin >> Price;
}
void buybook(){
int c;
cout << "nHow many copies to buy? ";
cin >> c;
cout << "nPay Rs."<< price*c;
}
};
Yes they can.
The scope of a member function is restricted to the class. It may be called by other members of the class or if called in a non member function, it is linked with an object of the class.
The class definition gives the data members and member functions. Based on the class definition, objects are declared. Each object will have its own set of data members but there will be one set of functions accessed by all the objects. So, when the class is defined, memory is allocated for member functions and they are stored there. When objects are declared, separate memory space is allocated for each object, sufficient to accommodate all its data members.
1. Member functions can access private and protected members of the class while non member functions have access to only public members and that too, through the objects.
2. Member functions don't have scope outside the class while non member functions have scope outside the class.
Advantages of inline function are:-
1) It does not require function calling overhead.
2) It also save overhead of variables push/pop on the stack, while function calling.
3) It also save overhead of return call from a function.
4) It increases locality of reference by utilizing instruction cache.
5) After in-lining compiler can also apply intraprocedural optmization if specified. This is the most important one, in this way compiler can now focus on dead code elimination, can give more stress on branch prediction, induction variable elimination etc..
#include < iostream.h>
#include < ctype.h>
class DH{int seat;
char name[20];
static int taken;
public:
void getdata(){
taken++;
cout<<"nSeat Number = "<< taken;
cout<<"nName = ";
cin.getline(name,20);
seat=taken;
}
static void status(){
cout<<"nTotal Seats = 50";
cout<<"nSeats Taken = "<< taken;
cout<<"n Available = "<< 50-taken;
}
void disp( ){
cout<<"nSeat No. = "<< seat;
cout<<"nName = "<< name;
}
};
DH ob[50];
int DH::taken;
void main(){
int i,s;
char mo='Y',c;
for(i=0;mo=='Y' && i<50;i++){
ob[i].getdata();
DH::status();
cout<<"nMore? (Y/N) ";
cin>>mo;
mo=toupper(mo);
c=cin.get();
}
cout<<"Enter Seat No. to display";
cin>>s;
ob[s-1].disp();
Three access specifiers are given by C++ :-
1. private : which disallow the accessibility of block outside the class
2. public : which allow the accessibility to outside of the class by any function
3. protected : which restrict the accessibility upto derived class only
With the proper use of access specifier the data can be hidden from unauthorized access.
A class binds together data and functions in a single unit to enforce encapsulation.
The class represents essential features of the object only, thus implementing data abstraction.
Some situations where defining a function as inline will not work –
1. If the function returns a value and contains a loop, switch or goto statement.
2. If the function does not return a value and contains a return statement.
3. If the function contains static variables.
A. 0 byte.
B. 1 byte.
C. 2 bytes.
D. 10 bytes.
The minimum amount of memory that can be reserved is 1 byte. Hence, the size of the object of empty class will be 1 byte.
A. ~ operator.
B. :: operator.
C. ^ operator.
D. & operator.
The way to access global data and functions is to use the scope resolution operator (::).
A. dynamic.
B. create.
C. new.
D. fresh.
The keyword 'new' is used to create an object dynamically. For Example : Sample p = new Sample; Here 'p' is an object of Sample type.
A. only once.
B. twice.
C. zero times.
D. thrice.
Destructors cannot be overloaded. It does not make any sense in passing any arguments to the destructors.
A. it is not a function.
B. it deallocates memory.
C. it is called whenever an object is deallocated.
D. it is called whenever an object gets invoked.
Constructors are called whenever an object gets invoked. While creating an object it is not required to return a value.
A. one.
B. two.
C. zero.
D. infinite.
An error value can never be returned from a constructor. However, when a runtime error occurs, an exception can be thrown from within a constructor.
A. private.
B. protected.
C. inline.
D. inherited.
Functions defined in class are by default inline and those defined outside are not inline. Functions can be made inline by writing the keyword inline in function’s definition.
A. Cannot take arguments
B. Can return only ~ type
C. Cannot be private
D. Can be inherited
The desructor has specific naming rules as follows:
The destructor has the same name as that of the class, preceded by tilde (~).
The destructor cannot take arguments.
The destructor has no return type.
A. Cannot take arguments
B. Cannot have default values
C. Can be inherited
D. Have no return type
C++ constructors have an implicit return type used by the compiler but we can't declare a return type or return a value.
A. Copy constructor
B. Default constructor
C. Parameterized constructor
D. Temporary constructor
Parameterized constructor contains arguments to assign the values to the class elements.
A. The program will print the output Extra.
B. The program will print the output Marks.
C. The program will print the output ExtraMarks.
D. The program will report compile-time error.
As the object is created, it invokes the constructor and then the destructor followed by it.
Output:
A. is deleted when no longer needed.
B. calls the default constructor.
C. is a temporary class.
D. is created during dynamic initialization.
A temporary instance is the one that lives in the memory as long as it is being used or referenced in an expression and after this it dies. The temporary instances are anonymous i.e. they do not bear a name.
A. 1234
B. 12344321
C. 4321
D. 12341234
The program will start to print 1 from because the variable is initialized to zero as val = 0. Then, it is incremented by 1, each time constructor is called, till val = 4. After that, destructor is called, which decrements val from 4 to 1.
Output:
A. uses default arguments.
B. calls the copy constructor.
C. calls the default constructor.
D. simply returns control.