Cognizant Technical Interview Questions


C language



  1. What is a pointer?
The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. The size of the pointer depends on the architecture. However, in 32-bit architecture, the size of a pointer is 2 byte.

Consider the following example to define a pointer which stores the address of an integer.

int n = 10;   
int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer. 

    2. What is a dangling pointer?
Dangling pointers arise when an object is deleted or de-allocated, without modifying the value of the pointer, so that the pointer still points to the memory location of the de-allocated memory.
In short pointer pointing to non-existing memory location is called  dangling pointer.
Example:Using free or de-allocating memory
#include<stdlib.h>
{
    char *ptr = malloc(Constant_Value);
    .......
    .......
    .......
    free (ptr);      /* ptr now becomes a dangling pointer */
}
We have declared the character pointer in the first step. After execution of some statements, we have de-allocated memory which is allocated previously for the pointer.

As soon as memory is de-allocated for pointer, pointer becomes dangling pointer

      3. What is a data type?

In the C programming language, data types are declarations for memory locations or variables that determine the characteristics of the data that may be stored and the methods (operations) of processing that are permitted involving them.
Data types specify how we enter data into our programs and what type of data we enter. C language has some predefined set of data types to handle various kinds of data that we can use in our program. These datatypes have different storage capacities.

C language supports 2 different type of data types:

Primary data types:
These are fundamental data types in C namely integer(int), floating point(float), character(char) and void.

Derived data types:
Derived data types are nothing but primary datatypes but a little twisted or grouped together like array, stucture, union and pointer.
4.     What is size of integer data type?
  • Integer data type allows a variable to store numeric values.
  • “int” keyword is used to refer integer data type.
  • The storage size of int data type is 2 or 4 or 8 byte.
  • It varies depend upon the processor in the CPU that we use.  If we are using 16 bit processor, 2 byte  (16 bit) of memory will be allocated for int data type.
  • Like wise, 4 byte (32 bit) of memory for 32 bit processor and 8 byte (64 bit) of memory for 64 bit processor is allocated for int datatype.
  • int (2 byte) can store values from -32,768 to +32,767
  • int (4 byte) can store values from -2,147,483,648 to +2,147,483,647.
  • If you want to use the integer value that crosses the above limit, you can go for “long int” and “long long int” for which the limits are very high
5.     What is the size of char data type?
  • Character data type allows a variable to store only one character.
  • Storage size of character data type is 1. We can store only one character using character data type.
  • “char” keyword is used to refer character data type.
  • For example, ‘A’ can be stored using char datatype. You can’t store more than one character using char data type.


6.     What is the size of unsigned char?
Type
Storage size
Value range
char
1 byte
-128 to 127 or 0 to 255
unsigned char
1 byte
0 to 255
signed char
1 byte
-128 to 127

  1. What is the size of signed char?
Type
Storage size
Value range
char
1 byte
-128 to 127 or 0 to 255
unsigned char
1 byte
0 to 255
signed char
1 byte
-128 to 127

  1. What is the size of long?
Type
Storage size
Value range
char
1 byte
-128 to 127 or 0 to 255
unsigned char
1 byte
0 to 255
signed char
1 byte
-128 to 127
int
2 or 4 bytes
-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int
2 or 4 bytes
0 to 65,535 or 0 to 4,294,967,295
short
2 bytes
-32,768 to 32,767
unsigned short
2 bytes
0 to 65,535
long
4 bytes
-2,147,483,648 to 2,147,483,647
unsigned long
4 bytes
0 to 4,294,967,295
  1. What is the size of short?
Type
Storage size
Value range
char
1 byte
-128 to 127 or 0 to 255
unsigned char
1 byte
0 to 255
signed char
1 byte
-128 to 127
int
2 or 4 bytes
-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int
2 or 4 bytes
0 to 65,535 or 0 to 4,294,967,295
short
2 bytes
-32,768 to 32,767
unsigned short
2 bytes
0 to 65,535
long
4 bytes
-2,147,483,648 to 2,147,483,647
unsigned long
4 bytes
0 to 4,294,967,295
  1. What is the range of integer data type in C?
Type
Storage size
Value range
char
1 byte
-128 to 127 or 0 to 255
unsigned char
1 byte
0 to 255
signed char
1 byte
-128 to 127
int
2 or 4 bytes
-32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
unsigned int
2 or 4 bytes
0 to 65,535 or 0 to 4,294,967,295
short
2 bytes
-32,768 to 32,767
unsigned short
2 bytes
0 to 65,535
long
4 bytes
-2,147,483,648 to 2,147,483,647
unsigned long
4 bytes
0 to 4,294,967,295
  1. What is the size of float data type?
Type
Storage size
Value range
Precision
float
4 byte
1.2E-38 to 3.4E+38
6 decimal places
double
8 byte
2.3E-308 to 1.7E+308
15 decimal places
long double
10 byte
3.4E-4932 to 1.1E+4932
19 decimal places

  1. What is malloc?
