0% found this document useful (0 votes)
19 views

C Language Mock Interview Questions

c interview

Uploaded by

Gaurav Kolambe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

C Language Mock Interview Questions

c interview

Uploaded by

Gaurav Kolambe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Interview Question on C Language

1] What is a pointer on pointer?

It’s a pointer variable which can hold the address of another pointer variable. It de-refers twice to
point to the data held by the designated pointer variable.

Eg: int x = 5, *p=&x, **q=&p;


Therefore ‘x’ can be accessed by **q.

2] What are the valid places for the keyword break to appear.
Break can appear only within the looping control and switch statement. The purpose of the break
is to bring the control out from the said blocks.

3] What is a NULL pointer?


NULL is used to indicate that the pointer doesn’t point to a valid location. Ideally, we should
initialize pointers as NULL if we don’t know their value at the time of declaration. Also, we should
make a pointer NULL when memory pointed by it is deallocated in the middle of a program.

4] What is the difference between variable declaration and variable definition?

Declaration associates type to the variable whereas definition gives the value to the variable

5] When to user -> (arrow) operator.


If the structure/union variable is a pointer variable, to access structure/union elements the arrow
operator is used.

6] What is typecasting?
Typecasting is a way to convert a variable/constant from one type to another type.

7] What is the default function call method?


By default the functions are called by value

Fortune Cloud Technologies Group, Pune 1


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
8] Can the main() function left empty?
Yes, possibly the program doing nothing

9] What are different storage class specifiers in C?


-> auto, register, static, extern

10] When should we use pointers in a C program?


1. To get address of a variable
2. For achieving pass by reference in C: Pointers allow different functions to share and modify
their local variables.
3. To pass large structures so that complete copy of the structure can be avoided.
4. To implement “linked” data structures like linked lists and binary trees.

11] 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.

12] When was C language developed?


C language was developed in 1972 at bell laboratories of AT&T.

13] What is the use of printf() and scanf() functions?


The printf() function is used to print the integer, character, float and string values on to the screen.
The scanf() function is used to take input from the user.

14] What is the use of the function in C?

C functions are used to avoid the rewriting the same code again and again in our program.
C functions can be called any number of times from any place of our program.
When a program is divided into functions, then any part of our program can easily be tracked.
C functions provide the reusability concept.

Fortune Cloud Technologies Group, Pune 2


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
15] What is recursion in C?
When a function calls itself, and this process is known as recursion. The function that calls itself is
known as a recursive function.

16] What is an array in C?


An Array is a group of similar types of elements.
It has a contiguous memory location.
It makes the code optimized, easy to traverse and easy to sort.
The size and type of arrays cannot be changed after its declaration.

17] What is dangling pointer in C?

If a pointer is pointing any memory location, but meanwhile another pointer deletes the memory
occupied by the first pointer while the first pointer still points to that memory location, the first
pointer will be known as a dangling pointer. This problem is known as a dangling pointer problem.
Dangling pointer arises when an object is deleted without modifying the value of the pointer. The
pointer points to the deallocated memory.

18] What is static memory allocation?

In case of static memory allocation, memory is allocated at compile time, and memory can't be
increased while executing the program. It is used in the array.
The lifetime of a variable in static memory is the lifetime of a program.

19] What is dynamic memory allocation?

In case of dynamic memory allocation, memory is allocated at runtime and memory can be
increased while executing the program. It is used in the linked list.
The malloc() or calloc() function is required to allocate the memory at the runtime.

20] What functions are used for dynamic memory allocation in C language?
malloc() , calloc() , realloc() , free()

21] What is the structure?


The structure is a user-defined data type that allows storing multiple types of data in a single unit.
It occupies the sum of the memory of all members.The structure members can be accessed only
through structure variables.Structure variables accessing the same structure but the memory
allocated for each variable will be different.

Fortune Cloud Technologies Group, Pune 3


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
22] What is a union?

The union is a user-defined data type that allows storing multiple types of data in a single unit.
However, it doesn't occupy the sum of the memory of all members. It holds the memory of the
largest member only.
In union, we can access only one variable at a time as it allocates one common space for all the
members of a union.

23] What is an auto keyword in C?

n C, every local variable of a function is known as an automatic (auto) variable. Variables which
are declared inside the function block are known as a local variable. The local variables are also
known as an auto variable. It is optional to use an auto keyword before the data type of a variable.
If no value is stored in the local variable, then it consists of a garbage value.

24] Can we compile a program without main() function?


Yes, we can compile, but it can't be executed.
But, if we use #define, we can compile and run a C program without using the main() function. For
example:

#include<stdio.h>
#define start main
void start() {
printf("Hello");
}

25] What is the maximum length of an identifier?


It is 32 characters ideally but implementation specific.

26] Can we access the array using a pointer in C language?


Yes, by holding the base address of array into a pointer, we can access the array using a pointer

27] Please explain a header file in C?


Every header file contains a set of predefined functions, meant to make programming in C simpler.
You need to include the specific header file in your C program to be able to use the functions
defined in it. For example, you can’t use print(), and scanf() functions without including the stdio.h
header file.

