SPPU 2015 Pattern question paper.Every year thousand of students appear for sppu exams,therefore it is very necessary for each and every student to be mentally prepared for the exams and for this case only we study the previous year question papers to get an insight of previous year question papers.

A good analysis of question papers can help you get a better head from others in the same exam as now you would be already prepared for those questions and that would even help you score better.The best advice that we can give you is that try to solve the maximum questions by yourself only rather than depending on solutions based books it would ultimately make your mind more lazy and you will put effort much less and the result would be that you would get less out of the subjects.

These all are as per updated 2015 pattern onwards which are of 50 marks.The question paper is divided into 8 questions of which you have to attempt any four, there is choice in every question,so among 1st two you have to attempt any one and so on.The major advantage of this pattern is that it gives you freedom to choose you among questions but each question comes with 12 to 13 marks per head so getting a question wrong in subject where start and final steps matter also count a lot.so we are also uploading a series of specially designed question bank for better preparation.

UNIT-III

Q .Explain Virtual function?
Ans:- Virtual Function is a function in base class, which is overrided in the derived class, and which tells the compiler to perform Late Binding on this function.
Virtual Keyword is used to make a member function of the base class Virtual.
Rules for Virtual Functions

  1. They Must be declared in public section of class.
  2. Virtual functions cannot be static and also cannot be a friend function of another class.
  3. Virtual functions should be accessed using pointer or reference of base class type to achieve run time polymorphism.
  4. The prototype of virtual functions should be same in base as well as derived class.
  5. They are always defined in base class and overridden in derived class. It is not mandatory for derived class to override (or re-define the virtual function), in that case base class version of function is used.
  6. A class may have virtual destructor but it cannot have a virtual constructor.
    Q. Explain Indirection operator?
    Ans:- It is the complement of &. It is a unary operator that returns the value of the variable located at the address specified by its operand.
    The following program executes the two operations

#INCLUDE

using namespace std;

int main () {
int var;
int *ptr;
int val;

var = 3000;

// take the address of var
ptr = &var;

// take the value available at ptr
val = *ptr;
cout << “Value of var :” << var << endl;
cout << “Value of ptr :” << ptr << endl;
cout << “Value of val :” << val << endl;

return 0;
}

Output:
Value of var :3000
Value of ptr :0xbff64494
Value of val :3000

Q .Explain Friend class and function in C++.
Ans:- Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class. For example a LinkedList class may be allowed to access private members of Node.
class Node
{
private:
int key;
Node next; / Other members of Node Class */

friend class LinkedList; // Now class LinkedList can
// access private members of Node
};

Friend Function Like friend class, a friend function can be given special grant to access private and protected members. A friend function can be:
a) A method of another class
b) A global function
class Node
{
private:
int key;
Node *next;

/* Other members of Node Class */
friend int LinkedList::search(); // Only search() of linkedList
// can access internal members
};

Following are some important points about friend functions and classes:
1) Friends should be used only for limited purpose. too many functions or external classes are declared as friends of a class with protected or private data, it lessens the value of encapsulation of separate classes in object-oriented programming.
2) Friendship is not mutual. If a class A is friend of B, then B doesn’t become friend of A automatically.
3) Friendship is not inherited (See this for more details)
4) The concept of friends is not there in Java.
A simple and complete C++ program to demonstrate friend Class

include

class A {
private:
int a;
public:
A() { a=0; }
friend class B; // Friend Class
};

class B {
private:
int b;
public:
void showA(A& x) {
// Since B is friend of A, it can access
// private members of A
std::cout << “A::a=” << x.a;
}
};

int main() {
A a;
B b;
b.showA(a);
return 0;
}
Output:
A::a=0

Q .Explain Static Fuction?

Ans:-Static functions in a class: Just like the static data members or static variables inside the class, static member functions also does not depend on object of class. We are allowed to invoke a static member function using the object and the ‘.’ operator but it is recommended to invoke the static members using the class name and the scope resolution operator.
Static member functions are allowed to access only the static data members or other static member functions, they can not access the non-static data members or member functions of the class.
// C++ program to demonstrate static
// member function in a class

#include

using namespace std;

class GfG
{
public:

// static member function
static void printMsg()
{
    cout<<"Welcome to GfG!";
}

};

// main function
int main()
{
// invoking a static member function
GfG::printMsg();
}
Output:
Welcome to GfG!

Q . What is the difference between initialization and assignment of variable in c++ programming?
Ans:-Initialization means to “to give the variable some initial or starting value to be stored in the memory location allotted to the variable during declaration. This can be done either during declaration or in the beginning of the loop.”
For eg: if you have an integer variable “n” then initialization is like
int n=7;
Assignment means to “assign a pre declared variable with some value or by the value of another variable of the same data type (hope you know about data types.!!). This can be done anywhere in body of the program.”
For eg: n=9;
or if you have another integer type variable “m”=5
then, n=m; //assigns value 5 to n
Q .Explain‘this’ pointer in C++.
Ans:-To understand ‘this’ pointer, it is important to know that how objects look at functions and data members of a class.

  1. Each object gets its own copy of the data member.
  2. All access the same function definition as present in the code segment.
    Meaning each object gets its own copy of data members and all objects share single copy of member functions.
    Then now question is that if only one copy of each member function exists and is used by multiple objects, how are the proper data members are accessed and updated?
    Compiler supplies an implicit pointer along with the functions names as ‘this’.
    The ‘this’ pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body of all nonstatic functions. ‘this’ pointer is a constant pointer that holds the memory address of the current object. ‘this’ pointer is not available in static member functions as static member functions can be called without any object (with class name).
    For a class X, the type of this pointer is ‘X* const’. Also, if a member function of X is declared as const, then the type of this pointer is ‘const X *const’ (see this GFact)
    Following are the situations where ‘this’ pointer is used:
    1) When local variable’s name is same as member’s name

#include

using namespace std;

/* local variable is same as a member’s name */
class Test
{
private:
int x;
public:
void setX (int x)
{
// The ‘this’ pointer is used to retrieve the object’s x
// hidden by the local variable ‘x’
this->x = x;
}
void print() { cout << “x = ” << x << endl; }
};

int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
Output:
x = 20

Q. Explain Late Binding?
Ans:- In the case of late binding, the compiler matches the function call with the correct function definition at runtime. It is also known as Dynamic Binding or Runtime Binding.
In late binding, the compiler identifies the type of object at runtime and then matches the function call with the correct function definition.
By default, early binding takes place. So if by any means we tell the compiler to perform late binding, then the problem in the previous example can be solved.
This can be achieved by declaring a virtual function.
To make a function virtual, we write the keyword virtual before the function definition.

include

using namespace std;

class Animals
{
public:
virtual void sound()
{
cout << “This is parent class” << endl;
}
};

class Dogs : public Animals
{
public:
void sound()
{
cout << “Dogs bark” << endl;
}
};

int main()
{
Animals *a;
Dogs d;
a= &d;
a -> sound();
return 0;
}

Q .Explain Smart pointers in CPP?
Ans:-Smart pointers are objects which store pointers to dynamically allocated (heap) objects. They behave much like built-in C++ pointers except that they automatically delete the object pointed to at the appropriate time. Smart pointers are particularly useful in the face of exceptions as they ensure proper destruction of dynamically allocated objects. They can also be used to keep track of dynamically allocated objects shared by multiple owners.
Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed.

include

using namespace std;
class SmartPtr
{
int *ptr; // Actual pointer
public:
// for use of explicit keyword
explicit SmartPtr(int *p = NULL) { ptr = p; }
// Destructor
~SmartPtr() { delete(ptr); }
// Overloading dereferencing operator
int &operator *() { return *ptr; }
};
int main()
{
SmartPtr ptr(new int());
*ptr = 20;
cout << *ptr;
// We don’t need to call delete ptr: when the object
// ptr goes out of scope, destructor for it is automatically
// called and destructor does delete ptr.
return 0;
}
Output:
20
Q .Explain Shared Pointer?
Ans:-A shared_ptr is a container for raw pointers. It is a reference counting ownership model i.e. it maintains the reference count of its contained pointer in cooperation with all copies of the shared_ptr. So, the counter is incremented each time a new pointer points to the resource and decremented when destructor of object is called.
Reference Counting: It is a technique of storing the number of references, pointers or handles to a resource such as an object, block of memory, disk space or other resources.
An object referenced by the contained raw pointer will not be destroyed until reference count is greater than zero i.e. until all copies of shared_ptr have been deleted.
So, we should use shared_ptr when we want to assign one raw pointer to multiple owners.

// C++ program to demonstrate shared_ptr

include

include

using namespace std;

class A
{
public:
void show()
{
cout<<“A::show()”<<endl;
}
};

int main()
{
shared_ptr p1 (new A);
cout << p1.get() << endl; p1->show();
shared_ptr p2 (p1);
p2->show();
cout << p1.get() << endl;
cout << p2.get() << endl;

// Returns the number of shared_ptr objects
//referring to the same managed object.
cout << p1.use_count() << endl;
cout << p2.use_count() << endl;

// Relinquishes ownership of p1 on the object
//and pointer becomes NULL
p1.reset();
cout << p1.get() << endl;
cout << p2.use_count() << endl;
cout << p2.get() << endl;

return 0;

}

QUESTION PAPER OBJECT ORIENTED PROGRAMMING