AmbitionBox

Discover Best Places to work in India

C Programming

C Programming Questions and Answers

Showing 1 - 10 of 12 questions

1

Assunming, integer is 2 byte, What will be the output of the program?

#include < stdio . h >int main()
{
    printf("%x\n", -1>>1);
    return0;
}
a

ffff

b

0fff

c

0000

d

fff0

correct answer a
Answer :

Option A

Explanation :

Negative numbers are treated with 2's complement method.

1's complement: Inverting the bits ( all 1s to 0s and all 0s to 1s)
2's complement: Adding 1 to the result of 1's complement.
Binary of 1(2byte) : 0000 0000 0000 0001
Representing -1:
1s complement of 1(2byte) : 1111 1111 1111 1110
Adding 1 to 1's comp. result : 1111 1111 1111 1111
Right shift 1bit(-1>>1): 1111 1111 1111 1111 (carry out 1)
Hexadecimal : f f f f
(Filled with 1s in the left side in the above step)

Note:
1. Fill with 1s in the left side for right shift for negative numbers.
2. Fill with 0s in the right side for left shift for negative numbers.
3. Fill with 0s in the left side for right shift for positive numbers.
4. Fill with 0s in the right side for left shift for positive numbers.

2

If an unsigned int is 2 bytes wide then, What will be the output of the program ?

#include < stdio . h >int main()
{
    unsignedint m = 32;
    printf("%x\n", ~m);
    return0;
}
a

ffff

b

0000

c

ffdf

d

ddfd

correct answer
Answer :

Option C

3

Assuming a integer 2-bytes, What will be the output of the program?

#include < stdio . h >int main()
{
    printf("%x\n", -1<<3);
    return0;
}
a

ffff

b

fff8

c

0

d

-1

correct answer b,d
Answer :

Option B

Explanation :

The system will treat negative numbers in 2's complement method. Example:

Assume the size of int is 2-bytes(16 bits). The integer value 1 is represented as given below:
Binary of 1: 00000000 00000001 (this is for positive value of 1)

1's complement of binary 1: 11111111 11111110
2's complement of binary 1: 11111111 11111111
Thy system will store '11111111 11111111' in memory to represent '-1'.

If we do left shift (3 bits) on 11111111 11111111 it will become as given below:

11111111 11111111 ---(left shift 3 times)---> 11111111 11111000.

So, 11111111 11111000 ---(binary to hex)---> FF F8. (Required Answer)

Note:
How is the negative number obtained from 2's complement value?

As stated above, -1 is represented as '11111111 11111111' in memory.

So, the system will take 2's complement of '11111111 11111111' to the get the original negative value back.

Example:
Bit Representation of -1: 11111111 11111111 Since the left most bit is 1, it is a negative number. Then the value is

1's complement: 00000000 00000000
2's complement: 00000000 00000001 (Add 1 to the above result)

Therefore, '00000000 00000001' = 1 and the sign is negative.

Hence the value is -1.

4

If an unsigned int is 2 bytes wide then, What will be the output of the program ?

#include < stdio . h >int main()
{
    unsignedint a=0xffff;
    ~a;
    printf("%x\n", a);
    return0;
}
a

ffff

b

0000

c

00ff

d

ddfd

correct answer a
Answer :

Option A

5

What will be the output of the program?

#include < stdio . h >int main()
{
    unsignedchar i = 0x80;
    printf("%d\n", i<<1);
    return0;
}
a

0

b

256

c

100

d

80

correct answer b
Answer :

Option B

6

What will be the output of the program?

#include < stdio . h >int main()
{
    printf("%d >> %d %d >> %d\n", 4 >> 1, 8 >> 1);
    return0;
}
a

4 1 8 1

b

4 >> 1 8 >> 1

c

2 >> 4 Garbage value >> Garbage valu

d

2 4

correct answer c
Answer :

Option C

7

What will be the output of the program?

#include < stdio . h >int main()
{
    char c=48;
    int i, mask=01;
    for(i=1; i<=5; i++)
    {
        printf("%c", c|mask);
        mask = mask<<1;
    }
    return0;
}
a

12400

b

12480

c

12500

d

12556

correct answer b
Answer :

Option B

8

What will be the output of the program?

# define P printf("%d\n", -1^~0);#define M(P) int main()\
             {\
                P\
                return0;\
             }
M(P)
a

1

b

0

c

-1

d

2

correct answer b
Answer :

Option B

9

What will be the output of the program ?

#include < stdio . h >int main()
{
    int i=32, j=0x20, k, l, m;
    k=i|j;
    l=i&j;
    m=k^l;
    printf("%d, %d, %d, %d, %d\n", i, j, k, l, m);
    return0;
}
a

0, 0, 0, 0, 0

b

0, 32, 32, 32, 32

c

32, 32, 32, 32, 0

d

32, 32, 32, 32, 32

correct answer c
Answer :

Option C

10

What will be the output of the program?

#include < stdio . h >int main()
{
    printf("%d %d\n", 32<<1, 32<<0);
    printf("%d %d\n", 32<<-1, 32<<-0);
    printf("%d %d\n", 32>>1, 32>>0);
    printf("%d %d\n", 32>>-1, 32>>-0);
    return0;
}
a

Garbage values

b

64 32
0 32
16 32
0 32

c

All zeros

d

8 0
0 0
32 0
0 16

correct answer b
Answer :

Option B

Select a company to compare with

vs

Similar Companies