Skip to content

Technical interview: C++

October 24, 2012

This is time for technical interview with Cisco. In this post, I try to review some C/C++ questions or put the best links to guide you best series of questions that you can study. First of all, I want to recommend you a book that all of the programmers read before interviewing with giant software companies such as Microsoft, Google, Amazoon, facebook, SAP. I cant even count how many of my friends got a job or internship  in these companies by reading this book. This book is called “Cracking the coding interview” by GAYLE LAAKMANN. Secondly, I’d like to put some C/C++ codes that are asked in interviews Thirdly, let me ask you some conceptual questions. subject by subject…

part 1:  The C language basics


1. What is a local block?

2. Should variables be stored in local blocks?

3. When is a switch statement better than multiple if statements?

4. Is a default case necessary in a switch statement?

5. Can the last case of a switch statement skip including the break?

6. Other than in a for statement, when is the comma operator used?

7. How can you tell whether a loop ended prematurely?

8. What is the difference between goto and long jmp( ) and setjmp()?

9. What is an lvalue? VS rvalue

10. Can an array be an lvalue? NO!!

11. What is an rvalue?

12. Is left-to-right or right-to-left order guaranteed for operator precedence?

13. What is the difference between ++var and var++?

14. What does the modulus operator do?

There are two type of control structures: -conditional;(if and switch-case) and loops (for,while, do-while)

If you have introduce variable for for loop you just have write for(;i<10;i=i+1)

Part 2: Bits and Bytes


1. What is the most efficient way to store flag values?

2. What is meant by “bit masking”?

3. Are bit fields portable?

4. Is it better to bitshift a value than to multiply by 2?

5. What is meant by high-order and low-order bytes?

6. How are 16- and 32-bit numbers stored?

Part 3 : Pre-processors


1. What is a macro, and how do you use it?

2. What will the preprocessor do for a program?

3. How can you avoid including a header more than once?

4. Can a file other than a .h file be included with #include?

5. What is the benefit of using #define to declare a constant?

6. What is the benefit of using enum to declare a constant?

7. What is the benefit of using an enum rather than a #define constant?

8. How are portions of a program disabled in demo versions?

9. Is it better to use a macro or a function?

10. What is the best way to comment out a section of code that contains comments?

11. What is the difference between #include and #include “file” ?

12. Can you define which header file to include at compile time?

13. Can include files be nested?

14. How many levels deep can include files be nested?

15. What is the concatenation operator?

Part no. 4: Arrays


1. Do array subscripts always start with zero?

2. Is it valid to address one element beyond the end of an array?

3. Can the sizeof operator be used to tell the size of an array passed to a function?

4. Is it better to use a pointer to navigate an array of values, or is it better to use a subscripted array name?

5. Can you assign a different address to an array tag?

6. What is the difference between array_name and &array_name?

7. Why can’t constant values be used to define an array’s initial size?

8. What is the difference between a string and an array?

part 5:  Variables 


1. Where in memory are my variables stored? (data segment-stack-Heap)

2. Do variables need to be initialized?

3. What is page thrashing?

4. What is a const pointer?

5. When should the register modifier be used? Does it really help?

6. When should the volatile modifier be used? – I bet you have never heard this question 😉

7. Can a variable be both const and volatile?

8. When should the const modifier be used?

9. How reliable are floating-point comparisons? -Floating-point numbers are the “black art” of computer programming.!!!

10. How can you determine the maximum value that a numeric variable can hold?

11. Are there any problems with performing mathematical operations on different variable types?

12. When should a type cast be used?

13. When should a type cast not be used?

14. Is it acceptable to declare/define a variable in a C header?

15. What is the difference between declaring a variable and defining a variable?

16. Can static variables be declared in a header file?

17. What is the benefit of using const for declaring constants?

Part 6: Functions:


1. When should I declare a function?

2. Why should I prototype a function?

3. How many parameters should a function have?

4. What is a static function?

5. Should a function contain a return statement if it does not return a value?

6. How can you pass an array to a function by value? Passing arrays of any kind to functions can be very costly in several ways!! Instead of passing arrays to functions by value, you should consider passing arrays to functions by reference

7. Is it possible to execute code even after the program exits the main() function?

8. What does a function declared as PASCAL do differently? 😉

9. Is using exit() the same as using return?

Part 7:  Pointers- All the times asked part 😉


1. What is indirection?

2. How many levels of pointers can you have?

3. What is a null pointer?

4. When is a null pointer used? 😀

5. What is a void pointer?

7. Can you subtract pointers from each other? Why would you?

8. Is NULL always defined as 0(zero)?

9. Is NULL always equal to 0(zero)?

10. What does it mean when a pointer is used in an if statement?

11. Can you add pointers together? Why would you?

12. How do you use a pointer to a function?

13. When would you use a pointer to a function?

14. Can the size of an array be declared at runtime?