28] What will happen if we include a header file twice in a C program?


When a header file is included twice in a C program, the second one gets ignored. In actual, the #,
called the include guard, preceding a header file ensures that it is included only once during the
compilation process.

Fortune Cloud Technologies Group, Pune 4


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
29] What are some of the limitations of C language?

Concept of OOPs , Run Time Checking , Concept of the Namespace ,


Lack of Exception Handling , insufficient Level for Abstraction:

30] Please explain what do you understand by while(0) and while(1)?

while(0) means that the looping conditions will always be false, i.e., the code inside the while loop
will not be executed. On the opposite, while(1) is an infinite loop. It runs continuously until coming
across a break statement mentioned explicitly.

31] What are command-line arguments?

To control a program from outside, we supply the command line argument parameter to the program
when the main method is invoked. The syntax is:
int main(int argc, char *argv[])
where argc, argv are command-line arguments.

32] What is the difference between Call by Value and Call by Reference?

When using Call by Value, you are sending the value of a variable as parameter to a function,
whereas Call by Reference sends the address of the variable. Also, under Call by Value, the value
in the parameter is not affected by whatever operation that takes place, while in the case of Call by
Reference, values can be affected by the process within the function.

33] What is the use of a '\0' character?

It is referred to as a terminating null character, and is used primarily to show the end of a string
value.

34] 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.

35] How do you generate random numbers in C?


Random numbers are generated in C using the rand() command. For example: anyNum = rand()
will generate any integer number beginning from 0, assuming that anyNum is a variable of type
integer

Fortune Cloud Technologies Group, Pune 5


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
36] What is the purpose of the keyword typedef?

It is used to alias the existing type. Also used to simplify the complex declaration of the type.

---------------------------------------------------------------------------------------------------------------

37] What is the difference between actual and formal parameters?

The parameters sent to the function at calling end are called as actual parameters while at the
receiving of the function definition called as formal parameters.

38] What is a preprocessor?


Preprocessor is a directive to the compiler to perform certain things before the actual compilation
process begins.

39] What will be the output of the C program?

#include<stdio.h>
int main()
{
unsigned char i = 0;
for(;i<=0;i++) ;
printf("%d\n",i);
return 0;
}

A. 127
B. 128
C. Program never ends
D. 0

Answer : C

Here, the char is declared as unsigned char. So the i++ in for loop can never yield negative
value and i >= 0 never becomes false so that it loop for infinite numb er of times.

Fortune Cloud Technologies Group, Pune 6


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
40] What will be the output of the C program, if input is 6?

#include<stdio.h>
int main()
{
int i;
for(i = 0; i>9; i+=3)
{
printf("for ");
}
return 0;
}

A. Nothing prints
B. for
C. for for for
D. None of the above

Answer: A
For loop condition fails at the first iteration itself. Thus it prints nothing.

41] What will be the output of the C program by considering 'b' as a User input?

#include<stdio.h>
int main() {
int i = 1, j = 1;
for(--i && j++ ; i<10; i+=2){
printf("loop ");}
return 0;
}

A. Compilation error
B. Program never ends
C. loop loop loop loop loop
D. None of the above
Answer : C
42] What will be the output of the C program?

Fortune Cloud Technologies Group, Pune 7


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
#include<stdio.h>
int main()
{
for(5;2;2)
printf("Hello");
return 0;
}

A. Compilation error
B. Program never ends
C. Hello
D. None of the above

Answer: B
Here in condition place of for loop, we just put a non zero value, thus it becomes infinite
for loop.

43] What will be the output of the C program?

#include<stdio.h>
#include<math.h>
int main(){
float a = 5.375;
char *p;
int i;
p = (char*)&a;
for(i = 0; i<1; i++)
printf("%d ", p[3]);
return 0;}

A. 3 numbers of address p
B. first number of address p
C. Some garbage value
D. Nothing prints

Answer: C
p[3] is similar to some address[3] which returns some garbage value. As it is a character
type the garbage value which is display will be between -128 to 127

Fortune Cloud Technologies Group, Pune 8


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
44] What will be the output of following code

#include
void call(int,int,int);
int main(){
int a=10;
call(a,a++,++a);
return 0;
}
void call(int x,int y,int z){
printf(“%d %d %d”,x,y,z);
}

A. 10 10 12
B. 12 11 11
C. 12 12 12
D. Compiler error

Answer : B
Default parameter passing scheme of c is cdecl I.e argument of fu nction will pass from
right to left direction.

First ++a will pass and a=11.


Then a++ will pass and a=11.
Then a will pass and a=12.

45]

int main(){
int color=2;
switch(color)
{
case 0: printf("Black");
case 1: printf("Blue");
case 2: printf("Green");
case 3: printf("Aqua");
default: printf("Other");
}
return 0;}

Output

GreenAquaOther

Explanation
There are no break statements, so all the statements after case 2 will be executed
including default statement.
Fortune Cloud Technologies Group, Pune 9
3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
46]

