ICT C++
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

ICT C++

  • 2,825 views
Uploaded on

ICT training in Bannari Amman

ICT training in Bannari Amman

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,825
On Slideshare
2,825
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
156
Comments
0
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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?
  • 21. 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.n” )<<” n” ;
    }
  • 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;
    }
  • 25. b. Compile time error
  • 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;
    }
  • 29. a. 20, 20
  • 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;
    }
  • 35. b. 100
  • 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;
    }
  • 39. c. Compile Time Error
  • 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);
    }
  • 43. b. 15, 14
  • 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;
    }
  • 53. d. 0 0
  • 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;
    }
  • 57. a. Error
  • 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;
    }
  • 65. c. This
  • 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);
    }
  • 71. a. 55 33
  • 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
  • 87. a. Encapsulation
  • 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
  • 89. c. Only i
  • 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
  • 91. a. friend
  • 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
  • 97. a. Variable, 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.
  • 100. What is Object?
  • 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.
  • 114. What are pointers?
  • 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 n”;}
    ~myclass( ) { cout<<"destructingn";}
    int getval( ) { return val;}
    };
    void display(myclass ob)
    {
    cout<<ob.getval()<<'n';
    }
    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 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;
    }
  • 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 memoryn";
    exit(0);
    }
    if(book::author=new char[64] ==0)
    {
    cerr<<"Error allocating memoryn";
    exit(0);
    }
    if(book::publisher=new char[128] ==0)
    {
    cerr<<"Error allocating memoryn";
    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 memoryn";
    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 memoryn";
    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)