15. Is it better to use malloc()or calloc()?First, is in the number of arguments. Malloc() takes a single argument (memory required in bytes), while calloc() needs two arguments. Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO. ….

16. Is it better to use malloc()or calloc()? 16. How do you declare an array that will hold more than 64KB of data?

17. What is the difference between farand near?

18. When should a farpointer be used?

19. What is the stack?

20. What is the heap?

21. What happens if you free a pointer twice?

22. What is the difference between NULL and NUL?

Part  8: ِData structures: All programmers should know something about basic data structures like stacks, queues and heaps. Graphs are a tremendously useful concept, and two-three trees solve a lot of problems inherent in more basic binary trees. 


  1.  What is a stack?
  2. Were do stacks come to play?
  3. What do these words mean? Push, Pop, Peek, LIFO, FILO
  4. What is queue data structure?

From here reference is : MIT on-line course

Part  9: OOP VS. procedural programming


  1. What is OOP programming?
  2. What are the principals of OOP? encapsulation, polymorphism, inheritance and abstraction
  3. What is encapsulation? grouping related data and functions together as objects and defining an interface to those objects.If someone hands us a class, we do not need to know how it actually works to use it; all we need to know about is its public methods/data – its interface. This is often compared to operating a car: when you drive, you don’t care how the steering wheel makes the wheels turn; you just care that the interface the car presents (the steering wheel) allows you to accomplish your goal.
  4. What is polymorphism?  allowing code to be reused between related types
  5. What is inheritance ? allowing a value to be one of several types, and determining at runtime which functions to call on it based on its type
  6. What is a class? A user-defined datatype which groups together related pieces of information
  7. What is field (member)? indicate what related pieces of information our datatype consists of
  8. What is instance? An instance is an occurrence of a class.Different instances can have their own set of values in their fields.
  9. How does copying an instance to other one work? Assigning one instance to another copies all fields
  10. How does passing a class to a function work?
    1. Passing by value passes a copy of the class instance to the function; changes aren’t preserved, and it will do nothing for us ,
    2. When a class instance is passed by reference, changes are reflected in the original
  11. What is method? functions which are part of a class
  12. What is a constructor? Method that is called when an instance is created class, since Manually initializing your fields can get tedious, there is no output type on it and a class can have multiple constructor
  13. Why make a copy constructor? Assigning all fields

(default copy constructor) may not be what you want. This is mostly because of pointers

  1. What are access modifiers?  Define where your fields/methods can be accessed from, Public, Private and protected are three types of access modifiers.  by default fields of  classes are private
  2. What is the difference between a class and a struct? [My interview question a QualComm]  Structs are a carry-over from the C; in C++, classes are generally used. In C++, they’re essentially the same as classes, except structs’ default access modifier is public.
  3. Can we have classes that accept inputs? If yes how? Yes, it is doable by defining constructor that accpets parameters, but we can’t put parameter definition at the class name definition
  4. What is a de-construxtor? A destructor is another special kind of class member function that is executed when an object of that class is destroyed. This all trouble  come from POINTER side. Pointers inside a class or a class made as a pointer. Like constructors, destructors have specific naming rules:
    1) The destructor must have the same name as the class, preceded by a tilde (~).
    2) The destructor can not take arguments. which means only one destructor may exist per class, as there is no way to overload destructors since they can not be differentiated from each other based on arguments.
    3) The destructor has no return type.
  5. What is the “this” pointer?   [HOW TO MAKE CHAINS OF FUNCTIONS]
    1.  If you have a constructor (or member function) that has a parameter of the same name as a member variable, you can disambiguate them by using “this” BUT we find using the “m_” prefix on all member variable names provides a better solution by preventing duplicate names altogether!
    2. Occasionally it can be useful to have a function return the object it was working with. Returning *this will return a reference to the object that was implicitly passed to the function by C++. One use for this feature is that it allows a series of functions to be “chained” together, so that the output of one function becomes the input of another function! The following is somewhat more advanced and can be considered optional material at this point.
  6. What is private  constructor and why should we make them? Occasionally, we do not want users outside of a class to use particular constructors. 
  7. What is statistic member variable? and how does it work? put static in front of member variable declaration and make it shared between instances. Again, it will be cool application to generate ID numbers.
  8. What is the story of friend functions, and classes? For maintenance, we need to have smaller classes. When having several classes that work together they have to friends and share private member variables, and functions. Note 1: It doesn’t matter, where to define friendship in private or public part.

part 11:  Overloading

will be continued …. Ali

post no. 5


