Menu
Topics Index
...
`

Test Contains Value


If you need explanation Read this topic

If you need Answer Take test on this topic

Test Contains Value
What will be the output of the following program?
import java.util.*;

public class HashSearch1
{
    public static void main(String args[])
    {
        Hashtable<String, String> hm = new Hashtable<String, String>();
        hm.put("A", "1");
        hm.put("B", "2");
        hm.put("C", "3");
        
        if(hm.containsValue("A"))
        {
            System.out.println("The Hashtable contains value");
        }
        else
        {
            System.out.println("The Hashtable does not contains value.");
        }
        
        if(hm.containsValue("2"))
        {
            System.out.println("The Hashtable contains value.");
        }
        else
        {
            System.out.println("The Hashtable does not contains value.");
        }
    }
}

A. The Hashtable contains value.
The Hashtable does not contains value.
B. The Hashtable does not contains value.
The Hashtable contains value.
C. The Hashtable contains value.
The Hashtable contains value.
D. The Hashtable does not contains value.
The Hashtable does not contains value.
E. Compilation Error or Runtime Error
Topic: Java Hashtable

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is b...hastable stores key value pairs...put method takes key as first and value as second argument. ...in the program....since hm.containsvalue("A") is false since there is no value "A"..so else ia executed and prints has table doesn't contain value...next hm.containsvalue(2) is true...since hash table contains an entry with value 2...so here it prints hash table contains value

Posted by Shashanka Mogaliraju    2015-03-11 03:49:43


ANS IS B

Hashtable was part of the original java.util and is a concrete implementation of a Dictionary.

However, Java 2 re-engineered Hashtable so that it also implements the Map interface. Thus, Hashtable is now integrated into the collections framework. It is similar to HashMap, but is synchronized.

Like HashMap, Hashtable stores key/value pairs in a hash table. When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.

boolean containsValue(Object value) :
Returns true if some value equal to value exists within the hash table. Returns false if the value isn't found.

So here,a is not value that is key so it printed hash table does not contain value.

As 2 is value present in hash table so it prints hash table contain value.

Posted by Mânïshå Mùlchåndânï    2015-03-11 04:36:20


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Shashanka Mogaliraju,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-12 01:52:49


© meritcampus 2019

All Rights Reserved.

Open In App