references from by Virtualians.pk Q) Describe the way to declare a template function as a friend of any class?(2marks)

Similar documents
C++ INTERVIEW QUESTIONS

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Curriculum Map. Discipline: Computer Science Course: C++

The C Programming Language course syllabus associate level

Advanced Data Structures

An Incomplete C++ Primer. University of Wyoming MA 5310

Fundamentals of Java Programming

Glossary of Object Oriented Terms

Object Oriented Software Design II

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

Java Interview Questions and Answers

13 Classes & Objects with Constructors/Destructors

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

C++ Programming Language

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Data Structures Using C++ 2E. Chapter 5 Linked Lists

C++FA 5.1 PRACTICE MID-TERM EXAM

OpenCL Static C++ Kernel Language Extension

El Dorado Union High School District Educational Services

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Java Application Developer Certificate Program Competencies

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Chapter 5 Functions. Introducing Functions

Java (12 Weeks) Introduction to Java Programming Language

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Moving from CS 61A Scheme to CS 61B Java

Sources: On the Web: Slides will be available on:

Compile-time type versus run-time type. Consider the parameter to this function:

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Laboratory Assignments of OBJECT ORIENTED METHODOLOGY & PROGRAMMING (USING C++) [IT 553]

Node-Based Structures Linked Lists: Implementation

CSI33 Data Structures

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Chapter 5 Names, Bindings, Type Checking, and Scopes

Chapter 4 OOPS WITH C++ Sahaj Computer Solutions

CEC225 COURSE COMPACT

C++ Overloading, Constructors, Assignment operator

Facebook Twitter YouTube Google Plus Website

Comp151. Definitions & Declarations

Description of Class Mutation Mutation Operators for Java

C++FA 3.1 OPTIMIZING C++

Ch 7-1. Object-Oriented Programming and Classes

Coding conventions and C++-style

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

public static void main(string[] args) { System.out.println("hello, world"); } }

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

Goals for This Lecture:

CS 111 Classes I 1. Software Organization View to this point:

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

Object Oriented Programming With C++(10CS36) Question Bank. UNIT 1: Introduction to C++

Java SE 8 Programming

Getting Started with the Internet Communications Engine

1. Polymorphism in C++...2

Introduction to Programming Block Tutorial C/C++

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

: provid.ir

Course Title: Software Development

Delegating Constructors (revision 3)

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

Programming and Software Development (PSD)

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Symbol Tables. Introduction

Classes and Pointers: Some Peculiarities (cont d.)

Chapter 13 Storage classes

Introduction to Data Structures

Subject Name: Object Oriented Programming in C++ Subject Code:

How to test and debug an ASP.NET application

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Sequences in the C++ STL

Lab Experience 17. Programming Language Translation

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Linked Lists: Implementation Sequences in the C++ STL

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

CIS 190: C/C++ Programming. Polymorphism

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Computer Programming C++ Classes and Objects 15 th Lecture

Basics of C++ and object orientation in OpenFOAM

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Appendix K Introduction to Microsoft Visual C++ 6.0

Certified PHP Developer VS-1054

Lecture J - Exceptions

Pseudo code Tutorial and Exercises Teacher s Version

C++ Crash Kurs. C++ Object-Oriented Programming

UML for C# Modeling Basics

Friendship and Encapsulation in C++

10CS35: Data Structures Using C

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Transcription:

