Interview Question


Country: India




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

Short Circuiting ....

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

what do u meant by short circuiting ??? what abt prcedence of operators??

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

z=++x||++y&&++z;
In the above line precedence of && > precedence of ||, hence its equivalent to
z=++x||(++y&&++z );

But once the ++x evaluates to 2 i.e. true, no need to further process,
Hence the o/p 2 1 1

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

But tell me if unary operators have the highest precedence among all (actually second highest)....then why here we are bothering about "&&" or "||"......first unary operators should be evaluated.... according to ur explanation answer is right but how ??? why we are not evaluating unary operators first ????
second thing in denis and ritchie ANSI C it is given that if their are to expressions f(x) and g(x) in a bigger expression then its not fixed that which of the "f()" or "g()" will be evaluated first.........what exactly it means and if it is so then why we bother about precedence and associativity in evaluating an expression.... clarify it with example

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

i support mr.deepak since precedence of unary operator is greater than of logical && and logical || operator at first x,y,z values are incremented then or opertion sholud be performed in that way answer should be performed o/p:2,2,1
but why o/p is 2,1,1 when i executed

- script7vineel December 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I got the following output :
2 1 1

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

yes answer is correct but why ??

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

@deepakyadav1830,
In the expression, Z = ++x||++y&&++z
the answer is coming 2,1,1 due to the OR property, here x is > 0, so according to OR(1 || something) gives the o/p 1 so it never goes to execute "something" .

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

I have the same doubt as deepak yadav. Its true that their would be short circuiting. But still unary operators come first and y and z would be incremented

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

Do they have engineers writing code like this?

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

i have the same doubt as deepak yadav. If we are calculating ++x first, then why not ++z and ++y

- coder_08 July 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what "deepakyadav" is telling (i.e regarding f(), g() ) is correct if they are as below:-
x = f() * g();// order of f()/g() evaluation is not defined.
OR
x(f(),g())

BUT in the case of an expression having "||" "&&" and "," the order of evaluation is defined and it is "left->right".

- Deep July 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

z=++x||++y&&++z

Short circuiting is fine.
I wanna ask that precedence of ++ is greater than =
so ++x will execute first then it will assigned to z
so z will be 2

Or this is the condition

z=++x||++y&&++z

here left of the || is taken as one component, as x is > 0 it skips the assignment left z as 1. after that z is incremented to 2.

- Psycho August 30, 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