Java Fundamentals Flashcards
The basic building blocks of the Java programming language.
Which string method returns the character at the specified index in the string?
charAt(int index)
String str = "Hello"; char ch = str.charAt(1); // ch will be 'e'
Which string method returns the length of the string?
length(); String str = "Hello"; int length = str.length(); // length will be 5
Which string method concatenates the specified string at the end of the current string?
concat(String str); String str1 = "Hello"; String str2 = " World"; String result = str1.concat(str2); // result will be "Hello World"
How do you compare the content of two strings?
equals(Object obj); String str1 = "Hello"; String str2 = "Hello"; boolean isEqual = str1.equals(str2); // isEqual will be true
How do you compare two strings while ignoring upper and lower case?
equalsIgnoreCase(String str); String str1 = "Hello"; String str2 = "hello"; boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2); // isEqualIgnoreCase will be true
How do you get the index of the first occurance of a substring?
indexOf(String str); String str = "Hello World"; int index = str.indexOf("World"); // index will be 6
How do you get a substring from a string?
substring(int beginIndex, int endIndex); String str = "Hello World"; String substring = str.substring(6, 11); // substring will be "World"
How do you convert all characters in a string to lowercase / uppercase?
toLowerCase(); / toUpperCase(); String str = "Hello World"; String lower = str.toLowerCase(); // lower will be "hello world" String upper = str.toUpperCase(); // upper will be "HELLO WORLD"
How do you remove leading and trailing whitespaces from the string?
trim(); String str = " Hello World "; String trimmed = str.trim(); // trimmed will be "Hello World"
How do you append the specified string or character to the end of the StringBuilder?
append(String str); / append(char c); StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); // sb is now "Hello World"
How do you insert the specified string or character at the specified position of a StringBuilder?
insert(int offset, String str); / insert(int offset, char c); StringBuilder sb = new StringBuilder("Hello"); sb.insert(5, " World"); // sb is now "Hello World"
How do you delete the characters from the start index to the end - 1 index of a StringBuilder?
delete(int start, int, end); StringBuilder sb = new StringBuilder("Hello World"); sb.delete(6, 11); // sb is now "Hello"
How do you delete a character from a StringBuilder at a specific index?
deleteCharAt(int index); StringBuilder sb = new StringBuilder("Hello World"); sb.deleteCharAt(5); // sb is now "Helo World"
How do you replace characters from the start index to the end - 1 index of a StringBuilder?
replace(int start, int end, String str); StringBuilder sb = new StringBuilder("Hello World"); sb.replace(6, 11, "Java"); // sb is now "Hello Java"
How do you reverse the order of the character in a StringBuilder?
reverse(); StringBuilder sb = new StringBuilder("Hello"); sb.reverse(); // sb is now "olleH"
How do you get the character at a specific index of a StringBuilder?
charAt(int index); StringBuilder sb = new StringBuilder("Hello"); char ch = sb.charAt(1); // ch will be 'e'
How do you get the length of a StringBuilder?
length(); StringBuilder sb = new StringBuilder("Hello"); int length = sb.length(); // length will be 5
How do you convert a StringBuilder into a String?
toString(); StringBuilder sb = new StringBuilder("Hello"); String str = sb.toString(); // str will be "Hello"
How do you in insert a key:value pair into a HashMap?
put(K key, V Value); HashMap<String, Integer> map = new HashMap<>(); map.put("one", 1); map.put("two", 2);
How do you retreive a value from a HashMap by the mapped key? What is the return value if the key is not present in the HashMap?
get(Object key); / returns null if the key is not present Integer value = map.get("one"); // value will be 1
How do you check if a key is present inside of a HashMap?
containsKey(Object key); boolean containsKey = map.containsKey("two"); // containsKey will be true
How do you check if a HashMap contains a value?
containsValue(Object value); boolean containsValue = map.containsValue(2); // containsValue will be true
How do you remove a mapping from a HashMap by key?
remove(Object key); map.remove("one");
How do you get the size of a HashMap?
size(); int size = map.size(); // size will be 1
How do you check if a HashMap contains any pairs at all?
isEmpty(); boolean isEmpty = map.isEmpty(); // isEmpty will be false
How do you get a set view of all the keys in a HashMap?
keySet(); Set<String> keys = map.keySet();
How do you get a collection view of all the values in a HashMap?
values(); Collection<Integer> values = map.values();
How do you get a set view of the key:value mappings contained in a HashMap?
entrySet(); Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
How do you remove all the key:value mappings from a HashMap?
clear(); map.clear();
How do you add new elements into a HashSet?
add(E element); HashSet<String> set = new HashSet<>(); set.add("Java"); set.add("Python"); set.add("C++");
How do you remove an element from a HashSet?
remove(Object o); set.remove("Python");
How do you check if a HashSet contains a value?
contains(Object o); boolean containsJava = set.contains("Java"); // true