![]() |
![]() |
![]() |
![]() |
|||||
|
C and C++ - Sample Questions and Answers
1.1. What are the differences between C++ and Java? C++ was developed a long time ago as a successor to C, a programming language. It is very flexible and users can do structured or object oriented programming with it. C++ code needs to be compiled into a bytecode that can be understood by the operating system that it is meant to run on, and moving it to another operating system requires more work, depending on the scale of the program. Java, on the other hand, is a programming language that focuses on being able to function in any operating system. Java achieves this by compiling into Java bytecode which is then run on a Java virtual machine that is installed on top of the operating system. There is a significant difference in speed between programs written in C++ and Java. Since C++ programs are written in native code, it can take advantage of optimizations that are unique to the operating system. Java cannot do this because it may break compatibility with other operating systems. The virtual machine also takes some time to translate the Java bytecode into something that can be run on the OS, further increasing the time needed to execute each command. Because of this, each of these programs are used to achieve completely different goals. Programmers who want to create large and heavy programs often opt to use C++ and create a program that can be run on a single operating system. An example of this is a game that uses heavy 3d graphics or image and video editing softwares. People who use Java do not really intend to make big programs but wants to maintain functionality across multiple platforms. The most prominent use of Java is in mobile phones where the operating system is much more varied compared to computers. Java sets standards that a mobile phone maker follows to allow the Java programs to work on their phones. Java is also very widely used in the internet. The Java runtime environment allows companies to publish applications on their websites that people can use. It executes on the server and can access resources on the server like databases. C++ and Java are syntactically very similar. Java’s designers intended this to make it easy for C++ developers to learn Java. Apart from this area of similarity, Java and C++ differ in a variety of ways, largely because of their different design goals. Security, portability, and rapid development were of paramount importance in the design of Java, while C++ is more concerned with performance and backward compatibility with C. Java is compiled to virtual machine byte-code and requires a Java Virtual Machine to run; C++ is compiled to native machine code. This usually makes C++ faster, but it gives Java greater potential for portability and security. C++ is a superset of C and maintains features such as programmer-controlled memory management, pointers, and a preprocessor for full backward compatibility with C. In contrast, Java eliminates these and other bug-prone features. Java replaces programmer memory deallocations with garbage collection. Java further dispenses with C++ features such as operator overloading, templates, and multiple inheritance. These choices make Java a better choice for rapid development and for projects where portability and security are more important than performance. In Java, all objects are passed by reference, whereas in C++, the default behavior is to pass objects by value. Java does not perform automatic type casting as C++ does. In Java, all methods are virtual, meaning the implementation for a method is selected according to the type of the object as opposed to the type of the reference. In C++, methods must be explicitly declared virtual. Java has defined sizes for primitive data types while type sizes are implementation dependent in C++. A limited form of multiple inheritance can be simulated in Java using interfaces. In situations where there is legacy C code and a great need for performance, C++ has certain benefits. In situations where portability, security, and speed of development are emphasized, Java may be a better choice. In summary: • C++ is a very capable and popular programming language while Java is a more recent programming language that maximizes the code’s portability • Programs written in C++ are much faster compared to those written in Java • C++ is commonly used for traditional computer programs while Java is primarily used for making online and mobile phone applications • C++ supports macros, Java does not support macros. • Java is platform independent, C++ is not. • Java has threads as part of the language. For C++, you need to use pthread, Qt, or other thread libraries. • Java has packages, C++ does not. C++ implements it by using namespaces (using namespace std;). • Java does not require that methods be declared prior to their invocation. C++ requires. • In Java all member functions are polymorphic, while only those functions that are declared virtual behave polymorphicaly in C++. • Java does not support dimensional arrays having more than two dimensions. • Java has no unsigned qualifier; integral types are signed and the (16-bit) char type is not. • C++ has pointers, Java does not support pointers. Pointers are inherently tricky to use and troublesome. • Java programming language eliminates multiple inheritance and operator overloading while C++ supports both. Java does not support multiple inheritances because it causes more problems than it solves. Instead Java supports multiple interface inheritance, which allows an object to inherit many method signatures from different interfaces with the condition that the inheriting object must implement those inherited methods. The multiple interface inheritance also allows an object to behave polymorphically on those methods. • Java does not support destructors but adds a finalize() method. Finalize methods are invoked by the garbage collector prior to reclaiming the memory occupied by the object, which has the finalize() method. This means you do not know when the objects are going to be finalized. Avoid using finalize() method to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these resources through the finalize() method. • Java does not include structures or unions because the traditional data structures are implemented as an object oriented framework. • All the code in Java program is encapsulated within classes therefore Java does not have global variables or functions. • C++ requires explicit memory management, while Java includes automatic garbage collection. 1.2. How does function declaration differ in C++ versus Java? Because C++ compilers do not possess look-ahead capability, it is necessary to declare a function prior to its invocation. A C++ function may be declared either via its implementation code, or by just declaring its prototype. A function prototype may contain (parameter) names for better program readability. However, the compiler simply ignores such names. Java does not require that methods be declared prior to their invocation. 1.3. Describe memory organization of an executable program. Executable programs have the following logical organization: • code: The code area contains the compiled code for the program. • static area: The static area stores static variables, that is, those that retain their values for the entire life of the program. • stack: The stack area stores automatic variables, for example, the ordinary variables. • heap: The heap stores dynamically allocated memory (using new or malloc). Static Storage: Global objects, static data members of a class, namespace variables, and static variables in functions reside in static memory. Static objects are allocated by the linker and their address remains the same throughout the program’s execution. Every static object is constructed only once during the lifetime of the program. By default, static data are initialized to binary zeroes. Static objects are subsequently initialized by their constructors. 1.4. What is the difference between the preprocessor directive #include “file.h” and #include This preprocessor directive tells the C++ compiler to include the definitions from a header file. The difference has to do with where the compiler goes to look for the requested file. When angle brackets are used, the compiler looks in a series of standard include directories. In the quoted case, the compiler first looks in the local directory and then checks the standard include directories only if the file is not found in the local directory. Angle brackets are generally used for standard files like stdio.h, whereas quotes are used for modules that the programmer writes. 1.5. Explain the different storage classes in C. Storage classes determine how long a variable is kept in memory, specify the scope of the variable, and can even hint at possible compiler optimizations. There are four storage classes in C: auto, register, static, and extern. The most common storage class is auto. The auto keyword can be used only inside a function. It tells the compiler that the variable is needed only while the function is executing and that its value need not be retained between calls to the function. Memory for auto variables is usually allocated on the stack. All local variables are auto by default, so although auto variables are used very frequently, most programmers omit the auto keyword. The register keyword does the same thing as auto, except it also hints to the compiler that the variable will be used frequently, and therefore it may be a good optimization to keep the variable in a register to reduce loads and stores. Most modern compilers ignore the register keyword because compiler tests show that most application programmers are not very good at determining optimal register assignments. static has two different meanings, depending on the context. At the external level, outside any functions, it specifies that the scope of the variable is limited to the file it is defined in. It cannot be referenced from another file. Inside a function definition, it means the variable should be allocated at a fixed location in memory (instead of on the stack) so it retains its value between function calls. The extern storage class is used when you need to reference a variable before it is defined or when you want to access a variable defined in a different file. Thus, the extern keyword allows you to declare what the variables are, but it does not create variables or allocate memory for them. 1.6. What is a heap? What are the two main applications of heaps? A heap is a complete binary tree whose elements have keys that satisfy the following heap property: the keys along any path from root to leaf are non-increasing. Heaps could represent family descendant trees because the heap property means that every parent is older than his/her children. Heaps are used to implement priority queues and the Heap Sort algorithm. 1.7. Why are heaps used to implement priority queues? Heaps are used to implement priority queues because they allow O(lg n) insertions and removals. This is because both the push() and the pop() functions are implemented by traversing a root-to-leaf path through the heap. Such paths are no longer than the height of the tree which is at most lg n (n being the total number of nodes). O is order of. 1.8. What is the difference between the Heap and the Stack? Where in memory are these located relative to the executing program? Heap is used to store dynamically allocated memory (new or malloc). Stack stores automatic variables, for example, the ordinary variables (int, const). The Stack and Heap are stored “below” the executing program. The Heap “grows” toward the program executable while the Stack grows away from it. 1.9. How does function declaration differ from function definition? A declaration introduces a name - an identifier - to the compiler. It tells the compiler “This function or this variable exists somewhere, and here is what it should look like.” A definition, on the other hand, says: “Make this variable here” or “Make this function here.” It allocates storage for the name. This meaning works whether you are talking about a variable or a function; in either case, at the point of definition the compiler allocates storage. For a variable, the compiler determines how big that variable is and causes space to be generated in memory to hold the data for that variable. For a function, the compiler generates code, which ends up occupying storage in memory. 1.10. Is there anything you can do in C++ that you cannot do in C? No. There is nothing you can do in C++ that you cannot do in C. After all you can write a C++ compiler in C.
|
||||