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 architecture
  1. 1 2

  2. 1 2 3

  3. Compile error

  4. Runtime exception

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

TreeSet requires elements to be mutually Comparable or a Comparator to be provided. Adding String '2' and '1' works, but adding Integer 3 causes ClassCastException at runtime when TreeSet tries to compare Integer with String. The TreeSet uses natural ordering which fails for mixed types.

Multiple choice technology architecture
  1. addSize

  2. getCust

  3. deleteRep

  4. putDimensions

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

JavaBeans naming conventions for getters: get() for non-boolean properties, is() for boolean properties. Option B (getCust) follows this pattern. Option A (addSize) suggests an add operation, not getter naming. Option C (deleteRep) doesn't follow getter/setter pattern. Option D (putDimensions) suggests a map-like operation, not JavaBean convention.

Multiple choice technology architecture
  1. Query length is limited in POST, not limited in GET.

  2. POST is secured whereas data is been submitted as part of URL in GET (not secured).

  3. By using POST method, we cannot submit the form.

  4. None

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

GET method submits form data as part of the URL in the query string, which is visible in browser history, logs, and can be bookmarked - making it insecure for sensitive data. POST method sends data in the request body, which is not visible in the URL and more secure. Additionally, GET has length limitations (URL length restrictions), while POST can send much larger amounts of data.

Multiple choice technology architecture
  1. public class MyRunnable extends Runnable{public void run(){}}

  2. public class MyRunnable extends Object{public void run(){}}

  3. public class MyRunnable implements Runnable{public void run(){}}

  4. public class MyRunnable implements Runnable{void run(){}}

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

To solve this question, the user needs to have knowledge of Java threads, Runnable interface, and the difference between extending and implementing a class/interface.

The code snippet above creates a Thread using a Runnable target. To use this code snippet, the user needs to create a class that implements the Runnable interface. The Runnable interface has only one method, which is the run() method.

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

A. public class MyRunnable extends Runnable{public void run(){}}

This option is incorrect because the Runnable interface cannot be extended. It can only be implemented.

B. public class MyRunnable extends Object{public void run(){}}

This option is incorrect because it does not implement the Runnable interface. Instead, it extends the Object class and defines a run() method, which is not the correct implementation of the run() method from the Runnable interface.

C. public class MyRunnable implements Runnable{public void run(){}}

This option is correct. It creates a class called MyRunnable that implements the Runnable interface. It defines the run() method, which is required by the Runnable interface. Therefore, this code will compile correctly.

D. public class MyRunnable implements Runnable{void run(){}}

This option is incorrect because the run() method is missing the public access modifier. The run() method must be declared as public since it is an implementation of the run() method from the Runnable interface.

Therefore, the correct answer is:

The Answer is: C

Multiple choice technology architecture
  1. ClassCastException

  2. IllegalStateException

  3. NumberFormatException

  4. None

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

Integer.parseInt throws NumberFormatException when the string cannot be parsed as a valid integer. The string 'one' contains letters, not digits, so parsing fails. ClassCastException is for incompatible type conversions, and IllegalStateException is for invalid state timing.

Multiple choice technology architecture
  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 'getXxx()' where Xxx is the property name with the first letter capitalized. 'getCust' correctly follows this convention. 'addSize' and 'deleteRep' don't start with 'get' or 'is', and 'putDimensions' starts with 'put' which isn't a standard accessor prefix.

Multiple choice technology architecture
  1. Query length is limited in POST, not limited in GET.

  2. POST is secured whereas data is been submitted as part of URL in GET (not secured).

  3. By using POST method, we cannot submit the form.

  4. None

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

GET method submits data as part of the URL (visible in browser history/logs), making it insecure for sensitive data. POST sends data in the request body, hiding it from the URL. Also, GET has URL length limitations (typically 2048 characters), while POST does not.

Multiple choice technology programming languages
  1. True

  2. False

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

Java is platform independent because Java source code compiles to bytecode that runs on any device with a Java Virtual Machine (JVM). The JVM interprets the bytecode for the specific underlying operating system and hardware.

Multiple choice technology architecture
  1. java.util.List

  2. java.lang.String

  3. java.io.BufferedInputStream.

  4. None of the above.

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

The Decorator pattern attaches additional responsibilities to an object dynamically. In Java's I/O streams, BufferedInputStream decorates an underlying InputStream to add buffering functionality. This allows adding behavior to individual objects without affecting other objects of the same class.