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. jspDestroy()

  2. jspInit()==

  3. _jspService()

  4. getParameter()

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

In JSP, jspInit() and jspDestroy() can be overridden by the page author to initialize or clean up resources. However, _jspService() is generated by the container and must never be overridden. The option for jspInit() has trailing equal signs, making jspDestroy() the cleanest correct choice.

Multiple choice technology programming languages
  1. jspDestroy()

  2. jspInit()

  3. _jspService()

  4. getParameter()

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

The _jspService() method is automatically generated by the JSP container and cannot be overridden. It implements the HttpJspPage interface and handles request processing. In contrast, jspInit() and jspDestroy() can be overridden to provide initialization and cleanup logic, and getParameter() is a standard request method.

Multiple choice technology programming languages
  1. setContentWriter()

  2. setWriter()

  3. setPrintType()

  4. setContentType()

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

setContentType() must be called before using PrintWriter to inform the browser about the content type (MIME type) and character encoding of the response. Without setting the content type first, the PrintWriter's output may not be correctly interpreted by the client. This is typically done via response.setContentType() before response.getWriter().

Multiple choice technology programming languages
  1. None of the above

  2. content safe

  3. synchornised

  4. thread safe

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

Setting isThreadSafe to "false" in a JSP page directive tells the container to implement the SingleThreadModel interface for that JSP. This ensures thread safety by guaranteeing that only one thread executes the JSP service method at a time, serializing concurrent requests.

Multiple choice technology programming languages
  1. The code will not compile.

  2. A RuntimeException will occur at lines 15/16

  3. An IOException will occur at line 18

  4. The string "Password=WebCert" will be returned to the requester

  5. A, B, and C above

  6. A, B, C, and D above.

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

The code uses ServletContext.getResourceAsStream() which DOES work for WEB-INF resources from server-side code. The file path '/WEB-INF/securefiles/secure.txt' is correct. getResourceAsStream() returns null if resource not found (no exception). br.readLine() returns the string content (no exception). System.out.println prints to console, not HTTP response. Therefore: A (compile error) is FALSE, B (RuntimeException) is FALSE, C (IOException) is FALSE, D (string returned to requester) is FALSE. The code compiles and runs without exceptions, but does NOT return the string to the HTTP requester. Since A, B, C, and D are all false, answer F (A, B, C, and D above) is correct.

Multiple choice technology programming languages
  1. getInitParameterNames()

  2. getInitParameter(String name)

  3. getServletName()

  4. getServletContext()

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

The getInitParameter(String name) method of a Servlet returns a String containing the value of the named initialization parameter, or null if the parameter does not exist. Other methods like getServletContext() do not return null.

Multiple choice technology programming languages
  1. A. Arrays.sort(s);

  2. B. s = new TreeSet(s);

  3. C. Collections.sort(s);

  4. D. s = new SortedSet(s);

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

To sort the keys in the props HashMap, the user needs to know about the different data structures available in Java that can help to sort a collection of keys. The user also needs to know about the methods provided by the Java Collections framework that can be used to perform sorting on collections.

Option A: Arrays.sort(s); is incorrect. The Arrays.sort() method is used to sort arrays, not sets or maps. It will result in a compilation error.

Option B: s = new TreeSet(s); is correct. A TreeSet is a sorted set that orders its elements based on their natural ordering or based on a comparator provided at the time of creation. By creating a new TreeSet using the Set of keys from the props HashMap, the keys will be automatically sorted.

Option C: Collections.sort(s); is incorrect. The Collections.sort() method is used to sort lists, not sets or maps. It will result in a compilation error.

Option D: s = new SortedSet(s); is incorrect. SortedSet is an interface and cannot be instantiated. Moreover, the Set obtained from the props HashMap is already sorted.

Therefore, the correct answer is:

The Answer is: B. s = new TreeSet(s);

Multiple choice technology programming languages
  1. addSize

  2. getCust

  3. deleteRep

  4. putDimensions

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

JavaBeans getter methods follow the pattern get where the property name starts lowercase. getCust() correctly accesses a property named cust. The other options don't follow JavaBeans conventions: addSize sounds like a bulk operation, deleteRep and putDimensions don't use the get prefix for accessors.

Multiple choice technology web technology
  1. A required input was not set or mapped and so did not exist at run-time

  2. The "switch" variable of a Branch operation in the Flow was NULL at run-

  3. The Flow executed an Exit operation with the "signal failure" option

  4. A Loop operation in the Flow service has no input array defined

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

A NullPointerException in Flow tracing most commonly occurs when trying to access a pipeline variable that doesn't exist because a required input wasn't set or mapped. Options B, C, and D cause different errors (branch on NULL, exit signal failure, or no loop array), not necessarily NPE.

Multiple choice technology programming languages
  1. The code does not compile.

  2. The code compiles cleanly and shows “Object Version”.

  3. The code compiles cleanly and shows “String Version”

  4. The code throws an Exception at Runtime.

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

In Java, a top-level class cannot be private - only public, abstract, and final are allowed. The code attempts to declare "private class AQuestion" which violates this rule. Therefore, the code does not compile. If it were a valid inner class, the overload resolution would select the more specific String method for null argument.

Multiple choice technology web technology
  1. Cookie class can not take parameters in it's constructor

  2. The request object is used for creating cookies

  3. Although no error will be reported the use of the <jsp:include/> action means that the response object can't be used to create cookies

  4. The <jsp:include/> action must be placed inside the script block

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

The action flushes the response buffer before including the target resource. Once headers are sent (which happens during the flush), you cannot set cookies because cookies require HTTP headers. Any attempt to add cookies via response.addCookie() after a jsp:include will fail or be ignored.