SlideShare a Scribd company logo
1 of 144
C++
Explain the different forms of Polymorphism.
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.
What are the differences between Constructors and Methods?
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 _____________ Polymorphism Inheritance Encapsulation Prototyping
b. Inheritance  Inheritance is the mechanism by which one object acquires the properties of another object.
What are the major benefits of object oriented programming?  Ease of maintenance Reduced development time due to reusability Consumes less resources during execution Top-down approach
Ease of maintenance Reduced development time due to reusability
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? Four wheeler Member function Get_vehicle_type Car Member function Get_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.
Explain in simple terms why an object-oriented language like C++ is different from a Classical language like C.
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.
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)
State Behaviour Fields Methods Encapsulation Class
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?
Polymorphism based on the principle that of  ‘One interface, multiple methods’
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  Animal ( say dog) Vehicle( say bicycle)
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)
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?
Unstructured
What is the output of the following code? 14 	 13	 12	 11 #include<iostream.h> #include<string.h> void main() { 	cout<<strlen(“Hello, World.” )<<” ” ; }
a. 14
What is the output of the following code? 123 	 Compile time error 	 321	 Run time error #include<iostream.h> void main() { /* this is /* an example */ of nested comment */ cout<<123<<endl; }
b. Compile time error
What is the output of the following code? 1	 Compile time error 	 0	 Run time error #include<iostream.h> void main() { cout<<; }
b. Compile time error  	(expected primary-expression before ';' token)
What is the output of the following code? 20, 20  20, 21  21, 22  22, 22  #include<iostream.h> void main() { 	int a = 20; 	int &n = a; 	n=a++; 	a=n++; 	cout<<a <<”,”<<n<<endl; }
a. 20, 20
What is the output of the following code? 21, 21  20, 21  21, 22  Compile Time Error #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
What is the output of the following code? 10 False  1 Error #include<iostream.h> void main() { bool a=10; 	cout<<a<<endl; }
c. 1
What is the output of the following code? 101 100 None Error : Cannot use main as identifier #include<iostream.h> void main() { int main; 	main = 100; 	cout<<main++<<endl; }
b. 100
What is the output of the following code? 0, 0, 0  1, 0, 0  2, 2, 2  3, 2, 2 #include<iostream.h> void main() { int a=0,x; x = ++a * --a; cout<<++a<< “,“ << a++ << ”,” << x <<endl; }
b. 1, 0, 0
What is the output of the following code? 32 0 Compile Time Error Run Time Error #include<iostream.h> void main() { 	a=32; 	cout<<a<<endl; 	int a; }
c. Compile Time Error
What is wrong with the following program? There is nothing wrong in the program.  Variable ‘b’ must not be initialized in the loop  Variable ‘b’ must declared before do-while the loop The condition for while loop is not valid #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
What is the output of the following program? 14, 14  15, 14 14, 15 15, 15 #include<iostream.h> void main() { char p[]="This is a test"; cout<<sizeof(p)<<","<<strlen(p); }
b. 15, 14
What is wrong with the following program? Array ‘a’ is not initialized properly  There is no problem Redeclaration of variable ‘i’  There is a run time error #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
What is the output of the following program? 100, 2,3,22,400  0,0,0,0,0  Error  400,22,3,2,100 #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
What is the output of the following program? No output	 1 2 3 4 5  1 2 3 0 0  There is a run time error #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
What is the output of the following program? 51, 42, 33, 24, 15,  40, 30, 20, 10, 00,  41, 32, 23, 14, 05,  00, 10, 20, 30, 40, #include<iostream.h> void main() { int i=5,j=0; while(i-- || j++) { 	cout<<i<<""<<j<<” ,” ; } }
b. 40, 30, 20, 10, 00,
What is the output of the following program? Error 	 1 0 0 1 0 0  #include<iostream.h> void main() { int a; bool b; a = 12 > 100; b = 12 >= 100; cout<<a<<" "<<b<<endl; }
d. 0 0
What is the output of the following program? Error 	 100, 200, 300, 100,  300, 200, 100, 0,  300, 200, 100, 1,  #include<iostream.h> void main() { int a = 100; { 	int a = 200; 	{	 	int a = 300 	cout<<a<<","; 	} cout<<a<<","; } cout<<a<<","; cout<<::a<<","; }
d. 300, 200, 100, 1,
What is the output of the following program? Error 	 1000 100 0 #include<iostream.h> void main() { int x=10; (x<0)?(int a =100):(int a =1000); cout<<a; }
a. Error
What is the output of the following program? 0 1 Compile Time Error Run Time Error #include<iostream.h> void main() { int a = 0; cout<<(a = 10/a); }
d. Run Time Error
What is the output of the following program? 1 2 3 4 5 	 2 4 6 8 10  Compile Time Error Run Time Error #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
What is the output of the following program? Both the strings are same  	 Both the strings are not same Compile Time Error Run Time Error #include<iostream.h> void main() { char str1[]=” India” , str2[]=” India”; if(str1==str2) cout<<”Both the strings are same”; else cout<<”Both the strings are not same”; }
b. Both the strings are not same
What is the output of the following code if user enters “This is a test”? 	 This is a test  This is a 	 This Error #include<iostream.h> #include<string.h> void main() { char str[8]; cin>>str; cout<<str; }
c. This
What is the output of the following code? 	 10  20 	 10  10 20  20 20  10 #include<iostream.h> void main() { int arr[] = {10,20,30,40,50}; int *ptr = arr; cout<< *ptr++<<" "<<*ptr; }
a. 10  20
What is the output of the following code? 	 6	 3 Compile Time Error 0 #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
What is the output of the following code? 	 55 33 	 33 55  11 55  11 33  #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
What is the output of the following code? 	 10 3 0 11 #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
What is the output of the following code? 	 16 7 11 10 #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
What is the namespace?
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:	 namespace identifier{	entities}
What is the Basic nature of "cin" and "cout" and what concept or principle we are using on those two?
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.
What is scope resolution operator?
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) int i=10;main(){	int i=5;	cout<<::i;	cout<<i;}
Can main() be overridden?
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.
In what way macros are different from template?
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: #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: Encapsulation  Information Hiding  Data abstraction  Inheritance
a. Encapsulation
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 i, ii and iii  i and ii Only i  i and iii
c. Only i
I want a nonmember function to have access to the private members of a class. The class must declare that function:  friend  inline static virtual
a. friend
What is a base class?  An abstract class that is at the top of the inheritance hierarchy A class with a pure virtual function in it A class that inherits from another class A class that is inherited by another class, and thus is included in that class
b. A class with a pure virtual function in it
When do preprocessor directives execute?  Before the compiler compiles the program  After the compiler compiles the program  At the same time as the compiler compiles the program  Never executes
a. Before the compiler compiles the program
An address is a ________ while a pointer is a _________.  Variable, Location  Variable, Constant Constant, Variable  Constant, Location
a. Variable, Location
What is the difference between declaration and definition?
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.
What is Object?
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
We can overload assignment operator as a normal function. But we cannot overload assignment operator as friend function why?
Because, Friend functions do not have a "this" pointer
What is an inline function?
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. 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?
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
Can we make any program in c++ without using any header file?
No
Can we have more than one constructor in a class?
Yes
What is public, private and protected?
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.
What are pointers?
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.
What is NULL pointer?
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.
What is reference variable in C++?
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.
What is the output of the following program? #include <iostream.h> class myclass { int val; Public: myclass(int I) {  val = I; cout<<”constructing ”;} ~myclass( ) { cout<<"destructing";} int getval( ) { return val;} }; void display(myclass ob) { cout<<ob.getval()<<''; } 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.
What is the output of the following program? #include "iostream.h" class base { public: base(){ cout << "constructing base ";} ~base(){ cout << "destructing base ";} }; class derived: public base{ public: derived() {cout<<"constructing derived ";} ~derived() {cout<<"destructing derived ";} }; 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.
Observe the following piece of code and reduce the amount of duplicate code. if(book::title=new char[256] == 0) { cerr <<"Error allocating memory";    exit(0); } if(book::author=new char[64] ==0) { cerr<<"Error allocating memory";   exit(0); } if(book::publisher=new char[128] ==0) { cerr<<"Error allocating memory";   exit(0); }
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"; 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 cerr<<"Error allocating memory"; exit(0);
When the interface exposed by C++ component is not compatible to the client, what needs to be done?
Client has to use Adapter design pattern to make interface compatible. Adapter publicly implements the interface to be used by the client
What is the output of following program? Why? #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.
Instead of char* what type can be used in pure C++ to describe the String?
basic_string is the class defined for this purpose, string is typedefed as  basic_string<char>
What happens if there is no catch block suitable to the throw type?
Application crashes due to unhandled exception
Is the program given below a valid one? #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.
Determine the output for the following program. #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
Determine the output for the following program. #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
How does the C++ compiler process inline functions included in a header file?
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).
What is a template?
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 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 function definition template <class T> T fn_name(argument name)

