Multiple choice questions in c



C programming language multiple choice questions and answers with explanation for interview

1.
What will be output if you will compile and execute the following c code?

void main(){
   int i=320;
   char *ptr=(char *)&i;
   printf("%d",*ptr);
}

(A) 320
(B) 1
(C) 64
(D) Compiler error
(E) None of above


Explanation:
As we're aware, the size of the int data type is two bytes, whereas a char pointer can address one byte at a time. Considering the memory representation of the int variable i with a value of 320...

As illustrated above, the char pointer ptr points solely to the first byte. The content of this byte, denoted by *ptr, is represented as '01000000,' translating to a decimal value of 64.
Data type tutorial. 

2.

What will be output if you will compile and execute the following c code?

#define x 5+2
void main(){
    int i;
    i=x*x*x;
    printf("%d",i);
}

(A) 343
(B) 27
(C) 133
(D) Compiler error
(E) None of above


Explanation:
As we understand, the #define directive in the preprocessor performs token pasting, where it pastes the value of a macro constant into the program prior to the actual compilation process. Upon inspection of the intermediate file, you will observe:
test.c 1:
test.c 2: void main(){
test.c 3: int i;
test.c 4: i=5+2*5+2*5+2;
test.c 5: printf("%d",i);
test.c 6: }
test.c 7:
You can absorb #define only pastes the 5+2 in place of x in program. So,
i=5+2*5+2*5+2
=5+10+10+2
=27
Preprocessor tutorial.

3.

What will be output if you will compile and execute the following c code?

void main(){
char c=125;
    c=c+10;
    printf("%d",c);
}

(A) 135
(B) +INF
(C) -121
(D) -8
(E) Compiler error


Explanation:
As we are aware, the char data type exhibits cyclic properties. When you increment or decrement char variables beyond their maximum or minimum values, respectively, they will repeat the same value according to the following cyclic order:

So,
125+1= 126
125+2= 127
125+3=-128
125+4=-127
125+5=-126
125+6=-125
125+7=-124
125+8=-123
125+9=-122
125+10=-121

4.

What will be output if you will compile and execute the following c code?

void main(){
   float a=5.2;
  if(a==5.2)
     printf("Equal");
  else if(a<5.2)
     printf("Less than");
  else
     printf("Greater than");
}

(A) Equal
(B) Less than
(C) Greater than
(D) Compiler error
(E) None of above


Explanation:

In C, the constant 5.2 is a double constant, and the size of a double data type is 8 bytes, whereas 'a' is a float variable with a size of 4 bytes. Consequently, the memory representation of the double constant 5.2 is as follows:

101.00110011 00110011 00110011 00110011 00110011 00110011 00110011

The content of the variable 'a' will be stored in memory as:

101.00110011001100110011

It's evident that the variable 'a' is smaller than the double constant 5.2. Notably, recurring float numbers like 4.5, 3.25, and 5.0 will have the same storage values in both float and double data types.

Please note: The memory representation of float and double data types is entirely different. If you wish to explore the actual memory representation, refer to questions number (60) and (61).

Data type tutorial.

5.

What will be output if you will compile and execute the following c code?

void main(){
  int i=4,x;
  x=++i + ++i + ++i;
  printf("%d",x);
}

(A) 21
(B) 18
(C) 12
(D) Compiler error
(E) None of above


Explanation:

In the expression ++a, the ++ operator is a pre-increment operator. In mathematical expressions, the pre-increment operator first increments the variable up to a breakpoint before assigning the final value to the variable. The process involves the following steps:

Step 1: Increment the variable 'a' up to the breakpoint.



Step 2: Begin assigning the final value of 7 to all variables 'i' in the expression.


So, i=7+7+7=21
Operator tutorial.

6.

What will be output if you will compile and execute the following c code?

void main(){
 int a=2;
 if(a==2){
   a=~a+2<<1;
   printf("%d",a);
 }
 else{
  break;
 }
}

(A) It will print nothing.
(B) -3
(C) -2
(D) 1
(E) Compiler error


Explanation:
Note that the keyword 'break' is not a part of the if-else statement. Consequently, attempting to use 'break' within an if-else block will result in a compiler error, specifically, 'Misplaced break'.
Control statement tutorial

7.

What will be output if you will compile and execute the following c code?

void main(){
  int a=10;
  printf("%d %d %d",a,a++,++a);
}

(A) 12 11 11
(B) 12 10 10
(C) 11 11 12
(D) 10 10 12
(E) Compiler error


Explanation:
In C, the printf function adheres to the cdecl parameter passing scheme. In this scheme, parameters are passed from right to left direction.

So, in the expression sequence ++a, a++, and a, the pre-increment operator ++a will be evaluated first, resulting in the variable value becoming =10. Subsequently, the post-increment operator a++ will be evaluated, leaving the variable value as =10. Finally, the variable a will be evaluated, and its value will be =12.
Function tutorial.

8.

What will be output if you will compile and execute the following c code?

void main(){
   char *str="Hello world";
   printf("%d",printf("%s",str));
}

(A) 11Hello world
(B) 10Hello world
(C) Hello world10
(D) Hello world11
(E) Compiler error


Explanation:
The printf function in C has an integer return type, and the value of this integer precisely matches the count of characters, including white spaces, that the printf function prints. Therefore, calling printf("Hello world") will return the value 13.

9.

What will be output if you will compile and execute the following c code?

#include "stdio.h"
#include "string.h"
void main(){
   char *str=NULL;
   strcpy(str,"cquestionbank");
   printf("%s",str);
}

(A) cquestionbank
(B) cquestionbank\0
(C) (null)
(D) It will print nothing
(E) Compiler error


Explanation:
It's important to note that attempting to copy any content using the strcpy function to a character pointer pointing to NULL is not allowed.
More questions of string.

10.

What will be output if you will compile and execute the following c code?

#include "stdio.h"
#include "string.h"
void main(){
  int i=0;
  for(;i<=2;)
   printf(" %d",++i);
}

(A) 0 1 2

(B) 0 1 2 3
(C) 1 2 3
(D) Compiler error
(E) Infinite loop


Explanation:
In a for loop, each part, namely the initialization, condition, and iteration expressions, is optional.
Complete tutorial of looping in C.

11.

What will be output if you will compile and execute the following c code?

void main(){
  int x;
  for(x=1;x<=5;x++);
    printf("%d",x);
}

(A) 4
(B) 5
(C) 6
(D) Compiler error
(E) None of above


Explanation:
The body of a for loop is optional. In the context of this question, the for loop will continue to execute until the value of the variable 'x' becomes six, causing the loop condition to evaluate to false.
Looping tutorial.

12.

What will be output if you will compile and execute the following c code?

void main(){
printf("%d",sizeof(5.2));
}

(A) 2
(B) 4
(C) 8
(D) 10
(E) Compiler error


Explanation:
The default type of a floating-point constant in C is double. Consequently, the constant 5.2 is considered a double constant, and its size is 8 bytes.
Detail explanation of all types of constant in C.

13.

What will be output if you will compile and execute the following c code?

#include "stdio.h"
#include "string.h"
void main(){
char c='\08';
printf("%d",c);
}


(A) 8
(B) ’8’
(C) 9
(D) null
(E) Compiler error


Explanation:
In C, any character starting with the character '', represents an octal number in character form. Octal digits are limited to 0, 1, 2, 3, 4, 5, 6, and 7. Since 8 is not a valid octal digit, the expression '\08' is considered an invalid octal character constant.
Hexadecimal character constant.

14.

What will be output if you will compile and execute the following c code?

#define call(x,y) x##y
void main(){
int x=5,y=10,xy=20;
printf("%d",xy+call(x,y));
}

(A) 35
(B) 510
(C) 15
(D) 40
(E) None of above


Explanation:
The '##' is a concatenation operator in the C preprocessor. It merges the operands together, for example: a##b results in ab. Upon inspecting the intermediate file, you will observe that the code has been transformed into the following intermediate code before the commencement of the actual compilation.
Intermediate file:
test.c 1:
test.c 2: void main(){
test.c 3: int x=5,y=10,xy=20;
test.c 4: printf("%d",xy+xy);
test.c 5: }
test.c 6:
It is clear call(x, y) has replaced by xy.
Preprocessor tutorial.

15.

What will be output if you will compile and execute the following c code?


int * call();
void main(){
int *ptr;
ptr=call();
clrscr();
printf("%d",*ptr);
}
int * call(){
int a=25;
a++;
return &a;
}


(A) 25
(B) 26
(C) Any address
(D) Garbage value
(E) Compiler error


Explanation:
In this context, the variable 'a' is a local variable with its scope and visibility limited to the function call. After returning the address of 'a' through the function call, the variable 'a' becomes inactive (dead), while the pointer 'ptr' still points to the address of 'a'. This situation is commonly referred to as the 'dangling pointer problem'.
Complete pointer tutorial.

16.

What is error in following declaration?

struct outer{
int a;
struct inner{
char c;
};
};

(A) Nesting of structure is not allowed in c.
(B)
It is necessary to initialize the member variable.
(C) Inner structure must have name.
(D) Outer structure must have name.
(E) There is not any error.


Explanation:
It is imperative to assign a name to an inner structure at the time of its declaration; otherwise, accessing the members of the inner structure becomes impossible. Hence, the correct declaration is:
struct outer{
int a;
struct inner{
char c;
}name;
};
Union tutorial.

17.

What will be output if you will compile and execute the following c code?

void main(){
int array[]={10,20,30,40};
printf("%d",-2[array]);
}

(A) -60
(B) -30
(C) 60
(D) Garbage value
(E) Compiler error


Explanation:
In c,
array[2]=*(array+2)=*(2+array)=2[array]=30

18.

What will be output if you will compile and execute the following c code?

void main(){
int i=10;
static int x=i;
if(x==i)
printf("Equal");
else if(x>i)
printf("Greater than");
else
printf("Less than");
}

(A) Equal
(B) Greater than
(C) Less than
(D) Compiler error
(E) None of above


Explanation:
Static variables are considered load-time entities, in contrast to auto variables, which are run-time entities. It is not permissible to initialize a load-time variable with a run-time variable. In this example, 'i' is a run-time variable, while 'x' is a load-time variable.
What is storage class?

19.

What will be output if you will compile and execute the following c code?

#define max 5;
void main(){
int i=0;
i=max++;
printf("%d",i++);
}

(A) 5
(B) 6
(C) 7
(D) 0
(E) Compiler error


Explanation:
#define is a token-pasting preprocessor directive. If you examine the intermediate file, such as 'test.i':
test.c 1:
test.c 2: void main(){
test.c 3: int i=0;
test.c 4: i=5++;
test.c 5: printf("%d",i++);
test.c 6: }
test.c 7:
It is evident that the macro constant 'max' has been replaced by 5. Attempting to increment this constant is not allowed and will result in a compiler error indicating 'Lvalue required'.
Preprocessor questions and answer.

20.

What will be output if you will compile and execute the following c code?

void main(){
double far* p,q;
printf("%d",sizeof(p)+sizeof q);
}

(A) 12
(B) 8
(C) 4
(D) 1
(E) Compiler error


Explanation:

It is evident that 'p' is a far pointer, and the size of a far pointer is 4 bytes. On the other hand, 'q' is a double variable, and the size of a double variable is 8 bytes.
Complete pointer tutorial.


Certainly! If you require further clarification or have additional questions regarding the C multiple-choice questions, feel free to ask. I'm here to help!


C programming language multiple choice questions and answers with explanation for interview

1.
What will be output if you will compile and execute the following c code?

void main(){
   int i=320;
   char *ptr=(char *)&i;
   printf("%d",*ptr);
}

(A) 320
(B) 1
(C) 64
(D) Compiler error
(E) None of above


Explanation:

As the size of the int data type is typically two bytes, it's noteworthy that a char pointer can point to one byte at a time. Considering the memory representation of an int variable, let's take the example of i = 320:

In the illustration provided earlier, it's observed that the char pointer ptr is pointing specifically to the first byte. The content of this byte, denoted as *ptr, is represented as '01000000,' equating to a decimal value of 64.

Data type tutorial. 

2.

What will be output if you will compile and execute the following c code?

#define x 5+2
void main(){
    int i;
    i=x*x*x;
    printf("%d",i);
}

(A) 343
(B) 27
(C) 133
(D) Compiler error
(E) None of above


Explanation:

As we understand, the #define directive in the preprocessor operates through token pasting, wherein it seamlessly incorporates the value of the macro constant into the program before the commencement of actual compilation. Upon inspecting the intermediate file, you'll observe:

test.c 1:
test.c 2: void main(){
test.c 3: int i;
test.c 4: i=5+2*5+2*5+2;
test.c 5: printf("%d",i);
test.c 6: }
test.c 7:
You can absorb #define only pastes the 5+2 in place of x in program. So,
i=5+2*5+2*5+2
=5+10+10+2
=27
Preprocessor tutorial.

3.

What will be output if you will compile and execute the following c code?

void main(){
char c=125;
    c=c+10;
    printf("%d",c);
}

(A) 135
(B) +INF
(C) -121
(D) -8
(E) Compiler error


Explanation:

As is well-known, the char data type exhibits cyclic properties. When char variables are increased or decreased beyond their maximum or minimum values, respectively, they repeat the same value in accordance with the cyclic order as follows:


So,
125+1= 126
125+2= 127
125+3=-128
125+4=-127
125+5=-126
125+6=-125
125+7=-124
125+8=-123
125+9=-122
125+10=-121

4.

What will be output if you will compile and execute the following c code?

void main(){
   float a=5.2;
  if(a==5.2)
     printf("Equal");
  else if(a<5.2)
     printf("Less than");
  else
     printf("Greater than");
}

(A) Equal
(B) Less than
(C) Greater than
(D) Compiler error
(E) None of above


Explanation:

In C, the constant 5.2 is considered a double constant, and the size of a double data type is 8 bytes. Conversely, variable 'a' is a float variable, and the size of a float variable is 4 bytes.

So double constant 5.2 is stored in memory as:
101.00 11001100 11001100 11001100 11001100 11001100 11001101
Content of variable a will store in the memory as:
101.00110 01100110 01100110
It's evident that the variable 'a' is less than the double constant 5.2. Notably, as 5.2 is a recurring float number, its representation differs for float and double types. However, numbers like 4.5, 3.25, and 5.0 will store the same values in both float and double data types. Please note that in memory, the storage of float and double data types follows completely different mechanisms. If you wish to explore the actual memory representation, you can refer to questions number (60) and (61).

Data type tutorial.

5.

What will be output if you will compile and execute the following c code?

void main(){
  int i=4,x;
  x=++i + ++i + ++i;
  printf("%d",x);
}

(A) 21
(B) 18
(C) 12
(D) Compiler error
(E) None of above


Explanation:

In the expression ++a, the ++ operator denotes pre-increment. In any mathematical expression, the pre-increment operator initially increments the variable up to a breakpoint before assigning the final value to all variables.

Step 1: Increment the variable 'a' up to the breakpoint.


Step 2: Commence assigning the final value of 7 to all variables 'i' in the expression.


So, i=7+7+7=21
Operator tutorial.

6.

What will be output if you will compile and execute the following c code?

void main(){
 int a=2;
 if(a==2){
   a=~a+2<<1;
   printf("%d",a);
 }
 else{
  break;
 }
}

(A) It will print nothing.
(B) -3
(C) -2
(D) 1
(E) Compiler error


Explanation:
Note that the keyword 'break' is not part of the if-else statement. Consequently, attempting to use 'break' within an if-else block will result in a compiler error, specifically, 'Misplaced break'.
Control statement tutorial

7.

What will be output if you will compile and execute the following c code?

void main(){
  int a=10;
  printf("%d %d %d",a,a++,++a);
}

(A) 12 11 11
(B) 12 10 10
(C) 11 11 12
(D) 10 10 12
(E) Compiler error


Explanation:

In C, the printf function adheres to the cdecl parameter passing scheme, where parameters are passed from right to left direction.


So, in the sequence ++a, a++, and a, the pre-increment operator ++a will be evaluated first, resulting in the variable value becoming =10. Subsequently, the post-increment operator a++ will be evaluated, leaving the variable value as =10. Finally, the variable a will be evaluated, and its value will be =12.
Function tutorial.

8.

What will be output if you will compile and execute the following c code?

void main(){
   char *str="Hello world";
   printf("%d",printf("%s",str));
}

