C



21. What is Dangling pointer?  

Dangling Pointer is a pointer that doesn't point to a valid memory location. Dangling pointers arise when an object is deleted or deallocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the deallocated memory. Following are examples.
// EXAMPLE 1
int *ptr = (int *)malloc(sizeof(int));
.............
.............
free(ptr); 
 
// ptr is a dangling pointer now and operations like following are invalid
*ptr = 10;  // or printf("%d", *ptr);
Run on IDE
// EXAMPLE 2
int *ptr = NULL
{
   int x  = 10;
   ptr = &x;
}
// x goes out of scope and memory allocated to x is free now.
// So ptr is a dangling pointer now.


22. What is the difference between C and Java?  

    • C is structure/procedure oriented programming language whereas Java is object oriented programming language.
    • C language program design is top down approach whereas Java is using bottom up approach.
    • C language is middle level language whereas Java is high level language.
    • Exception handling is not present in C programming language. Whereas exception handling is present in Java.
    • Polymorphism, virtual function, inheritance, Operator overloading, namespace concepts are not available in C programming language. Whereas Java supports all these concepts and features.


23. C Language has been developed in which Language?  

C language has been developed using Assembly Level Language.



24. What is the difference between the = symbol and == symbol?  

The = symbol is often used in mathematical operations. It is used to assign a value to a given variable. On the other hand, the == symbol, also known as "equal to" or "equivalent to", is a relational operator that is used to compare two values.




25. Distinguish between malloc() & calloc() memory allocation?  

Both allocates memory from heap area/dynamic memory. By default calloc fills the allocated memory with 0's.



26. What is memory leak? Why it should be avoided?  

Memory leak occurs when programmers create a memory in heap and forget to delete it. Memory leaks are particularly serious issues for programs like daemons and servers which by definition never terminate.
/* Function with memory leak */
#include <stdlib.h>
 
void f()
{
   int *ptr = (int *) malloc(sizeof(int));
 
   /* Do some work */
 
   return; /* Return without freeing ptr*/
}
    


27. What are local static variables? What is their use?  

A local static variable is a variable whose lifetime doesn't end with a function call where it is declared. It extends for the lifetime of complete program. All calls to the function share the same copy of local static variables. Static variables can be used to count the number of times a function is called. Also, static variables get the default value as 0. For example, the following program prints "0 1".
#include <stdio.h>
void fun()
{
    // static variables get the default value as 0.
    static int x;
    printf("%d ", x);
    x = x + 1;
}
 
int main()
{
    fun();
    fun();
    return 0;
}
// Output: 0 1
   


28. What are static functions? What is their use?  

In C, functions are global by default. The "static" keyword before a function name makes it static. Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.



29. What is the modulus operator?  

The modulus operator outputs the remainder of a division. It makes use of the percentage (%) symbol. For example: 10 % 3 = 1, meaning when you divide 10 by 3, the remainder is 1.



30. What is a nested loop?  

A nested loop is a loop that runs within another loop. Put it in another sense, you have an inner loop that is inside an outer loop. In this scenario, the inner loop is performed a number of times as specified by the outer loop. For each turn on the outer loop, the inner loop is first performed.



31. Which of the following operators is incorrect and why?( >=, <=, < >, ==)  

< > is incorrect. While this operator is correctly interpreted as "not equal to" in writing conditional statements, it is not the proper operator to be used in C programming. Instead, the operator != must be used to indicate "not equal to" condition.



32. Compare and contrast compilers from interpreters?  

Compilers and interpreters often deal with how program codes are executed. Interpreters execute program codes one line at a time, while compilers take the program as a whole and convert it into object code, before executing it. The key difference here is that in the case of interpreters, a program may encounter syntax errors in the middle of execution, and will stop from there. On the other hand, compilers check the syntax of the entire program and will only proceed to execution when no syntax errors are found.



33. Can the curly brackets { } be used to enclose a single line of code?  

While curly brackets are mainly used to group several lines of codes, it will still work without error if you used it for a single line. Some programmers prefer this method as a way of organizing codes to make it look clearer, especially in conditional statements.



34. How do you declare a variable that will hold string values?  

The char keyword can only hold 1 character value at a time. By creating an array of characters, you can store string values in it. Example: "char MyName[50]; " declares a string variable named MyName that can hold a maximum of 50 characters.



35. What are header files and what are its uses in C programming?  

Header files are also known as library files. They contain two essential things: the definitions and prototypes of functions being used in a program. Simply put, commands that you use in C programming are actually functions that are defined from within each header files. Each header file contains a set of functions. For example: stdio.h is a header file that contains definition and prototypes of commands like printf and scanf.



36. What is syntax error?  

Syntax errors are associated with mistakes in the use of a programming language. It maybe a command that was misspelled or a command that must was entered in lowercase mode but was instead entered with an upper case character. A misplaced symbol, or lack of symbol, somewhere within a line of code can also lead to syntax error.



37. What are variables and it what way is it different from constants?  

Variables and constants may at first look similar in a sense that both are identifiers made up of one character or more characters (letters, numbers and a few allowable symbols). Both will also hold a particular value. Values held by a variable can be altered throughout the program, and can be used in most operations and computations. Constants are given values at one time only, placed at the beginning of a program. This value is not altered in the program. For example, you can assigned a constant named PI and give it a value 3.1415 . You can then use it as PI in the program, instead of having to write 3.1415 each time you need it.



38. How do you access the values within an array?  

Arrays contain a number of elements, depending on the size you gave it during variable declaration. Each element is assigned a number from 0 to number of elements-1. To assign or retrieve the value of a particular element, refer to the element number. For example: if you have a declaration that says "intscores[5];", then you have 5 accessible elements, namely: scores[0], scores[1], scores[2], scores[3] and scores[4].



39. Can I use "int" data type to store the value 32768?Why?  

No. "int" data type is capable of storing values from -32768 to 32767. To store 32768, you can use "long int" instead. You can also use "unsigned int", assuming you don't intend to store negative values.



40. What is keyword auto for?  

By default every local variable of the function is automatic (auto). In the below function both the variables 'i' and 'j' are automatic variables.
void f() {
   int i;
   auto int j;
}
NOTE - A global variable can't be an automatic variable.


.Net Interview Question

PHP Interview Question

Java Interview Question

AngularJS Interview Questions