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. string strMessage = "Processing Successful"; string strScript = "<script language=javascript>alert('" + strMessage + "');</script>"; if(!ClientScript.IsClientScriptBlockRegistered("Startup")) {ClientScript.RegisterClientScriptBlock(Page.GetTy

  2. string strMessage = "Processing Successful"; string strScript = " alert('" + strMessage + "');"; if(!ClientScript.IsClientScriptBlockRegistered("Startup")) {ClientScript.RegisterClientScriptBlock(Page.GetType(), "startup", strScript);}

  3. string strMessage = "Processing Successful"; string strScript = "<script language=javascript>alert('" + strMessage + "');</script>"; ClientScript. RegisterStartupScript(Page.GetType(), "startup", strScript);

  4. string strMessage = "Processing Successful"; string strScript = " alert('" + strMessage + "');"; ClientScript. RegisterStartupScript(Page.GetType(), "startup", strScript);

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

RegisterClientScriptBlock registers script that executes during page load (before HTML renders), making it suitable for client-side alerts triggered by server-side processing. RegisterStartupScript would run after page load, which is less appropriate for immediate alerts.

Multiple choice technology programming languages
  1. ArithmeticException

  2. RunTimeException

  3. NumberFormatException

  4. NumberError

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

To solve this question, the user needs to be familiar with the parseInt() method and the types of exceptions it can throw.

The parseInt() method is used to convert a string to an integer. If the string contains illegal data (i.e. not a valid integer), the method will throw an exception.

Now, let's go through each option and explain why it is right or wrong:

A. ArithmeticException: This exception is thrown when an arithmetic operation produces an error, such as dividing by zero. It is not related to the parseInt() method, so this option is incorrect.

B. RunTimeException: This is a general exception that can be thrown at runtime for a variety of reasons. It is not specific to the parseInt() method, so this option is incorrect.

C. NumberFormatException: This is the correct answer. This exception is thrown by parseInt() when the string being parsed contains illegal data. For example, if the string "abc" is passed to parseInt(), a NumberFormatException will be thrown because "abc" is not a valid integer.

D. NumberError: This is not a standard Java exception, so this option is incorrect.

Therefore, the answer is: C. NumberFormatException

Multiple choice technology programming languages
  1. Error, Exception

  2. Exception, RunTimeException

  3. Throwable, RunTimeException

  4. ArithmeticException, RunTimeException

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

To solve the question and determine the order of exception types from most specific to least specific, you need to understand the hierarchy of exception classes in Java.

In Java, exceptions are organized in a hierarchical structure. The topmost class in this hierarchy is Throwable, which is the superclass of all exceptions and errors. The Throwable class has two immediate subclasses: Error and Exception.

Error represents serious issues that typically cannot be handled by your program, such as OutOfMemoryError or StackOverflowError. Errors are usually caused by external factors or problems with the Java Virtual Machine (JVM) itself.

Exception is the superclass of all exceptions that can be thrown by a Java program. It further branches into various subclasses, including RuntimeException and its subclasses.

RuntimeException and its subclasses represent exceptions that occur during the execution of the program and are often caused by logical errors or violations of programming conventions. Examples include NullPointerException or ArithmeticException.

Based on this hierarchy, we can determine the order of exception types from most specific to least specific:

  1. Option D: ArithmeticException, RunTimeException

    • ArithmeticException is a specific type of exception that occurs when an arithmetic operation fails, such as dividing by zero.
    • RuntimeException is a more general class that encompasses a broader range of runtime exceptions.
  2. Option C: Throwable, RunTimeException

    • Throwable is the top-level class in the exception hierarchy and is the superclass of all exceptions and errors.
    • RuntimeException is a specific subclass of Exception.
  3. Option B: Exception, RunTimeException

    • Exception is a broader class that encompasses a wide range of exceptions, including runtime and non-runtime exceptions.
    • RuntimeException is a more specific subclass of Exception that represents runtime exceptions.
  4. Option A: Error, Exception

    • Error is a subclass of Throwable and represents serious issues that usually cannot be recovered from.
    • Exception is a superclass that encompasses both runtime and non-runtime exceptions.

Therefore, the correct order of exception types from most specific to least specific is:

D. ArithmeticException, RunTimeException

Multiple choice technology programming languages
  1. This is not legal, so the program will not compile.

  2. The method throws the exception to its caller, exactly if there were no try{} block.

  3. The program halts immediately.

  4. The program ignores the exception.

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

If an exception is thrown inside a try block and no matching catch block is found in the current method, the exception propagates up the call stack to the calling method, behaving as if no try block existed.

Multiple choice technology programming languages
  1. Always after execution has left a try{} block, no matter for what reason.

  2. Only when an unhandled exception is thrown in a try{} block.

  3. Only when any exception is thrown in a try{} block.

  4. Always just as a method is about to finish.

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

The finally block is guaranteed to execute after execution leaves a try block, regardless of whether an exception was thrown, caught, or if the block exited via a return statement.

Multiple choice technology web technology
  1. read

  2. write

  3. flush

  4. getOutputStream

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

When using RequestDispatcher, calling flush() on the PrintWriter or ServletOutputStream commits the response. Once committed, you cannot forward or include (causing IllegalStateException). The flush() method is the direct trigger - read() and write() don't necessarily commit, and getOutputStream() just returns the stream without committing.

Multiple choice technology databases
  1. Trap it with a Handler

  2. Propagate it to the Calling Environment

  3. a & then b

  4. b & then a

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

Exception handling in PL/SQL follows a two-phase process: first trap the exception with an exception handler (WHEN clause in the EXCEPTION block), then optionally propagate it to the calling environment if the handler cannot fully resolve it. Option C ('a & then b') correctly captures this sequence - handle first, propagate if needed. Option A is incomplete because trapping alone doesn't complete the handling. Option B is incorrect as the first step must always be handling. Option D reverses the correct order - you cannot propagate before trapping.

Multiple choice technology programming languages
  1. True

  2. False

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

A method can declare throwing multiple exceptions using a comma-separated list in the method signature. For example: 'void myMethod() throws IOException, SQLException, CustomException {...}' is valid syntax. The throws clause can include any number of checked exceptions that the method might throw but not handle internally.

Multiple choice technology testing
  1. a.) Using the Mock class, related to corresponding Test class

  2. b.) Using the reflection if using Jdk 1.3 or higher version

  3. c.) Both a.) & b.)

  4. d.) No way to test it

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

Private methods can be unit tested using Java Reflection to bypass access modifiers, or by using mocking frameworks and testing subclass/helper structures. Thus, both options are valid approaches.

Multiple choice technology testing
  1. a.) There is no option to test it in Junit

  2. b.) Modifying the testcase to run with SecurityManager that prevents calling System.exit, then catch the SecurityException.

  3. c.) Remove the System.Exit() line then continue the testing with that methods.

  4. d.) Test it everything as-is.

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

To test methods containing System.exit(), you can configure a custom SecurityManager that throws a SecurityException when checkExit is called. The test case can then catch this exception to verify that the exit was triggered without terminating the JVM.

Multiple choice technology programming languages
  1. Garbage collection cannot be forced

  2. Call System.gc()

  3. Call System.gc(), passing in a reference to the object to be garbage-collected

  4. Call Runtime.gc()

  5. Set all references to the object to new values (null, for example)

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

In Java, garbage collection is managed entirely by the JVM and cannot be forced. Calling System.gc() or Runtime.getRuntime().gc() only suggests or requests that the JVM run the garbage collector, but does not guarantee or force its immediate execution.

Multiple choice technology web technology
  1. setTimeout('run()','50000');

  2. setTimer('run()','5');

  3. setTimeout('run()','500');

  4. setTimeout('run()','5000');

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

The setTimeout function schedules a function to execute after a specified delay in milliseconds. Option D correctly sets a 5-second delay (5000 milliseconds). Option A would wait 50 seconds, option C would wait only 0.5 seconds, and option B references a non-existent setTimer function. To convert seconds to milliseconds, multiply by 1000.