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
  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

A method is defined in a class as : void processUser(int i) { } If this method is overriden in a sub class,_

  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

AI Explanation

To answer this question, we need to understand the concept of method overriding in Java.

Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its parent class. When overriding a method, certain rules must be followed. Let's go through each option to understand why it is correct or incorrect:

Option A) The new method should return int - This option is incorrect. The return type of the overriding method can be a subtype of the return type of the overridden method, but it cannot be a different type.

Option B) The new method can return any type of values - This option is incorrect. The return type of the overriding method should be covariant with the return type of the overridden method. In other words, it should be the same type or a subtype of the return type of the overridden method.

Option C) The argument list of the new method should exactly match that of the overridden method - This option is correct. The argument list of the overriding method should be the same as the argument list of the overridden method. This ensures that the method signature remains the same.

Option D) The return type of the new method should exactly match that of the overridden method - This option is incorrect. As mentioned earlier, the return type of the overriding method can be a subtype of the return type of the overridden method, but it cannot be a different type.

The correct answer is C. The argument list of the new method should exactly match that of the overridden method. This ensures that the method signature remains the same.

Therefore, the correct answer is 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
  1. Thread

  2. Runnable

  3. Both of these

  4. none of these


Correct Option: B

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
  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
- Hide questions