MALLOC():
Ø  malloc () function is used to allocate space in memory during the execution of the program.
Ø  malloc () does not initialize the memory allocated during execution.  It carries garbage value.
Ø  malloc () function returns null pointer if it couldn’t able to allocate requested amount of memory.
Syntax Examples:
int *ptr;
ptr=(int*)malloc(sizeof (int));           //2 byte
long double*ldptr;
ldptr=(long double*)malloc(sizeof(long double))        // 2 byte
char*cptr;
cptr=(char*)malloc(sizeof(char));  //1 byte
int*arr;
arr=(int*)malloc(sizeof int()*10);    //20 byte
cahr*str;
str=(char*)malloc(sizeof(char)*50);          //50 byte

  1. What is the difference between do while and for loop?
while
do while
Loop is executed only when condition is true.
Loop is executed for first time irrespective of the condition. After executing while loop for first time, then condition is checked.
while ( condition) {
statements; //body of loop
}
do{
.
statements; // body of loop.
.
} while( Condition );
In 'while' loop the controlling condition appears at the start of the loop
In 'do-while' loop the controlling condition appears at the end of the loop.
The iterations do not occur if, the condition at the first iteration, appears false.
The iteration occurs at least once even if the condition is false at the first iteration.

  1. What is a string?
The string can be defined as the one-dimensional array of characters terminated by a null ('\0'). The character array or the string is used to manipulate text such as word or sentences. Each character in the array occupies one byte of memory, and the last character must always be 0. The termination character ('\0') is important in a string since it is the only way to identify where the string ends. When we define a string as char s[10], the character s[10] is implicitly initialized with the null in the memory.
There are two ways to declare a string in c language.
  1. By char array
  2. By string literal
Let's see the example of declaring string by char array in C language.
char ch[10]={'c''l''a''n''g''u''a''g''e''\0'};  
As we know, array index starts from 0, so it will be represented as in the figure given below.
While declaring string, size is not mandatory. So we can write the above code as given below:
char ch[]={'c''l''a''n''g''u''a''g''e''\0'};  
We can also define the string by the string literal in C language. For example:
char ch[]="clanguage";  
In such case, '\0' will be appended at the end of the string by the compiler.
  1. /0 in string?
It is the ascii value of null. It is use to identify whether there is null present in the string or not while performing string oeration.
  1. What is recursion?
Recursion is the process which comes into existence when a function calls a copy of itself to work on a smaller problem. Any function which calls itself is called recursive function, and such function calls are called recursive calls. Recursion involves several numbers of recursive calls. However, it is important to impose a termination condition of recursion. Recursion code is shorter than iterative code however it is difficult to understand.
Recursion cannot be applied to all the problem, but it is more useful for the tasks that can be defined in terms of similar subtasks. For Example, recursion may be applied to sorting, searching, and traversal problems.
Generally, iterative solutions are more efficient than recursion since function call is always overhead. Any problem that can be solved recursively, can also be solved iteratively. However, some problems are best suited to be solved by the recursion, for example, tower of Hanoi, Fibonacci series, factorial finding, etc.
  1. What is printf?
The printf() function is used for output. It prints the given statement to the console.
The syntax of printf() function is given below:
printf("format string",argument_list); 
The format string can be %d (integer), %c (character), %s (string), %f (float) etc.
  1. What is math.h?
C Programming allows us to perform mathematical operations through the functions defined in <math.h> header file. The <math.h> header file contains various methods for performing mathematical operations such as sqrt(), pow(), ceil(), floor() etc.
  1. What is the argument of a function?
A function in C can be called either with arguments or without arguments. These function may or may not return values to the calling functions. All C functions can be called either with arguments or without arguments in a C program. Also, they may or may not return any values.
  1. What is assignment operator in C?
These are used to assign the values for the variables in C programs.
·         In C programs, values for the variables are assigned using assignment operators.
·         For example, if the value “10” is to be assigned for the variable “sum”, it can be assigned as “sum = 10;”
·         There are 2 categories of assignment operators in C language. They are,
1. Simple assignment operator ( Example: = )
2. Compound assignment operators ( Example: +=, -=, *=, /=, %=, &=, ^= )

  1. What is the relational operator in C?
Relational operators are used to find the relation between two variables. i.e. to compare the values of two variables in a C program.
Operators
Example/Description
> 
x > y (x is greater than y)
< 
x < y (x is less than y)
>=
x >= y (x is greater than or equal to y)
<=
x <= y (x is less than or equal to y)
==
x == y (x is equal to y)
!=
x != y (x is not equal to y)
  1. What is the logical operator in C?
