Flash
BAN USERInnovative free wheeling spirit
- 0of 0 votes
AnswersWrite a program to generate the anagrams of a word
- Flash in India| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswersWrite a program to reverse a string
- Flash in India| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswersWhat is the output of following code snippet?
- Flash in Indiapublic static void main(String[] args){ int[][] data = {{123},{4,5,6}}; int[][] copy = data.clone(); copy[0][0] = 100; System.out.println(data[0][0]); System.out.println(copy[0][0]); copy[1] = new int[]{300,400,500}; System.out.println(data[1][1]); System.out.println(copy[1][1]); }
| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswersWhat would be the output of below code snippet?
- Flash in Indiaclass Super { int index = 5; public void printVal(){System.out.println("Super");} } class Sub extends Super { int index = 2; public void printVal(){System.out.println("Sub");} } public class OopTest { public static void main(String[] args){ Super sup = new Sub(); System.out.println(sup.index + " "); sup.printVal(); } }
| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswersWhat would be the result of executing the below code snippet?
- Flash in Indiapublic class CountDownLatchDemo { public static void main(String[] args)throws InterruptedException { final CountDownLatch latch = new CountDownLatch(3); latch.countDown(); latch.countDown(); new Thread(){ public void run(){ try{ Thread.sleep(3000); }catch(InterruptedException ex){ ex.printStackTrace(); } latch.countDown(); }; }.start(); System.out.println("Before"); latch.await(); System.out.println("After"); } }
| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswersHow many elements will the set object in the below snippet will contain after the program executes to last print statement?
- Flash in Indiapublic class ShortSetTest { public static void main(String[] args){ Set shortSet = new HashSet(); for(short i = 0; i < 100; i++){ shortSet.add(i); shortSet.remove(i-1); } System.out.println(shortSet); } }
| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswerWhat would the output of the following snippet?
- Flash in Indiapublic class TrickyNum<X extends Number> { private X x; public TrickyNum(X x){ this.x = x; } private double getDouble(){ return x.doubleValue(); } public static void main(String[] args) { TrickyNum<Integer> a = new TrickyNum<Integer>(new Integer(1)); System.out.println(a.getDouble()); } }
| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswersWhat happens when you try to compile below class?
- Flash in Indiapublic class EnumTest { public EnumTest(){ System.out.println("Inside Constructor"); } public void printType(){ enum PrinterType {DOT,INK,LASER} } }
| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswersGiven below code snippet, what would be the output?
- Flash in Indiapublic class DemoThread extends Thread { private int x = 2; public static void main(String[] args) throws Exception { new DemoThread().method(); } public DemoThread(){ System.out.println("test"); x = 5; start(); } public void method() throws Exception { join(); x = x - 1; System.out.println(x); } public void run(){ System.out.println("run"); x *= 2; } }
| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java - 0of 0 votes
AnswersFor code snippet given below, what would be the output?
- Flash in IndiaString s8 = "AB"; String s9 = s8.intern(); if(s9.equals(s8) && s9 == s8) System.out.println("All Equal"); if(s9.equals(s8) && s9!=s8) System.out.println("equals Equal"); if(!s9.equals(s8) && s9==s8) System.out.println("== Equal"); if(!s9.equals(s8) && s9!=s8) System.out.println("Nothing Equal");
| Report Duplicate | Flag | PURGE
JP Morgan Java Developer Java
public static void permutation(String str) {
permutation("", str);
}
private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) System.out.println(prefix);
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
}
}
Credit: StackOverflow (via Introduction to Programming in Java)
- Flash May 14, 2017The output is :
5
Sub
Instance variables CANNOT be overridden in Java. In Java only methods can be overridden.
When you declare a field with the same name as an existing field in a superclass, the new field hides the existing field. The existing field from the superclass is still present in the subclass, and can even be used ... subject to the normal Java access rules.
@NoOne - Thanks for showing that. It's the case indeed.
Updated the Java code using a set for accurate results. It goes as -
- Flash June 17, 2017