Vous êtes sur la page 1sur 6

C INTERVIEW QUESTIONS

What is the difference between C and C++ ? Would you prefer to use one over the other ? C is based on structured programming whereas C++ supports the object-oriented programming paradigm.Due to the advantages inherent in object-oriented programs such as modularity and reuse, C++ is preferred. However almost anything that can be built using C++ can also be built using C. What are the access privileges in C++ ? What is the default access level ? The access privileges in C++ are private, public and protected. The default access level assigned to members of a class is private. Private members of a class are accessible only within the class and by friends of the class. Protected members are accessible by the class itself and it's sub-classes. Public members of a class can be accessed by anyone. What is data encapsulation ? Data Encapsulation is also known as data hiding. The most important advantage of encapsulation is that it lets the programmer create an object and then provide an interface to the object that other objects can use to call the methods provided by the object. The programmer can change the internal workings of an object but this transparent to other interfacing programs as long as the interface remains unchanged. What is inheritance ? Inheritance is the process of deriving classes from other classes. In such a case, the sub-class has an 'is-a' relationship with the super class. For e.g. vehicle can be a super-class and car can be a sub-class derived from vehicle. In this case a car is a vehicle. The super class 'is not a' sub-class as the sub-class is more specialized and may contain additional members as compared to the super class. The greatest advantage of inheritance is that it promotes generic design and code reuse. What is multiple inheritance ? What are it's advantages and disadvantages ? Multiple Inheritance is the process whereby a sub-class can be derived from more than one super class. The advantage of multiple inheritance is that it allows a class to inherit the functionality of more than one base class thus allowing for modeling of complex relationships. The disadvantage of multiple inheritance is that it can lead to a lot of confusion when two base classes implement a method with the same name. What is polymorphism? Polymorphism refers to the ability to have more than one method with the same signature in an inheritance hierarchy. The correct method is invoked at run-time based on the context (object) on which the method is invoked. Polymorphism allows for a generic use of method names while providing specialized implementations for them. What do the keyword static and const signify ? When a class member is declared to be of a static type, it means that the member is not an instance variable but a class variable. Such a member is accessed using Class name. Member name (as opposed to Object. Member name). Const is a keyword used in C++ to specify that an object's value cannot be changed.

How is memory allocated/deallocated in C ? How about C++ ? Memory is allocated in C using malloc() and freed using free(). In C++ the new() operator is used to allocate memory to an object and the delete() operator is used to free the memory taken up by an object. What is UML ? UML refers to Unified Modeling Language. It is a language used to model OO problem spaces and solutions. What is the difference between a shallow copy and a deep copy? A shallow copy simply creates a new object and inserts in it references to the members of the original object. A deep copy constructs a new object and then creates in it copies of each of the members of the original object. In terms of syntax, what is a simple difference between struct declarations between C and C++ ? In C it is always necessary to include 'struct' keyword while declaring, but you could skip the 'struct' keyword in C++. Example, In C++ : struct_name struct_instance; In C : struct struct_name struct_instance; What is IDE (Integrated Development Environment) for C and how it benefits the programmers ? IDE is nothing but a text editor and compiler combination from where you can write, compile and execute your C programs from the IDE. Though it is not mandatory to use IDE for C programming, it can be very handy as it saves programmers time and hence his productivity. Tell any one disadvantage of the availability of several compilers for C ? Every C compiler is slightly different from the other. There could be some special functions specific to any particular compiler. If a program uses such functions then it might not compile in other compilers and hence could cause problems. How pointers help in improving memory efficiency during data passage between main and called functions ? When using pointers, especially for passing large pieces of data linked to each other by some logic (eg. arrays), one can just pass a address reference using pointers which is good enough for processing in most cases. What are flow control statements and why they are used ? Which is the simplest and most powerful flow control statement available in C ? Flow control statements are the ones that enable conditional execution of a program. For example, the logic would demand the program to do something if a condition is met and do something else if the condition is not met. "IF" statement can be thought of the simplest and most powerful flow control statement available in C. In a professional environment if you know that a program can be written using several levels of nested if statements, what would be your logical step ? Generally in professional environments, the readability of the code would be given very high priority. Generally nested if statements are not easily readable. Hence, a logical step would be to looking to break the nested if into simpler IF blocks by reducing levels as far as possible.

What is called a 'dead code' in terms of flow control statements ? Can you give an example in C ? Consider the below example in C if(1 > 2) { printf("will not be executed"); } else { printf("active code"); } In the above C code, the first printf statement will never get executed as 1 is obviously lesser than 2 at any given point in time. These kinds of statements which will never get executed regardless of time of execution are considered to be dead. Can C programs be written without loop statements at all. Can you tell with an example the difficulty presented by not using loop statements at all ? C programs can very well be written without any loop statements like FOR loop, WHILE loop, DO WHILE loop etc. But they adversely affect the lines of code (LOC) and programmers efforts. For example, if you were to print first 500 even natural numbers, you would require at least 500 LOC to complete. On the other hand the solution would be very straightforward and simple when using a loop statement. How C Functions prevent rework and hence save programmers time? When a C program employs functions rather than sequential lines of code, any change in the function's logic would be easy to make as it has to be done at only one place, namely the function definition. Hence the rework effort in case of program logic changes or program debugging becomes much easier. Tell a primary advantage of ANSI C standards while programming in C. Confirming to ANSI C standards helps in achieving portability across compilers. That is, the chance of a source C program compiling successfully in different compilers increases. Can you tell any three compilers supporting ANSI C standards? GCC, LCC and Visual C++ do support ANSI C standards. Which are 2 important data you share with compiler while declaring a variable? While declaring a variable, by declaring, the compiler will get to know the data type and name of the variable. If C program does not allow using variables, tell any one drawback you can think of? There would be innumerous drawbacks if variables arent allowed in C. For example, you will not even able to receive user input in the C programs as you will not have a place to store the data for processing. Thus the very basic dynamic nature of the programs will get affected in first place. Should C functions mandatorily return value to the caller? No, it is not mandatory for C functions to return value.