These operators are used to perform logical operations on the given expressions.
There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).
Operators
Example/Description
&& (logical AND)
(x>5)&&(y<5)
It returns true when both conditions are true
|| (logical OR)
(x>=10)||(y>=10)
It returns true when at-least one of the condition is true
! (logical NOT)
!((x>5)&&(y<5))
It reverses the state of the operand “((x>5) && (y<5))”
If “((x>5) && (y<5))” is true, logical NOT operator makes it false


  1. What is the bitwise operator in C?
These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits.
Bit wise operators in C language are & (bitwise AND), | (bitwise OR), ~ (bitwise NOT), ^ (XOR), << (left shift) and >> (right shift).
TRUTH TABLE FOR BIT WISE OPERATION & BIT WISE OPERATORS:
truth-table
  1. What are all decision control statements in C?
In decision control statements (if-else and nested if), group of statements are executed when condition is true.  If condition is false, then else part statements are executed.
There are 3 types of decision making control statements in C language. They are,
Ø  if statements
Ø    if else statements
Ø    nested if statements
“IF”, “ELSE” AND “NESTED IF” DECISION CONTROL STATEMENTS IN C:
Syntax for each C decision control statements are given in below table with description.
Decision control statements  
Syntax/Description
If Syntax:
if (condition)
{ Statements; }
Description:
In these type of statements, if condition is true, then respective block of code is executed.
if…else Syntax:
if (condition)
{ Statement1; Statement2; }
else
{ Statement3; Statement4; }
Description:
In these type of statements, group of statements are executed when condition is true.  If condition is false, then else part statements are executed.
nested if Syntax:
if (condition1){ Statement1; }
else_if(condition2)
{ Statement2; }
else Statement 3;Description:
If condition 1 is false, then condition 2 is checked and statements are executed if it is true. If condition 2 also gets failure, then else part is executed.
  1. What are all loop control statements in C?
Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out of the loop statements once condition becomes false.
TYPES OF LOOP CONTROL STATEMENTS IN C:
There are 3 types of loop control statements in C language. They are,
Ø  For
Ø  While
Ø  do-while
Syntax for each C loop control statements are given in below table with description.
For Syntax
for (exp1; exp2; expr3)
{ statements; }
Where,exp1 – variable initialization
( Example: i=0, j=2, k=3 )
exp2 – condition checking
( Example: i>5, j<3, k=3 )
exp3 – increment/decrement
( Example: ++i, j–, ++k )
while (condition)
{ statements; }where,
condition might be a>5, i<10
do while      do { statements; }
while (condition);where,
condition might be a>5, i<10
  1. What is the difference between single equal “=” and double equal “==” operators in C?
Assignment Operator (=)
Ø  = is an Assignment Operator in C, C++ and other programming languages, It is Binary Operator which operates on two operands.
Ø  = assigns the value of right side expression’s or variable’s value to the left side variable.
Let's understand by example:
Ø  x=(a+b);
Ø  y=x;
Here, When first expression evaluates value of (a+b) will be assigned into x and in second expression y=x; value of variable x will be assigned into y.
Equal To Operator (==)
Ø  == is an Equal To Operator in C and C++ only, It is Binary Operator which operates on two operands.
Ø  == compares value of left and side expressions, return 1 if they are equal other will it will return 0.

Let's understand by example:
int x,y;
x=10;
y=10;
if(x==y)
    printf("True");
else
    printf("False");
When expression x==y evaluates, it will return 1 (it means condition is TRUE) and "TRUE" will print.
So it's cleared now, ,both are not same, = is an Assignment Operator it is used to assign the value of variable or expression, while == is an Equal to Operator and it is a relation operator used for comparison (to compare value of both left and right side operands).
  1. What is the difference between pre increment operator and post increment operator?
Pre-increment operator: A pre-increment operator is used to increment the value of a variable before using it in a expression. In the Pre-Increment, value is first incremented and then used inside the expression.
Syntax:  a = ++x;
Post-increment operator: A post-increment operator is used to increment the value of variable after executing expression completely in which post increment is used. In the Post-Increment, value is first used in a expression and then incremented.
Syntax: a = x++;

28.                       What is the difference between pre decrement operator and post decrement operator?
Pre-decrement operator: A pre- decrement operator is used to decrement the value of a variable before using it in a expression. In the Pre- decrement, value is first decremented and then used inside the expression.
Syntax:  a = --x;
Post- decrement operator: A post- decrement operator is used to decrement the value of variable after executing expression completely in which post decrement is used. In the Post- decrement, value is first used in a expression and then decremented.
Syntax: a = x--;
  1. What is “&” and “*” operators in C?
The “&” operator is used to get the address or the memory location of a particular variable. But “*” operator is used to get the value stored inside a specified address.
  1. What is the purpose of main() function?
The main function invokes other functions within it and the execution of a program always starts with the main() function.


Comments

Popular posts from this blog

COGNIZANT INTERVIEW QUESTIONS - 2018

MIND TREE