C LANGUAGE QUESTIONS WITH SOLUTION

-->
C language objective types questions and answers with solution or explanation

1.

#include<stdio.h>
int main(){
    int a=0;
    #if (a==0)
         printf("Equal");
    #else if
         printf("Not equal");
    #endif
    return 0;
}

(A) Equal
(B) Not equal
(C) Null
(D) Garbage
(E) Compilation error


Explanation:

Syntax of conditional preprocessor directive (if) is:

#if <Constant expression>
#else
#endif

In this code (a==0) is not constant expression. A constant expression mean expression doesn’t contain any variables.
Note: const int a; Here a is also variable     

2.

What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    for(;NULL;)
         printf("cquestionbank");
    return 0;
}

(A) c
(B) bank
(C) cquestionbank
(D) Infinite loop
(E) Compilation error


Explanation:

Here NULL is micro constantan. Value of this symbolic constant is 0 or 0L as defined stdio.h:

#ifndef NULL
#  if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__)
#      define NULL    0
#  else
#      define NULL    0L
#  endif
#endif

So corresponding intermediate file of above code will be:


int main(){
    for(;0;)
    printf("cquestionbank");
    return 0;
}

As you know in c :
0: Means false
Non- zero: True

So for loop should not execute any time because intitial condtion is false. But it is bug of turbo c compiler. Loop will execute one time and it will print : cquestionbank

3.

What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int x=25;
    if(!!x)
         printf("%d",!x);
    else
         printf("%d",x);
    return 0;
}

(A) 0
(B) 25
(C) 1
(D) -1
(E) 2

Explanation:

! is negation operator.
!x = 0 if x is non-zero number.
!x = 1 if x is equal to zero.
So,
!!x
=! (! x)
=! (! 25)
=! (0)
=1
As we know in c:
Zero: It represents false.
Non-zero: It represents false.
if (1) means condition is true hence if part will execute.
!x =! 1 = 0
Hence printf statement will print zero in the console window.

4.

What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    float a=0.5, b=0.9;
    if(a&&b>0.9)
         printf("Sachin");
    else
         printf("Rahul");
    return 0;
}

(A) Sachin
(B) Rahul
(C) null
(D) Run time error
(E) Compilation error


Explanation:

Consider on the expression:
a && b > 0.9
As we know > operator enjoy higher precedence than && operator.
So expression will be
a && (b > 0.9)
=0.5 && (0.9 > 0.9)
Properties of > (Greater than operator)
a > b returns 1 if a is greater than b.
a > b return 0 if a is less than or equal to b.
So our expression became:
= 0.5 && 0
In c zero represents false and non-zero represents true.
= true && false
= false
= 0
So, if (0) means condition is false. Hence else part will execute.

5.

What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int x=5, y=10;
    if(!(!x) && x)
         printf("%d",x);
    else
         printf("%d",y);
    return 0 ;
}

(A) 1
(B) 0
(C)
5
(D) 10
(E) Compilation error


Explanation:

Consider on expression:
! (! x) && x
=! (! 5) && 5
=! 0 && 5
=1 && 5
=1
So, if condition is true.

6.

What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    char ch=321;
    printf("%d %c",ch,ch);
    return 0 ;
}

(A) 321 #
(B) 65 A
(C) 321 !
(D) 66 B
(E) Compilation error


Explanation:

Number 321 is beyond the range of char variable ch. So ch will store corresponding cyclic value as shown in the figure:


Equivalent cyclic value of 321 = 321 % 128 = 65
65 is ASCII value of character 'A'

7.

What will be output if you will execute following c code?

#include<stdio.h>
int main(){
int a,b;
    a = -3- -3;
    b = -3 - - (-3 );
    printf("%d %d",a,b);
    return 0 ;
}

(A) 0 0
(B) 0 -3
(C) -3 0
(D) 0 -6
(E) Compilation error


Explanation:

- Operator in c has two forms:
- : Unary minus operator (It requires one operand)
- : Binary minus operator (It requires two operands)
Note: Unary minus operator has higher precedence than binary minus operator.

8.

What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int x;
    x= -2 + 11 - 7 * 9 % 6 / 12;
    printf("%d",x);
    return 0 ;
}

