File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programming Diversions and the fly likes Post your favorite trick questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Programming Diversions
Bookmark "Post your favorite trick questions" Watch "Post your favorite trick questions" New topic
Author

Post your favorite trick questions

Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
I thought it would be fun to post your favorite short trick questions where the anser seems like one thing if you read it fast, but it really is another.

Here is one that I like:

There are five apples and you take three. How may apples do you have left?



Eric
[ October 22, 2008: Message edited by: Eric Pascarello ]
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
If the vice president dies who will be the president?
Jhon Clay
Greenhorn

Joined: Jul 16, 2007
Posts: 16
The President himself will continue until and unless the president has killed the vice president and is in court and has been ordered a life sentence or a death sentence
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
Is the following sentence true or false?
"This sentence is not true."
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Originally posted by John Stone:
Is the following sentence true or false?
"This sentence is not true."


It is neither. It's "mu".
[ November 13, 2008: Message edited by: pete stein ]
Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
There are five apples and you take three. How may apples do you have left?

3 apples?


Potrzeba jest matka wynalazków
Patel Chintan
Ranch Hand

Joined: Mar 01, 2007
Posts: 80
Suppose there is a town with just one male barber; and that every man in the town keeps himself clean-shaven: some by shaving themselves, some by attending the barber. shaves only those men who do not shave themselves.

question: Does the barber shave himself?



Chintan Patel, SCJP1.5, http://datewithjava.blogspot.com
Jules Bach
Ranch Hand

Joined: Apr 28, 2008
Posts: 71
I remember this one from school, it actually left me very confused at the time:

The Puzzle:
Three friends share a hotel room together, and the bill is $25.
The three friends pay $10 each, which the bell boy takes to the hotel manager.

The manager keeps $25, and hands back $5 to the bell boy to return as change.

But the bell boy can't split $5 three ways, so he gives the friends $1 each and keeps $2 as a tip.

Each friend paid $10 and got $1 back. $10-$1 = $9

There were three of them 3 X $9 = $27

They paid a total of $27 for the room, and the bell-boy kept $2, so that adds up to $29.

Where did the other dollar go?
Maja Gralewska
Ranch Hand

Joined: Sep 26, 2008
Posts: 92
If each of them only paid $9 each then they paid the manager $27 in total, therefor $27 - $25 = $2 change which the bell bot takes as a tip.

right??
Pranay Narotam
Greenhorn

Joined: Oct 08, 2008
Posts: 23
that other dollar is divided between the 3 friends..


I AM A HUMAN BEING
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Um, no - Maja had it. There is no reason at all to add the bellboy's $2 to the $27 paid by the three friends. But if you subtract it, everything makes perfect sense.
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3275
Q. When designing your new code, what level of importance would you give to the following attributes?

-- Performance
-- Maintainability
-- Extendability
-- Ease of use
-- Scalability

500+ Java Interview Questions and Answers | Java job hunting know how & Java resumes
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 504

Solve this!
Following sentence is true!
Above sentence is false!

:?

Millions saw the apple fall, but Newton asked why.
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

arulk pillai wrote:Q. When designing your new code, what level of importance would you give to the following attributes?

-- Performance
-- Maintainability
-- Extendability
-- Ease of use
-- Scalability

What's the trick here?


A good workman is known by his tools.
Ganesh Rapolu
Greenhorn

Joined: Jan 05, 2010
Posts: 1
I am in migrating my project from mysql to oracle, which is in Java. when i am doing so I am

getting org.hibernate.type.SerializationException: could not deserialize.This is occurring

when iam reading from table where the field type in Data Base is BLOB and in the hibernate

mapping file type specified is java.security.PublicKey.

This exception is occurring only with oracle data base not with Mysql.I don't have any clue

to resolve this.Please help me.
Thanks in advance.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Ganesh, welcome to JR.

You're supposed to create a new thread for your question in Hibernate section. You just hijacked the thread that also in wrong forum.

Read http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
Here's a C programming tricky question that I like:


