0

C++(Programming)

Description: Object Oriented Programming language objective questions
Number of Questions: 25
Created by:
Tags: Inheritance Polymorphism objects classes Java/Oracle /C/C++
Attempted 0/25 Correct 0 Score 0

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.

  1. To avoid ambiguity

  2. For reusability

  3. For faster processing

  4. None of the above

  5. All 1, 2 and 3


Correct Option: E
Explanation:

Virtual base class is used to reduce ambiguity, it cannot be used for faster processing and reusability.

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?

  1. ?:

  2. =

  3. -

  4. ++

  5. No such operator exists


Correct Option: A
Explanation:

Following are the operators which cannot be overloaded: Conditional/ ternary operator (?: ), Class member access operator ( . , -> ), Scope resolution operator ( :: ) and Size operator ( sizeof ).

Which of the following is/are not manipulators in C++? a) endl b) setfill
c) setprecision
d) width

  1. Only d

  2. Both a and b

  3. None of these

  4. All are manipulators

  5. Both b and c


Correct Option: A
Explanation:

Width is a function which specifies field width. For example, cout.width(4) function is called using cout object.

Which of the following operators cannot be overloaded using friend function? a) = b) -> c) [ ] d) -

  1. Both a and b

  2. Only b

  3. a, b and c

  4. All a, b, c and d

  5. Both b and d


Correct Option: C
Explanation:

Friend function cannot be used to overload: =,->,[ ],( ). So option 3 is the correct answer.

Which of the following is not a characteristic of Object Oriented programming?

  1. Data is hidden and cannot be accessed by external functions.

  2. Follows bottom-up approach in program design.

  3. New data and functions cannot be added whenever necessary.

  4. Program is divided into what are known as objects.

  5. None of the above


Correct Option: C
Explanation:

New data and functions can be added whenever necessary. For e.g. for(int i=0;i<4;i++) is valid in C++.

Which of the following keywords is/are not used in exception handling of C++? a) try
b) throws c) throw d) catch e) finally

  1. Only e

  2. Both b and c

  3. Both b and e

  4. All keywords are used

  5. All a, c and d


Correct Option: C
Explanation:

In C++, the exception handling mechanism is achieved using the keywords try, catch and throw.

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.

  1. Only a

  2. Both a and b

  3. All a, b and c

  4. Both are same

  5. Both b and c


Correct Option: C
Explanation:

In overriding both functions must have same signature or prototype. In overloading multiple functions have same name but different arguments like different number of arguments and different type of arguments.

Which of the following is not an infinite loop?

  1. for(int i=1;;i++)

  2. for(int i=1;i<=10;)

  3. for(int i=4;i<5;i++)

  4. for(;;)

  5. none of the above


Correct Option: C
Explanation:

i=44<5. As this is true, the loop is executed atleast once.

What will be the output of the following program fragment?

#include<iostream.h>

int i=10;

int main()

{

int i=20;

{

int i=30;

cout<<i<<::i;

}

return 0;

}

  1. Prints 3020

  2. Prints 3010

  3. Will result in run time error

  4. Prints 3030

  5. None of the above


Correct Option: B
Explanation:

First 'i' will be printed as 30, as preference is given to local variable. In front of second 'i', ::(scope resolution) operator is given. :: means to manipulate a global variable, in case a local variable also has the same name.

Function overloading: static polymorphism is achieved by function overloading. Which of the following is not allowed in function overloading?

  1. Same function name with different number of arguments

  2. Same function name with different return type

  3. Same function name with different sequence of arguments

  4. Same function name with different types of arguments

  5. All of the above


Correct Option: B
Explanation:

Function overloading doesn’t uses the return type for identification of function, so option 2 is correct, e.g. int fun(int ,int);double fun(int,double); is not allowed in function overloading.

Which of the following is not the characteristic of a constructor?

  1. Constructor has same name as that of the class.

  2. They don’t return value and are not of type void.

  3. Constructor can be private, public or protected.

  4. A class can have more than one constructor.

  5. None of the above


Correct Option: C
Explanation:

The constructors are always defined as public.

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;

  1. Both a and b

  2. Both c and d

  3. Only a

  4. All codes

  5. None of the above


Correct Option: A
Explanation:

