Infosys Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

int a;
int *pa = &a;
int *ps = pa+1;
int sz = (int)ps - (int)pa; // the size of the variable @a

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

your answer is wrong ,which book or crap r u following????
with your code answer is 1 for integer ......... bcoz of pointer arithmetic......

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

@deepak
Already fixed - just missed the cast to type int of the pointers.

- ashot madatyan June 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The logic is absolutely correct, but what I think (long int ) as a cast would be better, as the address, for a 64 bit machine would be bigger than the limit of int

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

(int)(&a+1)-(int)&a

- Anonymous December 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#define size_of( variable ) ((size_t)(&(variable)+1)-(size_t)(&(variable)))

- Angamuthu G June 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Where (size_t is a cast of unsigned long integer type) ?

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

#include <stdio.h>

#define mysizeof(type) (int)(&type+1)-(int)(&type)

struct temp
{
char *a;
int b;
float d[4];

};
int main()
{
int a;
char *p;
char b[5];
int d[10];
float c;
struct temp temp1;
printf("Size of p = %d \n",(int)(&a+1)-(int)(&a));
printf("Size of a = %d \n",sizeof(a));
printf("Size of a = %d \n",mysizeof(a));
printf("Size of p = %d \n",mysizeof(p));
printf("Size of b = %d \n",mysizeof(b));
printf("Size of d = %d \n",mysizeof(d));
printf("Size of c = %d \n",mysizeof(c));
printf("Size of Struct Size temp1 = %d \n",sizeof(temp1));
printf(" MyStruct Size = %d \n",mysizeof(temp1));

}
o/p:-
Size of p = 4
Size of a = 4
Size of a = 4
Size of p = 4
Size of b = 5
Size of d = 40
Size of c = 4
Size of Struct Size temp1 = 24
MyStruct Size = 24

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

#include<stdio.h>

#define any_size(any) (char*)(&any)-(char*)((&any)-1)

void main()
{
char c; //any type of variable char c;
printf("%d",any_size(c));
getch();
}

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

It can be found through following ways:
1. pointer airthmatic that is take pointer of same type, increment it and get difference between 2 memory addresses.
2. by storing largest value. Assume its integer and try to store number 2^n starting from 0. Compare stored value with original number.
If it is same, icrement number otherwise stop and n is the bit size of this data type.
3. Through shift operation. Intially set variable to -1. Then do right shift untill you getback number equal to 0.
Count how many times you shifted. This is the size of variable in bits.

- sanjay.kr.maurya June 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sol2-3 doesn't work for user defined type - struct, class

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

int a=(char *) ( &variable+1 ) - (char *)( &variable );

variable can be any data type i.e(Basic, Derive, Userdefine(struct,enum) ).

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

size of variable = Runtime.getRuntime().totalMemory - Runtime.getRuntime().freeMemory;

- Shravan kumar October 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

yes,,,,5ts g66d

- rav5 October 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void main()
{
	int x[2];
	long int xa[2];
	float y[2];
	double z[2];
	char *a,*b;
	a=(char *)&x[0];
	b=(char *)&x[1];
	printf("size of int=%d\n",b-a);
	a=(char *)&xa[0];
	b=(char *)&xa[1];
	printf("size of long double=%d\n",b-a);
	a=(char *)&y[0];
	b=(char *)&y[1];
	printf("size of float=%d\n",b-a);
	a=(char *)&z[0];
	b=(char *)&z[1];
	printf("size of double=%d\n",b-a);

}

- kathrotiyanimesh July 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Instead of doing all this why can't we take the hexadecimal value of the character and do XOR operation with all the numbers from 0 to 256 and whichever value gives 0 return that value.

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

Let X be the Data Type of the variable.
#include<stdio.h>
int main()
{
X array[2]={a,b};//a and b can have any value according to the data type
int size=&X[1 ]- &X[0];
printf("%d",size);
return 0;
}

- Soumyadeep Biswas September 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
main()
{
int *p=0;
p++;
printf("%d",p); //size of int, u can find size of other data types in the same way
}

- Pranav July 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

u can find it by luking at itz datatype and if in case u hv forgotten d datatype den u may as well use a bitwise operation like shiftleft bitwise on d variable until it becums itself.
hope dis may b f sum use to u.....:-)

- Surbhi June 20, 2012 | 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