Technical interview: C++
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.
- What is a stack?
- Were do stacks come to play?
- What do these words mean? Push, Pop, Peek, LIFO, FILO
- What is queue data structure?
From here reference is : MIT on-line course
Part 9: OOP VS. procedural programming
- What is OOP programming?
- What are the principals of OOP? encapsulation, polymorphism, inheritance and abstraction
- What is encapsulation? grouping related data and functions together as objects and deﬁning 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.
- What is polymorphism? allowing code to be reused between related types
- 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
- What is a class? A user-defined datatype which groups together related pieces of information
- What is field (member)? indicate what related pieces of information our datatype consists of
- What is instance? An instance is an occurrence of a class.Different instances can have their own set of values in their fields.
- How does copying an instance to other one work? Assigning one instance to another copies all fields
- How does passing a class to a function work?
- Passing by value passes a copy of the class instance to the function; changes aren’t preserved, and it will do nothing for us ,
- When a class instance is passed by reference, changes are reflected in the original
- What is method? functions which are part of a class
- 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
- Why make a copy constructor? Assigning all fields
(default copy constructor) may not be what you want. This is mostly because of pointers
- 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
- 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.
- 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
- 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.
- What is the “this” pointer? [HOW TO MAKE CHAINS OF FUNCTIONS]
- 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!
- 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.
- What is private constructor and why should we make them? Occasionally, we do not want users outside of a class to use particular constructors.
- 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.
- 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