C and C++ Interview Questions.

1. Difference between arrays and pointers?

Both array name and pointer can hold address. But address stored in array name is constant ie.. it cannot be reassigned.Where as a pointer variable can be reassigned with some other address.

Ex: 
int arr[5], *ptr, i;
ptr = &i;
arr++;
ptr++;

Incrementing the array name (arr++) will throw an error. Whereas incrementing ptr works fine.

2. What is the purpose of realloc( )?

EX:
#include <stdlib.h>
void *realloc (void *ptr, size_t size)

The block of memory pointed to by ptr is freed, and a new block is allocated. The new block contains the same contents as the original block up to the length passed in size. A pointer to the new block is returned. It is allowed, however, for the new block and the old block to begin at the same address. That is, the pointer returned by realloc () might be the same as the one passed in ptr.
If ptr is null, realloc() simply allocates size bytes of memory and returns a pointer to It. If size is zero, the memory pointed to by ptr is freed.
If there is no enough free memory in the heap to allocate size bytes, a null pointer is returned, and the original block is left unchanged.

3. Differentiate formal arguments and actual arguments?

A variable that’s used to receive passed values is called a formal argument or parameter. The value passed to the function is called the actual argument.

4. When does the compiler not implicitly generate the address of the first element of an array?

Whenever an array name appears in an expression such as

  • array as an operand of the sizeof operator
  • array as an operand of & operator
  • array as a string literal initializer for a character array

Then the compiler does not implicitly generate the address of the address of the first element of an array.

5. What is the purpose of atexit()?

Ex:

#include <stdlib.h>
int atexit(void (*func_name)(void));
  • The atexit() function causes the function pointed to by func_name to be called upon normal program termination.
  • The atexit() function returns zero if the function is successfully registered as a termination function and nonzero otherwise.
  • About 32 functions can be registered, and they will be called in the reverse order of their registration.

6. Difference between get () and getline()

The difference between get() and getline() is that get() leaves the newline character in the input stream, making it the first character seen by the next input operation, while getline() extracts and discards the newline character from the input stream.

7. Difference between cons t int *ptr and int* const ptr

  • In const int *ptr value cannot be changed
Ex:
int var1 = 23;
int var2 = 24;
const int *ptr = &var1;
*ptr = 300; //Not allowed
*ptr = &var2; //Allowed
  • In int* const ptr address cannot be changed
Ex:
int var1 = 23;
int var2 = 24;
int* const ptr = &var1;
*ptr = 300; //Allowed
*ptr = &var2; //Not allowed

8. What is Template in C++?

Templates are a mechanism that makes it possible to use one function or class to handle many different data types. By using templates, we can design a single class/function that operates on data of different types, instead of having to create a separate class/function for each type. When used with functions they are known as function templates, whereas when used with classes they are called class templates.

Ex:
#include<iostream.h>
template <class Template>
Template min_val(T a, T b)
{
return (x<y)?x:y;
} void main()
{
int i1 = 1, i2 = 2;
cout<<endl<<min_val(a, b);
float f1 = 1.1, f2 = 2.2;
cout<<endl<<min_val(f1, f2);
}

9. What is Pragma?

pragma is an implementation-defined directive that allows various instructions to be given to the compiler. For example, a compiler may have an option that supports program execution tracing. A trace option would then be specified by a #pragma statement.

10. Which operator executes faster in expression unary or binary operator? Why?

The expression n++ requires a single machine instruction such as INR to carry out the increment operation whereas, n+1 requires more instructions to carry out this operation.

11. What is modular programming?

If a program is large, it is subdivided into a number of smaller programs that are called modules or subprograms. If a complex problem is solved using more modules, this approach is known as modular programming.

12. What are the differences between malloc() and calloc()?

There are 2 differences. First, is in the number of arguments. malloc() takes a single
argument(memory required in bytes), while calloc() needs 2 arguments(number of variables to allocate memory, size in bytes of a single variable). Secondly, malloc() does not initialize the memory allocated, while calloc() initializes the allocated memory to ZERO.

13. Can a variable be both const and volatile?

Yes. The const modifier means that this code cannot change the value of the variable, but that does not mean that the value cannot be changed by means outside this code. If a variable is both const and volatile, the two modifiers can appear in either order.

14. When you call a function in a particular file in a program. Where does C++ looks for the function definition?

If the function prototype in that file indicates that function is static, the compiler will look only in that file for the function. Otherwise, the compiler looks in all the program file. If it finds two definitions, the compiler sends you an error message, for you can have only one definition for an external function. If it fails to definition in your files, the function then searches the libraries. This implies that if you define a function having the same name as a library function, the compiler will use your version rather that the library version.

15. What is conversion function in C++?

