Java Fundamentals Flashcards

The basic building blocks of the Java programming language.

1
Q

Which string method returns the character at the specified index in the string?

A

charAt(int index)

String str = "Hello";
char ch = str.charAt(1); // ch will be 'e'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which string method returns the length of the string?

A
length();

String str = "Hello";
int length = str.length(); // length will be 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which string method concatenates the specified string at the end of the current string?

A
concat(String str);

String str1 = "Hello";
String str2 = " World";
String result = str1.concat(str2); // result will be "Hello World"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you compare the content of two strings?

A
equals(Object obj);

String str1 = "Hello";
String str2 = "Hello";
boolean isEqual = str1.equals(str2); // isEqual will be true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you compare two strings while ignoring upper and lower case?

A
equalsIgnoreCase(String str);

String str1 = "Hello";
String str2 = "hello";
boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2); // isEqualIgnoreCase will be true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you get the index of the first occurance of a substring?

A
indexOf(String str);

String str = "Hello World";
int index = str.indexOf("World"); // index will be 6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you get a substring from a string?

A
substring(int beginIndex, int endIndex);

String str = "Hello World";
String substring = str.substring(6, 11); // substring will be "World"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you convert all characters in a string to lowercase / uppercase?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you remove leading and trailing whitespaces from the string?

A
trim();

String str = "  Hello World  ";
String trimmed = str.trim(); // trimmed will be "Hello World"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you append the specified string or character to the end of the StringBuilder?

A
append(String str); / append(char c);

StringBuilder sb = new StringBuilder("Hello");
sb.append(" World"); // sb is now "Hello World"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you insert the specified string or character at the specified position of a StringBuilder?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you delete the characters from the start index to the end - 1 index of a StringBuilder?

A
delete(int start, int, end);

StringBuilder sb = new StringBuilder("Hello World");
sb.delete(6, 11); // sb is now "Hello"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you delete a character from a StringBuilder at a specific index?

A
deleteCharAt(int index);

StringBuilder sb = new StringBuilder("Hello World");
sb.deleteCharAt(5); // sb is now "Helo World"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you replace characters from the start index to the end - 1 index of a StringBuilder?

A
replace(int start, int end, String str);

StringBuilder sb = new StringBuilder("Hello World");
sb.replace(6, 11, "Java"); // sb is now "Hello Java"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you reverse the order of the character in a StringBuilder?

A
reverse();

StringBuilder sb = new StringBuilder("Hello");
sb.reverse(); // sb is now "olleH"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you get the character at a specific index of a StringBuilder?

A
charAt(int index);

StringBuilder sb = new StringBuilder("Hello");
char ch = sb.charAt(1); // ch will be 'e'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you get the length of a StringBuilder?

A
length();

StringBuilder sb = new StringBuilder("Hello");
int length = sb.length(); // length will be 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How do you convert a StringBuilder into a String?

A
toString();

StringBuilder sb = new StringBuilder("Hello");
String str = sb.toString(); // str will be "Hello"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you in insert a key:value pair into a HashMap?

A
put(K key, V Value);

HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

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?

A
get(Object key); / returns null if the key is not present

Integer value = map.get("one"); // value will be 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How do you check if a key is present inside of a HashMap?

A
containsKey(Object key);

boolean containsKey = map.containsKey("two"); // containsKey will be true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

How do you check if a HashMap contains a value?

A
containsValue(Object value);

boolean containsValue = map.containsValue(2); // containsValue will be true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How do you remove a mapping from a HashMap by key?

A
remove(Object key);

map.remove("one");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How do you get the size of a HashMap?

A
size();

int size = map.size(); // size will be 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

How do you check if a HashMap contains any pairs at all?

A
isEmpty();

boolean isEmpty = map.isEmpty(); // isEmpty will be false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How do you get a set view of all the keys in a HashMap?

A
keySet();

Set<String> keys = map.keySet();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

How do you get a collection view of all the values in a HashMap?

A
values();

Collection<Integer> values = map.values();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

How do you get a set view of the key:value mappings contained in a HashMap?

A
entrySet();

Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

How do you remove all the key:value mappings from a HashMap?

A
clear();

map.clear();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

How do you add new elements into a HashSet?

A
add(E element);

HashSet<String> set = new HashSet<>();
set.add("Java");
set.add("Python");
set.add("C++");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

How do you remove an element from a HashSet?

A
remove(Object o);

set.remove("Python");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

How do you check if a HashSet contains a value?

A
contains(Object o);