More Related Content

What's hot

01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
Tareq Hasan
 

What's hot (19)

Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
 
C++ Interview Questions
C++ Interview QuestionsC++ Interview Questions
C++ Interview Questions
 
Differences between c and c++
Differences between c and c++Differences between c and c++
Differences between c and c++
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
 
C and C++ Industrial Training Jalandhar
C and C++ Industrial Training JalandharC and C++ Industrial Training Jalandhar
C and C++ Industrial Training Jalandhar
 
01. introduction to C++
01. introduction to C++01. introduction to C++
01. introduction to C++
 
Data types in c++
Data types in c++ Data types in c++
Data types in c++
 
C vs c++
C vs c++C vs c++
C vs c++
 
C++ interview question
C++ interview questionC++ interview question
C++ interview question
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Qcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharpQcon2011 functions rockpresentation_f_sharp
Qcon2011 functions rockpresentation_f_sharp
 
Polymorphism Using C++
Polymorphism Using C++Polymorphism Using C++
Polymorphism Using C++
 

Viewers also liked

Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism
023henil
 
1. introduction to bioethics
1.  introduction to bioethics1.  introduction to bioethics
1. introduction to bioethics
Mevelle Asuncion
 
Moral issue of birth deformities
Moral issue of birth deformitiesMoral issue of birth deformities
Moral issue of birth deformities
Mevelle Asuncion
 

