CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Our Services
Get 9,000 Interview Questions & Answers in an eBook.




Get it now !!
Send your Resume to 6000 Companies

Search Interview Questions

Question: void main()
{
int x=5;
printf("%d,%d,%d
",x,x<<2,x<<2);
}
what is the ans. of this question and how?
please explain it.

Answer: The answer would be

5 20 20

as x = 5 so first %d gives 5

second %d will take value of x after left shifting it twice, and shifting is done after converting the values to binary, binary value of 5 (000101)will be left shifted twice to make it binary 20(0010100), so x<<2 is 20

and as left shifting does not effect the original value of x its still 5 so third %d will also show 20.

Thanks



Category Database Interview Questions & Answers - Exam Mode / Learning Mode
Rating (0.7) By 621 users
Added on 8/18/2013
Views 5220
Rate it!
Question: void main()
{
int x=5;
printf("%d,%d,%d
",x,x<<2,x<<2);
}
what is the ans. of this question and how?
please explain it.

Answer:

The answer would be

5 20 20

as x = 5 so first %d gives 5

second %d will take value of x after left shifting it twice, and shifting is done after converting the values to binary, binary value of 5 (000101)will be left shifted twice to make it binary 20(0010100), so x<<2 is 20

and as left shifting does not effect the original value of x its still 5 so third %d will also show 20.

Thanks Source: CoolInterview.com

Answered by: Vishwas Rathi | Date: 8/18/2008 | Contact Vishwas Rathi Contact Vishwas Rathi

Look..........
Inside printf function the expression are evaluated Right to Left.
Hence first rightmost x<<2 will be executed
then x<<2 in middle
and
at last x.
<< is the left shift operator which shifts bits of a number to left.
So value of x<<2 when x=5 will be
20; value of x changes to 20
Again x<< 2 , when x=20 makes x=80;
So values will be pinted in output screen as:
80,80,20 Source: CoolInterview.com

Answered by: Pranjal Gururani | Date: 8/18/2008 | Contact Pranjal Gururani Contact Pranjal Gururani

answer is
5,20,20

Since << is a left shift operator, and left shifting by one bit multiplies the number by 2, left shifting by 2 bits multiplies the number by four. However the original number is not assigned withe the new value.

Hence 5<<2 = 20
However teh value of x doesnt not change..
Hence the expression is like,
print("%d,%d,%d",5,5<<2,5<<2)
which will display 5,20,20 Source: CoolInterview.com

Answered by: ram | Date: 8/19/2008 | Contact ram Contact ram

5 is 101
5<<1 left shift by 1 1010 i.e 10
5<<2 left shift by 2 10100 i.e 20 Source: CoolInterview.com

Answered by: sivaji | Date: 8/26/2008 | Contact sivaji Contact sivaji

ans:5,20,20
because the << operator gives the product of the left integer(i.e x=5) and the maximum number of possible binary numbers that can be obtained for the given right integer.(i.e 2)

using two binary bits we can create four different combinations.If the given number is 3 then the number of possible combinations is 2^3=8.
so the expression x<<2 gives X*2^2.

lly The expression x<<3 gives x*2^3.

In the same way the expression x>>2 gives x/2^2
Source: CoolInterview.com

Answered by: Mugilvannan | Date: 9/14/2008 | Contact Mugilvannan Contact Mugilvannan

Look..........
Inside printf function the expression are evaluated Right to Left.
Hence first rightmost x<<2 will be executed
then x<<2 in middle
and
at last x.
<< is the left shift operator which shifts bits of a number to left.
So value of x<<2 when x=5 will be
20; value of x changes to 20
Again x<< 2 , when x=20 makes x=80;
So values will be pinted in output screen as:
80,80,20


Source: CoolInterview.com

Answered by: sorabh | Date: 6/5/2009 | Contact sorabh Contact sorabh

i want to improve panchal gururani's answer........bco'z no one is absolutely right.

Inside printf function the expression are evaluated Right to Left.
Hence first rightmost x<<2 will be executed and evaluate 20 but x is not assigned 20,it is still 5.
then x<<2 in middle print again evaluate 20,not assigning 20 to x.
and
at last x.
Now it prints from left to right 5 20 20 Source: CoolInterview.com

Answered by: Sandeep Kumar | Date: 10/12/2009 | Contact Sandeep Kumar Contact Sandeep Kumar

answer is 80 80 20.
printf function prints from right to left. Source: CoolInterview.com

Answered by: naina modi | Date: 11/1/2009 | Contact naina modi Contact naina modi


If you have the better answer, then send it to us. We will display your answer after the approval

Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.

Post your answer here

Inform me about updated answers to this question.
Related Questions
View Answer
What is difference between stack and queue?
View Answer
What is the difference between oracle and sql?
View Answer
Can you take Image Backups using RMAN?

20. Can you use Back upsets created by RMAN with any other utility?
View Answer
How grid work. is it possible grid in 9i. how ?
View Answer
Why we use g with 10g .
View Answer
I have active redo log is it possible increase size and how?
View Answer
What is undo /rollback segment how you resize this ?
View Answer
What is steps of cloning?
View Answer
Any inbuilt utility of oracle available which make database mirroring ?
View Answer
What is lock and latch?
View Answer
My transaction take 30 min i want if my transaction data written in table after each 5 min . If transaction commit or rollback then do according work . this is done through pl/sql how?
View Answer
I want to write data from table to flat file this is possible through pl/sql how ?
View Answer
If i have unique key this is possible to create another unique key on table .
View Answer
If i have primary key this is possible to create another primary key on table.
View Answer
What is primary & unique key which type index oracle create when we make those key ?
View Answer
What is view is it possible to create index on view on view & how ?
View Answer
What is row changing how u eliminate ?
View Answer
How we determine index require rebuilding ?
View Answer
How we determine index require rebuilding ?
View Answer

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View All Database Interview Questions & Answers - Exam Mode / Learning Mode




India News Network
Latest 20 Questions
Payment of time- barred debt is: (a) Valid (b) Void (c) Illegal (d) Voidable
Consideration is defined in the Indian Contract Act,1872 in: (a) Section 2(f) (b) Section 2(e) (c) Section 2(g) (d) Section 2(d)
Which of the following is not an exception to the rule, "No consideration, No contract": (a) Natural love and affection (b) Compensation for involuntary services (c) Completed gift (d) Agency
Consideration must move at the desire of: (a) The promisor (b) The promisee (c) The promisor or any other party (d) Both the promisor and the promisee
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal
Cache = 0.046875 Seconds