Date post: | 19-May-2015 |
Category: |
Technology |
Upload: | karthikeyan-a-k |
View: | 2,535 times |
Download: | 0 times |
C++
Explain the different forms of Polymorphism.
Question No : 1
There are two types of polymorphism. One is
Compile time polymorphism and the other is run
time polymorphism. Compile time polymorphism
is method overloading. Runtime time
polymorphism is done using inheritance.
Distinct forms of polymorphism• Method overloading• Method overriding through inheritance
Answer
What are the differences between Constructors
and Methods?
Question No : 2
Constructors Methods
Purpose Create an instance of a class Group statements
Return
Type No return type, not even void void or a valid return type
Name
Same name as the class (first
letter is capitalized by
convention) -- usually a noun
Any name except the class.
Method names begin with a
lowercase letter by convention
-- usually the name of an action
Inheritanc
e Constructors are not inherited Methods are inherited
Answer
All mammals have some common characteristics such as
having hair, being warm-blooded and nurturing their young.
Dog is a type of mammal and has some additional
characteristics such as slobbering and running in circles. The
object oriented concept that allows the dog to derive the
common characteristics of the mammal is called _____________
a. Polymorphism
b. Inheritance
c. Encapsulation
d. Prototyping
Question No : 3
b. Inheritance
Inheritance is the mechanism by which one object
acquires the properties of another object.
Answer
What are the major benefits of object oriented
programming?
a. Ease of maintenance
b. Reduced development time due to reusability
c. Consumes less resources during execution
d. Top-down approach
Question No : 4
Ease of maintenance
Reduced development time due to reusability
Answer
Consider the following inheritance diagram. The
Get_vehicle_type function is present in both the super class
(Four wheeler) and the derived class (Car). If the Car
receives a message for Get_vehicle_type, which method is
executed?
Question No : 5
Four wheeler
Member functionGet_vehicle_type
Car
Member functionGet_vehicle_type
The method in the Car object is executed. A derived
class can override the base class member function
by supplying a new version of that function with the
same signature. When an object receives a
message, it first checks for the availability of the
method in this object. If it is not available, then it
consults its super-class. The first method found in
the hierarchy takes precedence and executed.
Answer
Explain in simple terms why an object-oriented
language like C++ is different from a Classical
language like C.
Question No : 6
In a classical language like C, data-structures and their procedures
tended to group logically, but it is the programmer's duty to devise and
enforce these groupings. In OOP, the language groups procedures with
their data type. This produces a decomposition grouped around the types
in a program. The programmer does not have to match up the right
procedure with the right data-type. Instead, variables know which
operations they implement.
OOP yields better decomposition and more opportunities for code reuse.
These strengths are especially important for writing large programs,
packaging up code into libraries for use by others, and programming in
teams.
Answer
Consider the following statements:
Real world objects contain _________ and _________.
A software objects’ state is stored in ____________
A software object’s behaviour is exposed through _____________
Hiding internal data from the outside world and accessing it only
through publicly exposed methods is known as data ______________
A blue print for a software object is called as a ________
Pick and fill the right words from the following.
(Behaviour, Methods, Encapsulation, State, fields, Class)
Question No : 7
State
Behaviour
Fields
Methods
Encapsulation
Class
Answer
Scenario:
Consider a Stack data structure (Last-in, First-Out). You might have a
program that requires three types of stack. One stack is used for Integer
values one stack is used for floating-point values, and one for
characters. The algorithm that implements each stack is the same, even
though the data being stored differs. In a non-Object Oriented language
like C, you would be required to create three different sets of stack
routines, with each set using different names. In OOP language you
specify a general set of stack routines that all share the same names.
Which feature of OOP allows you to implement the above?
Question No : 8
Polymorphism based on the principle that of
‘One interface, multiple methods’
Answer
Scenario:
Real-world objects share two characteristics: They all
have state and behavior. Identifying them is a great way
to begin thinking in terms of Object oriented
programming.
Give some examples of state and behavior for the
following
a. Animal ( say dog)
b. Vehicle( say bicycle)
Question No : 9
Dogs have
state (name, color, breed, hungry)
behavior (barking, fetching, wagging tail)
Bicycles have
state (current gear, current pedal, current speed)
behavior (changing gear, changing pedal, applying
brakes)
Answer
Scenario:
Imagine a programming environment where all the code is contained in a
single continuous block
The execution of flow statements are implemented through use of Goto
statements so that one can jump to a specified section of code. The source
code is notoriously difficult to read and debug. But being a scripting
language used for automating batch execution, the program typically runs
fast and does its job.
What kind of a programming environment are we referring to in
the above?
Is it object oriented, Structured or Unstructured?
Question No : 10
Unstructured
Answer
What is the output of the following code?
a. 14
b. 13
c. 12
d. 11
Question No : 11
#include<iostream.h>#include<string.h>void main(){
cout<<strlen(“Hello, World.\n” )<<” \n” ;}
a. 14
Answer
What is the output of the following code?
a. 123
b. Compile time error
c. 321
d. Run time error
Question No : 12
#include<iostream.h>void main(){
/* this is /* an example */ of nested comment */cout<<123<<endl;
}
b. Compile time error
Answer
What is the output of the following code?
a. 1
b. Compile time error
c. 0
d. Run time error
Question No : 13
#include<iostream.h>void main(){
cout<<;}
b. Compile time error
(expected primary-expression before ';' token)
Answer
What is the output of the following code?
a. 20, 20
b. 20, 21
c. 21, 22
d. 22, 22
Question No : 14
#include<iostream.h>void main(){
int a = 20;int &n = a;n=a++;a=n++;cout<<a <<”,”<<n<<endl;
}
a. 20, 20
Answer
What is the output of the following code?
a. 21, 21
b. 20, 21
c. 21, 22
d. Compile Time Error
Question No : 15
#include<iostream.h>void main(){
int a = 20,b=100;int &n = a;n=a++;n = &b;cout<<a <<” ,”<<n<<endl;
}
a. Compile Time Error
Explanation: Invalid conversion from int* to int
Answer
What is the output of the following code?
a. 10
b. False
c. 1
d. Error
Question No : 16
#include<iostream.h>void main(){
bool a=10;cout<<a<<endl;
}
c. 1
Answer
What is the output of the following code?
a. 101
b. 100
c. None
d. Error : Cannot use main as identifier
Question No : 17
#include<iostream.h>void main(){
int main;main = 100;cout<<main++<<endl;
}
b. 100
Answer
What is the output of the following code?
a. 0, 0, 0
b. 1, 0, 0
c. 2, 2, 2
d. 3, 2, 2
Question No : 18
#include<iostream.h>void main(){
int a=0,x;x = ++a * --a;cout<<++a<< “,“ << a++ << ”,” << x <<endl;
}
b. 1, 0, 0
Answer
What is the output of the following code?
a. 32
b. 0
c. Compile Time Error
d. Run Time Error
Question No : 19
#include<iostream.h>void main(){
a=32;cout<<a<<endl;int a;
}
c. Compile Time Error
Answer
What is wrong with the following program?
a. There is nothing wrong in the program.
b. Variable ‘b’ must not be initialized in the loop
c. Variable ‘b’ must declared before do-while the loop
d. The condition for while loop is not valid
Question No : 20
#include<iostream.h>void main(){
do{
int b=0;cout<<b;b++;
}while(b!=10);}
c. Variable ‘b’ must declared before do-while the
loop
Answer
What is the output of the following program?
a. 14, 14
b. 15, 14
c. 14, 15
d. 15, 15
Question No : 21
#include<iostream.h>void main(){
char p[]="This is a test";cout<<sizeof(p)<<","<<strlen(p);
}
b. 15, 14
Answer
What is wrong with the following program?
a. Array ‘a’ is not initialized properly
b. There is no problem
c. Redeclaration of variable ‘i’
d. There is a run time error
Question No : 22
#include<iostream.h>void main(){
int a[5] = {0};for(int i=0;i<2;i++)a[i]=i;for(int i=0;i<5;i++)cout<<a[i]<<endl;
}
b. There is no problem
Answer
What is the output of the following program?
a. 100, 2,3,22,400
b. 0,0,0,0,0
c. Error
d. 400,22,3,2,100
Question No : 23
#include<iostream.h>void main(){
int a[5] = {100,2,3,22,400};int b[5];b=a;for(int i=0;i<5;i++)cout<<b[i]<<endl;
}
c. Error
C++ forbids assignment of arrays
Answer
What is the output of the following program?
a. No output
b. 1 2 3 4 5
c. 1 2 3 0 0
d. There is a run time error
Question No : 24
#include<iostream.h>void main(){
int a[5] = {1,2,3};for(int i=0;i<5;i++)cout<<a[i]<<endl;
}
c. 1 2 3 0 0
Answer
What is the output of the following program?
a. 51, 42, 33, 24, 15,
b. 40, 30, 20, 10, 00,
c. 41, 32, 23, 14, 05,
d. 00, 10, 20, 30, 40,
Question No : 25
#include<iostream.h>void main(){
int i=5,j=0;while(i-- || j++){
cout<<i<<""<<j<<” ,” ;}
}
b. 40, 30, 20, 10, 00,
Answer
What is the output of the following program?
a. Error
b. 1 0
c. 0 1
d. 0 0
Question No : 26
#include<iostream.h>void main(){
int a;bool b;a = 12 > 100;b = 12 >= 100;cout<<a<<" "<<b<<endl;
}
d. 0 0
Answer
What is the output of the following program?
a. Error
b. 100, 200, 300, 100,
c. 300, 200, 100, 0,
d. 300, 200, 100, 1,
Question No : 27
#include<iostream.h>void main(){
int a = 100;{
int a = 200;{
int a = 300cout<<a<<",";
}cout<<a<<",";}cout<<a<<",";cout<<::a<<",";
}
d. 300, 200, 100, 1,
Answer
What is the output of the following program?
a. Error
b. 1000
c. 100
d. 0
Question No : 28
#include<iostream.h>void main(){
int x=10;(x<0)?(int a =100):(int a =1000);cout<<a;
}
a. Error
Answer
What is the output of the following program?
a. 0
b. 1
c. Compile Time Error
d. Run Time Error
Question No : 29
#include<iostream.h>void main(){
int a = 0;cout<<(a = 10/a);
}
d. Run Time Error
Answer
What is the output of the following program?
a. 1 2 3 4 5
b. 2 4 6 8 10
c. Compile Time Error
d. Run Time Error
Question No : 30
#include<iostream.h>void main(){
int x=0;while(x++<5){
static x;x+=2;cout<<x<<" ";
}}
c. Compile Time Error
C++ forbids declaration of `x' with no type
Answer
What is the output of the following program?
a. Both the strings are same
b. Both the strings are not same
c. Compile Time Error
d. Run Time Error
Question No : 31
#include<iostream.h>void main(){
char str1[]=” India” , str2[]=” India”;if(str1==str2)cout<<”Both the strings are same”;elsecout<<”Both the strings are not same”;
}
b. Both the strings are not same
Answer
What is the output of the following code if user
enters “This is a test”?
a. This is a test
b. This is a
c. This
d. Error
Question No : 32
#include<iostream.h>#include<string.h>void main(){
char str[8];cin>>str;cout<<str;
}
c. This
Answer
What is the output of the following code?
a. 10 20
b. 10 10
c. 20 20
d. 20 10
Question No : 33
#include<iostream.h>void main(){
int arr[] = {10,20,30,40,50};int *ptr = arr;cout<< *ptr++<<" "<<*ptr;
}
a. 10 20
Answer
What is the output of the following code?
a. 6
b. 3
c. Compile Time Error
d. 0
Question No : 34
#include<iostream.h>void main(){
int arr[] = {10,20,30,40,50};int x,*ptr1 = arr, *ptr2=&arr[3];x = ptr2 - ptr1;cout<<x;
}
b. 3
Answer
What is the output of the following code?
a. 55 33
b. 33 55
c. 11 55
d. 11 33
Question No : 35
#include<iostream.h>void main(){
int arr[][3]={0,11,22,33,44,55};int *a = &arr[0][0];cout<<arr[1][2]<<" "<<*(a+3);
}
a. 55 33
Answer
What is the output of the following code?
a. 10
b. 3
c. 0
d. 11
Question No : 36
#include<iostream.h>void main(){
int arr[2][3][2]={{{2,4},{7,8},{3,4},}, {{2,2},{2,3},{3,4}, }};cout<<(*(*(*arr+1)+2)+0)+7;
}
a. 10
Answer
What is the output of the following code?
a. 16
b. 7
c. 11
d. 10
Question No : 37
#include<iostream.h>void main(){
int arr[2][3][2]={{{2,4},{7,8},{3,4},}, {{2,2},{2,3},{3,4}, }};cout<<**(*arr+1)+2+7;
}
a. 16
Answer
What is the namespace?
Question No : 38
Namespaces allow to group entities like classes,
objects and functions under a name. This way the
global scope can be divided in "sub-scopes", each
one with its own name
The format of namespaces is:
Answer
namespace identifier{
entities}
What is the Basic nature of "cin" and "cout" and
what concept or principle we are using on those
two?
Question No : 39
Basically "cin and cout" are INSTANCES of istream
and ostream classes respectively, and the
concept which is used on cin and cout is operator
overloading. Extraction and Insertion operators
are overloaded for input and output operations.
Answer
What is scope resolution operator?
Question No : 40
The scope resolutions operator is also used to
find the value of a variable out of the scope of the
variable.
Example:
::i refers to the value just before the scope (i.e. 10)
Answer
int i=10;main(){
int i=5;cout<<::i;cout<<i;
}
Can main() be overridden?
Question No : 41
In any application, there can be only one main
function. In c++, main is not a member of any
class. There is no chance of overriding.
Answer
In what way macros are different from template?
Question No : 42
In case of macros, there is no way for the compiler to verify that the macro
parameters are of compatible types. The macro is expanded without any
special type checking.
If macro parameter has a post incremented variable (like c++), the increment
is performed two times.
Because macros are expanded by the preprocessor, compiler error messages
will refer to the expanded macro, rather than the macro definition itself. Also,
the macro will show up in expanded form during debugging.
Example: Macro:
Answer
#define min(i, j) (i < j ? i : j)template:template<class T> T min (T i, T j) {
return i < j ? i : j;}
The design of classes in a way that hides the
details of implementation from the user is known
as:
a. Encapsulation
b. Information Hiding
c. Data abstraction
d. Inheritance
Question No : 43
a. Encapsulation
Answer
Which of the following keywords do you think can
be used when declaring static members in a
class? Encapsulation
(i) Public (ii) Private (iii) Protected
a. i, ii and iii
b. i and ii
c. Only i
d. i and iii
Question No : 44
c. Only i
Answer
I want a nonmember function to have access to
the private members of a class. The class must
declare that function:
a. friend
b. inline
c. static
d. virtual
Question No : 45
a. friend
Answer
What is a base class?
a. An abstract class that is at the top of the inheritance
hierarchy
b. A class with a pure virtual function in it
c. A class that inherits from another class
d. A class that is inherited by another class, and thus is
included in that class
Question No : 46
b. A class with a pure virtual function in it
Answer
When do preprocessor directives execute?
a. Before the compiler compiles the program
b. After the compiler compiles the program
c. At the same time as the compiler compiles the
program
d. Never executes
Question No : 47
a. Before the compiler compiles the program
Answer
An address is a ________ while a pointer is a
_________.
a. Variable, Location
b. Variable, Constant
c. Constant, Variable
d. Constant, Location
Question No : 48
a. Variable, Location
Answer
What is the difference between declaration and
definition?
Question No : 49
The definition is the one that actually allocates space, and
provides an initialization value, if any.
There can be many declarations, but there must be exactly
one definition.
A definition tells the compiler to set aside storage for the
variable.
A declaration makes the variable known to parts of these
program that may wish to use it.
A variable might be defined and declared in the same
statement.
Answer
What is Object?
Question No : 50
An Object is an instance of a class.
Example:
Ferrari and maruthi are objects of class car.
An Object is basic runtime entities of its class
Answer
We can overload assignment operator as a
normal function. But we cannot overload
assignment operator as friend function why?
Question No : 51
Because, Friend functions do not have a "this"
pointer
Answer
What is an inline function?
Question No : 52
Inline functions are like macros in c language. The functions
are expanded in the line where it is invoked at the time of
compilation. These functions should not include any loops or
static members inside it. And also it should not be a recursive
one. Inline functions should return a value.
Keyword ‘inline’ should be included in the function definition.
Answer
inline int add(int a,int b){return a+b;}
void main(){int x=5;int y=10;cout<<?The sum is : ?<<add(x,y);}
What is the difference between deep copy and
shallow copy?
Question No : 53
A Shallow copy of an object copies all of the member field
values. If there are fields that point to dynamically allocated
memory, Shallow copy copies the pointer but the memory it
points to will not be copied. The field in both the original
object and the copy will then point to the same dynamically
allocated memory.
Whereas Deep copy copies all the fields and makes copies of
dynamically allocated memory pointed to by the fields. To
make a deep copy, one needs to overwrite the copy
constructor and assignment operator
Answer
Can we make any program in c++ without using
any header file?
Question No : 54
No
Answer
Can we have more than one constructor in a
class?
Question No : 55
Yes
Answer
What is public, private and protected?
Question No : 56
Public, protected and private are three access
specifiers in C++.
Public data members and member functions are
accessible outside the class.
Protected data members and member functions
are only available to derived classes.
Private data members and member functions
cannot be accessed outside the class.
Answer
What are pointers?
Question No : 57
A pointer is an address location of another variable. It
is a value that designates the address or memory
location of some other value (usually value of a
variable).
The value of a variable can be accessed by a pointer
which points to that variable.
To do so, the reference operator (&) is pre-appended
to the variable that holds the value.
A pointer can hold any data type, including functions.
Answer
What is NULL pointer?
Question No : 58
A pointer that points to a no valid location is known as
null pointer. Null pointers are useful to indicate
special cases.
Example:
No next node pointer in case of a linked list, which is an
indication of errors that pointer returned from functions.
Answer
What is reference variable in C++?
Question No : 59
A reference variable is just like pointer with few differences.
It is declared using & operator.
A reference variable must always be initialized.
The reference variable once defined to refer to a variable
cannot be changed to point to other variable.
You cannot create an array of references the way it is
possible with pointer.
Answer
What is the output of the following program?
Question No : 60
#include <iostream.h>class myclass{int val;
Public:myclass(int I){ val = I; cout<<”constructing \n”;}~myclass( ) { cout<<"destructing\n";}int getval( ) { return val;}
};void display(myclass ob){
cout<<ob.getval()<<'\n';}main(){
myclass a(10);display(a);return 0;
}
constructing
10
destructing
When a copy of an object is created to be used as an
argument to a function, the “constructor" function is not
called, but "copy constructor " is called. However, when a
copy is destroyed (usually by going out of scope when
the function returns), the destructor function is called.
Answer
What is the output of the following program?
Question No : 61
#include "iostream.h"class base {
public:base(){ cout << "constructing base \n";}~base(){ cout << "destructing base \n";}
};
class derived: public base{public:derived() {cout<<"constructing derived \n";}~derived() {cout<<"destructing derived \n";}
};
main(){
derived ob;return 0;
}
constructing base
constructing derived
destructing derived
destructing base
Constructors are called in the order of derivation and
destructors are called in the reverse order.
Answer
Observe the following piece of code and reduce
the amount of duplicate code.
Question No : 62
if(book::title=new char[256] == 0){ cerr <<"Error allocating memory\n"; exit(0);}
if(book::author=new char[64] ==0){ cerr<<"Error allocating memory\n"; exit(0);}
if(book::publisher=new char[128] ==0){ cerr<<"Error allocating memory\n"; exit(0);}
The following code snippet is appearing 3 times
in the original piece of code.
We can have it just one time. Remaining two
times it is redundant
Answer
book::title=new char[256] ;book::author=new char[64] ;book::publisher=new char[128];if((book::title&&book::author&&book::publisher) ==0){
cerr<<"Error allocating memory\n";exit(0);
}
cerr<<"Error allocating memory\n";exit(0);
When the interface exposed by C++ component
is not compatible to the client, what needs to be
done?
Question No : 63
Client has to use Adapter design pattern to make
interface compatible. Adapter publicly
implements the interface to be used by the client
Answer
What is the output of following program? Why?
Question No : 64
#include <iostream.h> class ConstFromConst { private : int num1; int num2; public: ConstFromConst(){num1=100;num2=101;ConstFromConst(109);}; ConstFromConst(int i1){num1 = 109;}; ConstFromConst(int i1,int i2){}; void print() { cout << num1<<endl; cout << num2<<endl; } virtual ~ConstFromConst(){}; }; void main() { //your code ConstFromConst a; a.print(); }
100
101
num1 and num2 will be 100 and 101 and not 109 and
101.
So when you call another constructor, another object is
created and then destroyed with in the scope of the
constructor that calls another constructor.
So calling another constructor is pretty useless in the
body of a constructor.
Answer
Instead of char* what type can be used in pure
C++ to describe the String?
Question No : 65
basic_string is the class defined for this
purpose, string is typedefed as
basic_string<char>
Answer
What happens if there is no catch block suitable
to the throw type?
Question No : 66
Application crashes due to unhandled exception
Answer
Is the program given below a valid one?
Question No : 67
#include<iostream.h>int func(int i);double func(int i);void main(void){
cout<<func(10);cout<<func(10.201);
}int func(int i){
return i;}double func(int i){
return i;}
No, because you cannot overload functions if they
differ only in terms of the data type they return.
They should vary with arguments they take in,
that are called as function overloading.
Answer
Determine the output for the following program.
Question No : 68
#include <iostream>using namespace std;class arith {public:void calc(int num1){
cout<<"Square of a given number: " <<num1*num1 <<endl;}void calc(int num1, int num2 ){
cout<<"Product of two whole numbers: " <<num1*num2 <<endl;}};int main() //begin of main function{
arith a;a.calc(5);a.calc(6,7);
}
Square of a given number: 25
Product of two whole numbers: 42
Answer
Determine the output for the following program.
Question No : 69
#include <iostream>using namespace std;class arith {public:void calc(int num1){
cout<<"Square of a given number: " <<num1*num1 <<endl;}void calc(int num1, int num2 ){
cout<<"Product of two whole numbers: " <<num1*num2 <<endl;}};int main() //begin of main function{
arith a;a.calc(5);a.calc(6,7);
}
Square of a given number: 25
Product of two whole numbers: 42
Answer
How does the C++ compiler process inline
functions included in a header file?
Question No : 69
The compiler, when it encounters a definition of an inline function as a
header, puts the function type (the signature combined with the return
value) and the function body in its symbol table. When the function is used
somewhere in the program, the compiler not only checks to ensure that
the call is correct but the return value is also used correctly. It then
substitutes the function body for the function call, thereby eliminating the
overhead. An inline function in a header file has a special status, since one
must include the header file containing the function and its definition in
every file where the function is used, and doesn’t end up with multiple
definition errors (however, the definition must be identical in all places
where the inline function is included).
Answer
What is a template?
Question No : 70
Templates allow creating generic functions that admit any
data type as parameters and return values without having
to overload the function with all the possible data types.
Its prototype is any of the two following ones:
The only difference between both prototypes is the use of
keyword class or typename, its use is indistinct since both
expressions have exactly the same meaning and behave
exactly the same way
Answer
template <class indetifier> function_declaration;template <typename indetifier> function_declaration;
We want to swap int, char , float.
We need not define 3 functions; instead we can implement
it by using templete
Answer
function definitiontemplate <class T>T fn_name(argument name)