This is part-3 of the coding interview questions. You may refer the complete list here :
Java Coding Interview Questions – Part 1
Java Coding Interview Questions – Part 2
Java Coding Interview Questions – Part 3
Java Coding Interview Questions – Part 4
1. What happens when you compile and run the following program ?
package quiz; public class Quiz26 { private int x = 10; public int y = 11; public static int z = 12; public static void main(String[] args) { int x = 11; int y = 12; int z = 13; new Quiz26().new Initialize().init(); } class Initialize{ void init(){ System.out.println(x + " " + y + " " +z); } } }
Output
10 11 12
The inner class has access to all of outer class's variables(even private ones), except for main's local variables.
2. What will be the output of following Java program ?
package quiz; public class Quiz30 { public static void main(String[] args) { String s = ""; StringBuffer sb1 = new StringBuffer("Hi"); StringBuffer sb2 = new StringBuffer("Hi"); StringBuffer sb3 = new StringBuffer(sb2); StringBuffer sb4 = sb3; if(sb1.equals(sb2)) s += "1"; if(sb2.equals(sb3)) s += "2"; if(sb3.equals(sb4)) s += "3"; String s1 = "Hi"; String s2 = "Hi"; String s3 = s2; if(s1.equals(s2)) s += "4"; if(s2.equals(s3)) s += "5"; System.out.println(s); } }
Output
345
The StringBuffer class doesn't override the equals() method, so different StringBuffer objects with same value won't be considered equal by the equals() methods.
However, String class's equals() method is overridden so that different String objects with same value are considered equal by equals() method.
3. Predict the output of following Java program.
package quiz; public class SuperClass { static String s = ""; static { s += "1"; } SuperClass(){ s += "2"; } }
package quiz; public class SubClass extends SuperClass{ SubClass(){ s += "3"; } static{ new SubClass(); System.out.println(s); } {s += "4";} public static void main(String[] args) {} }
Output
1243
Static init blocks run once, when a class is first loaded.
Instance init blocks run after the constructor's call to super(), but before the rest of constructor runs.
So, the instance init s += "4" run between the super() call and Subclass constructor code s += "3"
Output is 1243.
4. What will be the output of following Java program ?
package quiz; public class ClassA { String hello(){ return "A"; } }
package quiz; public class ClassB extends ClassA{ String hello(){ return "B"; } }
package quiz; public class ClassC extends ClassB { String hello(){ return "C"; } public static void main(String[] args) { // TODO Auto-generated method stub ClassA[] msgs = new ClassA[3]; msgs[0] = new ClassB(); msgs[1] = (ClassA) new ClassC(); msgs[2] = (ClassA) new ClassB(); for(ClassA a:msgs) System.out.print(a.hello()); } }
Output
BCB
The polymorphic invocations of the hello() method uses the version that corresponds to the object type and not the reference variable type.
5. Predict the output of following Java program.
package quiz; public class SuperClass { protected SuperClass(){ System.out.print("super "); } }
package quiz; public class SubClass extends SuperClass{ private SubClass(){ System.out.print("sub "); } public static void main(String[] args) { new SuperClass(); class InnerClass{ private InnerClass() {System.out.print("inner ");} {new SubClass();} {new SuperClass();} } new InnerClass(); } }
Output
super super sub super inner
For order of execution inside main, new SuperClass() executes first .. the InnerClass code is invoked after its declared.
Inside InnerClass code, the call to super() in the constructor executes before any instance blocks run.
You may also like :
Java Coding Interview Questions – Collections
© 2016 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post