Java Programming Fundamentals

Test your knowledge of core Java programming concepts including enums, exception handling, inheritance, threading, generics, and string manipulation.

10 Questions Published

Questions

Question 1 Multiple Choice (Multiple Answers)

Given: classA {} class B extends A {} class C extends A {} class D extends B {} Which three statements are true? (Choose three.)

  1. The type List is assignable to List<A>.
  2. The type List<Object> is assignable to List<?>.
  3. The type List<D> is assignable to List<? extends B>.
  4. The type List<? extends A> is assignable to List<A>.
  5. The type List<Object> is assignable to any List reference.
  6. The type List<? extends B> is assignable to List<? extends A>.
Question 2 Multiple Choice (Single Answer)

class Sub extends Super{ String name; Sub(String s) { super(s); name=s; } public static void main(String args[]){ Super sup = new Super("First"); Sub sub = new Sub("Second"); System.out.println(sub.name + " " + sup.name); } } class Super{ String name; Super(String s){ name =s ; } }

  1. Error: Super(java.lang.String) in Super cannot be applied to ()
  2. First Second
  3. Second First
  4. Runtime Exception
Question 3 Multiple Choice (Multiple Answers)

Given: 1. package com.company.application; 2. 3. public class MainClass { 4. public static void main(String[] args) { } 5. } And MainClass exists in the /apps/com/company/application directory. Assume the CLASSPATH environment variable is set to “.“ (current directory). Which two java commands entered at the command line will run MainClass? (Choose two.) A. java MainClass if run from the /apps directory B. java com.company.application.MainClass if run from the /apps directory C. java -classpath /apps com.company.application.MainClass if run from any directory D. java -classpath . MainClass if run from the /apps/com/company/application directory E. java -classpath /apps/com/company/application:. MainClass if run from the /apps directory F. java com.company.application.MainClass if run from the /apps/com/company/application directory

  1. A
  2. B
  3. C
  4. D
  5. E
  6. F
Question 4 Multiple Choice (Multiple Answers)

Click the Exhibit button. 10. class Inner { 11. private int x; 12. public void setX( int x) { this.x = x; } 13. public int getX() { return x; } 14. } 15. 16. class Outer { 17. private Inner y; 18. public void setY( Inner y) { this.y = y; } 19. public Inner getY() { return y; } 20. } 21. 22. public class Gamma { 23. public static void main( String[] args) { 24. Outer o = new Outer(); 25. Inner i = new Inner(); 26.int n=10; 27. i.setX(n); 28. o.setY(i); 29. // insert code here 30. System.out.println( o.getY().getX()); 31. } 32. } Which three code fragments, added individually at line 29, produce the output 100? (Choose three.)

  1. n = 100;
  2. i.setX( 100);
  3. o.getY().setX( 100);
  4. i = new Inner(); i.setX( 100);
  5. o.setY( i); i = new Inner(); i.setX( 100);
  6. i = new Inner(); i.setX( 100); o.setY( i);
Question 5 Multiple Choice (Multiple Answers)

Given: 1. public class Threads2 implements Runnable { 2. 3. public void nun() { 4. System.out.println(”run.”); 5. throw new RuntimeException(”Problem”); 6. } 7. public static void main(String[] args) { 8. Thread t = new Thread(new Threads2()); 9. t.start(); 10. System.out.println(”End of method.”); 11. } 12. } Which two can be results? (Choose two.) A. java.lang.RuntimeException: Problem B. run. java.lang.RuntimeException: Problem C. End of method. java.lang.RuntimeException: Problem D. End of method. run. java.lang.RuntimeException: Problem E. run. java.lang.RuntimeException: Problem End of method.

  1. A
  2. B
  3. C
  4. D
  5. E
Question 6 Multiple Choice (Multiple Answers)

Given: 1. public class TestString3 { 2. public static void main(String[] args) { 3. // insert code here 5. System.out.println(s); 6. } 7. } Which two code fragments, inserted independently at line 3, generate the output 4247? (Choose two.) A. String s = “123456789”; s = (s-”123”).replace(1,3,”24”) - “89”; B. StringBuffer s = new StringBuffer(”123456789”); s.delete(0,3).replace( 1,3, “24”).delete(4,6); C. StringBuffer s = new StringBuffer(”123456789”); s.substring(3,6).delete( 1 ,3).insert( 1, “24”); D. StringBuilder s = new StringBuilder(”123456789”); s.substring(3,6).delete( 1 ,2).insert( 1, “24”); E. StringBuilder s = new StringBuilder(”123456789”); s.delete(0,3).delete( 1 ,3).delete(2,5).insert( 1, “24”);

  1. A
  2. B
  3. C
  4. D
  5. E
Question 7 Multiple Choice (Multiple Answers)

Click the Exhibit button. 1. public class A { 2. public void method1() { 3. B b=new B(); 4. b.method2(); 5. // more code here 6. } 7. } 1. public class B { 2. public void method2() { 3.C c=new C(); 4. c.method3(); 5. // more code here 6. } 7. } 1. public class C { 2. public void method3() { 3. // more code here 4. } 5. } Given: 25. try { 26. A a=new A(); 27. a.method1(); 28. } catch (Exception e) { 29. System.out.print(”an error occurred”); 30. } Which two are true if a NullPointerException is thrown on line 3 of class C? (Choose two.)

  1. The application will crash.
  2. The code on line 29 will be executed.
  3. The code on line 5 of class A will execute.
  4. The code on line 5 of class B will execute.
  5. The exception will be propagated back to line 27.
Question 8 Multiple Choice (Multiple Answers)

Given: 33. try { 34. // some code here 35. } catch (NullPointerException e1) { 36. System.out.print(”a”); 37. } catch (RuntimeException e2) { 38. System.out.print(”b”); 39. } finally { 40. System.out.print(”c”); 41. } What is the result if a NullPointerException occurs on line 34?

  1. c
  2. a
  3. ab
  4. ac
  5. bc
  6. abc
Question 9 Multiple Choice (Multiple Answers)
  1. Given: 10. class One { 11. public One foo() { return this; } 12. } 13. class Two extends One { 14. public One foo() { return this; } 15. } 16. class Three extends Two { 17. // insert method here 18. } Which two methods, inserted individually, correctly complete the Three class? (Choose two.)
  1. public void foo() { }
  2. public int foo() { return 3; }
  3. public Two foo() { return this; }
  4. public One foo() { return this; }
  5. public Object foo() { return this; }
Question 10 Multiple Choice (Multiple Answers)

Given: 10. public class Fabric 11. public enum Color { 12. RED(0xff0000), GREEN(0x00ff00), BLUE(0x0000ff); 13. private final int rgb; 14. Color( int rgb) { this.rgb = rgb; } 15. public int getRGB() { return rgb; } 16. }; 17. public static void main( String[] argv) { 18. // insert code here 19. } 20. } Which two code fragments, inserted independently at line 18, allow the Fabric class to compile? (Choose two.)

  1. Color skyColor = BLUE;
  2. Color treeColor = Color.GREEN;
  3. Color purple = new Color( 0xff00ff);
  4. if( RED.getRGB() < BLUE.getRGB() ) {}
  5. Color purple = Color.BLUE + Color.RED;
  6. if( Color.RED.ordinal() < Color.BLUE.ordinal() ) {}