int main(){
char str[10]="Hello";
printf("%d,%d\n",strlen(str),sizeof(str));
return 0;}

Output

5,10

Explanation

strlen gives length of the string that is 5; sizeof gives total number of occupied memory for a
variable that is 8; since str is a pointer so sizeof(str) may be 2,4 or 8. It depends on the computer
architecture.

47]

int main(){
if(0);
printf("Hello");
printf("Hi");
return 0;}

Output

HelloHi

Explanation

There is a semicolon after the if statement, so this statement will be considered as separate
statement; and here printf("Hello"); will not be associated with the if statement.
Both printf statements will be executed.

48]

int main(){
int x,y;
int *ptr;
x=100;
ptr=&x;
y=*ptr;
printf("%d\n",y);
return 0;}

Fortune Cloud Technologies Group, Pune 10


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
Output

100

Explanation
Here, y is assigned the value stored at the pointee variable, which is pointer through ptr.

49]

int main() {
int val=1;

do{
val++;
++val;
}while(val++>25);

printf("%d\n",val);

return 0;}
Output

Explanation

Here, do while loop executes once and then it will check condition while will be false meanwhile
value will be increased 3 times (two times in do while body and once while checking the condition);
hence value will be 4.

50]

int main() {
printf("%d,%d,%d\n",sizeof(char*),
sizeof(int*),sizeof(float*));;
return 0;}

Output

8,8,8

Explanation

No matter which kind of pointers are. Pointer variables take same bytes in the memory. The value
may be 2, 4 or 8 depending on the computer architecture.

In our case we are using 64 bits computer architecture, so output is 8, 8, 8.

---------------------------------------------------------------------------------------------------------------
Fortune Cloud Technologies Group, Pune 11
3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
Fortune Cloud Technologies Group, Pune 12
3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
C Language Machine Test Programs

1] Write a C program to print hello world without using a semicolon (;).

2] Write a program to swap two numbers without using the third variable.

3] How will you print numbers from 1 to 100 without using a loop?

4] Write a program to show command line argument.

5] Write a program to check Armstrong number in C?

6] Write a program to reverse a given number in C?

7] Write a code to generate random numbers in C Language.

8] Write a code to print the following pattern.

*
**
***
****
*****

9] Write a program to print following pattern.

12345
1234
123
12
1

10] Write a program to print following pattern

A
BB
CCC
DDDD
EEEEE
Fortune Cloud Technologies Group, Pune 13
3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
11] Example of Call By Value.

12] Example of Call By Reference.

13] Write a program in C to find the square of any number using the function

14] Write a program in C to convert decimal number to binary number using the function.

15] Example for Function without argument and return value

16] Example for Function without argument and with return value(calculate the area of the
square)

17] Example for Function with argument and without return value(calculate the average of
five numbers.)

18] Example for Function with argument and with return value(check whether a number is
even or odd)

19] Write a program to print the factorial of a given number using recursion?

20] Write a program to print Fibonacci series using recursion?

Fortune Cloud Technologies Group, Pune 14


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
21] Write a program in C to store elements in an array and print it.

22] Write a program to calculate average of 5 subject using array.

23] Write a program in C to read n number of values in an array and display it in reverse
order.

24] Write a program in C to find the sum of all elements of the array.

25] Write a program in C to copy the elements of one array into another array.

26] Write a program in C to count a total number of duplicate elements in an array.

27] Write a program in C to print all unique elements in an array.

28] Write a program to find the largest element of an array

29] write a program to concatenate arrays

30] Write a c Program to Delete the Specified Integer from an Array

Fortune Cloud Technologies Group, Pune 15


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
31] Write a c program to print a address of a variable.

32] Write a program in C to find the maximum number between two numbers using a
pointer.

33] Write a program in C to find the largest element using Dynamic Memory Allocation.

34] Write a program in C to Calculate the length of the string using a pointer.

35] Write a program in C to count the number of vowels and consonants in a string using a
pointer.

36] Write a program in C to show the usage of pointer to structure.

37] Write a program in C to show a pointer to union.

38] Write a program show example of Null Pointer.

39] Write a program to add two matrices using array

40] Write a program to find transpose of a matrix

41] Write a program to find the frequency of a character in a string

Fortune Cloud Technologies Group, Pune 16


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com
42] Write a program to Find the number of vowels, consonants, digits and white spaces

43] Write a program to reverse a string using recursion.

44] Write a C Program to find the length of a string (without using strlen())

45] Write a C Program to Concatenate Two Strings (without using strcat())

46] Write a C Program to Copy String (Without Using strcpy())

47] Write a C Program to Remove all Characters in a String Except Alphabets

48] Write a program to show fgets() and puts() functions.

49] Write a program to pass string to function

50] Write a program in C to find maximum occurring character in a string

Study sincerely to become Employable

“There is no shortcut for Success”

All the Best..!

Fortune Cloud Technologies Group, Pune 17


3rd Floor, Abhinav Apartment, Besides Congress Bhavan, Shivajinagar Pune
Cell - +91 – 9766439090 | www.fortuncloudindia.com

You might also like