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 php
  1. Using a for loop

  2. Using a foreach loop

  3. Using a while loop

  4. Using a do..while loop

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

To modify array elements during iteration in older PHP versions, a 'for' loop using the index is safest. While 'foreach' can use references (&$value) in newer PHP, the 'for' loop directly accesses the original array index, ensuring the modification persists without reference-related side effects.

Multiple choice java
  1. Iterator

  2. Map

  3. Enumeration

  4. Iterable

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

The enhanced for-loop (for-each) works with arrays and any object implementing the Iterable interface. The Iterable interface requires the implementation of an iterator() method. While Maps and Iterators are related, the expression itself must implement Iterable (or be an array).

Multiple choice java
  1. Compilation fails

  2. one two three four

  3. four three two one

  4. four one three two

  5. one two three four one

  6. one four three two one

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

TreeSet stores elements in a sorted, ascending order (natural ordering for Strings). It also rejects duplicates. The strings 'four', 'one', 'three', 'two' are added, and the second 'one' is ignored. The natural alphabetical order is 'four', 'one', 'three', 'two'.

Multiple choice java
  1. _jspService()

  2. jspInit()

  3. jspDestroy()

  4. getParameter()

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

In the JSP lifecycle, the _jspService() method is generated by the container and corresponds to the body of the JSP page. The JSP specification forbids the user from overriding this method, whereas jspInit() and jspDestroy() can be overridden.

Multiple choice java
  1. public void amethod(int s, String i){}

  2. public void Amethod(int i, String s) {}

  3. public void amethod(int i, String mystring){}

  4. None of the above

  5. public int amethod(int i, String s){}

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

The question asks which method can be legally placed in the class. Option A changes only parameter names (s, i), which is invalid overloading (same signature). Option C changes a parameter name (mystring), same signature. Option E changes the return type only, which is invalid. Option B changes the method name to 'Amethod' (case sensitivity), creating a valid overloaded method.

Multiple choice java
  1. Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type

  2. The size of a string can be retrieved using the length property.

  3. Strings are a primitive type in Java that overloads the + operator for concatenation

  4. The String class is implemented as a char array, elements are addressed using the stringname[] convention

Reveal answer Fill a bubble to check yourself
C Correct answer
Multiple choice .net
  1. Button

  2. Form

  3. Label

  4. TextBox

  5. Timer

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

To answer this question, the user needs to know the purpose of the CancelButton property and what object it belongs to.

The CancelButton property is used to specify the button on a form that is clicked when the user presses the Esc key. This allows the user to quickly cancel an operation or close a dialog box.

Based on this information, we can conclude that the CancelButton property belongs to the Form object. Therefore, the correct answer is:

The Answer is: B (Form)

Multiple choice .net
  1. cause an error.

  2. cannot be done.

  3. de-select any selected item.

  4. Both a and b.

  5. All of the above.

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

To understand the effect of setting the SelectedIndex property of a ListBox to -1, it is important to know how a ListBox works and the purpose of the SelectedIndex property.

A ListBox is a control that displays a list of items, and the SelectedIndex property represents the index of the currently selected item in the list. It is an integer value that can range from 0 to (number of items - 1).

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

A. cause an error: This option is incorrect. Setting the SelectedIndex property of a ListBox to -1 does not cause an error. It is a valid operation that can be performed.

B. cannot be done: This option is incorrect. It is possible to set the SelectedIndex property of a ListBox to -1. It is a valid value that represents no selected item in the list.

C. de-select any selected item: This option is correct. When the SelectedIndex property of a ListBox is set to -1, it deselects any previously selected item in the list. This means that no item in the list will be highlighted or considered as the currently selected item.

D. Both a and b: This option is incorrect. Setting the SelectedIndex property of a ListBox to -1 does not cause an error, and it can be done. Therefore, option D is not the correct answer.

E. All of the above: This option is incorrect. While setting the SelectedIndex property of a ListBox to -1 does not cause an error, can be done, and deselects any selected item, option E is not the correct answer as it erroneously includes options A and B.

The Answer is: C. de-select any selected item.

Multiple choice .net
  1. Items.RemoveAt

  2. Item.RemoveAt

  3. Items.ClearAt

  4. Item.ClearAt

  5. Items.Clear

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

To solve this question, the user needs to have knowledge about the methods available for manipulating a ListBox control.

A ListBox is a control that allows users to select one or more items from a list of available options. To remove an item from a ListBox, the control provides several options.

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

A. Items.RemoveAt - This method removes the item at the specified index position from the ListBox control's Items collection. It removes a single item at a time, so it is the correct answer to this question.

