Interview Question for Dev Leads Dev Leads


Country: India
Interview Type: In-Person




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

P is defined as constant so no guaranteed behavior of p++. In fact I am not sure if it will compile as you are attempting to modify value of a constant

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

P is defined as constant so no guaranteed behavior of p++. In fact I am not sure if it will compile as you are attempting to modify value of a constant

- Anonymous June 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 votes

Hey, do u know what is constant...what u thinking is totally wrong....

const char* p = "Test"; here data is const not the pointer so *p = 'A' //error
p++; // no issue

char const *p = "Test"; //this and above are same thing const u putting after * or before * makes sensde

char const * p is same as const char *p

the logic was here string literals stored in RO data section so this will available even after stack become empty so this programme prints "This is test string";

char arr[] = "This is test string"; if i do like this then its char *const arr= "This is test string";

in this case if i try ptr++ it fail

so we do like

int i = 0;

while(*(arr+i) != '\0')
{
cout<<*(arr+i);
i++;
}

but u can change data here not the pointer.....

char* const fun()
{
char arr[] = "This is test string";
return arr;
}

this is wrong thing once stack empty data lost and if u try access it u get seg fault...

- Kavita June 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

May be "while(!p != '\0') " is the issue and it should be "while(*p != '\0')??

Otherwise I think the code is right. As @CodeWithMe suggested, since the string is allocated in read only section, the memory will not be reused when then function returns. So I think the code is legit.

- Chid June 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes while( *p != '\0') it should be i write ! mistakely

now i modify fun

const char * fun()
{
return "This is test string";
}

is there any issue ....

there is no issue string literals go in data section so without pointing to a variable we can directly return them....

- Kavita June 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes I think there is no issue with the code. Let me know if I am missing something

- Chid June 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

nothing will display because of the loop

while(!p!='\0')

the associativity of ! operator is Right to left due to this (p!='\0') will execute first and give True than !True become False than the control will not come inside the loop. if it come inside the loop than generate an error, because the string "This is test string" do not have the NULL at last and the loop will never end

- Ashok Gowla June 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

while(!p != '\0') or while(*p != '\0') ?

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

Its while(*p != '\0')

- Kavita June 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

while(!p!='\0') nothing will display

while(*p !='\0') will generate error because the loop never end

- Anonymous June 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry for my previous answer

while(*p !='\0') work fine

- Anonymous June 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

ssss

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

The program will compile but the output will be empty. If you will replace !p with *p in while loop then it will give you the correct result. Otherwise !p means that the address that is assigned to p we apply a not operator that is if p is 0 only then !p is true and then the loop will execute. If you replace !p with p then the output will be an infinite set of characters as the '\0' is not defined to be any valid address.

- vgeek June 13, 2014 | 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