From → Cisco

  1. Hello there, just became alerted to your blog through Google, and found that it is truly informative. I will be grateful if you continue this in future. Many people will be benefited from your writing. Cheers!

  2. Greeting from over the ocean. excellent blog I shall return for more.

  3. An interesting discussion is definitely worth comment. There’s no doubt that that you should publish more about this subject matter, it might not be a taboo subject but usually people don’t discuss such subjects. To the next! Cheers!!

  4. [points for functions]

    – Function concept is made to reuse the code without rewriting them
    – A function only returns a value, One of the tricks to get MORE than A VALUE is to send OUTPUTS by reference in the FUNCTION
    -Overload means writing a function in several versions for different types of inputs/outputs
    -Override concept is related to classes and means rewriting functions to do some some else[modification]
    – Forward declaration or function prototype is great job, since there will be no problem on order of functions
    -For recursive functions, considering ending condition is mandatory

  5. [Points for Arrays]
    Arrays can also be passed as arguments to functions. When declaring the function, simply specify the array as a parameter, without a dimension. The array can then be used as normal within the function. For example:
    using namespace std;
    int sum(const int array[], const int length) {
    long sum = 0;
    for(int i = 0; i < length; sum += array[i++]);
    return sum;

    int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7};
    cout << "Sum: " << sum(arr, 7) << endl;
    return 0;

  6. [Points for Arrays]
    It is important to note that arrays are passed by reference and so any changes made to the array within the function will be observed in the calling scope

  7. [Motivating Pointers]
    Memory addresses, or pointers, allow us to manipulate data much more flexibly; manipulating the memory addresses of data can be more efficient than manipulating the data itself. Just a taste of what we’ll be able to do with pointers
    1- More flexible pass-by-reference
    2-Manipulate complex data structures efficiently, even if their data is scattered in different memory locations
    3- Use polymorphism – calling functions on data without knowing exactly what kind of dataitis(more onthisinLectures7-8)

  8. Constant pointers

    There are two places the const keyword can be placed within a pointer variable declaration.
    This is because there are two different variables whose values you might want to forbid changing:
    1-The pointer itself and 2- The value it points to.

    – const int *ptr;
    Declares a changeable pointer to a constant integer.
    The integer value cannot be changed through this pointer, but the pointer maybe changed to point to a different constant integer.

    – int * const ptr;
    declares a constant pointer to changeable integer data. The integer value can be changed through this
    pointer, but the pointer may not be changed to point to a different constant integer.

    – const int * const ptr;
    forbids changing either the address ptr contains or the value it points to.

  9. NULL pointer
    Some pointers do not point to valid data; dereferencing such a pointer is a runtime error.
    Any pointer set to 0 is called a null pointer, and since there is no memory location 0, it is an invalid pointer.
    One should generally check whether a pointer is null before dereferencing it.
    Pointers are often set to 0 to signal that they are not currently valid.

  10. pointer-offset notation:
    You explicitly add your offset to the pointer and dereference the resulting address.
    For instance, an alternate and functionally identical way to express myArray[3] is *(myArray + 3)

  11. Reference:
    References are just pointers that are dereferenced every time they are used.

  12. char * Strings
    The type of a string value is char *: a string is actually an array of characters. When you set a char * to a string,
    you are really setting a point to the first character in the array that holds the string.

    You cannot modify string literals; to do so is either a syntax error or a runtime error.
    String literals are loaded into read-only program memory at program startup.

  13. ******** static keyword *********
    A variable with file scope can be accessed by any function or block within a single file. To declare a file scoped variable, simply declare a variable outside of a block (same as a global variable) but use the static keyword

  14. ******* Returning values by value, reference, and address *********

    Return by value ”
    is the simplest and safest return type to use. When a value is returned by value, a copy of that value is returned to the caller

    Return by value is the most appropriate when returning variables that were declared inside the function, or for returning function arguments that were passed by value. However, like pass by value, return by value is slow for structs and large classes.

    Return by references:
    Returning by reference has one additional downside that pass by reference doesn’t — you can not return local variables to the function by reference

    Return by reference is typically used to return arguments passed by reference to the function back to the caller. In the following example, we return (by reference) an element of an array that was passed to our function by reference:

  15. ****************************** Function Pointer = subroutine pointer ************************************
    Calling any function by its address i function pointer

    my QualComm interview question:
    Q: how to write a function inside a struct?
    A:No, but they can contain function pointers.

    here are several cases where pointers to function can be of use. One of the most common is the case where you are writing a function to perform a task (such as sorting an array), but you want the user to be able to define how a particular part of that task will be performed (such as whether the array is sorted in ascending or descending order). Let’s take a closer look at this problem as applied specifically to sorting, as an example that can be generalized to other similar problems.

  16. Passing by value passes a copy of the class instance
    to the function; changes aren’t preserved

    When a class instance is passed by reference,
    changes are reflected in the original

    • Method that is called when an instance is created
    • Can have multiple constructors

  17. There are lots of definitions for OOP, but 3 primary features of it are:

    Encapsulation: grouping related data and functions together as objects and defining an interface to those objects

    Inheritance: allowing code to be reused between related types

    Polymorphism: allowing a value to be one of several types, and determining at runtime which functions to call on it based on its type

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: