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. Closing the stream

  2. flushing the stream

  3. writing to the stream

  4. writing a line separator to the stream

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

BufferedWriter has the newLine() method to write a platform-dependent line separator to the stream. FileWriter does not have a specific line-separator method and inherits standard write methods from OutputStreamWriter and Writer. Both classes support closing, flushing, and writing.

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

A: False - programs cannot directly free memory; GC handles it automatically. B: False - GC doesn't prevent OutOfMemoryError if memory is exhausted faster than GC runs. C: True - programs can make objects eligible for GC by removing references (setting to null, reassigning). D: True - finalize() is called before GC, though it's deprecated since Java 9.

Multiple choice technology programming languages
  1. Tree

  2. Stack

  3. Queue

  4. Array

  5. LinkedList

  6. Map

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

Map is a core interface in the Java Collections Framework that represents a mapping of keys to values. Tree, Stack, Queue, and LinkedList are implementation classes, not interfaces. Array is a language construct, not a Collections Framework interface.

Multiple choice technology programming languages
  1. abstract, default, if, private, this

  2. do, implements, protected, boolean, throw

  3. case, extends, int, short, try

  4. import, break, double, exception, throws

  5. byte, else, instanceof, return, transient

  6. None of the above

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

All words in options A, B, C, and E are Java keywords. Option D contains 'exception' which is not a keyword (it's a class name in java.lang). 'import', 'break', 'double', and 'throws' are valid keywords.

Multiple choice technology programming languages
  1. )Integer

  2. Boolean

  3. Character

  4. Long

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

The Character wrapper class does not have a valueOf(String) method because it would be unclear which character from a multi-character string should be returned. Character only has valueOf(char). Other wrappers like Integer, Boolean, and Long all accept String input for conversion.

Multiple choice technology programming languages
  1. integer

  2. boolean

  3. character

  4. long

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

In Java, the wrapper classes Integer, Boolean, and Long all have static valueOf(String) methods that parse string representations into their respective wrapper types. The Character wrapper class does not have a valueOf(String) method - it has valueOf(char) instead, which requires a char primitive.

Multiple choice technology programming languages
  1. System.free()

  2. System.setGarbageCollection()

  3. System.setGarbage()

  4. System.gc()

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

System.gc() is the standard method to suggest JVM garbage collection, though it's only a suggestion to the JVM rather than a guarantee. The other options (System.free(), System.setGarbageCollection(), System.setGarbage()) are not valid Java API methods.

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.