0

Java Programming Language Quiz - 4

Description: Java Programming Language Quiz - 4
Number of Questions: 9
Created by:
Tags: java
Attempted 0/9 Correct 0 Score 0

What is the output of following piece of code ?

int x = 2;
 switch (x) {
 case 1:System.out.println("1?);
 case 2:
 case 3:System.out.println("3?);
 case 4:
 case 5:System.out.println("5?);
 }
  1. No output

  2. 3 and 5

  3. 1, 3 and 5

  4. 3


Correct Option: B,D
Explanation:

To solve this question, the user needs to understand the concept of switch case statements in Java. The switch statement tests the value of a variable and compares it with multiple cases. If a case matches the value of the variable, then the code inside that case is executed. If no matching cases are found, then the default case is executed.

In the given code, the variable x is initialized to 2. The switch statement checks the value of x, and since it matches the case 2, the code inside that case is executed. Since there are no break statements, the control falls through to the next case, which is 3. Thus, the output will be "3?".

The case for 1 and 5 are not executed because the value of x is not equal to 1 or 5.

Option A is incorrect because there is output from the code. Option B is incorrect because the output is not just 3 and 5. Option C is incorrect because there is no output for 1.

Therefore, the answer is:

The Answer is: D. 3

  1. the new method should return int

  2. the new method can return any type of values

  3. the argument list of new method should exactly match that of overriden method

  4. the return type of new method should exactly match that of overriden method


Correct Option: C
  1. The element will be successfully added

  2. ArrayIndexOutOfBounds Exception

  3. The Vector allocates space to accommodate up to 15 elements

  4. none of these


Correct Option: A,C

A class can be converted to a thread by implementing the interface ___.

  1. Thread

  2. Runnable

  3. Both of these

  4. none of these


Correct Option: B

AI Explanation

To answer this question, you need to understand the concept of converting a class into a thread in Java.

In Java, a class can be converted to a thread by implementing the Runnable interface. The Runnable interface defines a single method named run(), which is the entry point for the thread when it is started. By implementing this interface, the class can define the code that will be executed in a separate thread when the thread is started.

Option A) Thread - This option is incorrect because the Thread class itself represents a thread, rather than converting a class into a thread.

Option B) Runnable - This option is correct because implementing the Runnable interface allows a class to be converted into a thread by defining the run() method.

Option C) Both of these - This option is incorrect because only implementing the Runnable interface is required to convert a class into a thread. The Thread class is not necessary for this conversion.

Option D) none of these - This option is incorrect because implementing the Runnable interface is the correct way to convert a class into a thread.

The correct answer is Option B) Runnable. This option is correct because implementing the Runnable interface allows a class to be converted into a thread.

Therefore, the correct answer is B) Runnable.

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.

- Hide questions