CS304 Past paper Subjective

CS304 past paper including most repeated questions from final papers CS304. Past papers help you understanding the paper pattern as well as the types of questions asked in final exam.

For more papers visit


CS304 subjective 2023

Question No. 1: Write the names of two types of Templates. MARKS: 2

 Answer:- (Page 256) a. Function Templates (in case we want to write general function like printArray) b. Class Templates (in case we want to write general class like Array class)

Question No. 2: Describe the way to declare a template function as a friend of any class. 2marks

Answer:- (Page 294) template< class T > class B { int data; friend void doSomething( T ); // granting friendship to template doSomething in // class B friend A< T >; // granting friendship to class A in class B };

Question No. 3: Can a constructor throw an exception? How to handle the errors, when the constructor fails?

 Answer:- Yes, Constructor can throw an exception But In constructor if an exception is thrown than all objects created so far are destroyed. Exception due to constructor of any contained object or the constructor of a parent class can be caught in the member initialization list.

 Question No. 4: Why these operators can not be overloaded?

 Answer:- (Page 141) Reason: They take actual current object name, rather than value in their argument

Question No. 5: What is graceful termination method of Error handling; give its example using C++. Answer:- (Page 330) Program can be designed in such a way that instead of abnormal termination, that causes the wastage of resources, program performs clean up tasks, mean we add check for expected errors (using if conditions),

 Example – Graceful Termination int Quotient (int a, int b )

{ if(b == 0){ cout << “Denominator can’t “ << “ be zero” << endl; // Do local clean up exit(1); } return a / b; }

 Question No. 6: How can you describe Virtual destructor? Why Virtual destructor is used?

Answer:- (Page 234) When delete operator is applied to a base class pointer, base class destructor is called regardless of the object type. Only base class part of object will be deleted other parts will remain as it is this result in memory leak (wastage of memory), Virtual Destructors make it possible that complete object is being deleted so there is no memory leak (waste of memory).

 Question No. 7: Motor car can be called a good candidate for being an object so, this ibject contains Encapsulation characteristic. You are required to apply the concept of Encapsulation on “Motor Car”. Answer:- Yes Motor Care is a good candidate for apply the concept of encapsulation In Motor Car we have the following Objects which fully describes the phenomena of encapsulation. 1. Steer Wheels 2. Accelerate 3. Change Gear 4. Apply Brakes 5. Turn Lights On/Off

In encapsulation we just provide the user an interface and hide the Full implementation from the user because user don’t want to know the implementation he Just want his work should be done perfectly For example: Breaks of car, we gave just a paddle to the User and encapsulate how it works inside the engine. Same for accelerator, gear, steer wheels and turn on and off of Light.

Question No. 8: Suppose base class and derived class has a member function with same signature. Now call that member function through the pointers to a base class object

Question No. 9: What do you know about exception in initialization? Explain with example.

Answer:- (Page 345) Exception in Initialization List Exception due to constructor of any contained object or the constructor of a parent class can be caught in the member initialization list. Example Student::Student (String aName) : name(aName) /*The constructor of String can throw a exception*/ { … } Exception in Initialization List The programmer may want to catch the exception and perform some action to rectify the problem Example Student::Student (String aName) try : name(aName) { … } catch (…) { }

 Question No. 10: How can we set the default values for non type parameters?

 Answer:- (Page 284) We can set default value for this non type parameters, as we do for parameters passed in ordinary functions, template< class T, int SIZE = 10 > class Array { private: T ptr[SIZE]; public: void doSomething(); … }

Question No. 11: Explain the statement vector ivec (4,3)

 Answer:- This would create a vector of four elements, each one initialized to 3. ivec looks like this: 3 3 3 3

Question No. 12: Tell the procedure of making a class abstract in C++ by giving an example.

 Answer:- (Page 230) In C++, we can make a class abstract by making its function(s) pure virtual. Conversely, a class with no pure virtual function is a concrete class (which object can be instantiated) A class having pure virtual function(s) becomes abstract class Shape { … public: virtual void draw() = 0; }

Question No. 13: Write down a list of four intangible objects?

Answer:- 1. Time 2. Light 3. inteligence 4. Temperature 5.software Question No. 14: Give the pseudo code of non case sensitive comparison function of string class. Answer:- (Page 263) int caseSencompare( char* str1, char* str2 ) { for (int i = 0; i < strlen( str1 ) && i < strlen( str2 ); ++i) if ( str1[i] != str2[i] ) return str1[i] – str2[i]; return strlen(str1) – strlen(str2); }

 Question No. 15: Describe three properties necessary a container to implement generic algorithms. Answer:- (Page 301) We claimed that this algorithm is generic, because it works for any aggregate object (container) that defines following three operations a. Increment operator (++) b. Dereferencing operator (*) c. Inequality operator (!=)

Question No. 16: Mobile is a good entity for being an object. Write characteristics, behavior and unique ID.

 Answer:- Characteristics: Buttons, Battery, Screen Behavior: Calling, Sending Message, Internet, Multimedia Unique ID: Nokia

Question No. 17: What are the container requirements? Write details.

Answer As each container need to perform certain operations on the elements added to it like their copy while creating another instance of container or their comparison while performaing certain operation on them like their sorting , so the elements that we are going to add in containers should provide this kind of basic functionality. Examples of these functionalities are given below, When an element is inserted into a container, a copy of that element is made using, , v Copy Constructor v Assignment Operator So the elements need to be added to any container should provide copy and assignment functionality. • Associative containers and many algorithms compare elements so the elements that are added to associative containers should have this functionality, o Operator == o Operator < c++ does not provide functionality of comparison operator (==) or less than operator ( 2. #include 3. using namespace std; 4. template 5. class MyClass{ 6. public: 7. MyClass(){ 8. cout<<“This is class1″< 12. class MyClass{ 12. public: 13. MyClass(){ 14. cout<<“This is class2″< c1;

 23. MyClass c2; 24. system(“PAUSE”); 25. return 0; 26. }

Answer: Correct Code #include #include using namespace std; template class MyClass{ public: MyClass(){ cout<<“This is class1″< class MyClasss{ // class MyClass{ templates we do not specify type in definition. public: MyClasss(){ cout<<“This is class2″< c1; MyClasss c2;* system(“PAUSE”); return 0; }

Question No: 24 ( Marks: 5 ) What is random iterator? What is its relation with vectors?

Answer:- Random Access Iterators:- They have all the capabilities of bidirectional Iterators plus they can directly access any element of a container. You can think of vectors as smart arrays. They manage storage allocation for you, expanding and contracting the size of the vector as you insert or erase data. You can use vectors much like arrays, accessing elements with the [] operator. Such random access is very fast with vectors. as we can access any element of vector using its index so we can use random access iterator It’s also fast to add (or push) a new data item onto the end (the back) of the vector. When this happens, the vector’s size is automatically increased to hold the new item.

Leave a Comment