Tag: programming languages

Questions Related to programming languages

Synchronized resizable-array implementation of the List interface is _____________?

  1. Vector

  2. ArrayList

  3. Hashtable

  4. HashMap


Correct Option: A

AI Explanation

To answer this question, you need to understand the concept of synchronization and the resizable-array implementation of the List interface.

The synchronized resizable-array implementation of the List interface is known as a Vector.

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

Option A) Vector - This option is correct because a Vector is a synchronized resizable-array implementation of the List interface. It provides thread-safe operations, meaning that multiple threads can access and modify the Vector object without causing data corruption.

Option B) ArrayList - This option is incorrect because although ArrayList is a resizable-array implementation of the List interface, it is not synchronized. It does not provide inherent thread-safety, so if multiple threads access and modify the ArrayList simultaneously, it can lead to data corruption.

Option C) Hashtable - This option is incorrect because Hashtable is not a resizable-array implementation of the List interface. It is an implementation of the Map interface and provides key-value pairs, not a sequence of elements like the List interface.

Option D) HashMap - This option is incorrect because HashMap is also not a resizable-array implementation of the List interface. It is also an implementation of the Map interface, providing key-value pairs, not a sequence of elements like the List interface.

The correct answer is A) Vector. This option is correct because Vector is the synchronized resizable-array implementation of the List interface. It provides thread-safe operations, making it suitable for multi-threaded environments.

What is the output for the below code ? public class Test { public static void main(String argv[]){ ArrayList list = new ArrayList(); ArrayList listStr = list; ArrayList listBuf = list; listStr.add(0, "Hello"); StringBuffer buff = listBuf.get(0); System.out.println(buff.toString()); } }

  1. Hello

  2. Compile error

  3. java.lang.ClassCastException

  4. null


Correct Option: C

What is the output for the below code ? import java.util.LinkedList; import java.util.Queue; public class Test { public static void main(String... args) { Queue q = new LinkedList(); q.add("newyork"); q.add("ca"); q.add("texas"); show(q); } public static void show(Queue q) { q.add(new Integer(11)); while (!q.isEmpty ( ) ) System.out.print(q.poll() + " "); } }

  1. Compile error : Integer can't add

  2. newyork ca texas 11

  3. newyork ca texas

  4. newyork ca


Correct Option: B
Explanation:

To understand the output of the given code, it is important to know the concept of Queue in Java.

A Queue is a data structure that follows the First-In-First-Out (FIFO) principle. In a Queue, elements are added from the rear and removed from the front. The methods used to add elements to the Queue are 'add' and 'offer', whereas 'poll' and 'remove' are the methods used to remove elements from the Queue.

Now let's go through the code and analyze the output.

The code creates a Queue of Strings named 'q' and adds three Strings to it: "newyork", "ca", and "texas". Then the method 'show(q)' is called, and a new Integer object 11 is added to the Queue 'q' inside the 'show' method. Finally, the elements of the Queue are printed using the 'poll' method.

Since the Queue 'q' has been defined as a Queue of Strings, adding an Integer object to the Queue will result in a compile-time error. Therefore, option A is incorrect.

The 'poll' method removes and returns the head of the Queue. So, the output will be the elements of the Queue in the order they were added. Thus, option C is incorrect because it does not include the added Integer object.

Therefore, the correct answer is:

The Answer is: B. newyork ca texas 11

What are the new features added to Java 1.5?

  1. Boxing and UnBoxing

  2. Generics

  3. Enhanced for

  4. Iterator


Correct Option: A,B,C

Key word "transient" is used so that the state of the object is not saved... (T/F)

  1. True

  2. False


Correct Option: A

Can you call one constructor from another if a class has multiple constructors

  1. True

  2. False


Correct Option: A

What methods java providing for Thread communications

  1. Wait

  2. notify

  3. notifyAll

  4. Sleep


Correct Option: A,B,C