CS304-Object Oriented programming NEW Solved Final terms papers with references from 2012-2013 by Virtualians.pk Q) Describe the way to declare a template function as a friend of any class?(2marks) Answer:- (Page 294) Template<class U> Void dosomething (U); Template<class V>; Class A{ int data; ; Template<class T> Class B{ int data; friend void dosomething(t); friend A<T>; Prepared By: Irfan Khan (chief Admin) Page 1

; Q) Give the C++ code of template function to print the values of any type of array I int.this function will take 2 parameters one will be pointer and other will be will be size of array (mrk3) Answer:- (Page 257) Template<class T> Void printarray(t*array,int size) { For(int i=0;i<size;i++) Cout<<array[i]; int main() int iarray[5]={1,2,3,4,5; void printarray(iarray,5); return 0; Prepared By: Irfan Khan (chief Admin) Page 2

Q) Consider the code below, 1. template<typename T> class Test { 2. T value; 3. public: 1. Test( T val ) : value(val){ 1. Test(const Test<T>& c) : value(c.value) { 1. ; 1. int main() { 2. Test< int > t1( 0 ), t2( 0 ); 3. Test< float > t3 = t1; 4. system("pause"); 5. return 0; 6. Q) Give line number having any error/errors in this code explain the reason for error/s and give corrected code as well. Answer: Test< float > t3 = t1; There is an error in this line due to conversion from int to float but in a wrong way Q) Why Abstract class s objects cannot be instantiated? Answer:- (Page 230) Abstract class s objects cannot be instantiated they are used for inheriting interface or implementation so that derived classes can give implementation of these concepts. Prepared By: Irfan Khan (chief Admin) Page 3

Q) What output is produced by the following code? int wait_time=46; try { cout Try-block entered.\n ; if (wait_time > 30) throw wait_time; cout Leaving try-block.\n ; catch ( int thrown_value) { cout Exception thrown with\n wait_time equal to thrown_valueendl; cout After catch-block endl; Q) Write C++ code for the overloaded subscript operator of following template Vector class. Answer: template< class T > class Vector { private: T* ptr; Prepared By: Irfan Khan (chief Admin) Page 4

int size; public: int index; ; T& operator [](int); // overloaded subscript operator Q) What do you mean by Stack unwinding? Answer: - pg 336 The flow control (the order in which code statements and function calls are made)as a result of throw statement is referred as stack unwinding It takes place in two ways. Q) Write general syntax of nested try catch block? Answer:- pg 344 int main() { try{ try{ throw 1; Prepared By: Irfan Khan (chief Admin) Page 5

catch (float) { catch(int){ return 0; Q ( Marks: 3 ) Consider the code below, template< typename T > class T1 { public: T i; protected: T j; private: T k; friend void Test(); ; This code has a template class T1 with three members i,j and k and a friend function Test(), you have to describe which member/s of T1 will be available in function Test(). Answer:- All of them (i, j, k) will be available in function Test(). Q) In which situation do we need to implement Virtual inheritance? Explain with an example (5 marks) Answer:- In multiple inheritance while solving diamond problem virtual inheritance need to implement. The solution of avoid this problem is virtual inheritance so that in multiple inheritance only one copy of base class is generated as shown below instead of two Prepared By: Irfan Khan (chief Admin) Page 6

separate copies. In virtual inheritance there is exactly one copy of the anonymous base class object class Vehicle{ protected: int weight; ; class LandVehicle : public virtual Vehicle{ ; class WaterVehicle : public virtual Vehicle{ ; Example class AmphibiousVehicle: public LandVehicle, public WaterVehicle{ public: AmphibiousVehicle(){ weight = 10; 2mark Questions: Q) Write down a list of four intangible objects? Answer (page no 13) Hours Seconds Minutes Year Month Prepared By: Irfan Khan (chief Admin) Page 7

Q) Write three important features of virtual functions? Answer (page no230) 1) Virtual function achieve exactly same kind of functionality that can be achieved by complex code of switch statement 2) Target class of a virtual function call is determined at run-time automatically 3) In C++ we declare a function virtual by preceding the function header with keyword virtual Q) Give the C++ code of template function to print the values of any type of array I int. This function will take 2 parameters one will be pointer and other will be will be size of array? Repeated 3 mark Questions: Q) Describe the salient feature of abstract class? Answer (page no 36) They are present at or near the top in the class hierarchy to present most generalized behavior Main purpose is to be inherited by other classes Cannot be instantiated Promotes reuse Q) Define composition and give its example with coding? Answer (page no 124) If one object is part of another object(relationship of part and whole)in composition lifetime of one object depends upon the other. The part objects are essential components of the whole. For example: person is composed of eyes, feet etc, Prepared By: Irfan Khan (chief Admin) Page 8

Q) Define static and dynamic binding? Answer (page no 231) Static binding: means that target function for a call is selected at compile time. Dynamic binding: means that target function for a call is selected at run time. Q) What is constructor? Answer (page no 178) 5 mark Questions: Q) What is the difference (if any) between the two types of function declarations? template function_declaration; template function_declaration; Q) Give three advantages that Iterators provide over Cursors? Answer(page no 315) With iterators more than one traversal can be pending on a single container It allow to change the traversal strategy without changing the aggregate object They contribute towards data abstraction by emulating pointers. Q) What are container requirements? Answer (page no323) Q) What is random iterator? What is its relation with vectors? Answer (page no 325) They have all the capabilities of bidirectional iterators plus they can directly access any elements of a container. Relation with vector: Prepared By: Irfan Khan (chief Admin) Page 9