What's the result of compiling and running the above code ?
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1013
    
    3
Costi Ciudatu wrote:Here's a C programming tricky question that I like:


What's the result of compiling and running the above code ?


Unless there's some syntax gotcha I'm missing...

i divided by *p is: 1

I hope I don't seem too cocky when I classify that as "elementary" and not very "tricky" at all.
Costi Ciudatu
Ranch Hand

Joined: Oct 24, 2006
Posts: 74
There is a syntax gotcha you're missing (actually, everyone that I know has missed it, including myself; that's why I find it "tricky"):
in i/*p there's no space between / and *, therefore that's the beginning of a block comment that's never ended, so the compiler will complain.
Other than this, you're not "cocky" at all, the problem is indeed at most "elementary".
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1013
    
    3
Costi Ciudatu wrote:There is a syntax gotcha you're missing (actually, everyone that I know has missed it, including myself; that's why I find it "tricky"):
in i/*p there's no space between / and *, therefore that's the beginning of a block comment that's never ended, so the compiler will complain.
Other than this, you're not "cocky" at all, the problem is indeed at most "elementary".


D'oh! Not very tricky but i got it wrong anyhow. I looked right at the i/*p and wondered how it would parse those operators but completely missed that it was a comment delimiter.

C'mon, gimme another.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Currently, who is the senior Folk in Javaranch ?
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

write a c program to print hello world on screen,
your program should not contain ";"

I dont think this can be done in java though.


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Rizvan Asgarov
Ranch Hand

Joined: Jan 07, 2009
Posts: 451

seetharaman venkatasamy wrote:
Currently, who is the senior Folk in Javaranch ?

These are either Bear Bibeault or Marc Weber to my mind
But, Currently, who is the junior Folk in Javaranch ?


"Half of the science is to ask question"
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Rizvan Asgarov wrote:...who is the junior Folk in Javaranch ?

definately me !!
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

salvin francis wrote:write a c program to print hello world on screen,
your program should not contain ";"

I dont think this can be done in java though.



Rizvan Asgarov
Ranch Hand

Joined: Jan 07, 2009
Posts: 451

Such a question have been asked from me during my high school year:

If you come across with a new car that very very cheaper, so what you will think about it?
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1013
    
    3


What does this print?

Disclaimer: I don't have a C compiler with me, so I might have a syntax error.
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
The 2nd #include should probably be #define.
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1013
    
    3
Right, a #define. What does this code print? If someone has a C compiler and can compile and run this, please post the output here. Why did it print what it did?


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14432
    
  23

salvin francis wrote:
salvin francis wrote:write a c program to print hello world on screen,
your program should not contain ";"

I dont think this can be done in java though.




Almost, Salvin, but your example does not compile! Add { } after the if-statement and you're done. And indeed, this trick can't be translated to Java, because the expression inside the if-statement in Java is required to evaluate to a boolean, which is not so in C.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
(Titar is a name of bird in india)

Titar's front side 2 more titar.

Titar's back side 2 more titar.

Titar's upside 2 more titar.

Titar's downside 2 more titar.

Then tell me smarty, total no of titars ???



Jigar Naik


Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1013
    
    3
Jigar Naik wrote:(Titar is a name of bird in india)

Titar's front side 2 more titar.

Titar's back side 2 more titar.

Titar's upside 2 more titar.

Titar's downside 2 more titar.

Then tell me smarty, total no of titars ???



There might be a language issue here, but I think the number you're looking for is 2 + 2 + 2 + 2 + 1 = 9. That assumes one single titar is the subject of all four sentences.
Mazer Lao Tzu
Ranch Hand

Joined: Jan 20, 2010
Posts: 35
Patel Chintan wrote:Suppose there is a town with just one male barber; and that every man in the town keeps himself clean-shaven: some by shaving themselves, some by attending the barber. shaves only those men who do not shave themselves.

question: Does the barber shave himself?



No, he does not shave himself. He gets his wife to shave for him. Some go to barber and some shaving themselves does not mean that all men do one of those two options (After that, it is simple deduction to determine that he has a wife and makes her do it).



-- Mazer
Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 759
Suppose there is a town with just one male barber; and that every man in the town keeps himself clean-shaven: some by shaving themselves, some by attending the barber. shaves only those men who do not shave themselves.

question: Does the barber shave himself?






This sentence is unsatisfiable (a contradiction) because of the universal quantifier. The universal quantifier y will include every single element in the domain, including our infamous barber x. So when the value x is assigned to y, the sentence can be rewritten to , which simplifies to , a contradiction.



Wiki
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Jigar Naik wrote:(Titar is a name of bird in india)

Titar's front side 2 more titar.

Titar's back side 2 more titar.

Titar's upside 2 more titar.

Titar's downside 2 more titar.

Then tell me smarty, total no of titars ???



abbe question poonchna seekh.

its
Titar ke ek aage Titar
Titar ke ek peeche Titar
aage Titar,
peeche Titar,
bolo kitney Titar?


English Translation:

There is a Titar in front of a Titar
There is a Titar behind a Titar
Titar in front,
Titar behind,
How many Titar in all ?


Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1013
    
    3
salvin francis wrote:
There is a Titar in front of a Titar
There is a Titar behind a Titar
Titar in front,
Titar behind,
How many Titar in all ?


Let's see... I drew a diagram of the scenario described. I have a row of 15 titars side-by-side. In front of the fifth one is one more titar, so...
16. Final answer.

Let's take that concept a little farther...


At a small family gathering...
someone was there with their mother.
someone was there with their father.
someone was there with their sister.
someone was there with their brother.
someone was there with their son.
someone was there with their daughter.
someone was there with their aunt.
someone was there with their uncle.
someone was there with their neice.
someone was there with their nephew.
someone was there with their cousin.

How many people were there?
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Ryan McGuire wrote:
salvin francis wrote:
There is a Titar in front of a Titar
There is a Titar behind a Titar
Titar in front,
Titar behind,
How many Titar in all ?


Let's see... I drew a diagram of the scenario described. I have a row of 15 titars side-by-side. In front of the fifth one is one more titar, so...
16. Final answer.


Answer = 3

[Titar][Titar][Titar]

salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Ryan McGuire wrote:
At a small family gathering...
someone was there with their mother.
someone was there with their father.
someone was there with their sister.
someone was there with their brother.
someone was there with their son.
someone was there with their daughter.
someone was there with their aunt.
someone was there with their uncle.
someone was there with their neice.
someone was there with their nephew.
someone was there with their cousin.

How many people were there?


lets see,

If i am a guy - A
and i came with my sis - B
and my mom - C
and my Dad - D
and my Mon/Dad's Brother - E
and his wife - F
and his son/daughter - G.

1. someone was there with their mother. A to C
2. someone was there with their father. A to D
3. someone was there with their sister. A to B
4. someone was there with their brother. B to A
5. someone was there with their son. C or D to A
6. someone was there with their daughter. C or D to B
7. someone was there with their aunt. A or B to F
8. someone was there with their uncle. A or B to E
9. someone was there with their neice. E or F to B
10. someone was there with their nephew. E or F to A
11. someone was there with their cousin. A or B to G

Total = 7


not so sure
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
I think six is the minimum number of people involved. (Well, for, how shall I put it... traditional family trees. ) One solution is to take Salvin's answer and send E or F home. We need one or the other, but we don't need both. Or we can send them both home, if we can augment G with another cousin of opposite gender.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
salvin francis wrote:
Ryan McGuire wrote:
salvin francis wrote:
There is a Titar in front of a Titar
There is a Titar behind a Titar
Titar in front,
Titar behind,
How many Titar in all ?


Let's see... I drew a diagram of the scenario described. I have a row of 15 titars side-by-side. In front of the fifth one is one more titar, so...
16. Final answer.


Answer = 3

[Titar][Titar][Titar]


Two seems sufficient.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Post your favorite trick questions