Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
J2SE QUESTION 1. class A { static int k; public static void main(String[] args) { } System.out.println(k); } 2. class A { static { System.out.println(“SIB1”); } public static void main(String[] args) { int i = 0; System.out.println(test(1)); } static int test(int i) { return i++; } } Ans. SIB1 1 3. public class Z5 { static int i = test(); static int test() { System.out.println("from test"); main(null); return 55; } public static void main(String[] args) { System.out.println("from main"); System.out.println(i); } } Ans. from test from main 0 Praveen Kumar Chandaliya /CSE/PIET/JAVA INTERVIEW QUESTION/www.javapadho.com Page 1 J2SE QUESTION from main 55 4. ) Can we achieve 100% abstractness through interface? Ans: 5.) How to create an object to non-static inner class? Ans: 6) Write down any two usages of final keyword? Ans: 7) Constructors can be overloaded? Ans: 8.) Is abstract class is 100% abstractness? 9) Is Interface is a 100% abstract. 10) Is it possible to keep static with abstract?. 11) Is it possible to keep a class as abstract without having any abstract methods ()?. 12). class SdjManager1 { public static void main(String []args) { System.out.println(test()); } private static int test() { try {int i = 1000/(1000-1000); return 20; } catch(Throwable t) { return 0; } finally { return 1000; } return 2000; }} Ans : unrechable 13. public class Manager{ public static void main(String args[]){ String s1 = "too long"; System.out.println(s1.trim().trim().concat("tooshort").trim().replace("too", "2").trim().length()); } } Ans :13 14 public class Manager{ public static void main(String args[]){ String s1 = "---abc 123-hello---"; System.out.println(s1.trim().trim().concat(" ").trim().replace("-", " ").trim().length()); } } Praveen Kumar Chandaliya /CSE/PIET/JAVA INTERVIEW QUESTION/www.javapadho.com Page 2 J2SE QUESTION Ans :12 15. public class Manager{ public static void main(String args[]){ String s1 = "---abc 123-hello---"; s1.replace("-", " "); s1.trim(); System.out.println(s1.length()); } } Ans :19 16. public class SdjManager1 { public static void main(String []args) { StringBuffer sb = new StringBuffer("abc"); sb.append("cba").replace(0, 2, "c").substring(0, 4).reverse(); System.out.println(sb); } } Ans. reverse method not present in String class 17 public class SdjManager1 { public static void main(String []args) { StringBuilder sb = new StringBuilder(); System.out.println(sb.append("-123-").replace(0,2,"-")) ; System.out.println(sb.length()); sb.trimToSize(); System.out.println(sb.capacity()); }} 18. String class toString() method is returning content of String object. True/false? 19. String class equals() method is using == operator to compare two String objects. True/false? 20. public class I { public static void main(String[] args) { String s1 = "java"; String s2 = "java"; String s3 = new String(s1); String s4 = new String(s2); System.out.println("------"); System.out.println(s1 == s2); System.out.println(s3 == s4); System.out.println(s1 == s4); System.out.println(s3 == s4); System.out.println("--------------"); System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); } } Praveen Kumar Chandaliya /CSE/PIET/JAVA INTERVIEW QUESTION/www.javapadho.com Page 3 J2SE QUESTION 21. public class Test implements Runnable { public static void main(String[] args) { new Test(); } public Test() { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("Test"); } } 22. public class M4 { public static Runnable getRunnable() { Runnable r1=new Runnable() { public void run() { for(int i=0;i<1000;i++) { System.out.println(i); } } }; return r1; } public static void main(String[] args) { Thread t1=new Thread(getRunnable()); t1.start(); for(int i=1000;i<2000;i++) { System.out.println(i); } } } A)1000 – 1999 23. class C { int i; public String toString() { return "i="+i; } } public class Manager3 { public static void main(String[] args) { C c1 = new C(); Praveen Kumar Chandaliya /CSE/PIET/JAVA INTERVIEW QUESTION/www.javapadho.com Page 4 J2SE QUESTION c1.i = 10; System.out.println(c1); System.out.println(c1.i); } } 24. interface Calculate { int var=0; void cal(int item); } class Amount implements Calculate { int x; public void cal(int item) { if(item<2) { x = item; } else { x = item*item; } } } public class ManagerDemo { public static void main(String[] args) { Amount arr[] = new Amount[3]; for(int i=0;i<3;i++) { arr[i] = new Amount(); } arr[0].cal(0); arr[1].cal(1); arr[2].cal(2); System.out.println(arr[0].x+":"+arr[1].x+":"+arr[2].x); } } 25. public class Test implements Runnable { public static void main(String[] args) { new Test(); } Praveen Kumar Chandaliya /CSE/PIET/JAVA INTERVIEW QUESTION/www.javapadho.com Page 5 J2SE QUESTION public Test() { Thread t = new Thread(this); t.start(); t.start(); } public void run() { System.out.println("Test"); } } a) Test Test b) test IllegalThreadStateException b) StackOverFlow error c) Test 1. Are JVM's platform independent? JVM's are not platform independent. JVM's are platform specific run time implementation provided by the vendor. 2. What is the difference between a JDK and a JVM? JDK is Java Development Kit which is for development purpose and it includes execution environment also. But JVM is purely a run time environment and hence you will not be able to compile your source files using a JVM. 3. What is difference between Path and Classpath? Path and Classpath are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files. 4. Why is the main() method declared static? main() method is called by the JVM even before the instantiation of the class hence it is declared as static. 5. Can a main() method be declared final? Yes. Any inheriting class will not be able to have it's own default main() method. 6. Can a class be declared as protected? A class can't be declared as protected. only methods can be declared as protected. 7. What is the purpose of declaring a variable as final? A final variable's value can't be changed. final variables should be initialized before using them. 8. I don't want my class to be inherited by any other class. What should i do? You should declared your class as final. But you can't define your class as final, if it is an abstract class. A class declared as final can't be extended by any other class. 9. How is final different from finally and finalize()? final is a modifier which can be applied to a class or a method or a variable. final class can't be inherited,final method can't be overridden and final variable can't be changed. finally is an exception handling code section which gets executed whether an exception is raised or not by the try block code segment. finalize() is a method of Object class which will be executed by the JVM just before garbage collecting object to give a final chance for resource releasing activity. 10. Can a class be declared as static? We can not declare top level class as static, but only inner class can be declared static. 11. Can we declare a static variable inside a method? Static varaibles are class level variables and they can't be declared inside a method. If declared, the class will not compile. 12. Can a abstract class be declared final? Not possible. An abstract class without being inherited is of no use and hence will result in compile time error. 13. Why is an Interface be able to extend more than one Interface but a Class can't extend more than one Class? Basically Java doesn't allow multiple inheritance, so a Class is restricted to extend only one Class. But an Interface is a pure abstraction model and doesn't have inheritance hierarchy like classes(do remember that the base class of all classes is Object). So an Interface is allowed to extend more than one Interface. 14. Can an Interface be defined inside a class? Yes it's possible. 15. Can we define private and protected modifiers for variables in interfaces? No. Praveen Kumar Chandaliya /CSE/PIET/JAVA INTERVIEW QUESTION/www.javapadho.com Page 6 J2SE QUESTION 16. String class equals() method is using == operator to compare two String objects. 17. public class Manager{ public static void main(String args[]){ String letters = "a,b,c,d,e,f"; System.out.println(letters.split(",", 2)[0]); } } 18.Is it possible to keep any statements in between try and catch blocks? 19.What are checked exceptions? 20.What are un checked exceptions? 21. . public class Manager1 { static void test(){ throw new Error("error occured"); } public static void main(String[] args){ try{ test(); } catch(Error e){ System.out.println("caught"); } }} 22. public class Manager1 { static void test(){ throw new RuntimeException("error occured"); } public static void main(String[] args){ try{ test(); } catch(Error e){ System.out.println("caught"); } }} Ans : error occurred and abnormal terminate 23. public class Manager1 { public static void main(String[] args){ try { throw new Exception("fileNot Found",new FileNotFoundException()); } catch (Exception e) { System.out.println("caught"); }}} Praveen Kumar Chandaliya /CSE/PIET/JAVA INTERVIEW QUESTION/www.javapadho.com Page 7 J2SE QUESTION 24. What is the difference between an Abstract class and Interface? Abstract classes may have some executable methods and methods left unimplemented. Interfaces contain no implementation code. A class can implement any number of interfaces, but subclass at most one abstract class. An abstract class can have non abstract methods. All methods of an interface are abstract. An abstract class can have instance variables. An interface cannot. An abstract class can define constructor. An interface cannot. An abstract class can have any visibility: public, protected, private or none (package). An interface's visibility must be public or none (package). An abstract class inherits from Object and includes methods such as clone() and equals() Praveen Kumar Chandaliya /CSE/PIET/JAVA INTERVIEW QUESTION/www.javapadho.com Page 8