boolean containsJava = set.contains("Java"); // true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

How do you check the number of elements in a HashSet?

A
size();

int setSize = set.size(); // 2
34
Q

How do you check if a HashSet is empty?

A
isEmpty();

boolean isEmpty = set.isEmpty(); // false
35
Q

How do you remove all elements from a HashSet?

A
clear();

set.clear();
36
Q

How do you create a iterator on a HashSet?

A
iterator();

Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
    String element = iterator.next();
    // Do something with the element
}
37
Q

How do you use a forEach loop to print out all the values in a HashSet?

A
set.forEach(System.out::println);
38
Q

How do you import a HashSet into the working class?

A
import java.util.HashSet;
39
Q

How do you import an Iterator into your working class?

A
import java.util.Iterator;
40
Q

How do you import Arrays into your working class?

A
import java.util.Arrays;
41
Q

What is the built in method to sort arrays?

A
Arrays.sort(T[] a);

int[] numbers = {4, 2, 7, 1, 9};
Arrays.sort(numbers);
//Output: [1,2,4,7,9]
42
Q

How do you return a string representation of the contents of an array?

A
Arrays.toString(T[] a);

String[] names = {"Alice", "Bob", "Charlie"};
String namesString = Arrays.toString(names);
43
Q

What is a built in method that can search an array using binary search?

A
Arrays.binarySearch(T[], T key);

int[] sortedNumbers = {1, 2, 4, 7, 9};
int index = Arrays.binarySearch(sortedNumbers, 4); // returns 2
44
Q

How do you assign a specific value to all elements of an array?

A
Arrays.fill(T[] a, T val);

char[] chars = new char[5];
Arrays.fill(chars, 'X'); // {'X', 'X', 'X', 'X', 'X'}
45
Q

How do you copy an array up to a certain length?

A
Arrays.copyOf(T[] original, int newLength);

int[] sourceArray = {1, 2, 3, 4, 5};
int[] newArray = Arrays.copyOf(sourceArray, 3); // {1, 2, 3}
46
Q

How do you copy elements from an array from a starting index to an ending index?

A
Arrays.copyOfRange(T[] a, int start, int end);

original: The source array from which a range is to be copied.
from: The initial index of the range to be copied, inclusive.
to: The final index of the range to be copied, exclusive.
public class CopyOfRangeExample {
    public static void main(String[] args) {
        int[] originalArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
       
        // Copy elements from index 2 to 7 (exclusive)
        int[] copiedArray = Arrays.copyOfRange(originalArray, 2, 7);
       
        // Display the copied array
        System.out.println(Arrays.toString(copiedArray));
    }
}
47
Q

How do you create a stream from a specified array?

A
Arrays.stream(T[] array);

String[] fruits = {"Apple", "Banana", "Orange"};
Arrays.stream(fruits)
      .forEach(System.out::println);
48
Q

How do you sort the elements of an array in ascending order using a parallel sort?

A
parallelSort(T[] a);

double[] prices = {19.99, 10.99, 5.99, 29.99};
Arrays.parallelSort(prices);
49
Q

How do you check if two arrays are similar to each other?

A
Arrays.equals(T[] a, T[] a2);

int[] array1 = {1, 2, 3};
int[] array2 = {1, 2, 3};
boolean areEqual = Arrays.equals(array1, array2); // true
50
Q

What method can you use to add elements to an ArrayList?

A
add(E element);

ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("C++");
51
Q

What method can you use to remove and element from an ArrayList?

A
remove(Object o);

list.remove("Python");
52
Q

How do you retreive an element at a specific position from an ArrayList?

A
get(int index);

String language = list.get(0); // "Java"
53
Q

How do you get the size of an ArrayList?

A
size();

int listSize = list.size(); // 2
54
Q

How do you check if an ArrayList is empty?

A
isEmpty();

boolean isEmpty = list.isEmpty(); // false
55
Q

How do you check if an ArrayList has a specific element?

A
contains(Object o);

boolean containsJava = list.contains("Java"); // true
56
Q

How do you append all of the elements in the specified collection to the end of an ArrayList?

A
addAll(Collection<? extends E> c);

List<String> additionalLanguages = Arrays.asList("JavaScript", "Ruby");
list.addAll(additionalLanguages);
57
Q

How do you remove all of the elements in an ArrayList?

A
list.clear();
58
Q

How do you repeat an action for each value of an list?

A
forEach(Consumer<? super E> action);

list.forEach(System.out::println);
59
Q

How do you replace all elements of an ArrayList by applying a specific operation?