(A) 11Hello world
(B) 10Hello world
(C) Hello world10
(D) Hello world11
(E) Compiler error


Explanation:

The printf function in C has an integer return type, and the value of this integer precisely equals the count of characters, including white spaces, that the printf function prints. Therefore, calling printf("Hello world") will return the value 13.


9.

What will be output if you will compile and execute the following c code?

#include "stdio.h"
#include "string.h"
void main(){
   char *str=NULL;
   strcpy(str,"cquestionbank");
   printf("%s",str);
}

(A) cquestionbank
(B) cquestionbank\0
(C) (null)
(D) It will print nothing
(E) Compiler error


Explanation:

It's important to note that attempting to copy anything using the strcpy function to a character pointer pointing to NULL is not allowed.

More questions of string.

10.

What will be output if you will compile and execute the following c code?

#include "stdio.h"
#include "string.h"
void main(){
  int i=0;
  for(;i<=2;)
   printf(" %d",++i);
}

(A) 0 1 2

(B) 0 1 2 3
(C) 1 2 3
(D) Compiler error
(E) Infinite loop


Explanation:
In a for loop, each part—namely the initialization, condition, and iteration expressions—is optional.
Complete tutorial of looping in C.

11.

What will be output if you will compile and execute the following c code?

void main(){
  int x;
  for(x=1;x<=5;x++);
    printf("%d",x);
}

(A) 4
(B) 5
(C) 6
(D) Compiler error
(E) None of above


Explanation:
The body of a for loop is optional. In this context, the for loop will continue to execute until the value of the variable 'x' becomes six, causing the loop condition to evaluate to false.
Looping tutorial.

12.

What will be output if you will compile and execute the following c code?

void main(){
printf("%d",sizeof(5.2));
}

(A) 2
(B) 4
(C) 8
(D) 10
(E) Compiler error


Explanation:

The default type of a floating-point constant in C is double. Consequently, the constant 5.2 is considered a double constant, and its size is 8 bytes.

Detail explanation of all types of constant in C.

13.

What will be output if you will compile and execute the following c code?

#include "stdio.h"
#include "string.h"
void main(){
char c='\08';
printf("%d",c);
}


(A) 8
(B) ’8’
(C) 9
(D) null
(E) Compiler error


Explanation:

In C, any character that starts with the character ‘\’ represents an octal number in character form. Given that octal digits are limited to 0, 1, 2, 3, 4, 5, 6, and 7, the presence of '8' is not valid. Therefore, the expression ‘\08’ is considered an invalid octal character constant.

Hexadecimal character constant.

14.

What will be output if you will compile and execute the following c code?

#define call(x,y) x##y
void main(){
int x=5,y=10,xy=20;
printf("%d",xy+call(x,y));
}

(A) 35
(B) 510
(C) 15
(D) 40
(E) None of above


Explanation:

The '##' is a concatenation operator in the C preprocessor. It effectively concatenates its operands, as exemplified by 'a##b' resulting in 'ab'. Upon inspecting the intermediate file, you will find that the code has been transformed into the following intermediate code before the commencement of the actual compilation. Intermediate file:

test.c 1:
test.c 2: void main(){
test.c 3: int x=5,y=10,xy=20;
test.c 4: printf("%d",xy+xy);
test.c 5: }
test.c 6:
It is clear call(x, y) has replaced by xy.
Preprocessor tutorial.

15.

What will be output if you will compile and execute the following c code?


int * call();
void main(){
int *ptr;
ptr=call();
clrscr();
printf("%d",*ptr);
}
int * call(){
int a=25;
a++;
return &a;
}


(A) 25
(B) 26
(C) Any address
(D) Garbage value
(E) Compiler error


Explanation:

In this scenario, the variable 'a' is a local variable with its scope and visibility limited to the function call. After returning the address of 'a' through the function call, the variable 'a' becomes inactive (dead), while the pointer 'ptr' still points to the address of 'a'. This situation is commonly referred to as the 'dangling pointer problem'.

Complete pointer tutorial.

16.

What is error in following declaration?

struct outer{
int a;
struct inner{
char c;
};
};

(A) Nesting of structure is not allowed in c.
(B)
It is necessary to initialize the member variable.
(C) Inner structure must have name.
(D) Outer structure must have name.
(E) There is not any error.


