0% found this document useful (0 votes)
8 views12 pages

C QA 14th July2003 Updated

The document provides a series of questions and answers related to C++ programming concepts, including mutable and explicit keywords, class sizes, virtual functions, exception handling, and templates. It also discusses data structures like arrays and linked lists, sorting algorithms, and design constraints for Symbian. Additionally, it covers multithreading concepts and the Standard Template Library (STL).

Uploaded by

Janet Pam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

C QA 14th July2003 Updated

The document provides a series of questions and answers related to C++ programming concepts, including mutable and explicit keywords, class sizes, virtual functions, exception handling, and templates. It also discusses data structures like arrays and linked lists, sorting algorithms, and design constraints for Symbian. Additionally, it covers multithreading concepts and the Standard Template Library (STL).

Uploaded by

Janet Pam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

Question and Answers:

Question:
1. Expalin about Mutable Keyword
Answer:
If a constant member function wants to change a specific variable, then declare that
variable with mutable keyword.

This keyword can only be applied to non-static and non-const data members of a class

Example:
#include<iostream.h>
Class Samp
{
private:
mutable int var;
int ctr;
public:
void Assign(int a) const
{
var = a ;
ctr = a ;// Throws compiler error since const member function cannot
// change the data member
}
};

int main()
{
Samp s;
s.Assign(15);
return 1;
}

Question:
2. Explain Explicit Keyword
Answer:
By making constructor explicit, the compiler is told not to perform any automatic
conversion using that particular constructor.
Example:
#include <iostream>
using namespace std;
class A
{
//explicit A(int abc)
A(int abc)
{
cout << "Calling para constructor";
}
};

main()
{
A obj = 10; //The compiler interprets as A obj(10)
//When the constructor is "explicit" it will show compile time error
}

Question:
3. What is size of the following class on a 32-bit machine? Padding in Class?
class abs
{
char c;
int *ptr;
};
Answer:
Theoretically, Total Size = Sizeof ( C ) + Sizeof ( ptr ) = 1 + 4 = 5
Due the boundary access, the total size will be 8 bytes (with holes of 3 bytes).
In Case of Packed defined in compiler, then the size will be 5 bytes.
Padding:
On a 32-bit machine, Memory access will be of 4-bytes. Incase if the size of data type /
set of similar data type is less than the multiples of 4 bytes, then the appropriate bytes
will added (bytes that doesn’t hold any data). As a result, holes will occur.

There are two ways of changing the padding :


a. In VC IDE, project-> settings-> c++ tab-> for code generation we can change the
alignment.
b. We can change it programmatically by using the # pragma

Question:
4)Size of the following class
class A{

char c1;
char c2;
int *p;
virtual void f();
};
Answer:
The size of the class is 12 bytes.

Question:
5.what is the size of the Array of 10 objects for the class A ??
class A{
char c1;
int *p;
};
Answer:
Size of object * 10

On a 32-bit machine, the size for the 10 objects will 80 bytes and 10 objects pointer will be 40
bytes.

abs s3[10] ; //Size will be 80 bytes


abs *s4[10]; //Size will be 40 bytes

Question:
6.Why Virtual function is Used?
Answer:
In Polymorphism, if same function exists in both the base and derived classes, and need
to call the derived class function instead of base class function, Virtual function is used.

Question:
7.A class does not have Virtual functions, is it Good to have a Virtual Destructor? If
So why?
Answer:
Virtual Destructor is used only
1.When Base pointer pointing to derived class object (which is created on the heap), upon
using delete operator on base pointer, derived class Destructor will not be called since
delete invokes the destructor based on the type of the pointer (which is base type again).
Declaring the base destructor as virtual will avoid this.

It is good to have virtual destructors if the class would be used as base class for other
classes.

Question:
8. What will be the Size of object if the base class contains one virtual function?
What will be the Size of object if the base class contains two virtual functions?

Answer:
On a 32-bit machine:
Case 1:
The size of the base pointer will be 4 bytes irrespective of the data or functions or virtual
functions.
Case 2: (No Virtual Functions)
The size of the base object /base class will be 1 byte (No data members)
Else the The size of the base object /base class will be total size of the data members.
Case 3: ( Virtual Functions)
The size of the base object /base class will be 4 byte (With data members)
Else the The size of the base object /base class will be sum of total size of the data
members + 4 bytes .
Question:
9. What does the compiler do for the statement *p = *q where p and q are the objects of a
class for which there is no overloaded assignment operator??
OR Will *p=*q work without any errors if you have not provided any overloading
functionality for = operator in the class??

