Computer Knowledge

Java Core Classes and Threads

1,935 Questions

Java core classes and threads form the foundation of object oriented programming and are crucial for IT officer and programming exams. This includes concepts like string mutability, collections, and multithreading. Solve these questions to test your practical coding and theoretical knowledge.

String and StringBuffer classesThread execution methodsJava collections frameworkCharacter streams input outputVariable serialization rules

Java Core Classes and Threads Questions

Multiple choice technology programming languages
  1. Class A will not compile.

  2. Line 46 can throw the unchecked exception TestException.

  3. Line 45 can throw the unchecked exception TestException.

  4. Line 46 will compile if the enclosing method throws a TestException.

  5. Line 46 will compile if enclosed in a try block, where TestException

Reveal answer Fill a bubble to check yourself
D,E Correct answer
Explanation

TestException extends Exception, making it a checked exception. Line 46 calls a.sayHello() which throws TestException, so this line must either be in a try-catch block or the enclosing method must declare 'throws TestException'. Option D correctly states that the enclosing method must throw the exception. Option E correctly states that a try block with TestException handling would allow compilation. Options B and C incorrectly call it unchecked - it's checked.

Multiple choice technology programming languages
  1. declare reset() using the synchronized keyword

  2. declare getName() using the synchronized keyword

  3. declare getCount() using the synchronized keyword

  4. declare the constructor using the synchronized keyword

  5. declare increment() using the synchronized keyword

Reveal answer Fill a bubble to check yourself
A,C,E Correct answer
Explanation

WMLScript is a lightweight, simplified version of JavaScript designed specifically for low-bandwidth mobile devices running WAP (Wireless Application Protocol).

Multiple choice technology programming languages
  1. An exception may be thrown at runtime.

  2. The code may run with no output, without exiting.

  3. The code may run with no output, exiting normally.

  4. The code may rum with output “A B A B C C “, then exit.

  5. The code may rum with output “A B C A B C A B C “, then exit.

  6. The code may ruin with output “A B C A A B C A B C “, then exit.

Reveal answer Fill a bubble to check yourself
E,F Correct answer
Explanation

Line 15 has a typo: 'public void ruin()' should be 'public void run()' since Thread's entry point is run(). This means the threads actually execute the default no-op run() method, doing nothing. However, the threads start successfully, and since start() is called twice, both threads may or may not execute add() operations before main exits. Options E and F describe possible outputs: if threads execute, they print sequences of 'A B C'; the exact order and repetition depend on thread scheduling, which is nondeterministic.

Multiple choice technology programming languages
  1. The output may be “r1 = 6, r2 = 14”.

  2. The output may be “r1 = 5, r2 = 15”.

  3. The output may be “r1 = 8, r2 = 12”.

  4. The code may run (and complete) with no output.

  5. The code may deadlock (without completing) with no output.

  6. M IllegalStateException or InterruptedException may be thrown at

Reveal answer Fill a bubble to check yourself
A,B,E Correct answer
Explanation

Three threads are started doing transfers: r1 loses 5, r2 gains 5; then r2 loses 2, r1 gains 2; then r1 loses 1, r2 gains 1. If all complete in order, r1=8, r2=12 (not an option). If threads interleave, r1=6,r2=14 or r1=5,r2=15 are possible. Deadlock can occur if two threads hold one Record each and wait for the other's Record (circular wait). Options A, B, E are correct. Option C's output is impossible given the transfer amounts. No IllegalStateException or InterruptedException is thrown.

Multiple choice technology programming languages
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The servlet lifecycle guarantees that init() is called only once when the servlet is first loaded, and destroy() is called only once when the servlet is being removed from service. The servlet container manages these calls and developers should never call them manually. This once-only behavior ensures proper initialization and cleanup of servlet resources.

Multiple choice technology programming languages
  1. A

  2. B

  3. C

  4. D

  5. E

  6. F

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

To execute a class from a JAR file, the JAR must be in the CLASSPATH with its full path. Option C correctly places Poker.jar in /stuff/java and includes the full JAR path /stuff/java/Poker.jar in CLASSPATH. Wildcards like *.jar don't work in CLASSPATH.

Multiple choice technology programming languages
  1. A

  2. B

  3. C

  4. D

  5. E

Reveal answer Fill a bubble to check yourself
D,E Correct answer
Explanation

When start() is called, the thread runs independently. The main thread prints 'End of method.' first, then the spawned thread prints 'run.' followed by the RuntimeException stack trace. Option D shows this. Option E is also possible since thread scheduling is non-deterministic - the spawned thread might complete before the main thread prints.

Multiple choice technology programming languages
  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.

Reveal answer Fill a bubble to check yourself
B,E Correct answer
Explanation

When NullPointerException occurs deep in the call chain (C.method3), it propagates up through B.method2() and A.method1() back to the try-catch block. The catch block executes (line 29 prints 'an error occurred'). Code after the throw in any method never runs, so lines 5 in A and B don't execute.

Multiple choice technology web technology
  1. window.onunload=function() { alert('closing'); }

  2. window.onunload+=onClose; function onClose() { alert('closing'); }

  3. window.onunload=onClose; function onClose() { alert('closing'); }

  4. All the above

Reveal answer Fill a bubble to check yourself
A,C Correct answer
Explanation

In JavaScript, you can assign an event handler by setting the event property to an anonymous function or to a named function reference. Using += on event properties is invalid syntax in standard JavaScript.

Multiple choice technology web technology
  1. Hibernate does not support lazy initialization for detached objects.

  2. Access to a lazy association outside of the context of an open Hibernate session will result in an exception.

  3. Lazy fetching is way to avoid unnecessary column reads.

  4. All the above.

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

All statements are true: Hibernate cannot lazily initialize associations for detached objects, accessing a lazy association outside an active session throws a LazyInitializationException, and lazy fetching avoids loading unnecessary columns or associations.

Multiple choice technology platforms and products
  1. Highlight the method and select References > Workspace option in the search drop-down

  2. Possible only when we have MyEclipse plug-in

  3. N/A in Eclipse

  4. None of these

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

Eclipse provides a 'References' feature in the search menu that allows you to find all references to a method, including which methods call it. This back-tracing capability helps understand call hierarchies. Option B is incorrect because this is built into Eclipse, not MyEclipse-specific. Option C is incorrect because the feature exists natively.

Multiple choice technology programming languages
  1. It is possible for a program to free memory at a given time.

  2. Garbage Collection feature of Java ensures that the program never runs out of memory.

  3. It is possible for a program to make an object available for Garbage Collection.

  4. The finalize method of an object is invoked before garbage collection is performed on the object.

Reveal answer Fill a bubble to check yourself
C,D Correct answer
Explanation

Programs can suggest garbage collection by removing references (making objects eligible for GC), but cannot force immediate collection (A false). GC does not prevent running out of memory (B false). The finalize() method is called before an object is collected (C and D true).

Multiple choice technology testing
  1. A. Value

  2. B. Import

  3. C. GetCell

  4. D. GetValue

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

The DataTable.Value method is used to retrieve or set data from the Data Table during test execution. While GetCell can also retrieve data, Value is the primary method for parameter-based data access. Import is for loading external files, and GetValue is not a standard DataTable method.