As we can access any element of vector using its index so we can use random access iterator 2 mark Questions: Q) Give two uses of a destructor? It can be used to create class object without passing any argument. It is called destructor is called before the base class destructor is called It is also used to release the memory after it is used... Q) What is the purpose of template parameter? Answer (repeated) Q) Give the names of three ways to handle errors in a program? Answer (page no355) 1. Abnormal termination 2. Graceful termination 3. Return the illegal value 4. Return error code from function 5. Exception handling 3 mark Questions: Q) Describe the way to declare a template class as a friend class of any other class? Answer (repeated) Q) What is the purpose of template parameter? Ans: There are three kinds of template parameters: type non-type template You can interchange the keywords class and typename in a template parameter declaration. You cannot use storage class specifiers (static and auto) in a template parameter declaration. Q) What is meant by direct base class? Answer (page no 212) Direct base class is called explicitly listed in a derived class s header with a colon (: ) Example: Class child: public Parent 1{ ; Prepared By: Irfan Khan (chief Admin) Page 10

Q) State any conflict that may rise due to multiple inheritances? Answer (page no251) If more than one base class has a function with same signature then the child will have two copies of the function. Calling such function will result in ambiguity. 5 mark Questions: Q) Suppose the base class and the derived class each have a member function with the same signature. When you have a pointer to a base class object and call a function member through the pointer, discuss what determines which function is actually called, the base class member function or the derived-class function Repeated Q) What are container requirements? Repeated Q) Tell the logical error/s in the code given below with reference to resource management; also describe how we can correct that error/s. class Test{ public: int function1(){ try{ FILE *fileptr = fopen( filename.txt, w ); throw exception(); fclose(fileptr); return 0; Prepared By: Irfan Khan (chief Admin) Page 11

; catch(exception e){... Answer (page no349) In case of exception the call to fclose will be ignored and file will remain opened. Solution: class Test{ public: int function1(){ try{ FILE *fileptr = fopen( filename.txt, w ); fwrite( Hello world,1,11,fileptr);. throw exception(); fclose(fileptr); catch(exception e){ fclose(fileptr); throw; return 0; We can also make a separate class files handling to open file and destructor to close it Q) In which situation do we need to implement Virtual inheritance? Explain with an example? Answer (page no 257) It must be used when necessary. It can be used in the situations when programmer wants to use two distinct data members inherited from base class rather than one.. Example: Class Vehicle { Prepared By: Irfan Khan (chief Admin) Page 12

Protected: int weight; ; Class LandVehicle: public virtual Vehicle { ; Class WaterVehicle: public virtual Vehicle { ; Class AmphibiousVehicle: public LandVehicle, WaterVehicle { Public: AmphibiousVehicle (){ Weight=10; ; 2 mark Questions: Q) Write three important features of virtual functions Repeated Q) Give the name of three operation that a cursor or iterate generally provide? Answer (page no313) T*first() T*beyond() T*next(T*) Q) What do you know about function Template? Answer in two line? Answer (page no259) It is also called generic programming for a particular data type Its major benefits are reusability,writability,maintainabilty In c++ generic programming is done by using template..it is of two types: Function and Class templates Prepared By: Irfan Khan (chief Admin) Page 13

Q) What is a Virtual Destructor? Answer(page no 236) The destructor is called according to static type of any class pointer For example: if we have saved derived class pointer in base class pointer we will call destructor using delete operator the destructor of base class will be called as static type.this will destroy the base class object and derived class will not so we will use virtual destructor to call derived class destructor too.. Virtual~Shape()//call to destructor Q) Define static and dynamic binding. Repeated Q) What are container requirements? 3 mark Questions: When an element is inserted into a container a copy of that element is made using Copy and assignment Operator Associative container and many algorithms compare elements so the elements that are added to associative containers should have this functionality, Operator == Operator<= Q) Sort data in the order in which compiler searches a function. Complete specialization, generic Template, Partial specialization, Ordinary function.? Q) What is constructor? Prepared By: Irfan Khan (chief Admin) Page 14

Repeated 5mark Questions: Q) Describe problems with multiple inheritances? Repeated Q)Consider the code below, template< typename T > class T1 { public: T i; protected: T j; private: T k; friend void Test(); ; This code has a template class T1 with three members i,j and k and a friend function Test(), you have to Describe which member/s of T1 will be available in function Test (). Prepared By: Irfan Khan (chief Admin) Page 15

Repeated. Q) Give the c++ code of case sensitive comparison function of string class Answer (page no 266) Q) Write the average function that returns the average of all the element of an array. The argument to the function should be array name and the size of the array (type int) 2 marks Questions: Q) Describe the way to declare template class as a friend of any other class? Repeated. Q) Explain statement below Vector ivec (4, 3); Answer (not sure) Vector is the class name ivec is the object of it and it is taking array values for class vector Q) You are required to list down default access specifier for c++ class and c structure? Answer (page no 69) Public, Private and Protected... Q) State two reasons why virtual method cannot be static? Prepared By: Irfan Khan (chief Admin) Page 16

No, because it doesn't make any sense in C++. Virtual functions are invoked when you have a pointer/reference to an instance of a class. Static functions aren't tied to a particular instance, they're tied to a class. C++ doesn't have pointers-toclass, so there is no scenario in which you could invoke a static function virtually In C++, a static member function of a class cannot be virtual. For example, below program gives compilation error. #include<iostream> using namespace std; class Test { public: // Error: Virtual member functions cannot be static virtual static void fun() { ; 3 Marks Questions. Q) Describe three properties necessary for a container to implement generic algorithm? Answer (not sure) Vector Deque List Prepared By: Irfan Khan (chief Admin) Page 17

Q) In both virtual inheritance and multiple inheritance copy/copies of base class are created, List down how many copy/copies are created for: Virtual inheritance Multiple inheritances Ans:Copies: Virtual: Only one copy from any one parent is created. Multiple: Copies from both the parents are created. Q) Do you think that encapsulation is used to cope with complexity and simplifies the model and provide better understands? Explain the said perceptively with one real word example. Repeated Q) Describe the way to declare a template class as a friend class of any other class? Repeated Q) Give the name of two basic types of contains collectively called first class container? Answer (page no 321) Associative and Sequence containers are collectively referred to as the first-class container. Q) Show that OO model deals with the same entities /objects as they exist in a particular domain or in a particular real situation word use example of Ali derives a car? Prepared By: Irfan Khan (chief Admin) Page 18

