HashMap and LinkedHashMap CIP

Last Updated :
Discuss
Comments

Question 1

What is the default initial capacity of a HashMap?

  • A

    64

  • B

    32

  • C

    16

  • D

    None of these

Question 2

Which of the following statements is true about the HashMap class in Java?

  • A

    It maintains the insertion of the Map interface

  • B

    It is synchronized

  • C

    It is an implementation of the Map interface

  • D

    None of these

Question 3

Which of the following statements is true about the LinkedHashMap class in Java?

  • A

    It does not allow null keys or values.

  • B

     It is an implementation of the Map interface.

  • C

     It is an implementation of the HashMap interface.

  • D

    None of these

Question 4

What is the time complexity of the get() method in a HashMap in the worst case?

  • A

    O(1)

  • B

    O(n)

  • C

    O(n logn)

  • D

    O( log n )

Question 5

What is the syntax of creating a Hashmap in Java?

  • A

    HashMap<k, v> map = new HashMap<>();

  • B

    map<k, v> = new HashMap<>();

  • C

    HashMap<k, v> map = new HashMap();

  • D

    None of these

Question 6

How is LinkedHashMap declared in Java

  • A

    public class LinkedHashMap<K,​V> 

  • B

    public class LinkedHashMap<K,​V> implements Map<K,​V>

  • C

    public class LinkedHashMap<K,​V>  extends HashMap<K,​V> implements Map<K,​V>

  • D

    None of these

Question 7

What happens when a key-value pair already exists in the Map and we use put() to add the value associated with the same key?

  • A

    It adds one more key-value pair of the same key.

  • B

    It adds 2 values for the same key

  • C

    It throws an error

  • D

    It replaces the pre-existing value.

Question 8

What is the output of,

Java
import java.util.*;
class HelloWorld {
    public static void main(String[] args) {
      HashMap<String, Integer> m = new HashMap<String, Integer>();
      m.put("gfg", 10);
      m.put("ide", 16);
      m.put("courses", 25);
      System.out.print(m);
      System.out.print(m.size());
      
      for( Map.Entry< String, Integer >e: m.entrySet())
        System.out.print(e.getKey() + " " + e.getValue());
    }
}



 

  • A

    3courses 25gfg 10ide 16

  • B

    Error

  • C

    {courses=25, gfg=10, ide=16}3courses 25gfg 10ide 16

  • D

    None of these

Question 9

What is the output of the code,

Java
import java.util.*;
class HelloWorld {
    public static void main(String[] args) {
      HashMap<String, Integer> m = new HashMap<String, Integer>();
      m.put("gfg", 10);
      m.put("ide", 16);
      m.put("courses", 25);
      if( m.containsKey("ide"))
        System.out.println("yes");
     else
        System.out.println("no");
     m.remove("ide");
     System.out.println(m.size());
    }
}



 

  • A

    no

    3

  • B

    yes

    3

  • C

    yes

    2

  • D

    no

    2

Question 10

What is the output of the code,

import java.util.*;

class HelloWorld {

    public static void main(String[] args) {

      LinkedHashMap<Integer, String> m = new LinkedHashMap<>();

      m.put(10, "gfg");

      m.put(16, "IDE");

      m.put(25, "courses");

      m.remove( 25 );

      m.put( 20, "practice" );

      System.out.println(m);

    }

}


 

  • A

    Throws an error

  • B

    {10=gfg, 16=IDE, 20=practice}

  • C

    {10=gfg, 16=IDE, 25=courses, 20=practice}

  • D

    None of these

There are 10 questions to complete.

Take a part in the ongoing discussion