Java Programming Fundamentals
Test your knowledge of Java programming concepts including collections, threads, exception handling, method overloading, and static methods.
Questions
Select three correct statements
- A static method may override another static method
- A static method cannot override a non-static method
- A non-static method cannot override a static method
- A non-static method may be overloaded by a static method
- A synchronized method cannot be overridden
Select three correct statements.
- ) A static method may override another static method
- A static method cannot override a non-static method
- non-static method cannot override a static method
- A non-static method may be overloaded by a static method
- A synchronized method cannot be overridden
Which of the following are methods of the Thread class.
- public void run()
- public void start()
- public void exit()
- public final void setAccess()
- public final void setPriority(int priNbr)
- public final int getPriority()
Casting occurs commonly between numeric types
- True
- False
Which are valid declarations? (Choose all that apply)
- int $x
- int 123
- int _123
- int #dim
- int %percent
- int central_sales_region_Summer_2005_gross_sales
public class A { private int counter=0; public static int getInstanceCount() { return counter; } public A() { counter++; } } Given this code from Class B: 25. A a1 = new A(); 26. A a2 = new A(); 27. A a3 = new A(); 28. System.out.println(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
- Compilation fails because of an error on line 28
class Eggs { int doX(Long x, Long y) { return 1; } int doX(long... x) { return 2; } int doX(Integer x, Integer y) { return 3; } int doX(Number n, Number m) { return 4; } public static void main(String[] args) { new Eggs().go(); } void go() { short s = 7; System.out.print(doX(s,s) + " "); System.out.println(doX(7,7)); } } What is the result?
- 1 1
- 2 1
- 3 1
- 4 1
- 2 3
- 4 3
You read the following statement in a Java program that compiles and executes. submarine.dive(depth); What can you say for sure?
- depth must be an int
- dive must be a method.
- dive must be the name of an instance field.
- submarine must be the name of a class
- submarine must be a method.
Given: 11.classA { 12. public void process() { System.out.print(”A “); } } 13. class B extends A { 14. public void process() throws RuntimeException { 15. super.process(); 16. if (true) throw new RuntimeException(); 17. System.out.print(“B”); }} 18. public static void main(String[] args) { 19. try { ((A)new B()).process(); } 20. catch (Exception e) { System.out.print(”Exception “); } 21. } What is the result?
- Exception
- A Exception
- A Exception B
- A B Exception
- Compilation fails because of an error in line 14.
- Compilation fails because of an error in line 19.
- rbo = new ReallyBigObject(); 12. // more code here 13. rbo = null; 14. /* insert code here */ Which statement should be placed at line 14 to suggest that the virtual machine expend effort toward recycling the memory used by the object rbo?
- System.gc();
- Runtime.gc();
- System.freeMemory();
- Runtime.getRuntime().growHeap();
- Runtime.getRuntime().freeMemory();
- public class Threads5 { 2. public static void main (String[] args) { 3. new Thread(new Runnable() { 4. public void run() { 5. System.out.print("bar"); 6. }}).start(); 7. } 8. } What is the result?
- Compilation fails.
- An exception is thrown at runtime.
- The code executes normally and prints "bar".
- The code executes normally, but nothing prints.
- import java.util.*; 2. 3. public class LetterASort{ 4. public static void main(String[] args) { 5. ArrayList strings = new ArrayList(); 6. strings.add("aAaA"); 7. strings.add("AaA"); 8. strings.add("aAa"); 9. strings.add("AAaa"); 10. Collections.sort(strings); 11. for (String s : strings) { System.out.print(s + " "); } 12. } 13. } What is the result?
- Compilation fails.
- aAaA aAa AAaa AaA
- AAaa AaA aAa aAaA
- AaA AAaa aAaA aAa
- aAa AaA aAaA AAaa
- An exception is thrown at runtime