A
replaceAll(UnaryOperator<E> operator);

list.replaceAll(language -> language.toUpperCase());
60
Q

How do you sort the values of an ArrayList?

A
sort(Comparator<? super E> c);

list.sort(Comparator.reverseOrder());
61
Q

How do you import the ArrayList capabilities into your working class?

A
import java.util.ArrayList
import java.util.Arrays
import java.util.List
62
Q

What is a method that can be used to check if a character is a letter?

A
isLetter(char ch);

char letter = 'A';
boolean isLetter = Character.isLetter(letter); // true
63
Q

What is a method that can be used to check if a character is a digit?

A
isDigit(char ch);

char digit = '5';
boolean isDigit = Character.isDigit(digit); // true
64
Q

What is a method that can be used to check if a character is a whitespace?

A
isWhitespace(char ch);

char space = ' ';
boolean isWhitespace = Character.isWhitespace(space); // true
65
Q

How do you convert a character to be upper case?

A
toUpperCase(char ch);

char lowercaseChar = 'a';
char uppercaseChar = Character.toUpperCase(lowercaseChar); // 'A'
66
Q

How do you make a character into lower case?

A
toLowerCase(char ch);

char uppercaseChar = 'A';
char lowercaseChar = Character.toLowerCase(uppercaseChar); // 'a'
67
Q

How do you check if a character is upper case?

A
isUpperCase(char ch);

char uppercaseChar = 'A';
boolean isUpperCase = Character.isUpperCase(uppercaseChar); // true
68
Q

How do you check if a character is lower case?

A
toLowerCase(char ch);

char lowercaseChar = 'a';
boolean isLowerCase = Character.isLowerCase(lowercaseChar); // true
69
Q

How do you check if a character is a letter or digit?

A
isLetterOrDigit(char ch);

char specialChar = '$';
boolean isLetterOrDigit = Character.isLetterOrDigit(specialChar); // false
70
Q

What is the Java Virtual Machine (JVM)?

A

JVM is a program reponsible for converting Java byte-code into machine code. JVM is the reason for Java’s platform independence.

71
Q

How does Java enable high performance?

A

Java enables high performance by the use of Just In Time Compilers. Instead of the bytecode being converted into machine code, the JIT compiler runs the code directly. This typically happens when a method is called. JVM runs the code directly instead of interpreting it.

72
Q

What is JDK? (Java Development Kit)

A

JDK is the tools necessary to build, document and package Java programs.

73
Q

What is JRE? (Java Runtime Environment)

A

This is a software package from Oracle Corporation that provides the runtime environment for executing Java applications. It includes the Java Virtual Machine (JVM), core libraries, and other components required for running Java applications. Developers use JRE to test and run Java programs without the need for the full development kit.

74
Q

What is JVM? (Java Virtual Machine)

A

JVM is a part of the JRE and helps run Java bytecode.

75
Q

What is a classloader in Java?

A

In Java, a ClassLoader is a part of the Java Runtime Environment (JRE) responsible for dynamically loading Java classes into the Java Virtual Machine (JVM) at runtime. The Java ClassLoader is a crucial component of the Java runtime system, enabling the JVM to load classes as they are referenced in a Java program. There are three types of classloaders:
1. Bootstrap classloader
2. Extension classlaoder
3. System/application classlaoder

76
Q

Why is Java platform independent?

A

Java is platform independent because of the existence of bytecode. It lets the same program run on any machine.

77
Q

Why is Java not 100% object-oriented?

A

Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.

78
Q

What are wrapper classes in Java?

A

Wrapper classes convert the Java primitives into the reference types (objects). Every primitive data type has a class dedicated to it. These are known as wrapper classes because they “wrap” the primitive data type into an object of that class.

79
Q

What are constructors in Java?

A

In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.

There are two types of constructors:

  1. Default Constructor: In Java, a default constructor is the one which does not take any inputs. In other words, default constructors are the no argument constructors which will be created by default in case you no other constructor is defined by the user. Its main purpose is to initialize the instance variables with the default values. Also, it is majorly used for object creation.
  2. Parameterized Constructor: The parameterized constructor in Java, is the constructor which is capable of initializing the instance variables with the provided values. In other words, the constructors which take the arguments are called parameterized constructors.
80
Q

What is a singleton class? How can we make a class singleton?

A

Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private and make a getInstance method that checks if a instance already exists, otherwise create one.

81
Q

How do you reset a StringBuilder to contain no characters?

A

Use sb.setLength(0);