C Intermediate Interview Questions Part 3

0

 



39. How can you remove duplicates in an array?

The following program will help you to remove duplicates from an array.

#include <stdio.h>

int main()

{

int n, a[100], b[100], calc = 0, i, j,count;

printf("Enter no. of elements in array.n");

scanf("%d", &n);

printf("Enter %d integersn", n);

for (i = 0; i < n; i++)

scanf("%d", &a[i]);

for (i = 0; i<n; i++)

{

for (j = 0; j<calc; j++)

{

if(a[i] == b[j])

break;

}

if (j== calc)

{

b[count] = a[i];

calc++;

}

}

printf("Array obtained after removing duplicate elements");

for (i = 0; i<calc; i++)

{

printf("%dn", b[i]);

}

return 0;

}


40. Can we compile a program without a main() function?

Yes, we can compile a program without main() function Using Macro.

E.g.

#include<studio.h>

#define abc main

int abc ()

{

printf("Hello World ");

return 0;

}


41. Write a program to get the higher and lower nibble of a byte

without using shi operator?

#include<stdio.h>

struct full_byte

{

char first : 4;

char second : 4;

};

union A

{

char x;

struct full_byte by;

};

main()

{

char c = 100;

union A a;

a.x = c;

printf("the two nibbles are: %d and %d\n", a.by.first, a.by.second);

}


42. How do you override a defined macro?

To override a defined macro we can use #ifdef and #undef preprocessors as follows:

#ifdef A

#undef A

#endif

#define A 10

If macro A is defined, it will be undefined using undef and then defined again using

define.


43. Write a C program to check if it is a palindrome number or not using a recursive method.

#include <stdio.h>

#include <conio.h>

int reverse(int num);

int isPalindrome(int num);

int main()

{

int num;

printf("Enter a number: ");

scanf("%d", &num);

if(isPalindrome(num) == 1)

{

printf("the given number is a palindrome");

}

else

{

printf("the given number is not a palindrome number");

}

return 0;

}

int isPalindrome(int num)

{

if(num == reverse(num))

{

return 1;

}

return 0;

}

int reverse(int num)

{

int rem;

static int sum=0;

if(num!=0){

rem=num%10;

sum=sum*10+rem;

reverse(num/10);

}

else

return sum;

return sum;

}


44. C program to check the given number format is in binary or not.

#include<stdio.h>

#include<conio.h>

int main() {

int j,num;

printf("Please enter a number :");

scanf("%d",&num);

while(num>0)

{

j=num%10;

if( j!=0 && j!=1 )

{

printf("num is not binary");

break;

}

num=num/10;

if(num==0)

{

printf("num is binary");

}

}

getch();

}


45. C Program to find a sum of digits of a number using recursion.

#include<stdio.h>

#include<conio.h>

int sumOfDigits(int num)

{

static int sum = 0;

int rem;

sum = sum + (num%10);

rem = num/10;

if(rem > 0)

{

sumOfDigits(rem);

}

return sum;

}

int main() {

int j,num;

printf("Please enter a number :");

scanf("%d",&num);

printf("sum of digits of the number = %d ",sumOfDigits(num));

getch();

}


46. Can you tell me how to check whether a linked list is circular?

Single Linked List

Circular Linked List

Circular linked list is a variation of a linked list where the last node is pointing to the
first node's information part. Therefore the last node does not point to null.

Algorithm to find whether the given linked list is circular

A very simple way to determine whether the linked list is circular or not
Traverse the linked list
Check if the node is pointing to the head.
If yes then it is circular.
Let's look at the snippet where we code this algorithm.

47. What is the use of a semicolon (;) at the end of every program statement?

Create a structure for a linked list
Declare
-Variable to store data of the node.
-Pointer variable of struct type to store the address of next node.

function of datatype tool isCircular(firstgode){

-Store the value of first node in temp variable and make it traverse all nodes.
-temp-firstgode
-tempenext node pointed by temp(temp->next)
-run until temp is at null or firstNode
if (temp at null)
not circular and returns false
if (temp points first node)
return true as its circular.
}
function of datatype node newNode(data){
-To insert new nodes and link each one of them to the previous node by storing the addr
-Then make them point to NULL.
}
In int main function
-First insert nodes for circular linked list and check its nature by calling isCircular
-Since it is true through if statement it prints "yes..
-Second insert a normal linked list and check its nature by calling isCircular function
C Interview Questions
It is majorly related to how the compiler reads( or parses) the entire code and breaks
it into a set of instructions(or statements), to which semicolon in C acts as a
boundary between two sets of instructions.

48. How to call a function before main()?

To call a function before the main(), pragma startup directive should be used. E.g.-

#pragma startup fun
void fun()
{
printf("In fun\n");
}
main()
{
printf("In main\n");
}

The output of the above program will be -
In fun
In main
This pragma directive, on the other hand, is compiler-dependent. This is not
supported by gcc. As a result, it will ignore the startup directive and produce no error.
But the output, in that case, will be -

In main

49. Differentiate between the macros and the functions.

The differences between macros and functions can be explained as follows:


Macros Vs Functions
Macros
Functions
 It is preprocessed rather than compiled.  It is compiled not preprocessed.
 It is preprocessed rather than compiled.  Function checks for compilation errors.
 Code length is increased.  Code length remains the same.
 Macros are faster in execution.  Functions are a bit slower in execution.
 Macros are useful when a small piece of code is used multiple times in a program.  Functions are helpful when a large piece of code is repeated a number of times.

50. Differentiate Source Codes from Object Codes

The difference between the Source Code and Object Code is that Source Code is a collection of computer instructions written using a human-readable programming language while Object Code is a sequence of statements in machine language, and is the output aer the compiler or an assembler converts the Source Code. The last point about Object Code is the way the changes are reflected. When the Source Code is modified, each time the Source Code needs to be compiled to reflect the changes in the Object Code. 

Post a Comment

0Comments
Post a Comment (0)