Java Programming Fundamentals
Test your knowledge of Java language fundamentals including collections, static methods, polymorphism, and compilation behavior.
Questions
try{ File f = new File("a.txt"); }catch(Exception e){ }catch(IOException io){ } Is this code create new file name a.txt ?
- True
- False
- Compile error
- none
What is the output for the below code ? public class A { int k; boolean istrue; static int p; public void printValue() { System.out.print(k); System.out.print(istrue); System.out.print(p); } } public class Test{ public static void main(String argv[]){ A a = new A(); a.printValue();
- 0 false 0
- 0 true 0
- 0 0 0
- Compile error - static variable must be initialized before use.
What is the output for the below code ? 1. public class Test { 2. public static void main(String... args) { 3. int [] index = new int[5]; 4. System.out.println(index instanceof Object); 5. } 6. }
- Compilation fails with an error at line 4
- Compilation fails with an error at line 3
- true
- false
If we do ArrayList lst = new ArrayList(); What is the initial capacity of the ArrayList lst ?
- 8
- 10
- none
- 0
HashMap can be synchronized by _______ ?
- Map m = Collection.synchronizeMap(hashMap);
- Map m = Collections.synchronizeMap(hashMap);
- Map m = hashMap.synchronizeMap();
- none
What is the output for the below code ? public class A { public void printValue(){ System.out.println("A"); } } public class B extends A { public void printValue(){ System.out.println("B"); } } 1. public class Test { 2. public static void main(String... args) { 3. A b = new B(); 4. newValue(b); 5. } 6. public static void newValue(A a){ 7. if(a instanceof B){ 8. ((B)a).printValue(); 9. } 10. } 11.}
- A
- Compilation fails with an error at line 4
- B
- Compilation fails with an error at line 8
What is the output for the below code ? 1. public class Test {2. int i=8; 3. int j=9; 4. public static void main(String[] args){ 5. add(); 6. } 7. public static void add(){ 8. int k = i+j; 9. System.out.println(k); 10. } 11. }
- Compilation fails with an error at line 5
- Compilation fails with an error at line 8
- 17
- 0
What does it print? public class CleverSwap { public static void main(String[] args) { int x = 1984; int y = 2001; x ^= y ^= x ^= y; System.out.println("x = " + x + "; y = " + y); } }
- x = 0; y = 1984
- x = 2001; y = 1984
- x = 2001; y = 0
- x = 1984; y = 2001
- x = 0; y = 0
- Error
What does it print ? /** * Generated by the IBM IDL-to-Java compiler, version 1.0 * from F:\TestRoot\apps\a1\units\include\PolicyHome.idl * Wednesday, June 17, 1998 6:44:40 o'clock AM GMT+00:00 */ public class Test { public static void main(String[] args) { System.out.print("Hell"); System.out.println("o world"); } }
- Hello world
- Won't even Compile
- Run time error
- None of these
What does this program do ? public class BrowserTest { public static void main(String[] args) { System.out.print("iexplore:"); http://www.google.com; System.out.println(":maximize"); } }
- Compilation Error
- iexplore::maximize
- Runtime Error
- None of these
What does this program do ? public class Null { public static void greet() { System.out.println("Hello world!"); } public static void main(String[] args) { ((Null) null).greet(); } }
- Hello world!
- Throw a NullPointerException
- Won't even compile
- Run time error
What does it print ? import java.util.*; public class NameGame { public static void main(String args[]) { Map<String, String> m = new IdentityHashMap<String, String>(); m.put("Mickey", "Mouse"); m.put("Mickey", "Mantle"); System.out.println(m.size()); } }
- Exception
- Stack Overflow
- 2
- 1
For a COBOL program -
- EXE is created for the called program and DLL is created for the calling program
- EXE is created for the calling program and DLL is created for the called program
- EXE is created for the both, called program and calling program
- DLL is created for the both, called program and calling program