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.
Questions
- 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?
- 0.0
- Compilation fails
- A ParseException is thrown by the parse method at runtime
- A NumberFormatException is thrown by the parse method at runtime
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?
- 5
- 10
- 12
- 17
Given: 55. int []x= {1, 2,3,4, 5}; 56.int y[] =x; 57. System.out.println(y[2]); Which is true?
- Line 57 will print the value 2
- Line 57 will print the value 3
- Compilation will fail because of an error in line 55
- Compilation will fail because of an error in line 56
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.)
- Line 35 will not compile.
- Line 36 will not compile
- Line 37 will not compile.
- Line 38 will not compile.
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?
- for( Color c : Color.values())
- for( Color c = RED; c <= BLUE; c++)
- for( Color c; c.hasNext() ; c.next())
- for( Color c = Color[0]; c <= Color[2]; c++)
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.)
- Color skyColor = BLUE;
- Color treeColor = Color.GREEN;
- Color purple = new Color( 0xff00ff);
- if( Color.RED.ordinal() < Color.BLUE.ordinal() ) {}
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?
- Mr. John Doe
- An exception is thrown at runtime
- Compilation fails because of an error in line 12
- Compilation fails because of an error in line 15
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 2 3
- Compilation fails because of an error in line 12.
- Compilation fails because of an error in line 13
- Compilation fails because of an error in line 14
Given: 10. class Foo { 11. static void alpha() { /* more code here / } 12. void beta() { / more code here */ } 13. } Which two are true? (Choose two.)
- Foo.beta() is a valid invocation of beta().
- Foo.alpha() is a valid invocation of alpha().
- Method beta() can directly call method alpha().
- Method alpha() can directly call method beta().
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?
- public void logIt(String * msgs)
- public void logIt(String [] msgs)
- public void logIt(String... msgs)
- public void logIt(String msg1, String msg2, String msg3)
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?
- itemID(int itemId)
- update(int itemId)
- setItemId(int itemId)
- mutateItemId(int itemId)
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?
- Compilation of class A fails
- Line 28 prints the value 3 to System.out
- Line 28 prints the value 1 to System.out.
- A runtime error occurs when line 25 executes
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.)
- public void foo() { }
- public int foo() { return 3; }
- public Two foo() { return this; }
- public Object foo() { return this; }
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.)
- int foo() { /* more code here */ }
- void foo() { /* more code here */ }
- public void foo() { /* more code here */ }
- protected void foo() { /* more code here */ }
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?
- Compilation will succeed for all classes and interfaces.
- Compilation of class C will fail because of an error in line 2.
- Compilation of class C will fail because of an error in line 6.
- Compilation of class AImpl will fail because of an error in line 2.
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?
- Line 26 prints “a” to System.out
- Line 26 prints ‘b” to System.out
- An exception is thrown at line 26 at runtime.
- Compilation of class A will fail due to an error in line 6