Viewers also liked (20)

Seminar on polymorphism
Seminar on polymorphismSeminar on polymorphism
Seminar on polymorphism
 
Dynamic Polymorphism in C++
Dynamic Polymorphism in C++Dynamic Polymorphism in C++
Dynamic Polymorphism in C++
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
Polymorphism in c++ ppt (Powerpoint) | Polymorphism in c++ with example ppt |...
 
Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++Static and Dynamic polymorphism in C++
Static and Dynamic polymorphism in C++
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
1. introduction to bioethics
1.  introduction to bioethics1.  introduction to bioethics
1. introduction to bioethics
 
Moral issue of birth deformities
Moral issue of birth deformitiesMoral issue of birth deformities
Moral issue of birth deformities
 
Artificial insemination
Artificial inseminationArtificial insemination
Artificial insemination
 
Три хита по управлению проектами от компании "Гибкие технологии"
Три хита по управлению проектами от компании "Гибкие технологии"Три хита по управлению проектами от компании "Гибкие технологии"
Три хита по управлению проектами от компании "Гибкие технологии"
 
nsquared - Neil Roodyn
nsquared - Neil Roodyn nsquared - Neil Roodyn
nsquared - Neil Roodyn
 
Project Factory - Guy Gadney
Project Factory - Guy Gadney Project Factory - Guy Gadney
Project Factory - Guy Gadney
 
CS Education Event - University of NSW
CS Education Event - University of NSWCS Education Event - University of NSW
CS Education Event - University of NSW
 
CS Education Event - Full Extent
CS Education Event - Full ExtentCS Education Event - Full Extent
CS Education Event - Full Extent
 
Miguel rojo
Miguel rojoMiguel rojo
Miguel rojo
 

Similar to ICT C++

Eclipse Modeling Framework
Eclipse Modeling FrameworkEclipse Modeling Framework
Eclipse Modeling Framework
Ajay K
 
Mobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, PuneMobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, Pune
Bhuvan Khanna
 

Similar to ICT C++ (20)

C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]
 
