Java Programming Fundamentals

Test your knowledge of core Java programming concepts including method overloading, inheritance, access modifiers, enums, arrays, exception handling, and variable naming conventions.

16 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)
  1. public static void parse(String str) { 12. try { 13. float f= Float.parseFloat(str); 14. } catch (NumberFormatException nfe) { 15. f= 0; 16. } finally { 17. System.out.println(f); 18. } 19. } 20. public static void main(String[] args) { 21. parse(”invalid”); 22. } What is the result?
  1. 0.0
  2. Compilation fails
  3. A ParseException is thrown by the parse method at runtime
  4. A NumberFormatException is thrown by the parse method at runtime
Question 2 Multiple Choice (Single Answer)

Click the Exhibit button. 1. public class Test { 2. int x= 12; 3. public void method(int x) { 4. x+=x; 5. System.out.println(x); 6. } 7. } Given: 34. Test t = new Test(); 35. t.method(5); What is the output from line 5 of the Test class?

  1. 5
  2. 10
  3. 12
  4. 17
Question 3 Multiple Choice (Single Answer)

Given: 55. int []x= {1, 2,3,4, 5}; 56.int y[] =x; 57. System.out.println(y[2]); Which is true?

  1. Line 57 will print the value 2
  2. Line 57 will print the value 3
  3. Compilation will fail because of an error in line 55
  4. Compilation will fail because of an error in line 56
Question 4 Multiple Choice (Multiple Answers)

Given: 35. String #name = “Jane Doe”; 36.int$age=24; 37. Double_height = 123.5; 38. double~temp = 37.5; Which two are true? (Choose two.)

  1. Line 35 will not compile.
  2. Line 36 will not compile
  3. Line 37 will not compile.
  4. Line 38 will not compile.
Question 5 Multiple Choice (Single Answer)

Given: 11. public class Ball { 12. public enum Color { RED, GREEN, BLUE }; 13. public void foo() { 14. // insert code here 15. { System.out.println(c); } 16. } 17. } Which code inserted at line 14 causes the foo method to print RED, GREEN, and BLUE?

  1. for( Color c : Color.values())
  2. for( Color c = RED; c <= BLUE; c++)
  3. for( Color c; c.hasNext() ; c.next())
  4. for( Color c = Color[0]; c <= Color[2]; c++)
Question 6 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( Color.RED.ordinal() < Color.BLUE.ordinal() ) {}
Question 7 Multiple Choice (Single Answer)

Given: 11. public enum Title { 12. MR(”Mr.”), MRS(”Mrs.”), MS(”Ms.”); 13. private final String title; 14. private Title(String t) { title = t; } 15. public String format(String last, String first) { 16. return title + “ “ + first + “ “ + last; 17. } 18. } 19. public static void main(String[] args) { 20. System.out.println(Title.MR.format(”Doe”, “John”)); 21. } What is the result?

  1. Mr. John Doe
  2. An exception is thrown at runtime
  3. Compilation fails because of an error in line 12
  4. Compilation fails because of an error in line 15
Question 8 Multiple Choice (Single Answer)

Given: 11. public static void main(String[] args) { 12. Object obj =new int[] { 1,2,3 }; 13. int[] someArray = (int[])obj; 14. for (int i: someArray) System.out.print(i +“ “) 15. } ‘What is the result?

  1. 1 2 3
  2. Compilation fails because of an error in line 12.
  3. Compilation fails because of an error in line 13
  4. Compilation fails because of an error in line 14
Question 9 Multiple Choice (Multiple Answers)

Given: 10. class Foo { 11. static void alpha() { /* more code here / } 12. void beta() { / more code here */ } 13. } Which two are true? (Choose two.)

  1. Foo.beta() is a valid invocation of beta().
  2. Foo.alpha() is a valid invocation of alpha().
  3. Method beta() can directly call method alpha().
  4. Method alpha() can directly call method beta().
Question 10 Multiple Choice (Single Answer)

A programmer needs to create a logging method that can accept an arbitrary number of arguments. For example, it may be called in these ways: logIt(”log message 1 “); logIt(”log message2”,”log message3”); logIt(”log message4”, “log message5”, “log message6); Which declaration satisfies this requirement?

  1. public void logIt(String * msgs)
  2. public void logIt(String [] msgs)
  3. public void logIt(String... msgs)
  4. public void logIt(String msg1, String msg2, String msg3)
Question 11 Multiple Choice (Single Answer)

A programmer is designing a class to encapsulate the information about an inventory item. A JavaBeans component is needed to do this. The Inventoryltem class has private instance variables to store the item information: 10. private int itemId; 11. private String name; 12. private String description; Which method signature follows the JavaBeans naming standards for modifying the itemld instance variable?

  1. itemID(int itemId)
  2. update(int itemId)
  3. setItemId(int itemId)
  4. mutateItemId(int itemId)
Question 12 Multiple Choice (Single Answer)

Click the Exhibit button. 1. public class A { 2. 3. private int counter = 0; 4. 5. public static int getInstanceCount() { 6. return counter; 7. } 8. 9. public A() { 10. counter++; 11. } 12. 13. } Given this code from Class B: 25.A a1 =new A(); 26. A a2 =new A(); 27. A a3 =new A(); 28. System.out.printIn(A.getInstanceCount() ); What is the result?

  1. Compilation of class A fails
  2. Line 28 prints the value 3 to System.out
  3. Line 28 prints the value 1 to System.out.
  4. A runtime error occurs when line 25 executes
Question 13 Multiple Choice (Multiple Answers)

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 Object foo() { return this; }
Question 14 Multiple Choice (Multiple Answers)

Given: 10. class One { 11. void foo() {} 12. } 13. class Two extends One { 14. //insert method here 15. } Which three methods, inserted individually at line 14, will correctly complete class Two? (Choose three.)

  1. int foo() { /* more code here */ }
  2. void foo() { /* more code here */ }
  3. public void foo() { /* more code here */ }
  4. protected void foo() { /* more code here */ }
Question 15 Multiple Choice (Single Answer)

Click the Exhibit button. 1. public interface A { 2. public void doSomething(String thing); 3. } 1. public class AImpl implements A { 2. public void doSomething(String msg) { } 3. } 1. public class B { 2. public A doit() { 3. // more code here 4. } 5. 6. public String execute() { 7. // more code here 8. } 9. } 1. public class C extends B { 2. public AImpl doit() { 3. // more code here 4. } 5. 6. public Object execute() { 7. // more code here 8. } 9. } Which statement is true about the classes and interfaces in the exhibit?

  1. Compilation will succeed for all classes and interfaces.
  2. Compilation of class C will fail because of an error in line 2.
  3. Compilation of class C will fail because of an error in line 6.
  4. Compilation of class AImpl will fail because of an error in line 2.
Question 16 Multiple Choice (Single Answer)

Click the Exhibit button. 1. public class A { 2. public String doit(int x, int y) { 3. return “a”; 4. } 5. 6. public String doit(int... vals) { 7. return “b”; 8. } 9. } Given: 25. A a=new A(); 26. System.out.println(a.doit(4, 5)); What is the result?

  1. Line 26 prints “a” to System.out
  2. Line 26 prints ‘b” to System.out
  3. An exception is thrown at line 26 at runtime.
  4. Compilation of class A will fail due to an error in line 6