(A) 6
(B) 7
(C) 8
(D) 9
(E) Compilation error


Explanation:

Consider on the expression
x = -2 + 11 - 7 * 9 % 6 / 12
There are seven different type of operator in the above expression:



Operator
Precedence
Associative
-
1
Right to left
* , % , /
2
Left to right
- , +
3
Left to right
=
4
Right to left

First of all unary minus will perform the operation
 x = (-2) + 11 - 7 * 9 % 6 / 12
 Now multiplication operator will perform the operation
 x = (-2) + 11 - (7 * 9) % 6 / 12
 x = (-2) + 11 - 63 % 6 / 12
 After this modular division operator will perform operation:
 x = (-2) + 11 - (63 % 6) / 12
 x = (-2) + 11 - 3  / 12
 After this division will perform the operation:
 x = (-2) + 11 - (3 / 12)
 x = (-2) + 11 - 0
 Now binary plus operator will perform the operation:
 x = ((-2) + 11) - 0
 x = 9 - 0
 Now binary minus operator will perform the operation:
 x = 9
 At the end assignment operator will perform the operation and it will assign 9 to variable x.

9.

What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int x=3, y=4, z=4;
    printf("%d", (z>=y>=x?100:200));
   
    return 0 ;
}

(A) 100
(B) 200
(C) 0
(D) 1
(E) Compilation error


Explanation:

Consider on arithmetical expression:
z>=y>=x? 100:200
= (z >= y >= x? 100: 200)
= (4 >= 4 >= 3? 100: 200)
= ((4 >= 4) >= 3? 100: 200)
= (1 >= 3? 100: 200)
= (0? 100: 200)
= 200

10.

What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int a=30, b=40, x;
    x=(a!=10) && (b=50);
    printf("%d",x);
    return 0 ;
}

(A) 1
(B)
2
(C)
3
(D) 4
(E) Compilation error


Explanation:

(a!= 10) && (b = 50)
= (30! = 10) && (50)
= 1 && 50
= 1

11.

What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    float x=12.25, y=13.65;
    if(x=y)
         printf("x and y are equal");
    else
         printf("x and y are not equal");
    return 0 ;
}

(A) x and y are equal
(B) x and y are not equal
(C) It will print nothing
(D) Run time error
(E) Compilation error


Explanation:

= is assignment operator.
So, x=y value of variable y will assign to variable x.
    x= 13.65
As we know in c:
Zero represents false.
Any non-zero number represents true.
Hence if (13.65) is true condition.

12.

What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int i=1, j=1;
    for(;j;printf("%d%d\t",i,j))
         j=i++ <= 5;
    return 0 ;
}
(A) 1 2 3 4 5
(B) 11 12 13 14
(C)
21 31 41 51 61 70
(D) Infinite loop
(E) Compilation error


Explanation:

Syntax of for loop is:

for (exp1;exp2;exp3){
    <Statements>
}
Note: all Exp1, Exp2, Exp3 and <statements> are optional.
Order of evaluation is:
In first cycle
1. Exp1
2. Exp2
3. <Statement>
4. Exp2
In rest cycles
1. Exp2
2. <Statements>
3. Exp3
Now we come to our question.
In first cycle
1. It has not exp1
2. Exp2 is j which is equal to 1 i.e. condition is true
3. <Statements> is j=i++ <= 5
j = 1 <= 5
j = 1
i = 2 (Due to postfix increment operator)
4. Exp3 is printf statement which will print i as 2 and j as 1

In second cycle
1. Exp2 is j which is equal to 1 i.e. condition is true
2. <Statements> is j=i++ <= 5
j = 2 <= 5
j = 1
i = 3 (Due to postfix increment operator)
3. Exp3 is printf statement which will print i as 3 and j as 1

In third cycle
1. Exp2 is j which is equal to 1 i.e. condition is true
2. <Statements> is j=i++ <= 5
j = 3 <= 5
j = 1
i = 4 (Due to postfix increment operator)
3. Exp3 is printf statement which will print i as 4 and j as 1

In fourth cycle
1. Exp2 is j which is equal to 1 i.e. condition is true
2. <Statements> is j=i++ <= 5
j = 4 <= 5
j = 1
i = 5 (Due to postfix increment operator)
3. Exp3 is printf statement which will print i as 4 and j as 1

