C Language Mock Interview Questions
C Language Mock Interview Questions
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.
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.
Declaration associates type to the variable whereas definition gives the value to the variable
6] What is typecasting?
Typecasting is a way to convert a variable/constant from one type to another type.
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.
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.
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.
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()
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.
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.
#include<stdio.h>
#define start main
void start() {
printf("Hello");
}
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.
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.
It is referred to as a terminating null character, and is used primarily to show the end of a string
value.
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.
It is used to alias the existing type. Also used to simplify the complex declaration of the type.
---------------------------------------------------------------------------------------------------------------
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.
#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.
#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?
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.
#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
#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.
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;}
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.
---------------------------------------------------------------------------------------------------------------
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
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?
*
**
***
****
*****
12345
1234
123
12
1
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.
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.
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?
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.
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.
44] Write a C Program to find the length of a string (without using strlen())