import java.util.*; public class Elway { public static void main(String r[]) { ArrayList[] ls = new ArrayList[3]; for(int i=0; i<3; i++) { ls[i] = new ArrayList(); ls[i].add("a" + i); } Object o = ls; do3(ls); for(int i=0; i<3; i++) //insert code here } static Object do3(ArrayList[] a) { for(int i=0; i<3; i++) a[i].add("e"); return a; } } The code above compiles fine with: a. `System.out.println(((Object[])o)[i] + " ")` b. `System.out.println(((ArrayList[])o)[i] + " ")` but not with c. `System.out.println(o[i] + " ")` What is the reason? When we assigned ls to o, didn't Object o become an array?

Member Avatar
Member Avatar
+0 forum 4

List<? extends Integer> list = new ArrayList<Integer>(); for (Integer element : list) { System.out.println(element); List<? super Integer> list = new ArrayList<Integer>(); for (Integer element : list) { System.out.println(element); For the first three lines as Case 1 using extends it compiles fine. But for Case 2 of next three lines, it says incompatible types. Found Object, required Integer. My confusion is *? extends Integer* being **anything that is an Integer or sub of it** works. But why in Case 2 where *? super Integer* means **anything that is an Integer or a superclass of Integer** doesn't work when it's elements ar …

Member Avatar
Member Avatar
+0 forum 7

StringBuffer sb = new StringBuffer(new String("Hello")); //Case 1 StringBuffer sb1; //Case 2 String s = new String("Hello"); // Case 2 sb1=s; //Case 2 StringBuffer sb3; //Case 3 sb3 = new String("Hello");//Case 3 Case 1 compiles fine and outputs *Hello* but Case 2 and Case 3 gives compiler error saying incompatible types. Why is it so?

Member Avatar
Member Avatar
+0 forum 3

public class GenericDemo<E extends GenericDemo> { E innerE; public E doStuff(E e, GenericDemo<E> e2) { //insert code here } public E getE() { return innerE; } } The options are: 1. return e 2. return e2.getE() 3. return e2 4. return e.getE() Option (1) and (2) compile fine. Option (3) and (4) give compile error. 1. Precisely, for `e2` says, **incompatible types. Found: GenericDemo<E>. Required: E.** My doubts is that, for e2, isn't E that is required, part of GenericDemo<E> which is e2's type ? 2. For `return e.getE()` says, **incompatible types. Found: GenericDemo. Required: E.** Here I'm first confused …

Member Avatar
Member Avatar
+0 forum 8

foo |-test |-xcom |-A.class |-B.java And two files: package xcom; public class A{} package xcom public class B extends A{} Given above directory structure where root dir is foo. Class B extends A. Question is: Which allows B.java to compile? The answer is: Set the current dir to *test* then invoke `javac -cp . xcom/B.java` My doubt is why the option below won't work? 1. Set the current dir to *xcom* then invoke `javac -cp . B.java` ? Isn't this looking for A.class in xcom because of the (.) ? 2. By setting test as current dir, and using (.) …

Member Avatar
Member Avatar
+0 forum 10

public class IC1 { private int x = 3; static int y=4; class Inner { public void inmet() { System.out.println(x + " " + y);} } public void do() { Inner i = new Inner(); i.inmet(); } public static void main(String r[]) { IC1 ic = new IC1(); ic.do(); } } ![Errors](/attachments/large/3/Errors.PNG "Errors") Seems like a pretty straight forward program where in my knowledge I have followed all rules of Inner Class. Checked all {} and ; also. What could be the reason for the errors?

Member Avatar
Member Avatar
+0 forum 6

class Account { Long accNum, pwd} public class Banker { public static void main(String r[]) { new Banker().go(); // do more stuff } void go() { Account a1 = new Account(); a1.accNum = new Long("1024"); Account a2 = a1; Account a3 = a2; a3.pwd = a1.accNum.longValue(); a2.pwd = 4455L; } } It says in solution when line 5, "//do more stuff" is reached, four objects are eligible for GC. Three I know is Banker(), Account() and Long("1024"). My doubt is which is the fourth one? It says in solution that "Account object has two *Long* objects associated with it". So …

Member Avatar
Member Avatar
+0 forum 2

<? super Animal> means animal or it's superclass only. Then why inside the method it's allowing to add Dog object even though it's a subclass of Animal? This program compiles and runs! import java.util.*; class Cat {} class Animal{Animal() {System.out.println("Animal constructor");}} class Dog extends Animal{Dog() {System.out.println("Dog constructor");}} public class GenericDemo5 extends Animal{ public static void main(String r[]) { List l1 = new ArrayList(); //can add anything since no type here l1.add(new Dog()); l1.add(new Animal()); l1.add(30); met(l1); System.out.println(l1); } public static void met(List<? super Animal> l2) { l2.add(new Animal()); l2.add(new Dog()); System.out.println(l2); }} Even with Generic Instantiation(shown below) same output!! This …

Member Avatar
Member Avatar
+0 forum 11

import java.util.*; public class GenericDemo4 { public static void main(String r[]) { Set s1 = new HashSet(); s1.add(0); s1.add("1"); dostuff(s1); } static void dostuff(Set<Number> s) { do2(s); Iterator i = s.iterator(); while(i.hasNext()) System.out.println(i.next() + " "); Object [] oa = s.toArray(); for(int x = 0; x<oa.length; x++) System.out.println(oa[x] + " "); System.out.println(s.contains(1)); } static void do2(Set s2) { System.out.println(s2.size() + " "); } } On line 10 there is a type-safe set as a parameter for the method dostuff and it accepts non generic Set s1. How can it not flag an error for one of it's element being a …

Member Avatar
Member Avatar
+0 forum 7

class Sample1 { Sample1() {System.out.println("Sample1 const"); } void doStuff(){System.out.println("sample1 dostuff"); } } class Sample2 extends Sample1 { Sample2() {System.out.println("Sample2 const"); } } public class Sample3 extends Sample2 { Sample3() {System.out.println("Sample3 const"); } public static void main(String r[]) { new Sample3().go(); } void go() { super.doStuff(); } } The output is: Sample1 const Sample2 const Sample3 const sample1 dostuff My doubt is that super.doStuff() should invoke Sample2 method doStuff() if present. Why has it "propagated" upwards? I thought that happened only with no-arg super() being put implicitly (if not present) as first line of constructor.

Member Avatar
Member Avatar
+0 forum 4

public class Thread7 implements Runnable { public static void main(String r[]) { Thread t = new Thread(new Thread7()); t.start(); public void run() { System.out.println("C"); System.out.println("D"); } System.out.println("A"); try { t.join(); System.out.println("B"); } catch(Exception e){} } } Query1: On compiling this program I get "illegal start of expression" error pointing to public void run. But when I remove that block of code and place it outside main() the program compiles and runs successfully. What is the rule behind it? Query2: After I place run() outside main() the output is ACDB. What I don't understand is after t.start() is invoked shouldn't the …

Member Avatar
Member Avatar
+0 forum 1

Given: Sour extends Blue extends Pan `Pan p4 = new Sour();` Then set of 6 new statements given and asked to choose which ones will compile. They are: 1. `Pan p5 = p4;` 2. `Pan p6 = (Blue)p4;` 3. `Blue b2 = (Blue)b4;` 4. `Blue b3 = (Sour)p4;` 5. `Sour s1 = (Blue)p4;` 6. `Sour s2 = (Sour)p4;` Answer: 5th one won't compile. Rest all will compile and run without exception. Doubt: As a beginner I have understood that LHS should be usually a super class and **RHS should be subclass if downcast not required** like shown in original statement …

Member Avatar
Member Avatar
+0 forum 6

import java.io.*; public class Mac extends Laptop { public static void main(String r[]) { new Mac().crunch(); } //insert code here } class Laptop { void crunch() throws IOException { } } The question is simple. Insert a code that will make the program compile. Answers are: `void crunch()` or `void crunch() throws RuntimeException {}` Then a note was given for an incorrect option that `void crunch() throws FileNotFoundException {}` would be correct if crunch() invocation on line 5 was either handled or declared. Doubt: I understood the correct options. I did not understand how the invocation has to be handled …

Member Avatar
Member Avatar
+0 forum 1

Is it mandatory to handle an exception that is thrown using the `throw` keyword? If so is there a separate rule for Checked and Unchecked Exception, as in who should be mandatorily caught? public void met() { throw new ArithmeticException

Member Avatar
Member Avatar
+0 forum 1

int [] ia1 = {1,2,3}; int [][] ia2; Object o = ia1; ia2 = new int [3][3]; ia2[0] = (int[])o; ia2[0][0] = (int[])o; Answer is "Compilation fails in last line" Explanation: Arrays are objects, and that each array dimension is a separate type. So, for instance, ia2 is of type "two dimensional int array", which is a different type than ia1. Last line attempts to assign a one-dimensional array into an int. Doubt: Isn't the second last line also trying to assign a one dimensional int array to a two dimensional one? I'm terribly confused in the last two lines …

Member Avatar
Member Avatar
+0 forum 2

I noticed that when equals() is used to compare between two String objects it works fine. But when I compare two StringBuffer objects or two StringBuilder objects or mix of String and Builder and Buffer, equals() doesn't work even though I give same string value to the two objects being compared. However when I say it doesn't work, I mean it compiles fine, but it just says unequal according to if loop. Then I checked Java docs online under Buffer and Builder API and equals() is not listed under methods summary for them but it is listed for String. So …

Member Avatar
Member Avatar
+0 forum 5

import java.util.*; class SampleA { } class SampleB extends SampleA{ } class SampleC extends SampleA{ } class VectorDemo { public static void main(String r[]) { Vector<SampleA> v = new Vector<SampleA>(); v.add(new SampleB()); v.add(new SampleC()); SampleC rect = v.get(2); } } The output says "Incompatible types. Found SampleA, required SampleC. SampleC rect = v.get(2);" My doubt is first of all isn't this like using Wildcard where instead of saying Vector<? extends SampleA> v, we mentioned it a form of Vector<Type> v, which is one of the rules of Wild card which says if only <Type> is mentioned then it can accept …

Member Avatar
Member Avatar
+0 forum 3

Why does first two eg give true when a check done on them using if(x==y) but 3rd and 4th case give false? eg1: `String x = "Hello", y="Hello" ` eg2: `String x = "Hello";` `String y= "Hello";` eg3: `String x="Hello";` `String y = new String("Hello");` eg4: `String x = new String("Hello");` `String y = new String("Hello");`

Member Avatar
Member Avatar
+0 forum 3

The o/p is 10. But shouldn't it be 9? Is it got anything to do with Postfix or variable scoping has a role to play?

Member Avatar
Member Avatar
+0 forum 2

Compiles fine. But at runtime it gives ClassCastException. My doubt is that why is it so even though st which is of type Sample1 gets casted to Sample2 and generates exception even though Sample2 IS-A Sample1.

Member Avatar
Member Avatar
+0 forum 4

First statement: int[] it = new int[][]{{1}}[0]; //Valid Statement Second statement: int[][]it2 = new int[]{0}[0][0]; //Invalid Statement First one no explanation given. Second one is invalid because it attempts to access a two-dimensional object from a single dimension array. My query: 1. First of all can someone please interpret those two lines in simpler terms :( 2. What is the difference between the two statements that makes second invalid. 3. Finally why in first statement there are two curly braces, how is that interpreted, as in what it symbolizes? Terribly confused. Please help. Thanks in advance. P.S. This is from …

Member Avatar
Member Avatar
+0 forum 7

A question that I came across: Q: Can you pass a double to a method when it's argument is a float? A: No. Regardless of the double value, a double-sized variable is too big to fit in a float, so it can't be implicitly cast. Doubt: Shouldn't narrowing happen implicitly?

Member Avatar
Member Avatar
+0 forum 2

class Dog { public void bark() { System.out.println("Dog bark"); } } class Hound extends Dog { public void bark() { System.out.println("Hound bark"); } public void sniff() { System.out.println("Hound sniff"); } } public class CastingMethodCall { public static void main(String r[]) { ((Dog)new Hound()).bark(); //((Dog)new Hound()).sniff(); } } On executing above, I get o/p as "Hound bark". So it means the cast of Dog has nothing to play any role that I can safely assume, since new Hound().bark() also gives same o/p of "Hound bark". However, if I remove the comment from next line, i.e. ((Dog)new Hound()).sniff() and then execute program, …

Member Avatar
Member Avatar
+0 forum 3

class Mixer { Mixer() {} Mixer(Mixer m) { m1 = m; } Mixer m1; public static void main(String args[]) { Mixer m2 = new Mixer(); Mixer m3 = new Mixer(m2); m3.go(); Mixer m4 = m3.m1; m4.go(); Mixer m5 = m2.m1; m5.go(); } void go() { System.out.println("hi"); } } The answer to is " hi hi followed by an exception" Here are my doubts: a) When m2 is passed as an arg to Mixer constructor, are we passing the address to the Mixer() object m2 points to? If so, m1 = m will give the same address to m1 and hence …

Member Avatar
Member Avatar
+0 forum 1

public class Thread8 implements Runnable { Demo d; public static void main(String r[]) { new Thread8().disp(); } void disp() { d = new Demo(); new Thread(new Thread8()).start(); new Thread(new Thread8()).start(); } public void run() { d.fun(Thread.currentThread().getId()); } } class Demo { static long f=0; synchronized void fun(long id) { for(int i=1; i<3; i++){ System.out.println(id); Thread.yield(); } } } The program compiles properly but gives a NullPointerException. Why? Why did it not give a compile error for using a non-static reference variable d [Demo d] in static context i.e. in void disp()?

Member Avatar
Member Avatar
+0 forum 2

Here when I execute the program, the output I'm getting on all the tries is 342 (naturally first 34 prints together then 2 seconds later 2 prints). My doubts is that is the order of start() [x.start() followed by y.start()] important in order of execution? Shouldn't the output be 234? P.S. If I alter the start() to y.start() followed by x.start(), then also the output is 342. Help please. Thanks.

Member Avatar
Member Avatar
+0 forum 3

Two doubts: 1. Placing run() inside main() generates "illegal start of expression error"? So why can't run() be placed inside main()? 2.With the above code I am getting the output as A C D B. Why B comes last is because of join(). But what I'm unable to understand is that on invoking start() shouldn't run() get called immediately thus generating the output as C D A B? Help please..Thanks

Member Avatar
Member Avatar
-1 forum 2

I have a question in SCJP text book by Kathy Sierra and Bert Bates in Development chapter. Q. Given the default classpath: /foo And the directory structure: foo | test | xcom |--A.class |--B.java And these two files as mentioned in the code snippet section. Amongst the choice, 1. javac -classpath . xcom/B.java 2.javac -classpath xcom:. B.java Correct answer is option 1. But my doubt is that isn't option 2 going to perform same operation? If not, what is the difference between the two syntax?

Member Avatar
Member Avatar
+0 forum 1

General rules is that: 1) A private method cannot be overridden. 2) Subclass method should be a lower access modifier than a overridden superclass method. But the output of the program is "I am Son". Why? Is it because of the rule 2 the output comes? If that's the case here, then two rules of overriding I mentioned above are clashing, aren't they? Help please. Thanks.

Member Avatar
Member Avatar
+0 forum 8

Hey guys, I'm a starter so I had a doubt related to placement of a static variable. On line 5, for "static int d=5" compiler issues "illegal start of expression". But when I place it outside main() as Instance variable it works and prints "Hello 5". Why this thing? public class AbstractClass { public static void main(String args[]) { String s = "Hello"; static int d=5; System.out.println(s); System.out.println(d); } }

Member Avatar
Member Avatar
+0 forum 4

The End.