The models are used to understand the problem before starting developing it Ali House Car Ali lives in house Ali drives the car Q) In which situation we should use operator overloading and in which situation we should use Templates? Answer (page no 265) Template is used when to have exactly identical operations on different data types where as overloading is used when we have similar operations on different data types. Q) Write a Template Function that takes one Template parameter and always returns value that is equal to double of its parameter value ( Parameter *2)? Q) Write down the Advantages of Constructor and destructors in respect of class with no dynamic data members?? 3 marks Prepared By: Irfan Khan (chief Admin) Page 19

Q) Give the general syntax of nested try catch blocks? 3 marks Repeated Q) Write C++ code to overload the subscript [] operator for string class? 5 marks Answer(page no 164) Q) Write a template function that returns the average of all the elements of an array the argument to the function should be the array name and the size of array type (Type int)? 5 marks Repeated Q) Write the C++ code for the overloading subscript operator of following template vectors? 5marks Repeated Q) How we can set the default values for non-type parameters? Answer (page no 288) We can set default value for non-type parameters, as we do for parameters passed in ordinary functions, Q) Give the name of two templates? Answer: Function and class templates Q) Give one advantage and disadvantage of templates? Prepared By: Irfan Khan (chief Admin) Page 20

Answer: Advantage Reusability Disadvantage: Can consume memory if used without care Q) Name any two error handling techniques? Answer: Abnormal and Graceful termination Q) Describe two key components of STL? Answer Containers Iterators Q) How we can make constructors virtual? Answer We can make constructors virtual my placing the key word Virtual at the start of their name in base class and derived class For example: We have a class of Shape which takes a function draw constructor virtual Class Shape{ Public: Shape(); Virtual void draw()// call to structure which is virtual Prepared By: Irfan Khan (chief Admin) Page 21

; Q) What is the Role of static data members? Answer (page no189) The type that is used to destroy a reference or pointer is called its static type Student s; The static type of s is Student. Q) What is the difference between Iterators and Cursors? an iterator allows iteration of a container with no knowledge of the container itself. A cursor, on the other hand, allows iteration of a container as well but has implementation details specific to the container type, so it does keep a reference to the container. Additionally a cursor's interface mirrors the interface of the container kind of like the facade pattern. Q) Write the average function that returns the average of all the element of an array. The argument to the function should be array name and the size of the array (type int) Ans: Average Function: double avg_of_array ( const int Array[], const int size ) { int sum = Array[0]; for( int i = 1; i < size; i++ ) sum += Array[ i ]; return ( sum / size ); Prepared By: Irfan Khan (chief Admin) Page 22

Q) Write a Template Function that takes one Template parameter and always returns value that is equal to double of its parameter value ( Parameter *2)? Ans: Template Function: template < typename T > T return_double( const T number ) { return ( number * 2 ); Prepared By: Irfan Khan (chief Admin) Page 23