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 web technology
  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 an exception at runtime

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

The code mixes String objects ("2", "1") with Integer object (3) in a TreeSet without generics. TreeSet orders elements using compareTo, which throws ClassCastException when comparing incompatible types like String and Integer at runtime. Compilation succeeds due to raw type.

Multiple choice technology web technology
  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 an exception at runtime

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

The code mixes String objects ("2", "1") with Integer object (3) in a TreeSet without generics. TreeSet orders elements using compareTo, which throws ClassCastException when comparing incompatible types like String and Integer at runtime. Compilation succeeds due to raw type.

Multiple choice technology web technology
  1. java.util.HashSet

  2. java.util.LinkedHashSet

  3. java.util.List

  4. java.util.ArrayList

  5. java.util.Vector

  6. java.util.PriorityQueue

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

ArrayList is a resizable array implementation that provides indexed access via get(int index) and set(int index, E element) methods. Unlike Vector, ArrayList's methods are not synchronized, making it faster for single-threaded use. HashSet and LinkedHashSet don't provide indexed access. List is an interface, not a concrete class. Vector has synchronized methods. PriorityQueue doesn't support indexed access.

Multiple choice technology web technology
  1. 2 2 3 3

  2. 2 2 3 4

  3. 4 3 3 4

  4. 2 2 3 3 3

  5. 4 3 3 3 3

  6. 2 2 3 3 4

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

At line 16, pq.peek() returns '2' (sorted first among '2', '4'). Next, '1' is offered, '3' is added, and '1' is removed. At line 20, pq.poll() retrieves and removes the lowest element, '2'. Line 21 removes '2' (returns false because '2' was polled), so the if-block doesn't run. Line 22 polls '3' and peeks at '4'.

Multiple choice technology web technology
  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 an exception at runtime

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

TreeSet orders its elements as they are added by comparing them using their natural ordering. When the code attempts to add the Integer 3 after the String "2", the TreeSet throws a ClassCastException at runtime because Integer and String are not mutually comparable.

Multiple choice technology web technology
  1. A request is sent with the HTTP method HEAD.

  2. A request is sent with the HTTP method POST.

  3. A request is sent with the HTTP method GET.

  4. The parameter fullName is the only parameter passed to the web server in the request URL.

  5. The parameters fullName and sbmButton are passed to the web server in the request URL.

  6. The parameter fullName is the only parameter passed to the web server as part of the request body.

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

HTML forms without a method attribute default to GET (option C). The form data is sent in the request body for GET requests (option F). Options A, B, D, E are incorrect: GET (not HEAD or POST) is used, and both fullName and sbmButton parameters are sent in the body, not URL. The question asks which statements are true - options C and F correctly describe the default GET behavior and parameter passing.

Multiple choice technology web technology
  1. Fragment I compiles

  2. Fragment II compiles

  3. Fragment III compiles

  4. Fragment I executes without exception

  5. Fragment II executes without exception

  6. Fragment III executes without exception

Reveal answer Fill a bubble to check yourself
A,B,C,D,F Correct answer
Explanation

All three fragments compile since raw sets allow adding any Object. At runtime, HashSet and LinkedHashSet only require hashing, so they run without issues. However, TreeSet sorts elements, requiring them to be mutually comparable, throwing a ClassCastException when inserting an Object after a String.

Multiple choice technology programming languages
  1. Init(),start(),paint(),stop(),destroy()

  2. Init(),start(),paint(),destroy(),stop()

  3. Init(),paint(),start(),destroy(),stop()

  4. None of the above

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

The correct applet lifecycle follows this sequence: init() (initialization), start() (begins execution), paint() (rendering), stop() (pauses execution), and destroy() (cleanup). This order ensures proper initialization before execution and proper cleanup before termination.

Multiple choice technology programming languages
  1. event-inheritance model

  2. event-delegation model

  3. Both a & b

  4. Event-polymorhism model

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

The event-inheritance model uses subclasses to handle events (older AWT approach), while the event-delegation model uses listeners where event sources delegate handling to separate listener objects (modern Java approach). Both models are valid event handling strategies.

Multiple choice technology
  1. getAllResponseHeaders()

  2. getResponseHeader(header)

  3. getResponseHeader(All)

  4. getAllHttpResponseHeaders()

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

getAllResponseHeaders() is the correct XMLHttpRequest method that retrieves all HTTP response headers as a single string. getResponseHeader(header) gets a specific header. Options C and D are non-existent methods.