In fifth cycle
1. Exp2 is j which is equal to 1 i.e. condition is true
2. <Statements> is j=i++ <= 5
j = 5 <= 5
j = 1
i = 6 (Due to postfix increment operator)
3. Exp3 is printf statement which will print i as 6 and j as 1

In sixth cycle
1. Exp2 is j which is equal to 1 i.e. condition is true
2. <Statements> is j=i++ <= 5
j = 6 <= 5
j = 0
i = 7 (Due to postfix increment operator)
3. Exp3 is printf statement which will print i as 7 and j as 0

In seventh cycle
1. Exp2 is j which is equal to 0 i.e. condition is false. So loop will terminate.

13.

What function is used to release the allocated memory space?

(A) deallocate()
(B) release ()
(C) free ()
(D) drop()
(E) empty ()


Explanation:
**

14.

What will be output if you will execute following c code?

#include<stdio.h>
auto int a=5;
int main(){
    int x;
    x=~a+a&a+a<<a;
    printf("%d",x);
    return 0;
}


(A)
0
(B) 1
(C) 154
(D) 155
(E) Compilation error


Explanation:
**

15.

What will be output if you will execute following c code?

#include<stdio.h>
void main(){
    int a=5;
    {
         int b=10;
         ++b;
         ++a;
         {
             int a=20;
             ++a;
             a=++b;
         }
         ++a;
         ++b;
         printf("%d %d",a,b);
    }
    printf(" %d",a);
}

(A) 7 13 7
(B) 13 13 13
(C) 13 13 5
(D)
6  13 5
(E) Compilation error


Explanation:
**

16.

What will be output if you will execute following c code?

#include<stdio.h>
#include<conio.h>
void main(){
    int a[]={0,1,2,3,4,5,6,7,8,9,10};
    int i=0,num;
    num=a[++i+a[++i]]+a[++i];
    printf("%d",num);
}

(A) 6
(B) 7
(C) 8
(D) 9
(E) Compilation error


Explanation:

a[++i+a[++i]]+a[++i]

As we know in any expression pre increment operator first increment the value of variable  then final value assign to all variable.
Variable i has incremented three times so final value of I will be 3. Now 3 will be assigned to all i. So expression will be:
= a [3+a [3] +a [3]
= a [3 + 3] + 3
=a [6] + 3
= 6 + 3
=9

17.

What will be output if you will execute following c code?

#include<stdio.h>
#include<conio.h>
void main(){
    int i=3,val;
    val=sizeof f(i)+ +f(i=1)+ +f(i-1);
    printf("%d %d",val,i);
}
int f(int num){
    return num*5;
}

(A) 2 0
(B) 7 1
(C) 17 0
(D)
2 1
(E) Compilation error


Explanation:

Consider on the expression:
sizeof f(i)+ +f(i=1)+ +f(i-1)  //i=3
= sizeof f(3)+ +f(i=1)+ +f(i-1)
= sizeof 15 + +f(i=1)+ +f(i-1) 
= 2 + +f(i=1)+ +f(i-1)
= 2 + f(1) +  +f(0)  //i=1
= 2 + 5 + f(0)
= 7 + 0
= 7

18.

What will be output if you will execute following c code?

#include<stdio.h>
#include<conio.h>
void main(){
    int i;
    (i=8)+=1;
    printf("%d",i);
}

(A) 9
(B) 10
(C) 32
(D) 34
(E) Compilation error


Explanation:
**

19.

What will be output if you will execute following c code?

#include<stdio.h>
#include<conio.h>
void main(){
    char c=-'a';
    printf("%d",c);
}

(A) 65
(B) -65
(C)
-a
(D) -97
(E) Compilation error


Explanation:

ASCII value of character ‘a’ is 97.

20.

What will be output if you will execute following c code?

#include<stdio.h>
int main(){
    int num,a=5;
    num=-a--;
    printf("%d  %d",num,a);
}

(A) 5  4
(B) -4 4
(C) -5 4
(D) -4 5
(E) Compilation error


Explanation:

Post decrement operator first assign the value then it decrements the value. 
num = -a—-
num = -5
Now value of variable a will be 4.

Post a Comment