Samsung Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

the fun()

returns the how many letters processed in printf . result is 5 including \n.

- karthikeyan February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 4 vote

Good Question..
But as far as I know, return value is only the compiler check. It has nothing to do with the call stack...

Infact call stack has space reserved for :-
1> All function parameters.
2> All Variables within that scope
3> Any Variables which are used to call for another subroutine.

I feel its a tricky question,and even if you do something stupid as this.. Your code will not compile " Function should return a Value " or " Parameter Mismatched " in case of C / C++ . So call stack itself will not generate if the code is not able to compile.

- hprem991 November 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, in C++ this might no even compile, but in standard gcc that we use, this will surely compiles and gives result?.

I did some reading on this over google, and figured out, my previous answer to him that this is undefined was correct.

- Varun November 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yep. I wasn't sure about the answer either when I read this question, though I suspected it was undefined. But we seem to be reaching a consensus in this thread that it's UB.

I might note that -Wall would almost certainly catch this, and you might score points with your interviewer for mentioning that you compile with -Wall.

- eugene.yarovoi November 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, it wont compile.

- Sriramulu Appana April 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

think of what will happen when a value is returned :
the value returned with be saved in one or two register or stack .
if register is used , say R0 on ARM processor, with your code , It will be the value last time used , I guess it is some value in we used in printf for crap.

so %d could be any thing.

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

As far as i Know printf function always return the length of the string(C Programing by Ritchie), so if we dont return anything from the function then it takes the return type of the last statement which is length of string in fun(), on printing it in main will print the length...also main cant return void in gcc,g++ and most compilers but it hardly matters as it has to do nothing with the question.

The return type of printf function can be checked using nested printf in the main.

- Puran December 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

hi puran,

u are right, i am getting the length of string.

- deep.rnj February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

When a function call is made, a stack frame is created. As part of this frame, space for return value is also allocated. So, if nothing is explicitly returned; this space is never filled and hence whatever be in that stack at that memory location gets considered as return value.

Thanks,
Laxmi

- Laxmi Narsimha Rao Oruganti November 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey Laxmi,
thnx for pointing it out.
But, are u sure, do we have a space for return value on the stack for a function??

If I am correct, we don't have return value stored on stack, rather the value of EIP register is considered as return value for the function.

I might be wrong, but I am not convinced with your opinion.
I tried it just on my unix, and saw the value continues to be same for all instances of the program??

Also, gdb never showed any place where the return value is stored.

- Varun November 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The usual x86-32 calling convention is to use EAX as the return value, so for many compilers, printf will store 5 there and fun() will implicitly return 5 as well. The return value is not required by standard to be defined. Also, compilers are not required to accept void main().

- Anonymous November 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

(Note that EAX is a caller save, so no store need happen in fun after printf is called.)

- Anonymous November 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

From WikiPedia: h t t p://en.wikipedia.org/wiki/Call_stack
"Returning from the called function will pop the top frame off of the stack, perhaps leaving a return value."

Note that you have to search for 'return value' ('return address' is another data we store in call stack frame).

Thanks,
Laxmi

- Laxmi Narsimha Rao Oruganti November 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Laxmi, you are damn right, girl.

- God November 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Storing the parameters/local variables/return value on the stack frame is ONE technique, the compiler is free to choose to use registers, and many does, especially if optimization is for space.

- Selmeczy, Péter December 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

It's not guaranteed this behavior due to certain optimizations.
For example, on my machine compiled with gcc 4.5 with -O3 option gives 0.
That's because f is inlined and in the absence of a return value in f, 0 is pushed as second argument to printf instead of eax, as illustrated in assembler code below

...
main:
	pushl	%ebp
	movl	%esp, %ebp
	andl	$-16, %esp
	subl	$16, %esp
	movl	$.LC0, (%esp)
	call	printf
	movl	$0, 4(%esp) ;<-- here
	movl	$.LC1, (%esp)
	call	printf
	leave
	ret

- Anonymous November 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sounds reasonable, and thnx for pointing, I could have very well asked for dis-assemble code to figure this out then.
Yes, based on few pages over net, looks its undefined.

- Varun November 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

As far as i think..In this case we still have value of return address of next instruction Which is printf();Though Program will throw a Warning like "Fun() should return a value"...But it is a totally Compiler Specific...

- Rex November 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since, "printf" function always returns the number of characters printed. So func() returns '5'.

- Raja Sekhar March 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

i ran the code on visual studio,it is givng reslt 5

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

<pre lang="" line="1" title="CodeMonkey18673" class="run-this">/* The class name doesn't have to be Main, as long as the class is not public. */
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}jhhjgjkgvjvv

</pre><pre title="CodeMonkey18673" input="yes">
bbmb</pre>

- Anonymous November 11, 2011 | 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