C Programming :: Variables & Data Types

NA
SHSTTON
4
Solv. Corr.
4
Solv. In. Corr.
8
Attempted
0 M:0 S
Avg. Time

1 / 51

What will be output of the following "c" code?
#include<stdio.h>
int main()
{
float i, j;
scanf(%f  %f, &i, &j);
printf(%.2f %.3f, i, j);
return 0;
}

What will be the output for the give input 12.342 and 123.4568

A12.34 123.456

B12.342 123.4568

C12 123

DCompilation Error

ENone of these

Answer: Option A

Explanation:

I see couple of issues with question.



1. there should be compliation issue with this statement - printf(%.2f %.3f, i, j);



I have tried to complie 



$ gcc -o q8 q8.c

q8.c: In function ‘main’:

q8.c:6:8: error: expected expression before ‘%’ token

  scanf(%f %f,

Workspace

NA
SHSTTON
6
Solv. Corr.
4
Solv. In. Corr.
10
Attempted
0 M:0 S
Avg. Time

2 / 51

What will be output of the following "c" code?
#include<stdio.h>
void main()

{
float me = 1.1; 
double you = 1.1; 
if(me==you)
printf("I love U"); 
else
printf("I hate U"); 
}

AI hate U

BI love U

CCompiler Error

DNone of these

Answer: Option A

Explanation:

For floating point numbers (float, double, long double) the values cannot be predicted exactly. Depending on the number of bytes, the precession with of the value represented varies. Float takes 4 bytes and long double takes 10 bytes. So float stores 0.9 with less precision than long double.

Important Note:
Never compare or at-least be cautious when using floating point numbers with relational operators (== , >, <, <=, >=,!= ) .

Workspace

NA
SHSTTON
0
Solv. Corr.
11
Solv. In. Corr.
11
Attempted
0 M:0 S
Avg. Time

3 / 51

What will be output of the following "c" code?
#include<stdio.h>
int main(){
    int a=5;
    static int b=a;
    printf("%d %d",a,b);
    return 0;
}

A5 5

B5 0

C5 null

D5 Garbage

ECompilation error

Answer: Option E

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
1
Solv. Corr.
9
Solv. In. Corr.
10
Attempted
0 M:0 S
Avg. Time

4 / 51

What will be output of the following "c" code?
#include<stdio.h>
void main()
{
static int var = 5; 
printf("%d ",var--); 
if(var)
main();
}

A5 4 3 2 1 0

BInfinite loop

CCompiler Error

D5 4 3 2 1

ENone of these

Answer: Option D

Explanation:

When static storage class is given, it is initialized once. The change in the value of a static variable is retained even between the function calls. Main is also treated like any other ordinary function, which can be called recursively.

Workspace

NA
SHSTTON
1
Solv. Corr.
6
Solv. In. Corr.
7
Attempted
0 M:0 S
Avg. Time

5 / 51

What will be output of the following "c" code?
#include<stdio.h>
int main(){
    float x;
    x=(int)5.6f*3.2f/sizeof((int)6.6);
    printf("%f",x);
    return 0;
}

A8.96

B9.6

C8

D2

ECompilation error

Answer: Option C

Explanation:

Variable x is initially declared as float.

In line 4-

x=(int)5.6f*3.2f/sizeof((int)6.6);

or, x=(int)5.6f*3.2f/sizeof(6);        [Since, we are typecasting 6.6 to int]

or, x=(int)5.6f*3.2f/4;                    [Since, 6 is integer type, and size of int is 4 bytes]

or, x=(int)4.48;

or, x=4;                                         [Since, 4.48 is typecasted to int]

In line 5, we are converting int x=4 to float type and we are printing it.

Hence, the output is 4.000000.

Workspace

NA
SHSTTON
2
Solv. Corr.
5
Solv. In. Corr.
7
Attempted
0 M:0 S
Avg. Time

6 / 51

What will be output of the following "c" code?
#include<stdio.h>
int main(){
    float **(*ptr)[4]=(float **(*)[4])0;
    ptr+=5;
    printf("%d  %d",ptr,sizeof ptr);
    return 0;
}

A0 2

B5 2

C4 2

D40 2

ECompilation error

Answer: Option D

Explanation:

Here is no explanation for this answer

Workspace

NA
SHSTTON
2
Solv. Corr.
2
Solv. In. Corr.
4
Attempted
0 M:0 S
Avg. Time

7 / 51

What will be output of the following "c" code?
#include<stdio.h>
int main() { 
extern out; 
printf("%d", out); 
return 0;
} 
int out=100;

AUndefined reference symbol out

BCompiler Error

C100

Dgarbage Value

Answer: Option C

Explanation:

This is the correct way of writing the previous program.

Workspace

NA
SHSTTON
1
Solv. Corr.
3
Solv. In. Corr.
4
Attempted
0 M:0 S
Avg. Time

8 / 51

What will be output of the following "c" code?
#include<stdio.h>
void main()
{
extern int i;
i=20; 
printf("%d",i);
}

A20

Bgarbage Value

CLinker Error : Undefined symbol "I"

DNone of these

Answer: Option C

Explanation:

extern storage class specifies to the compiler that the memory for "i" is allocated in some other program and that address will be given to the current program at the time of linking.

But linker finds that no other variable of name "i" is available in any other program with memory space allocated for it.

Hence a linker error has occurred .

Workspace

NA
SHSTTON
14
Solv. Corr.
37
Solv. In. Corr.
51
Attempted
1 M:18 S
Avg. Time

9 / 51

What will be output of the following "c" code?
Note: 32 bit compiler

#include<stdio.h>
int main(){
    float **(*ptr)[4]=(float **(*)[4])0;
    ptr+=5;
    printf("%d  %d",ptr,sizeof ptr);
    return 0;
}

A0 2

B160 8

C40 2

D80 4

ECompilation error

Answer: Option D

Explanation:

ANSWER is wrong...should be 80,4
because size of float ** is 4 bytes

Workspace

NA
SHSTTON
2
Solv. Corr.
0
Solv. In. Corr.
2
Attempted
0 M:0 S
Avg. Time

10 / 51

What will be output of the following "c" code?
#include<stdio.h>
int main(){
    float x;
    x=(int)5.6 * 3.2/sizeof((int)6.6);
    printf("%f",x);
    return 0;
}

A8.96

B9.6

C4

D2

ECompilation error

Answer: Option C

Explanation:

Variable x is initially declared as float.

In line 4-

x=(int)5.6*3.2/sizeof((int)6.6);

or, x=(int)5.6*3.2/sizeof(6);        [Since, we are typecasting 6.6 to int]

or, x=(int)5.6*3.2/4;                    [Since, 6 is integer type, and size of int is 4 bytes]

or, x=(int)4.48;

or, x=4;                                         [Since, 4.48 is typecasted to int]

In line 5, we are converting int x=4 to float type and we are printing it.

Hence, the output is 4.000000.

Workspace