Download C++ 2 Marks Questions & Answers and more Study notes Computer Science in PDF only on Docsity!
- (^) Define Object Oriented Programming ✓ (^) It is an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand.
- (^) Write any four features of OOPS.
✓ Emphasis is on data rather than on procedure. ✓ (^) Programs are divided into objects. ✓ Data is hidden and cannot be accessed by external functions. ✓ Follows bottom -up approach in program design.
- (^) What are the basic concepts of OOS?
✓ Objects. ✓ Classes. ✓ Data abstraction and Encapsulation. ✓ Inheritance. ✓ Polymorphism. ✓ Dynamic binding. ✓ Message passing
- (^) What are objects?
✓ Objects are the basic run time entities in an object oriented system. ✓ They may represent a person, a place or any item that a program has to handle.
- (^) Define a class.
✓ A class is a collection of objects with similar attributes and operations. ✓ Once a class has been defined, we can create any number of objects belonging to the classes
- Define Encapsulation.
✓ The wrapping up of data and functions into a single unit is known as encapsulation.
- (^) Define Data hiding?
✓ The isolation of data from direct access by the program is called as data hiding or information hiding.
- (^) What is data abstraction? ✓ The insulation of data from direct access by the program is called as data hiding or information binding.
- (^) What are keywords? ✓ Keywords are explicitly reserved identifiers and cannot be used as names for the program variables or other user defined program elements. ✓ Eg: goto, if, struct , else ,union etc.
- (^) What are the various rules for naming the identifiers in C++? ✓ Only alphabetic characters, digits and underscore are permitted. ✓ The name cannot start with a digit. ✓ The upper case and lower case letters are distinct. ✓ A declared keyword cannot be used as a variable name.
- (^) What are the operators available in C++? ✓ All operators in C are also used in C++. ✓ In addition to insertion operator << and extraction operator >> ✓ The other new operators in C++ are, : : Scope resolution operator : : * Pointer-to-member declarator ->* Pointer-to-member operator .* Pointer-to-member operator delete Memory release operator endl Line feed operator new Memory allocation operator setw Field width operator
- (^) What is a scope resolution operator? ✓ Scope resolution operator is used to uncover the hidden variables. It also allows access to global version of variables. ✓ Scope resolution operator is used to define the function outside the class. ✓ Syntax: return type : : Eg: void x : : getdata()
- (^) What are Memory management operators?
✓ new and delete operators are called as memory management operators since they allocate the memory dynamically. ✓ new operator can be used to create objects of any data type. ✓ Initialization of the memory using new operator can be done. This can be done as, pointer-variable = new data type; ✓ delete operator is used to release the memory space for reuse. delete pointer-variable;
- (^) What are manipulators? ✓ setw, endl are known as manipulators. ✓ Manipulators are operators that are used to format the display. ✓ The endl manipulator when used in an output statement causes a linefeed to be inserted and its effect is similar to that of the newline character”\n”. ✓ setw - sets the field width Eg: cout<<setw(5)<<sum<<endl;
- What do you mean by enumerated datatype? ✓ An enumerated datatype is another user-defined datatype, which provides a way for attaching names to numbers, thereby increasing comprehensibility of the code. Eg: enum shape{ circle, square, triangle} enum color{ red, blue, green, yellow}
- (^) What are symbolic constants? ✓ There are two ways for creating symbolic constants in C++: ✓ Using the qualifier constant. ✓ (^) Defining a set of integer constants using enum keyword. Eg: const int size =10; char name [size];
- (^) What are the differences between ‘break’ and ‘continue’ statement. S No break continue 1 Exits from current block / loop
Loop takes next iteration
checked. The do…while loop will execute at least one time even though the condition is false at the very first time.
- (^) What are the Escape Sequences present in ‘C++’? \n - New Line \b - Backspace \t - Form feed \’ - Single quote \ - Backspace \t - Tab \r - Carriage return \a - Alert \” - Double quotes
- (^) List the types of operators.
S No Operators Types Symbolic Representation 1 2 3 4 5 6 7 8 9 Arithmetic operators Relational operators Logical operators Increment and Decrement operators Assignment operators Bitwise operators Comma operator Conditional operator
= , - , * , / and %
, < , == , >=, <= and != && , || and! ++ and -- = , + = , - = , * = , / = , ^ = , ; = , & = & , | , ^ , >> , << , and ~ , ? :
- Distinguish between while..do and do..while statement. dhile.. do..while
(i) Executes the statements within the while block if only the condition is true.
(i) Executes the statements within the while block at least once.
(ii) The condition is checked at the starting of the loop
(ii) The condition is checked at the end of the loop
- (^) Distinguish Increment and Decrement operators. S No Increment ++ Decrement -- 1 2 3 4
Adds one to its operand Equivalent x = x + 1 Either follow or precede operand Example : ++x; x++;
Subtracts one from its operand Equivalent x = x - 1 Either follow or precede operand Example : --x; x--;
- (^) Give the syntax for the ‘for’ loop statement for (Initialize counter; Test condition; Increment / Decrement) { statements; } ✓ Initialization counter sets the loop to an initial value. This statement is executed only once. ✓ The test condition is a relational expression that determines the number of iterations desired or it determines when to exit from the loop. The ‘for’ loop continues to execute as long as conditional test is satisfied. When condition becomes false, the control of program exists the body of the ‘for’ loop and executes next statement after the body of the loop. ✓ The increment / decrement parameter decides how to make changes in the loop. ✓ The body of the loop may contain either a single statement or multiple statements.
- (^) What is a Pointer? ✓ A pointer is a variable that contains the address of another variable.
- (^) What is an array? ✓ Array is a collection of homogenous data stored under unique name. The values in an array is called as 'elements of an array.' These elements are accessed by numbers called as 'subscripts or index numbers.' Arrays may be of any variable type. Syntax : <data-type> <array_name> [size]; Ex : int a[10];
- What are Functions?
✓ A function is a module or block of program code which deals with a particular task.
main(); }
- (^) What is function prototype?
✓ The function prototype describes function interface to the compiler by giving details such as number ,type of arguments and type of return values ✓ Function prototype is a declaration statement in the calling program. ✓ Syntax datatype function_name(argument list); Eg float volume(int x, float y);
- (^) What is an inline function?
✓ An inline function is a function that is expanded in line when it is invoked. ✓ That is compiler replaces the function call with the corresponding function code. ✓ The inline functions are defined as inline function-header { function body }
- (^) What is a default argument?
✓ Default arguments assign a default value to the parameter, which does not have matching argument in the function call. ✓ Default values are specified when the function is declared. ✓ Eg : float amount(float principal, int period, float rate=0. 15) ✓ Function call is value=amount(5000,7); ✓ Here it takes principal=5000 & period=7 And default value for rate=0. value=amount(5000,7,0.34) Passes an explicit value 0f 0.34 to rate We must add default value from right to left
- (^) How to create an object? ✓ Once the class has been declared, we can create variables of that type by using the classname
✓ Syntax : classname objectname; ✓ Example : area x;
- (^) How the class is specified? Syntax of class ✓ Generally class specification has two parts
- class declaration - It describes the type and scope of its member
- class function definition - It describes how the class functions are implemented
✓ Syntax: class class_name { private: variable declarations; function declaration; public: variable declaration; function declaration; };
- (^) How to access a class member? ✓ object-name. function-name(actual arguments) Eg: x.getdata(100,75.5);
- (^) How the member functions are defined? ✓ Member functions can be defined in two ways - outside the class definition Member function can be defined by using scope resolution operator:: General format is return type class_ name :: function-name(argument declaration) {
- Inside the class definition This method of defining member function is to replace the function declaration by the actual function definition inside the class. ✓ Eg: class item {
✓ It cannot access member names directly. ✓ It has to use an object name and dot membership operator with each member name. eg: ( A. x )
- (^) Distinguish between Call by Value and Call by Reference.
Call by Value Call by Reference This is the usual method to call a function in which only the value of the variable is passed as an argument
In this method, the address of the variable is passed as an argument Any alternation in the value of the argument passed is local to the function and is not accepted in the calling program
Any alternation in the value of the argument passed is accepted in the calling program(since alternation is made indirectly in the memory location using the pointer) Memory location occupied by formal and actual arguments is different
Memory location occupied by formal and actual arguments is same and there is a saving of memory location Since a new location is created, this method is slow Since the existing memory location is used through its address, this method is fast There is no possibility of wrong data manipulation since the arguments are directly used in an application
There is a possibility of wrong data manipulation since the addresses are used in an expression. A good skill of programming is required here
- (^) What is function overloading? Give an example. ✓ Function overloading means we can use the same function name to create functions that perform a variety of different tasks. ✓ Example: int add( int a, int b); int add( int a, int b, int c); double add( int p, double q); //Function calls add (3 , 4); add (3, 4, 5); add (3 , 10.0);
- (^) What is operator overloading? ✓ C++ has the ability to provide the operators with a special meaning for a data type. ✓ (^) This mechanism of giving such special meanings to an operator is known as Operator overloading.
- (^) Define constructor
✓ A constructor is a special member function whose task is to initialize the objects of its class. ✓ It is special because its name is same as class name. ✓ The constructor is invoked whenever an object of its associated class is created. ✓ It is called constructor because it constructs the values of data members of the class Eg: class integer { …… public: integer( ); //constructor ……… }
- (^) Define default constructor ✓ The constructor with no arguments is called default constructor Eg: class integer { int m,n; public: integer( ); ……. };
integer::integer( )//default constructor { m=0; n=0; }
the statement
- (^) Define copy constructor
✓ A copy constructor is used to declare and initialize an object from another object. ✓ It takes a reference to an object of the same class as an argument Eg: integer i2(i1); would define the object i2 at the same time initialize it to the values of i1. ✓ Another form of this statement is Eg: integer i2=i1; The process of initializing through a copy constructor is known as copy initialization.
- Define dynamic constructor
✓ Allocation of memory to objects at time of their construction is known as dynamic constructor. The memory is allocated with the help of the NEW operator
class string { char *name; int length; public: string( ) { length=0; name=new char[ length +1]; } };
void main( ) { string name1(“Louis”), name3(Lagrange); }
- Define destructor
✓ It is used to destroy the objects that have been created by constructor. Destructor name is same as class name preceded by tilde symbol(~) Eg; ~integer() { } ✓ A destructor never takes any arguments nor it does it return any value. ✓ The compiler upon exit from the program will invoke it.
- (^) Define multiple constructors (constructor overloading). ✓ The class that has different types of constructor is called multiple constructors Eg: #include<iostream. h> #include<conio.h> class integer { int m,n; public: integer( ) //default constructor { m=0;n=0; } integer(int a,int b) //parameterized constructor { m=a; n=b; } integer(&i) //copy constructor { m=i. m; n=i.n; } void main() { integer i1; //invokes default constructor integer i2(45,67);//invokes parameterized constructor integer i3(i2); //invokes copy constructor
- (^) How will you overload Unary and Binary operator using Friend functions? ✓ When unary operators are overloaded using friend function, it takes one reference argument (object of the relevant class) ✓ When binary operators are overloaded using friend function, it takes two explicit arguments.
- (^) Explain basic to class type conversion with an example. ✓ Conversion from basic data type to class type can be done in destination class. ✓ Using constructors does it. Constructor takes a single argument whose type is to be converted.
Eg: Converting int type to class type
class time { int hrs,mins; public: …………. Time ( int t) //constructor { hours= t/60 ; //t in minutes mins =t % 60; } }; ✓ Constructor will be called automatically while creating objects so that this conversion is done automatically.
- (^) Explain class to basic type conversion with an example. ✓ Using Type Casting operator, conversion from class to basic type conversion can be done. It is done in the source class itself. Eg: vector : : operator double( ) { double sum=0; for(int I=0;I<size;I++) sum=sum+v[ i ] *u[ i ] ; return sqrt ( sum ) ; }
This function converts a vector to the corresponding scalar magnitude.
- Explain one class to another class conversion with an example. ✓ Conversion from one class type to another is the combination of class to basic and basic to class type conversion. Here constructor is used in destination class and casting operator function is used in source class. Eg: objX = objY ✓ objX is the object of class X and objY is an object of class Y. The class Y type data is converted into class X type data and the converted value is assigned to the obj X. ✓ Here class Y is the source class and class X is the destination class.
- (^) List out some of the error handling functions ✓ eof() -Returns true if end-of-file is encountered ✓ fail() -Returns true when an input or output operation has failed ✓ bad() -Returns true if invalid operation is attempted by or any uncoverable error has occurred ✓ good() -Returns true if no error has occurred
- (^) List the class member’s visibility / Access Specifiers ✓ There are three visibilities of class members. ✓ They are i) Public ii)Private iii)Protected
- (^) Explain the public visibility ✓ The class members are visible to the base class, derived classes and outside the class through the objects
- (^) Explain the private visibility ✓ The class members are visible only to the base class itself but not to the derived class
- (^) Explain the protected visibility ✓ The class members are visible to the base and derived classes
- What is meant by single inheritance? ✓ If a single class is derived from a single base class is called single inheritance.
- (^) What is multiple inheritance? ✓ If a class is derived from more than one base class, it is called multiple inheritance.
- (^) What is hierarchical inheritance?