B. Item.RemoveAt - This method attempts to remove an item with a specific name from the ListBox. Since it tries to remove an item based on its name, this option is incorrect.

C. Items.ClearAt - This method does not exist in the ListBox control. Therefore, this option is incorrect.

D. Item.ClearAt - This method does not exist in the ListBox control. Therefore, this option is incorrect.

E. Items.Clear - This method removes all items from the ListBox control's Items collection at once. Therefore, this option is incorrect.

The Answer is: A. Items.RemoveAt

Multiple choice .net
  1. Checked

  2. CheckedChanged

  3. Selected

  4. SelectedChanged

  5. SelectionChanged

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

To determine which event is activated when a RadioButton is selected, you need to understand the event model of the programming framework or language you are working with. In this case, assuming you are referring to a common event model, such as the one used in Microsoft's .NET framework, the answer would be B. CheckedChanged.

In most event-driven programming frameworks, a RadioButton control has a "Checked" property that represents its checked state. When the user selects or deselects a RadioButton, the Checked property changes, triggering the CheckedChanged event. This event is specifically designed to handle the change in the checked state of a RadioButton.

Therefore, the answer is:

B. CheckedChanged

Multiple choice .net
  1. Use the Class and Method combo boxes in the Code Editor window.

  2. Double click on the object in the Form Designer window.

  3. Type the procedure declaration in the Code Editor window.

  4. Both a and b.

  5. All of the above.

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

To answer this question, you need to understand what is a procedure stub and what is an object's default event in VB.Net. A procedure stub is a skeleton code that defines the name, parameters, and return type of a procedure, but does not contain any executable statements. An object's default event is the event that is triggered when the user interacts with the object in a common way, such as clicking a button or selecting an item from a list. For example, the default event for a button is the Click event, and the default event for a list box is the SelectedIndexChanged event.

Let's go through each option to understand why it is correct or incorrect:

Option A) Use the Class and Method combo boxes in the Code Editor window. - This option is correct because you can use the Class and Method combo boxes in the Code Editor window to select an object and its default event, and then Visual Studio will automatically generate a procedure stub for you. For example, if you select Button1 and Click from the combo boxes, Visual Studio will create a procedure stub like this:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

End Sub

Option B) Double click on the object in the Form Designer window. - This option is correct because you can also double click on an object in the Form Designer window to create a procedure stub for its default event. Visual Studio will open the Code Editor window and generate a procedure stub similar to option A.

Option C) Type the procedure declaration in the Code Editor window. - This option is incorrect because typing the procedure declaration in the Code Editor window is not enough to create a procedure stub for an object's default event. You also need to specify which object and event the procedure handles using the Handles keyword. For example, if you type this:

Private Sub Button1_Click(sender As Object, e As EventArgs)

End Sub

You still need to add Handles Button1.Click at the end of the declaration to make it a valid procedure stub.

Option D) Both a and b. - This option is correct because both options A and B are valid ways to write a procedure stub for an object's default event.

Option E) All of the above. - This option is incorrect because it includes option C, which is not a valid way to write a procedure stub for an object's default event.

The correct answer is D. This option is correct because both options A and B are valid ways to write a procedure stub for an object's default event.

Multiple choice .net
  1. Close

  2. Peak

  3. Flush

  4. Both a and b.

  5. All of the above.

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

To solve this question, the user needs to have a basic understanding of the StreamReader and StreamWriter classes in .NET.

Option A: Close - This method is found in both the StreamReader and StreamWriter classes. It is used to close the underlying stream and free up any resources associated with it.

Option B: Peek - This method is not found in both the StreamReader and StreamWriter classes. It is only found in the StreamReader class and is used to return the next available character without reading it from the underlying stream.

Option C: Flush - This method is found in both the StreamReader and StreamWriter classes. It is used to flush the buffer and write any remaining data to the underlying stream.

Option D: Both A and B - This option is incorrect because option B is not found in both the StreamReader and StreamWriter classes.

Option E: All of the above - This option is incorrect because option B is not found in both the StreamReader and StreamWriter classes.

Therefore, the correct answer is:

The Answer is: A. Close.

Multiple choice .net
  1. IO

  2. Stream

  3. StreamIO

  4. Both a and b.

  5. All of the above.

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

Both StreamReader and StreamWriter inherit from the abstract Stream class, which is the base class for all input/output streams in .NET. IO is a namespace, not a class. StreamIO isn't a standard .NET class. The Stream class provides fundamental read/write methods that these classes extend.