Interview Question


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
4
of 4 vote

i = 300;
=> i = 100101100 //in binary in word format => B B Hb 0001 0010 1100 where B = Byte and Hb = Half Byte

(A)=> in memory (assuming it is Little endian))

0x12345678 - 0010- 1100
0x12345679 - 0000- 0001
0x1234567a - 0000 - 0000
0x1234567b - 0000 - 0000

0x1234567c - Location of next intezer(location of ptr++ or ptr + 1 where ptr is an intezer pointer as ptr is of type int => on doing ++ptr it will increment by 4 byte(**size of int**))

when

(B)we do char *ptr = &i;
ptr will become of type char => on doing ++ptr it will increment by 1 byte(**size of char**)
so on doing ++ptr it will jump to location -> 0x12345679 (which has 0000- 0001)
now we are doing
*++ptr = 2
=> 0x12345679 will be overwritten by 2 => 0x12345679 will have 00**10** - 0000 instead of 000**1** - 0000

so the new memory content will look like this :


(C)0x12345678 - 0010 - 1100
0x12345679 - 0000- 0010
0x1234567a - 0000 - 0000
0x1234567b - 0000 - 0000

which is equivalent to => B B Hb 0010 00101100 where B = Byte and Hb = Half Byte
=> 556

- softy June 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice explanation...

- Ravindian June 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey, why you are taking 4bits instead of 1 byte..... like, according to my understanding representation of 300 in little endian would be,
0x12345678 - 0010 1100
0x12345679 - 0000 0001
0x1234567a - 0000 - 0000
0x1234567b - 0000 - 0000
Correct me if I am wrong. Although it will not affect the end result :)
And can you please explain for big endian also??

- max June 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The memory layout canbe also like what you have pointed out.This will be for Big Endian machine where higher Byte value goes to lower byte position or address and Lower Byte Value to lower byte position or address.

Like
0x56677788 is the address of an int having the space for 8 bits
_ _ _ _ _ _ _ _

First four will be for the place for the lower byte value for little endina machine and for higher value like 0010 as you have pointed out !

- softy June 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What course..err.. company?

- Anonymous June 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry, actual answer will depend on whether the machine is big endian/little endian.

- sourav June 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First, you will have to cast the pointer to int (&i) to a pointer to char (char*). As soon as you do it and increment it, you will be addressing the byte #1 (second byte) of the original int. So, the actual output will depend on the size of int and the endianness of the machine this code is compiled for.

- ashot madatyan June 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it will print 300.
he is updating next memory
i=300 ->2000
*p=300
++p->2004
*p=2->2004
printf("%d",i)->2000 ====300

- Anonymous June 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

++p->2004 will not be 2004 as it is char* not int*.See my explaination aove.I have verified it on a 64 bit little endian machine.

- softy June 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

cannot assign integer to char pointer

- yuvaraj June 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Error

- Rishi June 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Gives error due invalid pointer assignment

- Tripaam June 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

o/p
556

- govind.chauhan143 June 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

answer is 556

- jatin June 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

556

- Anonymous June 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think it will give error, because first it is pointing to an allocated location, after that it is trying to point to next location and try to assign a value into it which is not allocated yet.

- sourav June 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If most significant byte stores first then it will print 256, or if least significant byte stores first then it will print 556.

- sourav June 06, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

will it not be 301(12D)
>0x12343557 --> 01
>0x1234558 --> 2C

*++ptr --> 0x1234558
2D

- softy June 25, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More