>> is bitwise right shift operator.a)(32)10=(100000)2. After >>2 we get, (8)10=(001000)2b) (16)10=(010000)2. After >>1 we get, (8)10=(001000)2. This option is correct as we are getting ‘8’ after right shift by 2.

The cin used as unformatted input is ___________.

  1. Class defined in iostream.h header file.

  2. Object of the istream class.

  3. Package having different class definition.

  4. Function which is already defined.

  5. None of the above


Correct Option: B
Explanation:

cin is the predefined object of the istream class and is used for performing the standard input operations.

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. 1 0 01 1 01 1 1

  2. 0 0 10 1 01 0 0

  3. 1 0 00 1 00 0 1

  4. 0 0 00 0 00 0 0

  5. None of the above


Correct Option: C
Explanation:

If we trace the program as follows: j=0 j=1 j=2k=0 k=0 k=0k==j k.

Templates are used _________.

  1. as decision making statement

  2. to achieve generic programming

  3. to reduce ambiguity

  4. to achieve polymorphism in C++

  5. none of the above


Correct Option: B
Explanation:

Templates enable us to define generic classes and functions and thus provide support for generic programming.

What will be the order of execution of class constructors in following code? class alpha{ - - -}; class beta { - - -}; class gamma: public beta, public alpha{- - -};

  1. beta, alpha, gamma

  2. alpha, gamma , beta

  3. gamma , beta, alpha

  4. all of the above

  5. none of the above


Correct Option: A
Explanation:

Beta is initialized first because it has been declared first in the derived class header line. Then alpha and lastly the derived class.

Which of the following is not a file mode parameter used with open() function?

  1. ios::app

  2. ios::trunc

  3. ios::cur

  4. ios::ate

  5. none of the above


Correct Option: C
Explanation:

ios::cur. This parameter is used with seekg() or seekp() function which defines current position of the pointer.

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?

  1. 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.

  2. We need to use single pointer variable to refer to the objects of different classes.

  3. 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.

  4. All the above.

  5. None of these


Correct Option: D
Explanation:

The necessity of virtual function is given by all the above 3 options, so option 4 is the correct answer.

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.

  1. Declaration : friend void operator –(space &s); Definition :void operator –(space &s){ s.x=-s.x; s.y=-s.y;}

  2. Declaration : friend void operator –(); Definition : void space operator –(){ x=-x; y=-y;}

  3. Declaration : friend void operator –(space &s); Definition : void operator –(space &s){ x=-x; y=-y;}

  4. Unary minus operator cannot be overloaded using friend function

  5. None of the above


Correct Option: A
Explanation:

There is no need to use scope resolution operator if function is defined as friend. We have to pass the arguments by reference.

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.

  1. Only a

  2. Both a and b

  3. All a, b and c

  4. Both b and c

  5. None of the above


Correct Option: C
Explanation:

The program code is not simple if it contains loops, decision making statements and recursion. Option 3 is the correct answer.

What does the setbase manipulator do?

  1. Sets the precision

  2. Sets the initial or basic value of variable

  3. Converts the data of one number system to another

  4. No such manipulator present in C++

  5. Sets the base type like decimal, binary, hexadecimal


Correct Option: C
Explanation:

It is conversion manipulator that converts data of one number system to another.e.g.int x=15;cout<.

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; }

  1. base d base s derived d derived s

  2. base d base s base d base s

  3. base d base s base d derived s

  4. none of the above

  5. all a, b and c


Correct Option: C
Explanation:

The show() function is defined as virtual while display() function is not defined as virtual. 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.

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

  1. To a public function in class A

  2. To a public function in class B

  3. To a public function in class C

  4. All of the above

  5. Both 1 and 2


Correct Option: E
Explanation:

Both 1 and 2 are correct.

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.

  1. derived class

  2. base class

  3. produces compile time error

  4. produces runtime error

  5. none of the above


Correct Option: A
Explanation:

Base b; Base *p; Derived d; p=&d; p->xyz(); as pointer is holding the address of derived class object and functions are defined virtual, derived class function xyz() is called.

_______ are used for distinctly separating the scopes of the declarations and definitions in a program where the name clashes are likely to occur.

  1. Virtual functions

  2. templates

  3. Namespaces

  4. Inline function

  5. None of above


Correct Option: C
Explanation:

Namespaces (user defined) are used for distinctly separating the scopes of the declarations and definitions in a program where the name clashes are likely to occur.

- Hide questions