What information does function prototype carry? Can you give an example? Function prototype tells compiler about the number and type of arguments required by the function, the type of the return value (void if there is no return value) and the name of the function. e.g int myfunction (int a, char c); Are parameter names mandatory in function prototype? Can you give an example ? No, parameter names are not mandatory. Just a mention of the type of parameters would do. For example, the below prototype is perfectly valid. int myfunction(int,char); Can you explain the difference between data types and modifiers? Data types are the ones including int, float, char, double that determine the type and storage of the data within the variables. Whereas modifiers are used in conjunction with data types to precisely control the amount of storage to a variable of a particular data type. short, long etc are modifiers. Is it possible to precisely tell the size of short int in any machine? This depends upon the implementation. Guideline from ANSI C on the size of short int is that the size of short int should be lesser than or equal to than that of int. Does C have automatic garbage collection? What this can lead to? C does not have automatic garbage collection. In worst cases, this can lead to a memory leak. Memory leak occurs when dynamically allocated memory is not available for reuse. Tell a common mistake that programmers often do when using a new variable as counter for FOR loops? What could be a simple preventive measure? When using a new variable, some programmers tend to forget the initialization part. There could be several preventive measures. One measure could be that programmer can consider declaring two or three extra variables which can be used for future FOR loops as and when required. You would be knowing that Boolean data type stores either true or false value. Can you tell the minimum size required to store a variable of Boolean type? At the least, a Boolean storage should be capable of storing a '0' representing false and '1' representing true. Hence, just 1 bit of storage is sufficient. As you would be knowing, data types like char and int have signed and unsigned variants. When the programmer doesn't qualify a variable with 'signed' or 'unsigned' keyword, Which of the variants is assigned default as per C standards ? Answer : C standards doesn't mention about the default variant between signed and unsigned. Hence, C compiler designers are free to choose between the two. However, in most cases, one can find that unsigned is default.

In two different C++ implementations, can 'signed int' and 'signed long int' have same storage size? Answer : As per C++ standards, 'signed int' requires a minimum of 16 bits for storage with 'signed long int' requires a minimum of 32 bits. Having said that, on different machines, the storage sizes can be same. This is because, the standard doesn't mention a maximum storage value. For example, in one implementation, signed int can be given 32 bits of storage which is double the minimum specified value Assume you have a program which uses two character variables. Now, if you wish to use the power of C string functions on data contained in those two variables what you would do? Answer : One way is to declare the character variables as single length string arrays. By this, you can start using string functions seamlessly. Why comments are necessary and what difficulty a C program will lead to without comment ? What syntax will you use for C comments ? Answer : Comments are necessary for a C program to make it more readable. Especially if you have a C project with hundreds of lines of code, and if there are no comments to explain the code, the maintenance of the application will become very tedious. In C comments will be enclosed with /* and */. Is it possible to pass command line arguments to C programs? If yes, can you write down the prototype for main function with command line arguments? Answer : Yes, it is possible to pass command line arguments to C programs i.e main function is capable of receiving and processing command line arguments. The prototype for the same is int main ( int argc, char *argv[] ) Here, argc denotes the number of command line arguments and *argv[] is the list of arguments. Some argue C as loosely typed (weakly typed) language. Can you tell with an example why it is called so ? Answer : In C char data can be used in place of integers in case of arithmetic operations. These kinds of features should not be permitted in strongly typed languages. Hence, C is called as weakly typed by some. For example, below program is valid in C #include #include main() { clrscr(); int a = 10; char b = 'c'; printf("\n%d", a + b); printf("\n%d", b); getch(); return 0; }

The output will be 109 99

In regards to variable declarations within functions, explain 'auto' keyword. Answer : Variables used in C programs are qualified by 'storage classes'. They define the visibility and life time of the variables. 'Auto' is the default storage class of variables. The variables of storage class 'auto' can be used only within the declared function. Can you explain the usage of 'void' keyword? Answer : C functions can optionally return value and work with one or more than one passed parameters. void is used in the following scenarios. a. A function does not return a value. b. A function does not require parameters. c. A function does not return a value as well as does not require parameters. Can you explain the usage of 'register' keyword? Answer : 'Register' refers to a storage class which recommends that the corresponding variable/variables need to be stored in CPU registers. (CPU registers are fast memory access locations.) With regard to program control flow, explain 'goto' keyword? Answer : 'goto' keyword is used to force program control to move to a labeled location.