Tag: java

Questions Related to java

  1. double D = 45456.444;

  2. long L = 45784;

  3. int I = L;

  4. int J = (int) D;


Correct Option: C
  1. After object B is notified, or after two seconds

  2. After the lock on B is released, or after two seconds

  3. Two seconds after object B is notified

  4. Two seconds after lock B is released.


Correct Option: A

Given:

TreeSet map = new TreeSet();
 map.add("one");
 map.add("two");
 map.add("three");
 map.add("four"};
 map.add("one");
 Iterator it = map.iterator();
 while (it.hasNext() ) {
 System.out.print( it.next() + " " );
 }
  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


Correct Option: D

AI Explanation

To answer this question, let's analyze the given code step by step:

TreeSet map = new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it = map.iterator();
while (it.hasNext()) {
    System.out.print(it.next() + " ");
}

In this code, a TreeSet named map is created. A TreeSet is a collection that stores elements in a sorted and unique order.

The following elements are added to the map:

  • "one"
  • "two"
  • "three"
  • "four"
  • "one" (Note that "one" is added again)

Next, an iterator named it is created using the iterator() method of the TreeSet. The iterator allows iterating over the elements in the TreeSet in a sequential manner.

The while loop is then used to iterate over the elements in the TreeSet using the iterator. The condition it.hasNext() checks if there are more elements to iterate over. If there are, the loop continues.

Inside the loop, it.next() is used to retrieve the next element from the TreeSet. This element is then printed using System.out.print().

Let's analyze the output of the code:

  • The elements in the TreeSet are stored in a sorted order.
  • Since "four" comes before "one" in alphabetical order, "four" is printed first.
  • Next, "one" is printed.
  • "three" comes before "two" in alphabetical order, so "three" is printed next.
  • Finally, "two" is printed.

Therefore, the output of the code is: "four one three two".

The correct answer is D. "four one three two".

Which is true about a method-local inner class?

  1. It must be marked final

  2. It can be marked abstract

  3. It can be marked public

  4. It can be marked static


Correct Option: B

Which is true about an anonymous inner class?

  1. It can extend exactly one class and implement exactly one interface

  2. It can extend exactly one class and can implement multiple interfaces

  3. It can extend exactly one class or implement exactly one interface

  4. It can implement multiple interfaces regardless of whether it also extends a class

  5. It can implement multiple interfaces if it does not extend a class


Correct Option: C

Which is true?

  1. The java command can access classes from more than one package, from a single JAR file.

  2. JAR files can be used with the java command but not with the javac command.

  3. In order for JAR files to be used by java, they MUST be placed in the /jre/lib/ext sub-directory within the J2SE directory tree.

  4. When a part of a directory tree that includes subdirectories with files is put into a JAR file, all of the files are saved in the JAR file, but the subdirectory structure is lost.


Correct Option: A

Which of the following statement is true?

  1. X extends Y is correct if and only if X is a class and Y is an interface

  2. X extends Y is correct if and only if X is an interface and Y is a class

  3. X extends Y is correct if X and Y are either both classes or both interfaces

  4. X extends Y is correct for all combinations of X and Y being classes and/or interfaces.


Correct Option: C

AI Explanation

To answer this question, you need to understand the concept of inheritance in object-oriented programming.

Option A) X extends Y is correct if and only if X is a class and Y is an interface - This option is incorrect because in Java, a class can extend another class or implement an interface. It is not limited to only extending classes and implementing interfaces.

Option B) X extends Y is correct if and only if X is an interface and Y is a class - This option is incorrect because in Java, a class can extend another class or implement an interface. It is not limited to only extending interfaces and implementing classes.

Option C) X extends Y is correct if X and Y are either both classes or both interfaces - This option is correct. In Java, a class can extend another class and implement multiple interfaces. Similarly, an interface can extend multiple interfaces. So, X extends Y is correct if both X and Y are either both classes or both interfaces.

Option D) X extends Y is correct for all combinations of X and Y being classes and/or interfaces - This option is incorrect because, as explained above, extending a class and implementing an interface are two different concepts in Java. The correct statement is that X extends Y is correct if both X and Y are either both classes or both interfaces.

The correct answer is Option C.

  1. The attributes of the class are all private

  2. The class refers to a small number of other objects

  3. The object contains only a small number of variables

  4. The object is referred to using an anonymous variable, not directly

  5. The reference variable is declared for an interface type, not a class. The interface provides a small number of methods


Correct Option: E
  1. must be created via a Java IDE.

  2. can run on different machines, but only with the same Operating System.

  3. are typically based on Servlets, JSPs and Enterprise Java Beans.

  4. need no runtime environment.


Correct Option: C