Q: Why methods like wait() , notify(), notifyAll() are present in Object class and not in Runnable Interface?
Ans : http://www.coderanch.com/t/234332/Threads-Synchronization/java/why-does-Object-class-have
Puzzles (Probability Questions)
Q: Find the probability of getting a number with 7 between 100 and 999 (both inclusive).
Ans : 252/900
between 100 to 200, there are 19 numbers ..
19*8 = 152
+ 100 numbers in b/w 700 to 799.
Q: What is the number of comparisons in the worst case to merge two sorted lists containing n elements each?
A. 2n
B. 2n-1
C. 2n+1
D. 2n-2
Ans : 2n-1
Cyclomatic complexity (or conditional complexity) is a software metric (measurement). It was developed by Thomas J. McCabe Sr. in 1976 and is used to measure the complexity of a program. It directly measures the number of linearly independent paths through a program's source code. For instance, if the source code contained no decision points such as IF statements or FOR loops, the complexity would be 1, since there is only a single path through the code. If the code had a single IF statement containing a single condition there would be two paths through the code, one path where the IF statement is evaluated as TRUE and one path where the IF statement is evaluated as FALSE.
Mathematically, the cyclomatic complexity of a structured program[note 1] is defined with reference to a directed graph containing the basic blocks of the program, with an edge between two basic blocks if control may pass from the first to the second (the control flow graph of the program). The complexity is then defined as:[2]
M = E - N + 2P
where
M = cyclomatic complexity
E = the number of edges of the graph
N = the number of nodes of the graph
P = the number of connected components.
The same function as above, shown as a strongly-connected control flow graph, for calculation via the alternative method. For this graph, E = 10, N = 8 and P = 1, so the cyclomatic complexity of the program is still 3.
An alternative formulation is to use a graph in which each exit point is connected back to the entry point. In this case, the graph is said to be strongly connected, and the cyclomatic complexity of the program is equal to the cyclomatic number of its graph (also known as the first Betti number), which is defined as:[2]
M = E - N + P
This may be seen as calculating the number of linearly independent cycles that exist in the graph, i.e. those cycles that do not contain other cycles within themselves. Note that because each exit point loops back to the entry point, there is at least one such cycle for each exit point.
For a single program (or subroutine or method), P is always equal to 1. Cyclomatic complexity may, however, be applied to several such programs or subprograms at the same time (e.g., to all of the methods in a class), and in these cases P will be equal to the number of programs in question, as each subprogram will appear as a disconnected subset of the graph.
It can be shown that the cyclomatic complexity of any structured program with only one entrance point and one exit point is equal to the number of decision points (i.e., 'if' statements or conditional loops) contained in that program plus one.[2][3]
Cyclomatic complexity may be extended to a program with multiple exit points; in this case it is equal to:
p - s + 2
where p is the number of decision points in the program, and s is the number of exit points
XOR of two numbers a and b can be found :
a^b or a|b & !(a&b)
Welcome Message
Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.
Top Navigation Menu
September 14, 2009
August 25, 2009
Three ants puzzle
There are three ants at the three corners of a regular
triangle. Each ant starts moving on a straight line toward
another, randomly chosen corner. What is the probability
that none of the ants collide?
2 comments:
Anonymous said...
25%. From the ant's perspective each can go left or right. There are 8 poss combinations - LLL, LLR, LRL, RLL, LRR, RLR, RRL, and RRR. You can also work that out by raising 2 (the number of choices) to the power of 3 (the number of choosers) = 2 x 2 x 2 = 8. In only 2 of these cases (LLL and RRR will there be no collisions, so 2 / 8 = 0.25
Secret Squïrrel
March 9, 2008 6:34 AM
Neeraj said...
There are two directions tomove for each ant
Thus total 2x2x2 =8 movements in different directions
out of these only two result in non - collisions of any of the ant - either all clockwise or all counter clockwise
Thus rest 6 result in at leaat 1 collission.
Thus probablility of non collision = 2/8=1/4=0.25
Reference : http://puzzles4you.blogspot.com/2008/03/three-ants.html
triangle. Each ant starts moving on a straight line toward
another, randomly chosen corner. What is the probability
that none of the ants collide?
2 comments:
Anonymous said...
25%. From the ant's perspective each can go left or right. There are 8 poss combinations - LLL, LLR, LRL, RLL, LRR, RLR, RRL, and RRR. You can also work that out by raising 2 (the number of choices) to the power of 3 (the number of choosers) = 2 x 2 x 2 = 8. In only 2 of these cases (LLL and RRR will there be no collisions, so 2 / 8 = 0.25
Secret Squïrrel
March 9, 2008 6:34 AM
Neeraj said...
There are two directions tomove for each ant
Thus total 2x2x2 =8 movements in different directions
out of these only two result in non - collisions of any of the ant - either all clockwise or all counter clockwise
Thus rest 6 result in at leaat 1 collission.
Thus probablility of non collision = 2/8=1/4=0.25
Reference : http://puzzles4you.blogspot.com/2008/03/three-ants.html
Three ants puzzle
There are three ants at the three corners of a regular
triangle. Each ant starts moving on a straight line toward
another, randomly chosen corner. What is the probability
that none of the ants collide?
25%. From the ant's perspective each can go left or right. There are 8 poss combinations - LLL, LLR, LRL, RLL, LRR, RLR, RRL, and RRR. You can also work that out by raising 2 (the number of choices) to the power of 3 (the number of choosers) = 2 x 2 x 2 = 8. In only 2 of these cases (LLL and RRR will there be no collisions, so 2 / 8 = 0.25
There are two directions to move for each ant
Thus total 2x2x2 =8 movements in different directions
out of these only two result in non - collisions of any of the ant - either all clockwise or all counter clockwise
Thus rest 6 result in at leaat 1 collission.
Thus probablility of non collision = 2/8=1/4=0.25
Reference : http://puzzles4you.blogspot.com/2008/03/three-ants.html
triangle. Each ant starts moving on a straight line toward
another, randomly chosen corner. What is the probability
that none of the ants collide?
25%. From the ant's perspective each can go left or right. There are 8 poss combinations - LLL, LLR, LRL, RLL, LRR, RLR, RRL, and RRR. You can also work that out by raising 2 (the number of choices) to the power of 3 (the number of choosers) = 2 x 2 x 2 = 8. In only 2 of these cases (LLL and RRR will there be no collisions, so 2 / 8 = 0.25
There are two directions to move for each ant
Thus total 2x2x2 =8 movements in different directions
out of these only two result in non - collisions of any of the ant - either all clockwise or all counter clockwise
Thus rest 6 result in at leaat 1 collission.
Thus probablility of non collision = 2/8=1/4=0.25
Reference : http://puzzles4you.blogspot.com/2008/03/three-ants.html
How many such points on the earth
Q Find all the points on the globe from which if you travel 1km south, then 1 km east and then 1 km north; you end up at the same point from which you started.
Hints:
1. There is an infinite amount of such points.
2. The answer is 100% true, no tricks - so don't waste your time searching for any.
Ans : Half the answer is this:
There is some place on the globe, where the perimeter of the earth is exactly 1 km. If you start walking from any point which is 1 km north of that ring, and walk 1 km south, you'll end up on the 1 km perimeter. You'll then go 1 km east which will take you all the way around the globe and then you'll go 1 km north and end up in the starting point.
This is the answer which many people give, but it's not full. It gives us a stip (or a ring) of points with this property, but there is also an infinite amount of such strips (or rings):
The full answer, as given by UnToha, is that the perimeter of the walk around the globe doesn't have to be 1 km. It can be any factor of 1 km so that when walking 1 km east, you'll walk some K times around the globe (where K has to be an integer), thus making several complete trips around the globe. This gives us an infinite amount of rings, each having an infinite amount of points, all of which have this strange property.
reference : http://neworder.box.sk/forum/viewtopic.php?id=40242
Hints:
1. There is an infinite amount of such points.
2. The answer is 100% true, no tricks - so don't waste your time searching for any.
Ans : Half the answer is this:
There is some place on the globe, where the perimeter of the earth is exactly 1 km. If you start walking from any point which is 1 km north of that ring, and walk 1 km south, you'll end up on the 1 km perimeter. You'll then go 1 km east which will take you all the way around the globe and then you'll go 1 km north and end up in the starting point.
This is the answer which many people give, but it's not full. It gives us a stip (or a ring) of points with this property, but there is also an infinite amount of such strips (or rings):
The full answer, as given by UnToha, is that the perimeter of the walk around the globe doesn't have to be 1 km. It can be any factor of 1 km so that when walking 1 km east, you'll walk some K times around the globe (where K has to be an integer), thus making several complete trips around the globe. This gives us an infinite amount of rings, each having an infinite amount of points, all of which have this strange property.
reference : http://neworder.box.sk/forum/viewtopic.php?id=40242
java.lang.String
Q : If we create a custom class String in the directory structure like java.lang and import it in our class. Which version of String class would be imported : Custom class or Standard Library class ??
Ans : Java has been coded such that it would pick its Standard library class only even if we define class with same name and same package structure.
So the class might compile but would give a runtime exception if we try to invoke a method which we defined in our custom class n which is not present in the std library class.
e.g.
package java.lang;
public final class String {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public String toString(){
return "Custom Java Class";
}
public static void myMethod(){
System.out.println("My custom java class");
}
}
and a class
import java.lang.String;
public class StringTesting {
/**
* @param args
*/
public static void main(String[] args) {
String str = new String();
java.lang.String.myMethod();
}
}
the code will compile coz at the compile time , its referring to the custom java class , but at run time, the standard java class would be loaded and since it doesnt have myMethod(), it would generate run time exception.
This is because when our custom java.lang.String class is referenced, the class loader checks if this has already been loaded and it finds that a class with the same fully classified class name has already been loaded (which is the one provided by java) and so does not load the custom java.lang.String class
Ans : Java has been coded such that it would pick its Standard library class only even if we define class with same name and same package structure.
So the class might compile but would give a runtime exception if we try to invoke a method which we defined in our custom class n which is not present in the std library class.
e.g.
package java.lang;
public final class String {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public String toString(){
return "Custom Java Class";
}
public static void myMethod(){
System.out.println("My custom java class");
}
}
and a class
import java.lang.String;
public class StringTesting {
/**
* @param args
*/
public static void main(String[] args) {
String str = new String();
java.lang.String.myMethod();
}
}
the code will compile coz at the compile time , its referring to the custom java class , but at run time, the standard java class would be loaded and since it doesnt have myMethod(), it would generate run time exception.
This is because when our custom java.lang.String class is referenced, the class loader checks if this has already been loaded and it finds that a class with the same fully classified class name has already been loaded (which is the one provided by java) and so does not load the custom java.lang.String class
August 24, 2009
Command to see decompiler output
javap -s -verbose -l com.xyz.collection.IteratorException >op.txt
Labels:
Decompiler,
java
Nice Question
class Test {
static int m(byte a, int b) { return a+b; }
static int m(short a, short b) { return a-b; }
public static void main(String[] args) {
System.out.println(m(12, 2));// compile-time error
}
}
static int m(byte a, int b) { return a+b; }
static int m(short a, short b) { return a-b; }
public static void main(String[] args) {
System.out.println(m(12, 2));// compile-time error
}
}
Labels:
interview,
java,
OOPS,
Overloading
A very good interview question (OverridingTest ) --> Static Polymorphism
Q: What would be the output of the following program ?
public class OverridingTest {
//~ Methods --------------------------------------------------------------------------------------------------------
public static void abc(Object str) {
System.out.println("Object");
}
public static void abc(String str) {
System.out.println("String");
}
public static void main(String[] args) {
// Example of Static polymorphism
abc(null);
abc((Object) null);
String[] str = { null, null, null, null };
for (String st : str) {
abc(st);
}
}
}
Ans : String
Reason : http://www.javafaq.nu/java-article383.html
public class OverridingTest {
//~ Methods --------------------------------------------------------------------------------------------------------
public static void abc(Object str) {
System.out.println("Object");
}
public static void abc(String str) {
System.out.println("String");
}
public static void main(String[] args) {
// Example of Static polymorphism
abc(null);
abc((Object) null);
String[] str = { null, null, null, null };
for (String st : str) {
abc(st);
}
}
}
Ans : String
Reason : http://www.javafaq.nu/java-article383.html
Labels:
interview,
java,
Static Polymorphism
Subscribe to:
Posts (Atom)