Constructors only provide for converting another type to the class type. To do the reverse, you have to use a special form of C++ operator function called a conversion function. Conversion functions resemble user-defined type casts, and you can use them the way you would use a type cast.

16. When a copy constructor is called?

  • While initializing one class object to another,
  • While passing a class by value as argument, the constructor initializes the corresponding formal argument in the called function.
  • When you have a function return an object, the copy constructor initializes a temporary object used to convey the object’s values to the calling program.

17. Advantages of a macro over a function?

Macro gets to see the Compilation environment, so it can expand __ __TIME__ __FILE__ #defines. It is expanded by the preprocessor. you can’t do this without macros #define PRINT(EXPR) printf( #EXPR “=%d\n”, EXPR) PRINT( 5+6*7 ) // expands into printf(”5+6*7=%d”, 5+6*7 ); You can define your mini language with macros: #define strequal(A,B) (!strcmp(A,B)) Macros are a necessary evils of life. The purists don’t like them, but without it no real work gets done.

18. What restriction applies to overloading the following operators?

= () [] ->
These operators must be defined using a member function

19. Which operators cannot be overloaded?

The operators that cannot be overloaded are . .* :: ?:

20. Can recursion be applied on inline functions?

Recursion cannot be applied to inline function.

21. What is a friend function? When should we go for friend function?

  • A friend function is a nonmember function that’s granted access to a class’s private members
  • One of the most common reasons for using a friend functions is when you need a function to access two separate classes. In such case, a function should be a member of both classes. That isn’t possible, but we can make the function a friend to both classes, belonging to neither, and able to access both.

22. When should a far pointer be used?

Sometimes you can get away with using a small memory model in most of a given program. There might be just a few things that don’t fit in your small data and code segments. When that happens, use explicit far pointers and function declarations to get at the rest of memory. A far function can be outside the 64KB segment most functions are shoehorned into for a small-code model. (Often, libraries are declared explicitly far, so they’ll work no matter what code model the program uses.) A far pointer can refer to information outside the 64KB data segment. Typically, such pointers are used with farmalloc() and such, to manage a heap separate from where all the rest of the data lives. If you use a small-data, large-code model, you should explicitly make your function pointers far.

23. Why destructors of a base class needs to be virtual?

Destructors for a base class should be virtual functions. That way, if you delete an object identified by a pointer of or reference, the program will first call the object destructor instead of the destructor associated with the pointer or reference type

24. Why is passing an object by reference preferable to passing it by value?

Passing an object by reference instead of by value enables the function to avail itself of virtual functions. Also, passing an object by reference instead of value may use less memory and time, particularly for large objects. The main advantage of passing by value is that it protects the original data, but you can accomplish the same end by passing the reference as a const type.

25. Difference between clog and cerr

  • The cerr object corresponds to the standard error stream, which you can use for displaying error messages. By default, this stream is associated with the standard output device, typically a monitor, and the stream is unbuffered. That means information is sent directly to the screen without waiting for a buffer to fill or for a newline character.
  • The clog object also corresponds to the standard error stream. By default, this stream is associated with the standard output device, typically a monitor, and the stream is buffered.

26. What is an lvalue?

An lvalue is an expression to which a value can be assigned. The lvalue expression is located on the left side of an assignment statement, whereas an rvalue is located on the right side of an assignment statement. Each assignment statement must have an lvalue and an rvalue. The lvalue expression must reference a storable variable in memory. It cannot be a constant.

27. What is a void pointer?

A void pointer is a C convention for a raw address. The compiler has no idea what type of object a void Pointer really points to.

                                                    For Ex: int *ptr; //ptr points to an int. void *ptr; //p doesn’t point to any data
                                                    type
                                                

In C and C++, any time you need a void pointer, you can use another pointer type. For example, if you have a char*, you can pass it to a function that expects a void*. You don’t even need to cast it. In C (but not in C++), you can use a void* any time you need any kind of pointer, without casting. (In C++, you need to cast it). A void pointer is used for working with raw memory or for passing a pointer to an unspecified type. Some C code operates on raw memory. When C was first invented, character pointers (char *) were used for that. Then people started
getting confused about when a character pointer was a string, when it was a character array, and when it was raw memory.

28. What is the role of iostream.h file in C++ I/O?

The iostream.h file defines the classes, constants, and manipulators used to manage input and output. These objects manage the streams and buffers used to handle I/O. The file also creates standard objects (cin, cout, cerr, and clog) used to handle the standard input and output streams connected to every program

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

You can’t declare a static variable without defining it as well (this is because the storage class modifiers static and extern are mutually exclusive). A static variable can be defined in a header file, but this would cause each source file that included the header file to have its own private copy of the variable, which is probably not what was intended.

30. What is indirection?

If you declare a variable, its name is a direct reference to its value. If you have a pointer to a variable, or any other object in memory, you have an indirect reference to its value.

Resources