C++ Interview Questions
C++ Interview Questions
C++ Interview Questions
4. In C, why is the void pointer useful? When would you use it? The void
pointer is useful becuase it is a generic pointer that any pointer can be cast
into and back again without loss of information.
C++ object-oriented
Q. In the derived class, which data member of the base class are
visible?
In the public and protected sections.
C++ algorithm
(From IBM) What classes you have enjoyed the most during your
school years?
Answer: I like the class I am taking this semester, which involves a group
project that needs great amount of team efforts. I really enjoy work with a
group of people because we can learn new materials mutually.
>
m IBM) According to your group project you just mentioned, what’s the
responsibility for each member in your group?
Answer: We have five people in our group. So far we have two web servers
set up; one will be the back up system and the other will be the main system.
Our leader coordinates the schedule. Two members are working on the
database and do the coding for the connection between database and Java
serverlets. One member is working on the user browser interface. All
members will assign some classes to work on and perform the final test at the
end. We have group meeting every Saturday to ensure our schedule is on
track.
2. What is polymorphism?
3. How do you find out if a linked-list has an end? (i.e. the list is not a
cycle)
You can find out by using 2 pointers. One of them goes 2 nodes each time.
The second one goes at 1 nodes each time. If there is a cycle, the one that
goes 2 nodes each time will eventually meet the one that goes slower. If that
is the case, then you will know the linked-list is a cycle.
4. How can you tell what shell you are running on UNIX system?
You can do the Echo $RANDOM. It will return a undefined variable if you are
from the C-Shell, just a return prompt if you are from the Bourne shell, and a
5 digit random numbers if you are from the Korn shell. You could also do a ps
-l and look for the shell with the highest PID.
5. What is Boyce Codd Normal form?
char *str;
err_num = 2;
return (str);
while(len)
str[i++]=s[–len];
str[i] = NULL;
return (str);
3) Implementation (coding)
4) Testing
6) Obsolescence
A: -Application layer
-Presentation layer
-Session layer
-Transport layer
-Network layer
-Physical layer
A1 B-star trees have better data structure and are faster in search than
Binary trees, but it’s harder to write codes for B-start trees.
Q2 Write the psuedo code for the Depth first Search.(Asked by
Microsoft)
A2
dfs(G, v) //OUTLINE
Mark v as "discovered"
For each vertex w such that edge vw is in G:
If w is undiscovered:
dfs(G, w); that is, explore vw, visit w, explore from there
as much as possible, and backtrack from w to v.
Otherwise:
"Check" vw without visiting w.
Mark v as "finished".
A3. The simplest rehashing policy is linear probing. Suppose a key K hashes
to location i. Suppose other key occupies H[i]. The following function is used
to generate alternative locations:
rehash(j) = (j + 1) mod h
where j is the location most recently probed. Initially j = i, the hash code for
K. Notice that this version of rehash does not depend on K.
Answer: Mergesort always makes recursive calls to sort subarrays that are
about half size of the original array, resulting in O(n log n) time.
Answer: The real power of arrays comes from their facility of using an index
variable to traverse the array, accessing each element with the same
expression a[i]. All the is needed to make this work is a iterated statement in
which the variable i serves as a counter, incrementing from 0 to a.length -1.
That is exactly what a loop does.
// Container of fruit
class BasketOfFruit {
BasketOfFruit() ;
void insert( Fruit & f ) ;
// …
}
// Container of apples
class BasketOfApples /* ??? */ {
// …
}
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 ;
}
5. What default methods are declared implicitly by the C++ compiler for the
class below:
class Empty
{
};
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.
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)
Thread 4: (priority: 4)
if (checkAlarms()) /* If any serious alarms exist */
Disk.write(AlarmData); /* Buffer that data to disk for immediate
network transmit */
yield();
Q: What are the differences between a C++ struct and C++ class?
A: Two.
There are two formats for initializers in C++ as shown in the example that
follows. The first format uses the traditional C notation. The second format
uses constructor notation.
Q: How does throwing and catching exceptions differ from using setjmp and
longjmp?
A: The throw operation calls the destructors for automatic objects instantiated
since entry to the try block.
delete this;
A: There are three acceptable answers: "Never," "Rarely," and "When the
problem domain cannot be accurately modeled any other way."
A: The simple answer is that a virtual destructor is one that is declared with
the virtual attribute.
Q: Explain the ISA and HASA class relationships. How would you implement
each in a class design?
A: One that can be modified by the class even when the object of the class or
the member function doing the modification is const.
A: The ability to determine at run time the type of an object by using the
typeid operator or the dynamic_cast operator.
This solution assumes that two library vendors don’t use the same namespace
identifier, of course.
A: Yes. The ANSI committee added the bool intrinsic type and its true and
false value keywords.
1. What is a Make file?(Fujitsu) Make file is a utility in Unix to help compile
large programs. It helps by only compiling the portion of the program that
has been changed.
5. Name some major differences between C++ and Java. C++ has
pointers; Java does not. Java is platform-independent; C++ is not. Java has
garbage collection; C++ does not.
1. Q1: Could you tell something about the Unix System Kernel? (from
ITCO )
A1: The kernel is the heart of the UNIX openrating system, it’s reponsible for
controlling the computer’s resouces and scheduling user jobs so that each one
gets its fair share of resources.
Q2: What are each of the standard files and what are they normally
associated with? (from ITCO )
A2: They are the standard input file, the standard output file and the standard
error file. The first is usually associated with the keyboard, the second and
third are usually associated with the terminal screen.
Q3: Detemine the code below, tell me exectly how many times is the
operation sum++ performed ? (from ITCO )
A5: The "ARP" stands for Address Resolution Protocol. The ARP standard
defines two basic message types: a request and a response. a request
message contains an IP address and requests the corresponding hardware
address; a replay contains both the IP address, sent in the request, and the
hardware address.