Pages

Thursday, 27 January 2022

Top 16 Hashmap interview question and answers 2023? QAE - SDET - SDE Java Collections Interview Questions 2023

 


Pre-requisite: How to install JDK and set Java Home path in Windows and MacOs ?

Ans: Click here for Steps

Java Interview question Set-1:

SET-01 1-10

Java Interview question Set-2:

SET-02 10-20

Java Interview question Set-3:

SET-03 21-30

Below are the Top Hashmap Interview questions and answers for Java Interview preparation:

1. How to initialize a map with key as string and value as Integer?

HashMap<String, Integer> map = new HashMap<>();


2. How to add and remove elements from Hashmap?


Add Elements:

                          map.put("james", 10);

        map.put("sidharth", 30);

        map.put("shukla", 20);


Remove Elements:


        map.remove("vishal", 10);


3. How to iterate through hashmap?


for (Map.Entry<String, Integer> e : map.entrySet())

            System.out.println("Key: " + e.getKey()

                              + " Value: " + e.getValue());

    }


4. Does hashmap allow duplicate key and values?


Ans:

Hashmap doesnt allow duplicate key, but allows duplicate values


5. Does hashmap allow null key and values for Hashmap?


Ans:
Hashmap
allows null key but only once,multiple null values supported


6. How to sort Hashmap in Java?



Click Here For The Answer



7. Do hashmap is thread safe?



Ans: Hashmap is not thread safe as it is not synchronized



8. Does hashmap maintain insertion order?


Ans


Hashmap doesn't maintain insertion order, but Treemap/LinkedhashMap maintains the insertion order.



9. Which method helps to get all the keys in hashmap?


  • keyset(): Returns a Set view of the keys contained in this map


....> HashMap<String, Integer> map = new HashMap<>();

....>  map.keyset();


"String Question and Answers" <== Click



10. How to replace value in hashmap?


with replace():Replaces the entry for the specified key only if it is currently mapped to some value


HashMap<String, Integer> map = new HashMap<>();

map.put("james", 10);

map.replace("james", 50);


11.  What is Hashmap? 


- Part of Collection Framework

- Implements Map interface

- There are 4 main fields are Hash , Key , Value , Node


12. How to find duplicate elements using Hashmap ?


Click here for Answer With Code


13. Difference between Hashmap and Hashtable?


Hashtable:

Hashtable is synchronized. It is thread-safe

Hashtable doesn't allow any null key or value.

Hashtable is inherited from Dictionary class.



Hashmap:

HashMap is non synchronized.It's not-thread safe.

HashMap allows one null key and multiple null values.

HashMap is inherited from AbstractMap class.


Bonus:

14. How hashmap works?


HashMap in Java works on hashing principles. Hashing is a process in which hash functions are used to link keys and values in HashMap.
If you want an in-depth explanation of how Hashmap works, 

please refer to this link.


15. Can we store a null key in Java HashMap?

Yes, HashMap allows one null key, which is stored at the first location of the bucket array e.g., bucket[0] = value. HashMap doesn't call HashCode() on the null key because it will throw NullPointerException, hence when a user calls the get() method with null, then the value of the first index is returned.

16. Can you store the duplicate value in Java HashMap? 

Yes, you can put duplicate values in HashMap of Java. It allows duplicate values; that's why when you retrieve all values from the Hashmap by calling the values() method, it returns a Collection and not a Set.




Array Question and Answers } <--Click



REVISION:

41. What is Eager and Lazy Initialization of Singleton Class ?


42. What modifiers are accepted for Main class in Java?

Ans: public, abstract & final are permitted
 
43. Write code to reverse a string without using function?

Ans:
public class reverse {
    public static void main (String args[]) {
       
            String str = "automationreinvented";
            int n = str.length();
            String rev="";
            for (int i=n-1;i>=0;i--)
             rev = rev + str.charAt(i);
            System.out.println(rev);

          }
    }

44. How to do string manipulation in java?





Friday, 21 January 2022

What we know about the "STRING"? MCQ on String for concepts and interview preparation QAE - SDET - AUTOMATION QA 2022

 


 Pre-requisite: How to install JDK and set Java Home path in Windows and MacOs ?

Ans: Click here for Steps


Java Interview question Set-1: SET-01 1-10

Java Interview question Set-2: SET-02 10-20

Java Interview question Set-3: SET-03 21-30


Here are the top 5 things to know about Java strings:

  1. Strings are objects:
    In Java, strings are objects of the String class, which is included in the java.lang package. This means that strings have a wide range of methods available for manipulating and processing them.

  2. Strings are immutable:
    In Java, strings are immutable, which means that once a string is created, it cannot be changed. Any operation that appears to modify a string actually creates a new string object. This has implications for memory usage and performance.

  3. String literals:
    String literals are enclosed in double quotes (e.g. "hello"), and they are automatically created as String objects when the program is executed. String literals are also pooled, which means that if two string literals have the same value, they will be references to the same object.

  4. String concatenation:
    Strings can be concatenated using the "+" operator or the concat() method. When concatenating strings in a loop or other performance-sensitive code, it is more efficient to use a StringBuilder or StringBuffer object, which can avoid creating many intermediate string objects.

  5. String comparisons:
    Strings can be compared using the equals() method or the equalsIgnoreCase() method. The == operator can also be used to compare strings, but it checks for reference equality, not value equality. When comparing many strings in a performance-sensitive application, it can be more efficient to use the intern() method to intern the strings and compare their references.


1. What is the output for below?

String str1 = "test";
String str2 = "one";

String str3 = str1.concat(str2);


Ans:

a) testone

b) test

c) one

d) Let me check in IDE :)


2. What is the output for below?

String str1 = "test";
String str2 = "test";

String str3 = new String("test");


System.out.println(str1.equals(str2));

System.out.println(str1.equals(str3));


Ans:

a) true,true

b) false, true

c) false, false

d) Let me check in IDE :)


3. What is the output for below?

String str1 = "test";

String str2 = "test";

String str3 = new String("test");


System.out.println(str1==str2));

System.out.println(str1==str3);


a) true, true

b) true, false

c) false, true

d) false, false


4. What is the output for below?

String str = "test";

System.out.println(str1.charAt(2)); 


a) s

b) e

c) error

d) es


5. What is the output for below?

System.out.println("012301230123".replace(55, "45"));

 System.out.println("012301230123".replace("01", "45"));


a) compile error, 452345234523

b) 452345234523, 452345234523

c) error, error

d) Let me check in IDE :)


6. What is the output for below?

System.out.println("tes".substring(3));

System.out.println("test".substring(3));



a) error, error

b) error, t

c)  , t

d) t, t


7. How many ways we can create String Object in Java?


a) String s = "abc"; String s1 = new String("abc");

b) Literal, Keyword

c)  Both A and B

d)  Not sure


CLICK HERE FOR ANSWERS OF ALL QUESTIONS



8. Is String thread safe?


a) true

b) false

c) Not sure


9. What is the output for below?

String str8 = new StringBuilder("test").reverse().toString();

System.out.println(str8);


a) test

b) tset

c) Not sure


10. Which one is thread safe: StringBuffer or StringBuffer ?


a) Both

b) StringBuffer

c) StringBuilder

d) None of the options


Click below link for the answers of above 1-10 Question



CLICK HERE FOR ANSWERS OF ALL THE QUESTIONS


To go through all the string manipulation related interview Q&A, refer the link here: String Manipulation Interview Q&A





String Related Q&A

*******************************************************************
For any doubts or career guidance, reach out to me 


********************************************************************

These commands cover a range of basic UI interactions and are a good starting point for building automated tests with Cypress.

Do remember that knowing Linux is one of the most important aspect to work as an SDET.

Basic Linux Commands for Automation QA


****************************************

TOP 15 BDD - CUCUMBER Interview Q&A


************************************************

✍️AUTHORLinkedIn Profile

************************************************

Learn (API-Microservice)Testing+ Selenium UI Automation-SDET with Self Paced Videos prepared by FAANG employees and LIVE Doubt Session 

SDET TRANING VIDEOS AVAILABLE with Live Doubt Session(course-1 below,API TRaining Videos With Class Notes and Coding Set) and (API+UI, both course-1 & 2 below) Check Training Page for Course Content or reach out @whatsapp +91-9619094122. 
This includes classnotes, 300+ interview questions, 3 projects, Java Coding question set for product companies along with career guidance from FAANG employees for Automation and SDET.

For more details whatsapp : https://lnkd.in/dnBWDM33

*************************************************

SeleniumWebdriver Automation Testing Interview Questions:

https://automationreinvented.blogspot.com/search/label/SeleniumWebdriver

API Testing Interview Question Set:

https://automationreinvented.blogspot.com/2022/03/top-80-api-testing-interview-questions.html

DevOps Interview Q&A:

https://automationreinvented.blogspot.com/2021/11/top-11-devops-interview-questions-and.html 

Kubernetes Interview Question Set

https://automationreinvented.blogspot.com/search/label/Kubernetes

Docker Interview Question Set

https://automationreinvented.blogspot.com/Docker

Linux Interview question Set

https://automationreinvented.blogspot.com/search/label/Linux

Automation Testing/SDET Framework Design

https://automationreinvented.blogspot.com/search/label/FrameworkDesign

Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java

GIT Interview Question Set:

https://automationreinvented.blogspot.com/2021/09/top-40-git-interview-questions-and.html

Coding Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Coding%20Questions

Mobile Testing Interview Question Set:

https://automationreinvented.blogspot.com/search/label/Mobile%20Testing

Python Interview Question Set for QAE - SDET - SDE:

https://automationreinvented.blogspot.com/search/label/Python