Computer Knowledge

Java Core Classes and Threads

1,473 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. The before() method will print 1 2

  2. The before() method will print 1 2 3

  3. The before() method will print three numbers, but the order cannot be determined

  4. The before() method will not compile

  5. The before() method will throw exception at runtime

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

The code will throw a ClassCastException at runtime. A TreeSet is genericized to hold objects of the same type, but raw types (without generics) allow mixing. When the code adds '2' (String), then 3 (auto-boxed to Integer), then '1' (String), the TreeSet tries to sort them using natural ordering. When it compares the Integer 3 with String '1', it cannot cast Integer to String, causing a ClassCastException.

Multiple choice technology programming languages
  1. 1 Object

  2. 2 Objects

  3. 3 Objects

  4. 4 Objects

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

The statement String s = 'abc' creates one object in the String Constant Pool. String s1 = new String('def') creates two objects: one in the String Constant Pool and one in the heap. In total, three objects are created, making '3 Objects' the correct answer.

Multiple choice technology testing
  1. show(long a,byte b){ }

  2. show(byte b, long a){ }

  3. runtime exception

  4. none of the above

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

In Java, calling show(10, 10) passing two integer literals (int) requires implicit promotions. Both show(long, byte) and show(byte, long) would require one promotion to long and one demotion/narrowing to byte, which is invalid without casting. Hence, it causes a compile-time ambiguity or error, matching none of the above options.

Multiple choice technology programming languages
  1. True

  2. False

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

To solve this question, the user needs to know the basic concept of thread safety in Java and how it applies to local variables in methods like service(), doPost() and doGet().

The answer to this question is:

A. True

Explanation:

Local variables declared within a method are thread-safe because they are confined to the method and are not shared between threads. Each thread, when executing the method, will have its own copy of the local variables declared within the method. As a result, other threads cannot modify the values of these local variables, ensuring thread safety.

Therefore, the correct answer is option A, true.

Multiple choice technology programming languages
  1. getParameterValue()

  2. getParameters()

  3. getParamValues()

  4. getParameterValues()

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

The getParameterValues() method of the ServletRequest interface returns an array of String objects containing all of the values that the given request parameter has, or null if the parameter does not exist. The other methods do not exist or return single values.

Multiple choice technology programming languages
  1. True

  2. False

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

Java has both static and dynamic dispatch. Static dispatch occurs for private, static, and final methods - these are resolved at compile time based on the declared type. Dynamic dispatch (virtual method invocation) occurs for overridden instance methods, resolved at runtime based on the actual object type.

Multiple choice technology security
  1. Identifying all possible erroneous inputs and managing how the application responds to them

  2. During application execution, if some special conditions are met, then a specific subroutine 'exception handler' is called

  3. Commercial runtime environments have tools that record debugging information from memory at the time of exception to provide 'root-cause' analysis information later.

  4. All of the above

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

To answer this question, the user needs to have knowledge about exception handling in programming.

Option A is incorrect because identifying all possible erroneous inputs and managing how the application responds to them is a part of input validation. It is not the same as exception handling.

Option B is partially correct. During application execution, if some special conditions are met, then a specific subroutine 'exception handler' is called. Exception handling involves detecting errors that occur during the execution of a program and taking appropriate action to handle them. The "specific subroutine" is the exception handler which is executed when an exception is thrown.

Option C is also partially correct. Commercial runtime environments have tools that record debugging information from memory at the time of exception to provide 'root-cause' analysis information later. This tool is essential to debug errors and improve the quality of the software.

Option D is correct because all the options A, B, and C are correct explanations of exception handling.

Therefore, the answer is: D. All of the above.