Accenture Interview Question for Software Engineer / Developers


Team: dx
Country: United States




Comment hidden because of low score. Click to expand.
1
of 3 vote

The result might be compiler/platform specific. I think that there's no any warranty that the compiler will put local variables on 2 sequential adjacent addresses.The difference would be 1 if a and b were elements of the same array, for local two local variables the result might be surprising.

- Fan March 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

In this case this will come as 1 or -1 depending on compiler.
but if tweak the code a bit to
d1 = &a;
d2 = &b;
and diff = d1-d2, then result will =/-4 for int.
This gets lil confusing.

- Varun March 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

I think output for all four cases will be 1.
In C difference between two addresses of same data type is usually 1, because addresses for memory is allocated to variable from heap memory.
And thus the difference between two consecutive addresses is 1

- Anonymous March 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

The output is 1 in all cases and the memory is allocated from stack but not from heap as they are local variables of the function. As the variables are created one by one, memory would be allocated to them sequentially.

- Vijay March 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Memory allocation is done on stack. So, the difference will be 1 in all the cases.

- alex March 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The answer I get from my compiler is 3, 3, 12, and 2. The C Standard makes no guarantees about packing of local variables. Moreover, as for subtracting two pointers the Standard states "Unless both pointers point to elements of the same array object, or one past the last element of the array object, the behavior is undefined." So, we have undefined behavior. And don't get me started on "void main".

- Anonymous March 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The reason for 1 is , in C the pointer will be incremented always by the size of pointer type, for example if the pointer type is int and size of int is 4 bytes, and you increment the pointer it always increment the pointer by 4 bytes, and similarly if you do addition or subtraction it is always %sizeofpointer, in this case it's %4.
This below example gave me bit clarity.

#include<stdio.h>

main()
{
	int arr[2]={10,20};
	
   	printf("%0x\n",&arr[0]);
   	printf("%0x\n",&arr[1]);
   	printf("%d\n",(char*)&arr[1]- (char*)&arr[0]);
}

- Kiran Kumar S April 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

declare diff variable as a void pointer like.....void *diff...it won,t throw error..

- RKC November 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It seems to be size of int, float, char etc.

- Peter December 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think error because how can u decler variable again and again in c

- priyambar kumar August 21, 2018 | Flag Reply


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