Explanation:
It is essential to assign a name to an inner structure at the time of its declaration; otherwise, accessing the members of the inner structure becomes impossible. Hence, the correct declaration is:

struct outer{
int a;
struct inner{
char c;
}name;
};
Union tutorial.

17.

What will be output if you will compile and execute the following c code?

void main(){
int array[]={10,20,30,40};
printf("%d",-2[array]);
}

(A) -60
(B) -30
(C) 60
(D) Garbage value
(E) Compiler error


Explanation:
In c,
array[2]=*(array+2)=*(2+array)=2[array]=30

18.

What will be output if you will compile and execute the following c code?

void main(){
int i=10;
static int x=i;
if(x==i)
printf("Equal");
else if(x>i)
printf("Greater than");
else
printf("Less than");
}

(A) Equal
(B) Greater than
(C) Less than
(D) Compiler error
(E) None of above


Explanation:

Static variables are considered load-time entities, whereas auto variables are run-time entities. It is not permissible to initialize any load-time variable with a run-time variable. In this example, 'i' is a run-time variable, while 'x' is a load-time variable.

What is storage class?

19.

What will be output if you will compile and execute the following c code?

#define max 5;
void main(){
int i=0;
i=max++;
printf("%d",i++);
}

(A) 5
(B) 6
(C) 7
(D) 0
(E) Compiler error


Explanation:
#define is a token-pasting preprocessor directive. If you examine the intermediate file, such as 'test.i':
test.c 1:
test.c 2: void main(){
test.c 3: int i=0;
test.c 4: i=5++;
test.c 5: printf("%d",i++);
test.c 6: }
test.c 7:
It is evident that the macro constant 'max' has been replaced by 5. Attempting to increment this constant is not allowed, and it will result in a compiler error indicating 'Lvalue required'.

Preprocessor questions and answer.

20.

What will be output if you will compile and execute the following c code?

void main(){
double far* p,q;
printf("%d",sizeof(p)+sizeof q);
}

(A) 12
(B) 8
(C) 4
(D) 1
(E) Compiler error


Explanation:

It's evident that 'p' is a far pointer, and the size of a far pointer is 4 bytes. On the other hand, 'q' is a double variable, and the size of a double variable is 8 bytes.

Complete pointer tutorial.


If you need more explanation of above C multiple choice questions you are free to ask.

15 comments:

Anonymous said...

Really Its awesome.....

Manoj said...

Answer of Question number is=19
How? not 21 why ?

1m2r3a said...

19.

What will be output if you will compile and execute the following c code?

#define max 5;
void main(){
int i=0;
i=max++;
printf("%d",i++);
}

The correct answer is :
(E) Compiler error


Explanation:
file: test.i
test.c 1:
test.c 2: void main(){
test.c 3: int i=0;
test.c 4: i=5;++; //because u have define max as 5;
test.c 5: printf("%d",i++);
test.c 6: }
test.c 7:

C:\Users\Robert\Documents\c\test9\56789.c||In function 'main':|
C:\Users\Robert\Documents\c\test9\56789.c|4|error: expected expression before ';' token|

||=== Build finished: 1 errors, 0 warnings ===|

Anonymous said...

gud work....

My Thoughts in SW Engineering said...

This code gives me compiler error
#define max 5;

void main(){
int i=0;
i=max++;
printf("%d",i++);
}

/////////////////////
The output of this code is 19 and I don't know why !!


void main(){
int i=4,x;
x=++i + ++i + ++i;
printf("%d",x);
}

Anonymous said...

NYC...questions...plzzz increases the no of questions

Unknown said...

Very nice questions,beautifully explained.
Thanks a lot brother.

Unknown said...

very nice questions...plzzz increases the no of questions

kushi chopra said...

whether we can use double quotes to include header files

Unknown said...

All questions. are very good but..less no of questions

CHETAN P said...

yes kushi chopra you can include but there are two ways... just presentation is different

1) ==> system define .h(header file)

2) "myhead.h" ==> user define .h(header file)

Vishnu Thorat said...

its really nice c test skills............

Unknown said...

it depends upon compiler used..
for gcc its 21

for turbo its 19

Unknown said...

Great Website.

Unknown said...

its true