C++ programing lanuage
C++ programing lanuageC++ programing lanuage
C++ programing lanuage
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org)  (usef...Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org)  (usef...
Lab manual object oriented technology (it 303 rgpv) (usefulsearch.org) (usef...
 
maXbox Starter 45 Robotics
maXbox Starter 45 RoboticsmaXbox Starter 45 Robotics
maXbox Starter 45 Robotics
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
Unit 1 of c++ part 1 basic introduction
Unit 1 of c++ part 1 basic introductionUnit 1 of c++ part 1 basic introduction
Unit 1 of c++ part 1 basic introduction
 
C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
 
PRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptxPRINCE PRESENTATION(1).pptx
PRINCE PRESENTATION(1).pptx
 
Eclipse Modeling Framework
Eclipse Modeling FrameworkEclipse Modeling Framework
Eclipse Modeling Framework
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Intro to C++ - Class 2 - Objects & Classes
Intro to C++ - Class 2 - Objects & ClassesIntro to C++ - Class 2 - Objects & Classes
Intro to C++ - Class 2 - Objects & Classes
 
Intro To C++ - Class 2 - An Introduction To C++
Intro To C++ - Class 2 - An Introduction To C++Intro To C++ - Class 2 - An Introduction To C++
Intro To C++ - Class 2 - An Introduction To C++
 
System programmin practical file
System programmin practical fileSystem programmin practical file
System programmin practical file
 
Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++Generalized Functors - Realizing Command Design Pattern in C++
Generalized Functors - Realizing Command Design Pattern in C++
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
Going open source with small teams
Going open source with small teamsGoing open source with small teams
Going open source with small teams
 
EclipseCon 2008: Fundamentals of the Eclipse Modeling Framework
EclipseCon 2008: Fundamentals of the Eclipse Modeling FrameworkEclipseCon 2008: Fundamentals of the Eclipse Modeling Framework
EclipseCon 2008: Fundamentals of the Eclipse Modeling Framework
 
Mobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, PuneMobile Developer Summit 2012, Pune
Mobile Developer Summit 2012, Pune
 

More from Karthikeyan A K (6)

Web 3.0
Web 3.0Web 3.0
Web 3.0
 
Rails sopinoffs - Haml
Rails sopinoffs - HamlRails sopinoffs - Haml
Rails sopinoffs - Haml
 
Large scale web apps
Large scale web appsLarge scale web apps
Large scale web apps
 
The magic of ruby
The magic of rubyThe magic of ruby
The magic of ruby
 
Data Structure
Data StructureData Structure
Data Structure
 
C programming
C programmingC programming
C programming
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 

ICT C++

  • 1. C++
  • 2. Explain the different forms of Polymorphism.
  • 3. 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.
  • 4. What are the differences between Constructors and Methods?
  • 5.
  • 6. 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 _____________ Polymorphism Inheritance Encapsulation Prototyping
  • 7. b. Inheritance Inheritance is the mechanism by which one object acquires the properties of another object.
  • 8. What are the major benefits of object oriented programming? Ease of maintenance Reduced development time due to reusability Consumes less resources during execution Top-down approach
  • 9. Ease of maintenance Reduced development time due to reusability
  • 10. 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? Four wheeler Member function Get_vehicle_type Car Member function Get_vehicle_type
  • 11. 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.
  • 12. Explain in simple terms why an object-oriented language like C++ is different from a Classical language like C.
  • 13. 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.
  • 14. 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)
  • 15. State Behaviour Fields Methods Encapsulation Class
  • 16. 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?
  • 17. Polymorphism based on the principle that of ‘One interface, multiple methods’
  • 18. 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 Animal ( say dog) Vehicle( say bicycle)
  • 19. 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)
  • 20. 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?
  • 22. What is the output of the following code? 14 13 12 11 #include<iostream.h> #include<string.h> void main() { cout<<strlen(“Hello, World.” )<<” ” ; }
  • 23. a. 14
  • 24. What is the output of the following code? 123 Compile time error 321 Run time error #include<iostream.h> void main() { /* this is /* an example */ of nested comment */ cout<<123<<endl; }
  • 26. What is the output of the following code? 1 Compile time error 0 Run time error #include<iostream.h> void main() { cout<<; }
  • 27. b. Compile time error (expected primary-expression before ';' token)
  • 28. What is the output of the following code? 20, 20 20, 21 21, 22 22, 22 #include<iostream.h> void main() { int a = 20; int &n = a; n=a++; a=n++; cout<<a <<”,”<<n<<endl; }
  • 30. What is the output of the following code? 21, 21 20, 21 21, 22 Compile Time Error #include<iostream.h> void main() { int a = 20,b=100; int &n = a; n=a++; n = &b; cout<<a <<” ,”<<n<<endl; }
  • 31. a. Compile Time Error Explanation: Invalid conversion from int* to int
  • 32. What is the output of the following code? 10 False 1 Error #include<iostream.h> void main() { bool a=10; cout<<a<<endl; }
  • 33. c. 1
  • 34. What is the output of the following code? 101 100 None Error : Cannot use main as identifier #include<iostream.h> void main() { int main; main = 100; cout<<main++<<endl; }
  • 36. What is the output of the following code? 0, 0, 0 1, 0, 0 2, 2, 2 3, 2, 2 #include<iostream.h> void main() { int a=0,x; x = ++a * --a; cout<<++a<< “,“ << a++ << ”,” << x <<endl; }
  • 37. b. 1, 0, 0
  • 38. What is the output of the following code? 32 0 Compile Time Error Run Time Error #include<iostream.h> void main() { a=32; cout<<a<<endl; int a; }
  • 40. What is wrong with the following program? There is nothing wrong in the program. Variable ‘b’ must not be initialized in the loop Variable ‘b’ must declared before do-while the loop The condition for while loop is not valid #include<iostream.h> void main() { do { int b=0; cout<<b; b++; }while(b!=10); }
  • 41. c. Variable ‘b’ must declared before do-while the loop
  • 42. What is the output of the following program? 14, 14 15, 14 14, 15 15, 15 #include<iostream.h> void main() { char p[]="This is a test"; cout<<sizeof(p)<<","<<strlen(p); }
  • 44. What is wrong with the following program? Array ‘a’ is not initialized properly There is no problem Redeclaration of variable ‘i’ There is a run time error #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; }
  • 45. b. There is no problem
  • 46. What is the output of the following program? 100, 2,3,22,400 0,0,0,0,0 Error 400,22,3,2,100 #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; }
  • 47. c. Error C++ forbids assignment of arrays
  • 48. What is the output of the following program? No output 1 2 3 4 5 1 2 3 0 0 There is a run time error #include<iostream.h> void main() { int a[5] = {1,2,3}; for(int i=0;i<5;i++) cout<<a[i]<<endl; }
  • 49. c. 1 2 3 0 0
  • 50. What is the output of the following program? 51, 42, 33, 24, 15, 40, 30, 20, 10, 00, 41, 32, 23, 14, 05, 00, 10, 20, 30, 40, #include<iostream.h> void main() { int i=5,j=0; while(i-- || j++) { cout<<i<<""<<j<<” ,” ; } }
  • 51. b. 40, 30, 20, 10, 00,
  • 52. What is the output of the following program? Error 1 0 0 1 0 0 #include<iostream.h> void main() { int a; bool b; a = 12 > 100; b = 12 >= 100; cout<<a<<" "<<b<<endl; }
  • 54. What is the output of the following program? Error 100, 200, 300, 100, 300, 200, 100, 0, 300, 200, 100, 1, #include<iostream.h> void main() { int a = 100; { int a = 200; { int a = 300 cout<<a<<","; } cout<<a<<","; } cout<<a<<","; cout<<::a<<","; }
  • 55. d. 300, 200, 100, 1,
  • 56. What is the output of the following program? Error 1000 100 0 #include<iostream.h> void main() { int x=10; (x<0)?(int a =100):(int a =1000); cout<<a; }
  • 58. What is the output of the following program? 0 1 Compile Time Error Run Time Error #include<iostream.h> void main() { int a = 0; cout<<(a = 10/a); }
  • 59. d. Run Time Error
  • 60. What is the output of the following program? 1 2 3 4 5 2 4 6 8 10 Compile Time Error Run Time Error #include<iostream.h> void main() { int x=0; while(x++<5) { static x; x+=2; cout<<x<<" "; } }
  • 61. c. Compile Time Error C++ forbids declaration of `x' with no type
  • 62. What is the output of the following program? Both the strings are same Both the strings are not same Compile Time Error Run Time Error #include<iostream.h> void main() { char str1[]=” India” , str2[]=” India”; if(str1==str2) cout<<”Both the strings are same”; else cout<<”Both the strings are not same”; }
  • 63. b. Both the strings are not same
  • 64. What is the output of the following code if user enters “This is a test”? This is a test This is a This Error #include<iostream.h> #include<string.h> void main() { char str[8]; cin>>str; cout<<str; }
  • 66. What is the output of the following code? 10 20 10 10 20 20 20 10 #include<iostream.h> void main() { int arr[] = {10,20,30,40,50}; int *ptr = arr; cout<< *ptr++<<" "<<*ptr; }
  • 67. a. 10 20
  • 68. What is the output of the following code? 6 3 Compile Time Error 0 #include<iostream.h> void main() { int arr[] = {10,20,30,40,50}; int x,*ptr1 = arr, *ptr2=&arr[3]; x = ptr2 - ptr1; cout<<x; }
  • 69. b. 3
  • 70. What is the output of the following code? 55 33 33 55 11 55 11 33 #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); }
  • 72. What is the output of the following code? 10 3 0 11 #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; }
  • 73. a. 10
  • 74. What is the output of the following code? 16 7 11 10 #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; }
  • 75. a. 16
  • 76. What is the namespace?
  • 77. 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: namespace identifier{ entities}
  • 78. What is the Basic nature of "cin" and "cout" and what concept or principle we are using on those two?
  • 79. 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.
  • 80. What is scope resolution operator?
  • 81. 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) int i=10;main(){ int i=5; cout<<::i; cout<<i;}
  • 82. Can main() be overridden?
  • 83. 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.
  • 84. In what way macros are different from template?
  • 85. 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: #define min(i, j) (i < j ? i : j) template:template<class T> T min (T i, T j) {  return i < j ? i : j;}
  • 86. The design of classes in a way that hides the details of implementation from the user is known as: Encapsulation Information Hiding Data abstraction Inheritance
  • 88. 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 i, ii and iii i and ii Only i i and iii
  • 90. I want a nonmember function to have access to the private members of a class. The class must declare that function: friend inline static virtual
  • 92. What is a base class? An abstract class that is at the top of the inheritance hierarchy A class with a pure virtual function in it A class that inherits from another class A class that is inherited by another class, and thus is included in that class
  • 93. b. A class with a pure virtual function in it
  • 94. When do preprocessor directives execute? Before the compiler compiles the program After the compiler compiles the program At the same time as the compiler compiles the program Never executes
  • 95. a. Before the compiler compiles the program
  • 96. An address is a ________ while a pointer is a _________. Variable, Location Variable, Constant Constant, Variable Constant, Location
  • 98. What is the difference between declaration and definition?
  • 99. 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.
  • 101. 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
  • 102. We can overload assignment operator as a normal function. But we cannot overload assignment operator as friend function why?
  • 103. Because, Friend functions do not have a "this" pointer
  • 104. What is an inline function?
  • 105. 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. 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);}
  • 106. What is the difference between deep copy and shallow copy?
  • 107. 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
  • 108. Can we make any program in c++ without using any header file?
  • 109. No
  • 110. Can we have more than one constructor in a class?
  • 111. Yes
  • 112. What is public, private and protected?
  • 113. 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.
  • 115. 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.
  • 116. What is NULL pointer?
  • 117. 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.
  • 118. What is reference variable in C++?
  • 119. 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.
  • 120. What is the output of the following program? #include <iostream.h> class myclass { int val; Public: myclass(int I) { val = I; cout<<”constructing ”;} ~myclass( ) { cout<<"destructing";} int getval( ) { return val;} }; void display(myclass ob) { cout<<ob.getval()<<''; } main() { myclass a(10); display(a); return 0; }
  • 121. 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.
  • 122. What is the output of the following program? #include "iostream.h" class base { public: base(){ cout << "constructing base ";} ~base(){ cout << "destructing base ";} }; class derived: public base{ public: derived() {cout<<"constructing derived ";} ~derived() {cout<<"destructing derived ";} }; main() { derived ob; return 0; }
  • 123. constructing base constructing derived destructing derived destructing base Constructors are called in the order of derivation and destructors are called in the reverse order.
  • 124. Observe the following piece of code and reduce the amount of duplicate code. if(book::title=new char[256] == 0) { cerr <<"Error allocating memory"; exit(0); } if(book::author=new char[64] ==0) { cerr<<"Error allocating memory"; exit(0); } if(book::publisher=new char[128] ==0) { cerr<<"Error allocating memory"; exit(0); }
  • 125. 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"; 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 cerr<<"Error allocating memory"; exit(0);
  • 126. When the interface exposed by C++ component is not compatible to the client, what needs to be done?
  • 127. Client has to use Adapter design pattern to make interface compatible. Adapter publicly implements the interface to be used by the client
  • 128. What is the output of following program? Why? #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(); }
  • 129. 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.
  • 130. Instead of char* what type can be used in pure C++ to describe the String?
  • 131. basic_string is the class defined for this purpose, string is typedefed as basic_string<char>
  • 132. What happens if there is no catch block suitable to the throw type?
  • 133. Application crashes due to unhandled exception
  • 134. Is the program given below a valid one? #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; }
  • 135. 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.
  • 136. Determine the output for the following program. #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); }
  • 137. Square of a given number: 25 Product of two whole numbers: 42
  • 138. Determine the output for the following program. #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); }
  • 139. Square of a given number: 25 Product of two whole numbers: 42
  • 140. How does the C++ compiler process inline functions included in a header file?
  • 141. 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).
  • 142. What is a template?
  • 143. 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 template <class indetifier> function_declaration; template <typename indetifier> function_declaration;
  • 144. We want to swap int, char , float. We need not define 3 functions; instead we can implement it by using templete function definition template <class T> T fn_name(argument name)