C++ Programming Questions

Since I was called in suddenly for work and I don’t have my homework with me, I decided to do some programming questions I found on the web. If you’re not a programmer, turn away now. If you are, you might learn something new. (I did.)

  • What is the output of printf(”%d”)
    Well, generally you’ll prob a decimal value to the screen, but you don’t specify what to print. So depending on the compiler you’ll get nothing or a garbage value.

  • What will happen if I say delete this
    Would you demolish a building while you were standing inside it? Deleting this would have the compiler try and delete the object that you are involved in. While I believe it’s legal code, after you delete the object, you’ll probably end up with errors as you tried to access now unallocated memory.

  • Difference between “C structure” and “C++ structure”.
    A c structure does not have a constructor/destructor/etc. The reason is, c is not an object oriented language and there does not even know what a ctor or dtor are. Likewise, they don’t have members or anything beyond the simple data types. Everything is public, and since there are no members, there is no reason to have private/protected variables anyway.

  • Difference between a “assignment operator” and a “copy constructor”
    An assignment operator creates a new object and assigns the values to this newly created object. A copy constructor just copies data over from one pre-existing object to another pre-existing object. Thus, the main difference is if it’s new or not.

  • What is the difference between “overloading” and “overriding”?
    You overload functions so they can accept different data types. For example: add(int, int) and add(float, float) Overriding is when you take something that currently exists (ie: the + operator) and override it so it does what you want to do instead. (You can make + multiply numbers, if you really wanted to… and if you were dumb.)

  • Explain the need for “Virtual Destructor”.
    A virtual method is a method that is meant to be extended or changed by child classes. If you have any virtual functions, you should have a virtual destructor as well. The reason is that without it, it may not free memory allocated by your function correctly.

  • Can we have “Virtual Constructors”?
    No, all constructors are run from the base class all the way to the current child. Otherwise, it may not allocate memory correctly. Also, if a child constructor attempts to change something, the base class has to set it up first.

  • What are the different types of polymorphism?
    Um.. different types? Not quite sure what this means. Polymorphism is when you take a base class (most likely a pure-virtual class) and change it to do different things. They may all act the same way, but they’re different objects and do different things.

    Note: I just looked this up and understand it now. There is static and dynamic polymorphism. Static is like operator overloading, where it creates different names for each class. (It mangles the names so add(int, int) does not look the same as add(float, float)) The second is dynamic, which is like I described above with virtual classes.

  • What are Virtual Functions? How to implement virtual functions in “C”
    Um.. you would have to do it all explicitly on your own. C++ has a vtable that keeps track of all the functions. I guess in C you would have to create a pointer to functions and manage the table yourself. Seems pretty nasty to me.

  • What are the different types of Storage classes?
    You have the inherent ones likes array, and the included ones like the stl library.

  • What is Namespace?
    A way of scoping your code. Everything consists in the global namespace (::) to begin with and then things are scoped into their own namespaces to help protect variables and to keep the global namespace uncluttered.

  • What are the types of STL containers?.
    Wow, lets see if I can remember them all. vector, stack, queue, deque, list, map, set… I’m sure there are more, but that’s all I can think of.

  • Difference between “vector” and “array”?
    vectors are safer than arrays. vectors include bounds checking, they grow automatically, and are much more robust than arrays.

  • How to write a program such that it will delete itself after execution?
    Delete itself after execution? Um… destructors do that automagically. If you mean it deletes the executable after running, wow, interesting question. I’m a firm believer that anything is possible on a computer, so I guess this would be too. First, you have to know the OS. On a linux OS, you can just system(“unlink filename”); and there you go. However, windows isn’t as easy. Since windows locks the file while it’s open, you have to be sneaky. The easiest way is to create a batch file that attempts to delete it, and once it doesn’t exist anymore, deletes itself.

  • Can we generate a C++ source code from the binary file?
    A decompiler, eh? Yes, you can create C++ source code from a binary file. No, it will not be pretty, and it may not even run correctly. When a compiler optimizes code, it is like it performs a one-way hash on it. Therefore, it’s possible to guess, but highly unlikely it’ll be correct.

  • What are inline functions?
    Inline functions are functions that replace all the function calls in the code with itself. This makes the code run faster at the cost of an increased executable size.

  • What is “strstream” ?
    It’s a class that works much like C char* worked. I haven’t worked with them much and looking it up, it appears that sstream is a replacement for strstream. I’ve always used stl strings myself.

  • Explain “passing by value”, “passing by pointer” and “passing by reference”
    Passing by value. This is for when you assign something such as int i; blah(i); You’re passing the value only.
    Passing by pointer. int *i; *i = 1; blah (*i); Basically, you pass a pointer that points to the value in memory. This is useful for data structures that are of unknown sizes, such as a linked list, or even char*
    Passing by reference. This is where you pass the memory address of a value. ie: int *i; blah(&i); When you pass by value, it creates a new int and stores the value in the new variable. When you pass by reference, however, you actually interact with the variable itself. So, any changes that happen in the function will still exist afterwards.

  • Have you heard of “mutable” keyword?
    It’s been a while… (looks it up) Ahh yes. It allows a value to be updatable even if the object itself is const. I have never once used it. I honestly don’t know why you would use it except for extremely rare cases (and a good debug case that I saw on the webpage I looked at.) So, yes, I’ve heard of it only long enough to remember I won’t use it often. (if at all)

  • What is a “RTTI”?
    RTTI stands for runtime type info. Basically, you can determine what type an object is at runtime.

  • Is there something that I can do in C and not in C++?
    No. Since C++ was built with C, I guess if you can do in C, you can write it so you can do it in C++.

  • What is the difference between “calloc” and “malloc”?
    To be honest, I don’t know what calloc is. (looks it up) Ok, it’s just a malloc that zeroes the memory as it allocates it. I think I’ll stick with new either way.

  • What will happen if I allocate memory using “new” and free it using “free” or allocate sing “calloc” and free it using “delete”?
    You get a memory leak. First of all, delete calls the dtor. Free doesn’t even know what a ctor is. Likewise, new calls the ctor, and malloc just sets memory aside.

  • Difference between “printf” and “sprintf”.
    One prints values to a the stdout data stream, while the other returns it. For example, you use printf to print stuff to the console, and sprintf to fill a char*.

  • What is “map” in STL?
    A map is a pair/value data type. Using this, you can (wow.. I just started doing php…) fruit[“Oranges”]= 10; fruit[“Apples”] = 5;

  • When shall I use Multiple Inheritance?
    As little as possible, I would think. If you have to inherit more than one class, things just start getting really complicated. While there may be cases where it is logical, I think they would be few and far between. It’s especially troublesome because you have to worry about which initializes first, and stuff like that.

  • Explain working of printf.
    Um.. ok. You pass a string with ‘tokens’ in it such as %d %f %c etc. First sprint goes through the string and replaces occurrences of those tokens with the values you submitted after the string. (The string is often called a format string.) Once it completes the replacements, it passes it on to the stdout stream.

  • Talk sometiming about profiling?
    Profiling is timing an application. I know visual studio comes with a profiler. You can use this to do benchmarks on your code. A simple version would be to call time in linux, or even to have a function print the time at the beginning and end of main. This is not very accurate though. Also, ‘sometiming’ ?? Is that a pun?

  • How many lines of code you have written for a single program?
    A couple hundred? I don’t know. I haven’t written many large programs.

  • How to write Multithreaded applications using C++?
    It depends on your OS. In windows you use CreateThread, in linux you use fork(). There are many different types of threads you can make depending on your needs. You can then suspend, or kill threads as you need to.

  • Write any small program that will compile in “C” but not in “C++”
    void *new (unsigned int size) { return (malloc(size)); } This will fail under C++ because your using a reserved word.

  • What is Memory Alignment?
    initializing and freeing memory as it’s being used. C does this with malloc/free, and c++ with new/delete.

  • Why preincrement operator is faster than postincrement?
    *cough* I don’t know. I thought it would be the same since they both +=1; Just at different times. ok. Now that I looked it up, it’s obvious. the preincrement is faster because it can INC the value and call it good. The post has to create a temp value to return and then inc the value. The things you learn.

  • What are the techniques you use for debugging?
    Depends on the program. For smaller ones I just like to add cout << "Var = " << var << endl; etc. ASSERTS and stuff work too. I also love to use watches and step through stuff in the compiler. For larger programs, you have test cases and stuff that you run on the code to ensure proper results.
  • How to reduce a final size of executable?
    Increase the optimizations. Use DLL’s when available. Don’t use inline functions. Don’t store debug information in the executable.

  • Give 2 examples of a code optimization.
    Um.. that I use or that the compiler uses? I know that using initialization lists for classes and assigning values to variables when they’re created is faster than doing it otherwise. Also, using inline functions will make your program run faster. If it’s asking how the compiler optimizes stuff… I’m entirely unsure.

About Matthew Jones

Writer, Programmer, Astronomer, Dreamer, Wisher, Fighter. Always striving to be better than I was.
This entry was posted in Uncategorized and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *