Wednesday, April 9, 2008

C++ Programming coding interview questions with solutions - 6

C++ Coders / Developers interview questions and answers

1. Design and implement a String class that satisfies the following:
Supports embedded nulls
Provide the following methods (at least)
Constructor
Destructor
Copy constructor
Assignment operator
Addition operator (concatenation)
Return character at location
Return substring at location
Find substring
Provide versions of methods for String and for char* arguments

2. Given the following classes
class Fruit {
// ?
}

class Apple : public Fruit {
// ?
}

class Peach : public Fruit {
// ?
}

// Container of fruit
class BasketOfFruit {
BasketOfFruit() ;
void insert( Fruit & f ) ;
// ?
}

// Container of apples
class BasketOfApples /* ??? */ {
// ?
}

Should BasketOfApples derive from BasketOfFruit? Why or why not?
What is the general principle that determines the answer?

3. Describe briefly what the following function does. What standard function is it most like ?
int f( char *p ) {
int n = 0 ;
while ( *p != 0 ) n = 10*n + *p++ - ?0′ ;
return n ;
}

4. Describe briefly what function ?a? does in the following code fragment.
struct s {
struct s *next ;
}

a( struct s *p, struct s *x ) {
while ( p->next != 0 ) p = p->next ;
p->next = x ;
x->next = 0 ;
}

5. What default methods are declared implicitly by the C++ compiler for the class below:
class Empty
{
};

6. Given a system with a hard realtime priority, multithreaded architecture, with priorities from 1 (least) to 5 (most), explain the major flaw in the design below:

The following objects are shared between the threads:

Disk : This class is a singleton. The read() and write() methods both block on a simple atomic lock()/unlock() shared between the two. (ie, only one thread can access the disk, thru either read or write, at any given time). It also has a waitForData() method, which blocks (without claiming the lock) until either a timeout elapses, or data is ready. It returns true upon returning due to new data, false upon returning due to the timeout.

Network : This class is a singleton. The read() and write() methods both block on a simple atomic lock()/unlock() shared between the two. (ie, only one thread can access the disk, thru either read or write, at any given time).

Sensor: The Sensor class accesses a number of physical sensors. The first method, ?waitForData()?, blocks until data has been collected from the sensors. The second method, ?processData()?, does a series of long and cpu-intensive calculations on the data. These calculations often take several minutes. It then returns the processed data.

Each of the following threads is running concurrently. Assume that the psuedocode in each thread is looped infinitely (ie, encased in a while(true) { }. It is extremely important that information buffered to the disk be sent to the network as quickly as possible, this is why Thread 1 runs at priority 5. The system conditions checked in thread 3 are not particularly important events (not as important as the calculations done in thread 2). If the events aren?t transmitted over the network for several minutes, it?s not a problem at all. They do, however, contain a large amount of system information. Thread 4 watches for serious system alarms, indicating serious problems. These are a serious concern and if not quickly buffered to the disk and sent to the network, can cause serious revenue loss.

Thread 1: (priority: 5)
while(!Disk.waitForData()) { yield(); } /* Wait until someone has
written data to the disk */
Network.write(Disk.read()); /* Write the data buffered on the disk to
the network */

Thread 2: (priority: 2)
while(!Sensor.waitForData()) { yield(); } /* Wait until the sensors
have picked up data */
Disk.write(Sensor.processData()); /* process the data and write it to
the disk. */

Thread 3: (priority: 1)

if (checkSystemCondition1()) /* If system condition 1 is true.. */
Disk.write(SystemCond1Data); /* Grab the data on the system
condition and buffer it to disk */
if (checkSystemCondition2()) /* see above*/
Disk.write(SystemCond2Data);
if (checkSystemCondition3()) /* see above */
Disk.write(SystemCond3Data);
yield();

Thread 4: (priority: 4)
if (checkAlarms()) /* If any serious alarms exist */
Disk.write(AlarmData); /* Buffer that data to disk for immediate
network transmit */
yield();

See more C++ and other computer languages important, mostly asked questions with solutions / detailed answers and programs for reputed IT and software developing companies only on PreviousPapers.blogspot.com

No comments: