what is the output of compiling an running the following code? import java.util.*; class CheckTest { public static void main(String [] args) { Hashtable ht = new Hashtable(); ht.put("chec", "check"); ht.put(1000, "check"); // line 1 ht.put("check", 20.01); // line 2 System.out.print(ht.get("chec") + " "); System.out.print(ht.get(1000) + " "); //line 3 System.out.print(ht.get("check")); //line 4 } }

  1. Compilation fails due to error at line 1,2,3,4

  2. Compilation fails due to error at line 3,4

  3. Compiles fine and exception is thrown at runtime.

  4. Compiles fine and prints "check check 20.01"


Correct Option: D
Explanation:

To solve this question, the user needs to understand the concepts of Hashtable and data types in Java.

The code creates a Hashtable named ht, which maps keys to values. It puts three key-value pairs into the Hashtable:

  • "chec" is mapped to "check"
  • 1000 is mapped to "check"
  • "check" is mapped to 20.01

The code then retrieves the values corresponding to three keys and prints them out.

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

Option A: Compilation fails due to error at line 1,2,3,4 This option is incorrect. There are no compilation errors in this code.

Option B: Compilation fails due to error at line 3,4 This option is incorrect. There are no compilation errors in this code.

Option C: Compiles fine and exception is thrown at runtime. This option is incorrect. The code compiles without errors, but no exception is thrown at runtime. The output of the code is "check check 20.01".

Option D: Compiles fine and prints "check check 20.01" This option is correct. The code compiles without errors, and when run, it prints "check check 20.01". The keys "chec" and 1000 are mapped to the value "check", while the key "check" is mapped to the value 20.01.

Therefore, the answer is: D. Compiles fine and prints "check check 20.01".

Find more quizzes: