HashMap and LinkedHashMap CIP

Last Updated :
Discuss
Comments

Question 1

What is the default initial capacity of a HashMap?

  • 64

  • 32

  • 16

  • None of these

Question 2

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

  • It maintains the insertion of the Map interface

  • It is synchronized

  • It is an implementation of the Map interface

  • None of these

Question 3

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

  • It does not allow null keys or values.

  •  It is an implementation of the Map interface.

  •  It is an implementation of the HashMap interface.

  • None of these

Question 4

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

  • O(1)

  • O(n)

  • O(n logn)

  • O( log n )

Question 5

What is the syntax of creating a Hashmap in Java?

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

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

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

  • None of these

Question 6

How is LinkedHashMap declared in Java

  • public class LinkedHashMap<K,​V> 

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

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

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

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

  • It adds 2 values for the same key

  • It throws an error

  • 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());
    }
}



 

  • 3courses 25gfg 10ide 16

  • Error

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

  • 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());
    }
}



 

  • no

    3

  • yes

    3

  • yes

    2

  • 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);

    }

}


 

  • Throws an error

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

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

  • None of these

There are 10 questions to complete.

Take a part in the ongoing discussion