Q.
We have a base class and an inherited class. Suppose we use a pointer to the
inherited class and then typecast it to the base class pointer. Now, if we
invoke a virtual function using this pointer, which method will it call? Base
class method or inherited class method?
That
is:
Child
*child = new Child();
Parent*
parent = child; // note: no typecast is needed for upcast
parent->my_func();
A. Still inherited class
virtual method.
Virtual functions are resolved by an implicit vtable, which is an array
of function pointers for every virtual function in the class hierarchy. Casts
will not affect this table.
Q.
A copy constructor is provided by the compiler. So why would a programmer need
to define a copy constructor?
A.
For the purpose of deep copy.
Q.
What is a static variable in C++?
A. A data member that is NOT tied to an object
instantiation, but is global across all instantiations of the class; for
example can be used to keep a count of the number of objects instantiated.
Q.
In C, if we have a static variable in a source file, can we access that in
another source file using extern?
A.
No; static in C limits the scope of the variable to the file only.
Q.
If you are given a variable X, how would you count the number of 1s in its
binary form?
A.
int
countOnes (int X)
{
int Y = X;
int count = 0;
while (Y != 0)
{
if ((Y & 1) == 1) count++;
Y >>= 1;
}
return count;
}
Complexity
= O (N), where N = length of X in binary form.
Better
algorithm (based on “Programming Interviews” book)
int
countOnes (int X)
{
int Y = X;
int count = 0;
while (Y != 0)
{
Y &= (Y -1);
count++;
}
return count;
}
Complexity
= O (M), where M = number of 1s in X in binary form.
Q.
What is an inline function?
A. (based on 'How to program C++' by Deitel & Deitel)
C++ provides inline functions to help reduce function-call overhead - especially for small functions. The qualifier inline "advises" the compiler to generate a copy of the function's code in place (when appropriate) to avoid a function call. The trade-off is that multiple copies of the function code are inserted in the program (thus making the program larger). The compiler can ignore the inline qualifier and typically does so for all but the smallest functions.
Note:
- Any change to an inline function could require all clients of the function to be recompiled.
- Using inline functions could reduce execution time but increase program size.
C++ provides inline functions to help reduce function-call overhead - especially for small functions. The qualifier inline "advises" the compiler to generate a copy of the function's code in place (when appropriate) to avoid a function call. The trade-off is that multiple copies of the function code are inserted in the program (thus making the program larger). The compiler can ignore the inline qualifier and typically does so for all but the smallest functions.
Note:
- Any change to an inline function could require all clients of the function to be recompiled.
- Using inline functions could reduce execution time but increase program size.
Q.
What are some examples of using a function pointer?
A. (See answer to a related question below.)
A.
Q.
What are some of the drawbacks of using a function pointer?
A.
Q.
If you are designing a board package, that has, let’s say 8 pins, how would
design a routine that makes sure that no pins are shorted, that values that are
read off the hardware have stayed the same?
A.
walking-0 and walking-1
Q.
In ARM processor, what is the difference between an interrupt and an exception?
A.
Interrupts are treated as exceptions, however in the register, there is
Interrupt mask flag. Interrupts can be masked off and serviced later, but not
exceptions.
Q.
What is LIFO in data structure? What is an example of a data structure that
uses LIFO?
A.
Last In First Out. Stack.
Q.
How is a Queue different from a Stack?
A.
Queue uses a Head and a Tail, it is a FIFO data structure.
Q.
What is the difference between a preemptive and a cooperative operating system?
A.
Q. Walk me through the steps from when you type in a Web page on your browser to when you see the content getting displayed.
C++:
Q. What is inheritance? What does it provide us?
Q. What is overloading?
Q. Explain the access levels public, private and protected.
Q. If I give you a large piece of code to debug without any debugger to step through it, how would you debug?
Q. How do you do run-time error handling in C++?
A. Using try/catch to do exception-handling.
(based on C++: The Complete Reference)
C++ exception handling is based upon three keywords: try, catch, and throw. In the most general terms, program statements that you want to monitor for exceptions are contained in a try block. If an exception (i.e. an error) occurs within the try block, it is thrown (using throw). The exception is caught, using catch, and processed.
Code that you want to monitor for exceptions must have been executed from within a try block. (Functions called from within a try block may also throw an exception.) Exceptions that can be thrown by the monitored code are caught by a catch statement, which immediately follows the try statement in which the exception was thrown.
..
If you throw an exception for which there is no applicable catch statement, an abnormal program termination may occur. Throwing an unhandled exception causes the standard library function terminate() to be invoked. By default, terminate() calls abort() to stop your program, but you can specify your own termination handler.
#include <iostream>
using namespace std;
int main()
{
cout << "Start\n";
try { // start a try block
cout << "Inside try block\n";
throw 100;
cout << "This will not execute";
}
catch (int i) { // catch an error
cout << "Caught an exception -- value is: ";
cout << i << "\n";
}
cout << "End";
}
C:
Q. What are far and near pointers?
A.
(from stackoverflow.com)
4 registers are used for 4 segments on the 16-bit x86 segmented memory architecture
- Data segment, Code segment, Stack segment and Extra Segment
Near pointers refer (as an offset) to the current segment.
Far pointers use segment info and an offset to point across segments.
(based on 'Deep C Secrets')
An address on the Intel 8086 is formed by combining a 16-bit segment with a 16-bit offset. The segment is shifted left four places before the offset is added. This means that many different segment/offset pairs can point to the same one address.
segment
(shifted left 4 bits) offset resulting address
A0000 + FFFF = AFFFF
.
.
AFFF0 + 000F = AFFFF
A C compiler-writer needs to make sure that pointers are compared in canonical form on a PC, otherwise two pointers that have different bit patterns but designate the same address may wrongly compare unequal. This will be done for you if you use the "huge" keyword, but does not occur for the "large" model. The far keyword in Microsoft C indicates that the pointer stores the contents of the segment register and the offset. The near keyword means the pointer just holds a 16-bit offset, and it will use the value already in the data or stack segment register.
Q. What is a void pointer?
A.
(based on 'C: The Complete Reference')
In C it is permissible to assign a void * pointer to any other type of pointer. It is also permissible to assign any other type of pointer to a void * pointer.
A void * pointer is called a generic pointer. It is used to specify a pointer whose base type is unknown. The void * type allows a function to specify a parameter that is capable of receiving any type of pointer argument without reporting a type mismatch.
It is also used to refer to raw memory (such as that returned by malloc() ) when the semantics of that memory are not known. No explicit cast is required to convert to or from a void * pointer.
(based on 'Functional C Programming')
When used in a pointer declaration, void defines a generic pointer.
Q. When would you use a function pointer?
A.
(based on 'Programming in C' by Stephen G. Kochan)
- One common application for pointers to functions is in passing them as arguments to other functions. Standard C library uses this, for example, in the function qsort. This function takes as one of its arguments a pointer to a function that is called whenever qsort needs to compare 2 elements in the array being sorted. In this manner, qsort can be used to sort arrays of any type, as the actual comparison of any 2 elements in the array is made by a user-supplied function, and not by the qsort function itself.
- Another common application for function pointers is to create what is known as dispatch tables. You can't store functions themselves inside the elements of an array. However it is valid to store function pointers inside an array.
For example, you might create a table for processing different commands that will be entered by a user. Each entry in the table could contain both the command name and a pointer to a function to call to process that particular command. Now whenever the user enters a command, you can look up the command inside the table and invoke the corresponding function to handle it.
(based on 'Learn C the hard way' by Zed A. Shaw)
The main use for this is to pass callbacks to other functions, or to simulate classes and objects.
Format: int (*POINTER_NAME) (int a, int b)
Steps:
- int callme (int a, int b) // normal function declaration
- int (*callme) (int a, int b) // wrap function name with pointer syntax
- int (*compare_cb) (int a, int b) // change name to pointer name
Example:
int (*tester) (int a, int b) = sorted_order;
printf ("TEST: %d is same as %d", tester (2,3), sorted_order (2,3));
General:
Q. What is a signal?
A.
(based on "Embedded Software" book Section 3.3.3)
Embedded OSs implement IPC based on one or some combination of memory sharing, message passing, and signaling mechanisms.
Signals are indicators to a task that an asynchronous event has been generated by some external event (other processes, board hardware, timers) or some internal event (problems with instructions being executed). When a task receives a signal, it suspends executing the current instruction stream and context switches to a signal handler.
Signals are typically used for interrupt handling in an OS, because of their asynchronous nature.
No comments:
Post a Comment