Answer:

Example:
#include <iostream>
using namespace std;
class A
{
int abc;
public :
char *Str;
A(unsigned int abc = 0, char *ptr = "test")
{
if(ptr != NULL)
{
Str = new char[strlen(ptr) + 1];
strcpy(Str, ptr);
}
else
{
Str = new char[abc];
}
}
};

main()
{
A *obj1 = new A();
A *obj2 = new A();

//Pointer Values are different


cout << "[" << (int)obj1->Str << "] [" << (int) obj2->Str << "]" << endl;
*obj2 = *obj1; //Bitwise copy Both the pointer value of str will be same.
//Pointer Values are same
cout << "[" << (int)obj1->Str << "] [" << (int) obj2->Str << "]" << endl;
return 1;
}
The Data members are Bitwise copied .
Question:
10.
Func ()
{
……….
……..
……..
int *p = new int ();
}

Will the code compile? IF not Why?


Answer:
The code will compile.

Question:
11. i) What does happen when the allocated object pointer ( *p = new abs) loses the
pointer in a function ???
ii)How can it be handled in C++??

Answer:
i) Memory Leak occurs
ii) Using Exception Handling.
Delete pointer in the catch block.
Question:
12.Mention two ways of Overcoming Runtime Error?
Which is the best and why?
Answer:
Two ways:
a)Exception Handling using Try-Catch-Throw
b)Error Handling using Local control structures
Exception Handling using Try-Catch-Throw is best
1. less expensive, better readability and avoidance of complicated nested ifs.
2. In If-else, we need to check the return value (error code) which again uses if-
statements.

Question:
13.Exception Handling in C++ ??
Answer:
try
{…
}
catch()
{ …
throw()
}

Question:
14. Exception handling( In a function f() , you are allocating a memory on heap and
calling another function g(). Exception happens in the function g(). How do you
handle the ptr that has been allocated? )
What will you do?
If you want to return some values after Exception has occurred how this can be
accomplished?
Answer:
Using rethrow we can free the memory.
#include <iostream>

using namespace std;


int Fun ();
int G ();
main()
{
int ret = Fun();
cout << "Value of return in main is " << ret << endl;
}

int Fun ()
{
//Allocating memory
int *abc = new int[10];
try
{
//It will receive exception
G ();
}
catch(int Num)
{
//The num will receive 10 from function G
if(Num == 10)
{
cout << "G function received Exception Value " << Num << endl;
delete[] abc;
abc = NULL;
}
return 0;
}
return 1;
}

int G ()
{
//Exception occurs
try
{
//because of some reason it is throwing an exception
throw 10;
}
catch(int Num)
{
cout << "Catching the exception in Function Name G and Value is" <<
Num << endl;
//rethrow concept. It throws 10 to the caller(Function F)
throw;
}
}

Question:
15.Any knowledge/programming done on Network?

Question:
16.What is Simplex??
Answer:
Flow of data transfer in only single-directional. SenderReceiver

Question:
17.What is the difference between an array and Linked list??

Answer:
Array Linked List
a) Elements are stored in contiguous memory Elements stored in different memory
locations locations can be used.

b)Suitable for minimal number of elements Suitable for huge number of elements.

c) Data manipulation like sorting, searching, Data manipulation like sorting,


searching, insertion and deletion of elements searching, insertion and deletion of
for huge arrays is complicated elements for huge list is easy

Question:
18.How to insert a value into array?
How to insert a value into Linked list?
Answer:
The value is inserted only at the end of the array. If the element is to be inserted in
the middle, then a lot data manipulation has to be done (moving the elements further
and then inserting the elements at the required position.)
The value can be inserted in the beginning or in the end or in the middle (anywhere).

Question:
19.Explain Quick Sort
Answer:
a. Assign an iterator ‘p’ for the initial postion of the array and iterator ‘q’ for the nth
element. Find the mid element.
b. Navigate through the element from the beginning till the mid element, checking
against the a[q] . If any of the element is less than a[q] ,then swap the elements
c. Navigate through the element from the end till the mid element, checking against the
a[p] . If any of the element is greater than a[q] ,then swap the elements
d. Repeat the steps a and b until the elements are lesser till mid elements and greater
after the mid element.
e. Repeat the steps:a to d for the first half and second half until all the elements are
sorted.

Question:
20.
What is Auto_ptr?
Answer:
The template class describes an object that stores a pointer to an allocated object of type
Type* that ensures that the object to which it points gets destroyed automatically when
control leaves a block.
Example for auto_ptr is as follows:
#include <memory>
#include <iostream>
#include <vector>

using namespace std;


class A
{
public :
A()
{
cout << "Calling Constructor" << endl;
}
~A()
{
cout << "Calling Destructor" << endl;
}
};
//The object(A) will be freed at the end of this function. No need to free this object by
free call
void function ()
{
auto_ptr<A> pi(new A());
return;
}

int main( )
{
cout << "Before Function Call" << endl;
function( );
cout << "After Function Call" << endl;
return 1;
}

Question:
21) Explain preemptive mutlithreading and co-operative multithreading
Answer:
Preemptive multithreading:
At any point of time, thread of high priority holds the CPU. In case if the incoming
thread’s priority is higher than the executing thread, then the running thread relinquishes
the cpu to the incoming thread.
Co-operative multithreading:
Any thread holding the CPU(running), relinquishes the CPU only after it has finished
execution. Other threads have to wait for the CPU for execution.

Question:
22.
What are Templates?
Answer:
Templates are generic class that can be defined for functions and classes.
C++ templates enable you to define a family of functions or classes that can operate on
different types of information. Use templates in situations that result in duplication. For
example, you can use function templates to create a set of functions that apply the same
algorithm to different data types. Templates are sometimes a better solution than C
macros and void pointers,

Example 1: ( Function Templates)

template<class T>
void SWAP ( T a , T b)
{
T temp;
temp = a ;
a = b;
b = temp ;
cout<<a<<" " <<b<<endl;
}

int main()
{
int res;

SWAP (2,3);
SWAP (2.5,3.2);
SWAP ('a', 'b');

cout<<"hai";
return 1;
}
Example 2: (Class Templates)

template<class T>
class Base
{
private:
T Tinfo;
public:

void Disp(T Tinfo)


{
cout<<Tinfo<<endl<<"Get the input" <<endl;
cin>>Tinfo;
cout<<"Value : "<<Tinfo<<endl;
}
};
int main()
{

Base<int> b;
b.Disp(5);
Base<char> b1;
b1.Disp('s');
return 1;
}

Question:
23.A template of Class type is declared and extended by linked List, if you pass a
char the size of the code is 4 kilobytes, what will happen if you pass an integer?
Will the Size get changed? If so which one will Change Source Code or Object
Code?

Answer:
Yes. Source code for char will be more than that of the integer whereas the object code
will be lesser than that of the integer.

Question:
24.Write about STL briefly
Answer:
Standard Template Library provides framework for extending the facilities and for a
better design.
It provides iterators, allocators and make use of them for algorithms and containers.

Various Containers used are:


Sequencers:
Vector -Template for elements access.
List -sequence optimized for insertion and deletion of elements
Deque - used when additions and removals of elements occur at the
end.(double ended queue)
Adapters : (Containers)
Containers are created by providing suitable interfaces for the sequencers.
They are stack, queue and priority queue

Associative containers:
Associative containers are used for text processing and symbolic processing.
Using Unique keys:
Map -Lookup based on the key. Sequence of (key, value) pairs
Set - A map where the values are irrelevant and can be tracked only by
using keys
Using Non-Unique keys:
Multimap -Lookup based on the key. Sequence of (non-unique key, value)
pairs
Multiset - A map where the values are irrelevant and can be tracked only by
using non-unique keys
Almost Containers:
Containers that can hold elements but lack in some aspects of standard container
interface.
Array -built-in arrays
String -optimized for string of characters
Valarray - optimized for numeric computation
Bitset -set of flags for indicating binary conditions

Question:
25. Design constraints of Symbian.
Answer:
1. Must be able to provide hours of operation without powered down at all.
2. Should support low-power processors with limited amounts of memory
3. Low battery so few of the function calls like bluetooth send and receive use more
battery and hence the battery time gets reduced.
4. Code reuse should be maximized.
5. Multiple inheritance is not supported.
6. Reliability can be enhanced with better error-handling framework which enables
graceful recovery from run-time errors such as running out of memory, the
battery getting low or the drop of a communication link.
7. Recalling phones to install service packs is not feasible.
8. Kernel size can be reduced and the system servers should have the capacity of
providing device-driver functionality
9. Needs plug-in facility for protocol stack with necessary abstraction.
10. Input mechanisms can be enhanced by pen input for data entry.

You might also like