# C Programming :: Declarations and Initializations

NA
SHSTTON
46
Solv. Corr.
35
Solv. In. Corr.
81
Attempted
0 M:16 S
Avg. Time

1 / 54

What is the output of the following problem?
#include <stdio.h>
int main()
{
int i=4;
if(i=0)
printf("statement 1");
else
printf("statement 2");
return 0;
}

Astatement 1

Bstatement 2

CCompilation Error

DNo Output

Explanation:

statement 1 will only be printed when i value=0. But i value is initialized as i=4 so it doesn't go with the first condition hence the second condition is accepted and statement 2 is printed.

Workspace

NA
SHSTTON
133
Solv. Corr.
53
Solv. In. Corr.
186
Attempted
0 M:52 S
Avg. Time

2 / 54

What is the output of this C code?
#include<stdio.h>
int main() {
int i,j;
j = 10;
i = j++ - j++;
printf("%d %d", i,j);
return 0;
}

A0 12

B12 12

C0 0

D12 0

Explanation:

It also depends on the compiler, if optimization level (O1/O2/O3) is set, it will be 0 12 else it will be -1 12. for that you while compiling the program, compile with different optimization level and you will get a different answer.

But if you declare j as volatile variable, compiler optimization level dont impact.
As here in option 0 and 12 is only mention, not -1 12 then option A will be correct option. i.e 0 12

Workspace

NA
SHSTTON
71
Solv. Corr.
103
Solv. In. Corr.
174
Attempted
0 M:0 S
Avg. Time

3 / 54

What is the output of the following 'C' program?
#include<stdio.h>
int main() {
{
int  const * p=5;
printf("%d",++(*p));
return 0;
}

ACompiler error

B5

C6

DNone of these

Explanation:

p is a pointer to a "constant integer". But we tried to change the value of the "constant integer".

Workspace

NA
SHSTTON
36
Solv. Corr.
132
Solv. In. Corr.
168
Attempted
0 M:16 S
Avg. Time

4 / 54

What is the output of the following 'C' program?
#include<stdio.h>
#define max
int main(){
printf("%d",max);
return 0;
}

A0

Bnull

CGarbage

DCompilation error

E-1

Explanation:

#define max(a,b) ((a).(b)?(a):(b)) This is the complete and correct way to define the function max . In the code its incomplete hence it shows compiler error.

Workspace

NA
SHSTTON
79
Solv. Corr.
94
Solv. In. Corr.
173
Attempted
0 M:16 S
Avg. Time

5 / 54

What is the output of the following 'C' program?
#include<stdio.h>
void main() {
int x = 10,y = 10, z = 5, i;
i = x;
printf("%d",i==x);
}

A1

BError

C0

D5

Explanation:

The value of x initialised is 10. Also i=x so i=10. Now when comparing condition  (i==x) it will give boolean result that is if the condition is true it will return 1 else it will return 0. So here the condition is true coz x=i hence output is 1.

Workspace

NA
SHSTTON
56
Solv. Corr.
79
Solv. In. Corr.
135
Attempted
0 M:7 S
Avg. Time

6 / 54

What does the following 'C' program do ?
#include<stdio.h>
void main()
{
unsigned int num;
int i;
scanf("%u", &num);
for (i = 0; i < 16; i++)
printf("%d", (num << i & 1 << 15)? 1:0);
}

AIt prints all even bits form num

BIt prints binary equivalent of num

CIt prints all odd bits from num

DNone of these

Explanation:

Here integer (unsigned) is assumed as 2 bytes so that for loop is till 16 i.e (2byte = 16 bits.)

As unsigned integer so user is expected to enter positive number.

statement in printf (num << i & 1 << 15)? 1:0 is used to convert decimal to binary equivalent.

in the above statement below things has been used.

<< ----> left shift

& -----> And operator

? : ------> trinery operator

Ex: if the number is 10 and its binary equivalent 1010 or (00000000000001010 in 16 bit)

the printf statement will be

for i = 0

(10 << 0 & 1 << 15)>?1:0

for i = 1

(10 << 1 & 1 << 15)>?1:0

similarly for i = 2, 3, 4 , ---- 15

Workspace

NA
SHSTTON
33
Solv. Corr.
102
Solv. In. Corr.
135
Attempted
0 M:27 S
Avg. Time

7 / 54

What is the output of the following program?
#include<stdio.h>
int main()
{
int x,y=2,z,a;
x = (y*=2) + (z=a=y);
printf ("%d", x);
return 0;
}

A7

B8

C6

Dsyntactically wrong

Explanation:

x = (y*=2) (z=a=y); Meaning of y*=2 means y=y*2 hence y value becomes 2*2=4.

Current value of y =4 so z=a=y=4.

Hence x=4+4=8

Workspace

NA
SHSTTON
23
Solv. Corr.
103
Solv. In. Corr.
126
Attempted
0 M:35 S
Avg. Time

8 / 54

What is the output of the following program?
#include<stdio.h>
void main()
{
int x,y=2,z,a;
if(x=y%2)
z=2;
a=2;
printf("%d %d",z,x);
}

A1..0

B2..0

C2..1

DGarbage-value 0

ECompilation Error

Explanation:

The value of y%2 is 0. This value is assigned to x.
The condition will if (0) so z goes uninitialized.

Workspace

NA
SHSTTON
29
Solv. Corr.
90
Solv. In. Corr.
119
Attempted
0 M:35 S
Avg. Time

9 / 54

What is the output of the following 'C' program?
#include <stdio.h>
void fun(void)
{
static int s = 0;
s++;
if(s == 10)
return;
fun();
printf("%d ", s);
}

int main(void)
{
fun();
}

A9 times 10

B10 times 10

CCompilation Error

DNone of these

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
16
Solv. Corr.
101
Solv. In. Corr.
117
Attempted
0 M:0 S
Avg. Time

10 / 54

What is the output of the following 'C' program?
#include <stdio.h>
void main(){
char c=125;
c=c+10;
printf("%d",c);
}

A135

BINF

C-121

DCompilation Error

Explanation:

Here char is signed so,the size of char is 1 bytes and its value range are -128 to 127,

and it will be something like below

-128 -127 -126 .......... 0 ....... 126 127 in the cycle.

Now as per questions c = 125 it is under the above limit.

but when 10 added to it, it becomes 125 10 = 135

125 126 127 -128 -127 -126 -125 -124 -123 -122 -121

So, count above 135 will fall at -121, so Answer will be -121

If in the question it would have given unsigned char then answer will be 135

Workspace