C++(Programming)
Object Oriented Programming language objective questions
Questions
Why is there a need for virtual base class in hybrid/diamond inheritance?
Multiple inheritance: more than one class is derived from single base class.
Multilevel inheritance: single class is derived from many base classes.
- To avoid ambiguity
- For reusability
- For faster processing
- None of the above
- All 1, 2 and 3
Operator overloading: the process in which operators are used to operate on different datatypes. For example, ‘+’ operator when used with integer of float, it does addition, while if it is used with character it does concatenation.
Which of the following operators cannot be overloaded?
- ?:
- =
- -
- ++
- No such operator exists
Which of the following is/are not manipulators in C++?
a) endl
b) setfill
c) setprecision
d) width
- Only d
- Both a and b
- None of these
- All are manipulators
- Both b and c
Which of the following operators cannot be overloaded using friend function?
a) =
b) ->
c) [ ]
d) -
- Both a and b
- Only b
- a, b and c
- All a, b, c and d
- Both b and d
Which of the following is not a characteristic of Object Oriented programming?
- Data is hidden and cannot be accessed by external functions.
- Follows bottom-up approach in program design.
- New data and functions cannot be added whenever necessary.
- Program is divided into what are known as objects.
- None of the above
Which of the following keywords is/are not used in exception handling of C++?
a) try
b) throws
c) throw
d) catch
e) finally
- Only e
- Both b and c
- Both b and e
- All keywords are used
- All a, c and d
What is the difference between function overloading and function overriding?
a) In overloading multiple functions are available with same name, while in overriding single function is rewritten.
b) Function overloading is possible in any program while function overriding is possible in inheritance.
c) In overriding both functions must have same signature. In overloading multiple functions have same name but different arguments.
- Only a
- Both a and b
- All a, b and c
- Both are same
- Both b and c
Which of the following is not an infinite loop?
- for(int i=1;;i++)
- for(int i=1;i<=10;)
- for(int i=4;i<5;i++)
- for(;;)
- none of the above
What will be the output of the following program fragment?<o:p>
#include<iostream.h><o:p>
int i=10;<o:p>
int main()<o:p>
{<o:p>
int i=20;<o:p>
{<o:p>
int i=30;<o:p>
cout<<i<<::i;<o:p>
}<o:p>
return 0;<o:p>
}<o:p>
- Prints 3020
- Prints 3010
- Will result in run time error
- Prints 3030
- None of the above
Function overloading: static polymorphism is achieved by function overloading.
Which of the following is not allowed in function overloading?
- Same function name with different number of arguments
- Same function name with different return type
- Same function name with different sequence of arguments
- Same function name with different types of arguments
- All of the above
Which of the following is not the characteristic of a constructor?
- Constructor has same name as that of the class.
- They don’t return value and are not of type void.
- Constructor can be private, public or protected.
- A class can have more than one constructor.
- None of the above
In which of the following code fragments the variable ‘x’ is evaluated to 810 int x;
a) x=32;
b) x=16;
c) x=38;
d) x=36;
x=x>>2; x=x>>1; x=x>>2; x=x>>2;
- Both a and b
- Both c and d
- Only a
- All codes
- None of the above
The cin used as unformatted input is ___________.
- Class defined in iostream.h header file.
- Object of the istream class.
- Package having different class definition.
- Function which is already defined.
- None of the above
What will be the output of array variable table after executing the following code?
include<iostream.h>
int main()
{
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
{
if(k==j)
table[j][k]=1;
else table[j][k]=0;
}
}
for(int i=0;i<3;i++)
{
for(int l=0;l<3;l++)
{
cout<<table[i][l] ;
}
cout<<endl;
}
return 0;
}
- 1 0 01 1 01 1 1
- 0 0 10 1 01 0 0
- 1 0 00 1 00 0 1
- 0 0 00 0 00 0 0
- None of the above
Templates are used _________.
- as decision making statement
- to achieve generic programming
- to reduce ambiguity
- to achieve polymorphism in C++
- none of the above
What will be the order of execution of class constructors in following code?
class alpha{ - - -};
class beta { - - -};
class gamma:
public beta,
public alpha{- - -};
- beta, alpha, gamma
- alpha, gamma , beta
- gamma , beta, alpha
- all of the above
- none of the above
Which of the following is not a file mode parameter used with open() function?
- ios::app
- ios::trunc
- ios::cur
- ios::ate
- none of the above
Why is there a need for virtual function when we access the function (having same name as that in base class) in derived class through the use of a pointer declared as pointer to the base class?
- We can not use the object name(with dot operator), as runtime polymorphism is achieved only when a virtual function is accessed through a pointer to the base class.
- We need to use single pointer variable to refer to the objects of different classes.
- When function is made virtual, C++ determines which function to use at run time based on the type of object pointed by the base class.
- All the above.
- None of these
What changes are to be made in prototype/signature of unary minus(-) operator overloading function to convert it into friend function?
Declaration : void operator –();
Definition :void space:: operator –(){ x=-x; y=-y;}; where space is class name and x and y are integer variables.
- Declaration : friend void operator –(space &s); Definition :void operator –(space &s){ s.x=-s.x; s.y=-s.y;}
- Declaration : friend void operator –(); Definition : void space operator –(){ x=-x; y=-y;}
- Declaration : friend void operator –(space &s); Definition : void operator –(space &s){ x=-x; y=-y;}
- Unary minus operator cannot be overloaded using friend function
- None of the above
What is/are the situation(s) where function is not treated as inline?
a) If the function contains recursion
b) If the function contains nested loop
c) If the function contains multiple decision making statement like switch
Inline function: whenever the function is called the function calling is replaced by function body.
- Only a
- Both a and b
- All a, b and c
- Both b and c
- None of the above
What does the setbase manipulator do?
- Sets the precision
- Sets the initial or basic value of variable
- Converts the data of one number system to another
- No such manipulator present in C++
- Sets the base type like decimal, binary, hexadecimal
What will be the output of following code?
class Base
{
public: void display()
{cout<<”t base d”;
}
virtual void show()
{
cout<<”t base s”;
}
};
Class Derived: public Base
{
public: void display()
{
cout<<”t derived d”;
}
void show()
{
cout<<”t derived s”; }
};
int main()
{
Base B;
Derived D;
Base *bptr;bptr=&B;
bptr->display();
bptr->show();
bptr=&D;
bptr-> display();
bptr-> show();
return 0;
}
- base d base s derived d derived s
- base d base s base d base s
- base d base s base d derived s
- none of the above
- all a, b and c
Consider the following code:
class A
{
int i1;
protected:int i2;
public:int i3;
};
class B:public A
{
Public:int i4;
};
class C:B{ };
The variable i2 is accessible
- To a public function in class A
- To a public function in class B
- To a public function in class C
- All of the above
- Both 1 and 2
If there is a pointer p to object of a base class and it contains the address of an object of derived class and both classes contain a virtual member function xyz(), then the statement p->xyz(); will cause the version of xyz() in the ___________ class to be executed.
- derived class
- base class
- produces compile time error
- produces runtime error
- none of the above
_______ are used for distinctly separating the scopes of the declarations and definitions in a program where the name clashes are likely to occur.<o:p>
-
Virtual functions<o:p></o:p> -
templates<o:p></o:p> -
Namespaces<o:p></o:p> -
Inline function<o:p></o:p> - None of above