What is the general function of the Period and Duration classes?

What is the general function of the Period and Duration classes?



Answer: Both classes share the same methods that allow them to manage spans of their respective units: Period corresponds to date and Duration corresponds to time. They can be created with generic methods such as ofYears(5), ofMinutes(3), between(ld1, ld2), etc.

What is the general function of the LocalDateTime class?

What is the general function of the LocalDateTime class?



Answer: has the same time zones and methods of the LocalTime and LocalDate classes, representing date followed by time in one object. The format for date and time are usually separated by a capital T.

What is the function of the parse(CharSequence text) and parse(CharSequence text, DateTimeFormatter formatter) methods of the LocalDate class?

What is the function of the parse(CharSequence text) and parse(CharSequence text, DateTimeFormatter formatter) methods of the LocalDate class?



Answer: Creates an Object from the string provided it is in ISO 8601 format. If the optional formatter object is included, then the String could must be phrased as such.

What is the function of the toString(Object o) and toString(Object o, String nullDefault) methods of the Objects class?

What is the function of the toString(Object o) and toString(Object o, String nullDefault) methods of the Objects class?



Answer: toString(Object o) will return the same results as the regular toString() method if the parameter is not null or returns null if the parameter is null. toString(Object o, String nullDefault) will do the same thing as the previous function, with the exception that if the value is null, it will return the second string parameter instead of null.

What is the function of the replace(K key, V value) and replace(K key, V oldValue, V newValue) of the Map interface?

What is the function of the replace(K key, V value) and replace(K key, V oldValue, V newValue) of the Map interface?



Answer: The first replace() method will replace the value if the provided key matches the provided value; returns the old value if it was successful, returns null if failed. The second replace() method will replace the oldValue with the newValue only if the provided key matches the oldValue. returns true if successful, false if not.

What is the function of the remove(Object key) and remove(Object key, Object value) methods of the Map interface?

What is the function of the remove(Object key) and remove(Object key, Object value) methods of the Map interface?



Answer: The first remove() method will remove the Key-Value pair from the map; returns the value that is removed or null if the key didn't match any of the pairs or if the value is null. The second remove() method will do the same thing, but it will return true if the it was successful.

What is the function of the put(K key, V value), putAll(Map, m) and putIfAbsent(K key, V value) methods of the Map interface?

What is the function of the put(K key, V value), putAll(Map<K,V>, m) and putIfAbsent(K key, V value) methods of the Map interface?



Answer: The put() method will add the Key-Value pair to the map. returns the previous value stored with the same key. putAll() will copy all the provided pairs from the included map. The putIfAbsent method will add the Key-Value pair only if it is not already part of the map. Returns the value mapped to the provided key - new or existing.

What is the function of the remove(Object o), removeAll(Collection c) and removeIf(Predicate"? super E" filter) methods from the Collection interface?

What is the function of the remove(Object o), removeAll(Collection<?> c) and removeIf(Predicate"? super E" filter) methods from the Collection interface?




Answer: remove() returns true if the object was present and then removed. removeAll() returns true if at least one element was found and removed. removeIf() removes any elements from the collection that satisfy an included predicate.

What is the function of the contains(Object o) and containsAll(Collection c) methods from the Collection interface?

What is the function of the contains(Object o) and containsAll(Collection<?> c) methods from the Collection interface?



Answer: contains() returns true if the object is found in the collection, while containsAll returns true if all elements of the collection are found inside the original. For both of these methods, all elements and objects should implement the equals() method. In the case of sets, the hashCode() method should be implemented as well.

Explain the three methods of the Iterable interface?

Explain the three methods of the Iterable interface?



Answer: The iterator() returns an object of the collection structure that is able to use the next(), hasNext() and remove() methods. The forEach(Consumer"? super T" function) method applies an included function to each element in a collection structure. Finally, the splititerator() method returns an object equipped with functions used for parallel processing.

What is the function of the add() and addAll() methods?

What is the function of the add() and addAll() methods?



Answer: add() is a boolean that adds a single method to a collection that implements the Collection interface, returning true if successful. The addAll() method does the same thing, only returning true if at least one method is added successfully.

What is the function of the "? extends T" and "? super T" generics

What is the function of the "? extends T" and "? super T" generics


PS: They normally use <> symbols, but quizlet is retarded
The first one signals that the data type should either be T, or a child of T. The second one indicates that the data type should be T or a parent class of T.

What is a generic?

What is a generic?



Generics specify what type of data will be fed into the collection. Helps to prevent unwanted data from being collected.

What is a faster method of checking to see if a new element already exists in a collection than using the equals() method on each element?

What is a faster method of checking to see if a new element already exists in a collection than using the equals() method on each element?



Answer: Use the hashCode() element on all members of the collection, sorting all elements with equal hashes in a single bucket. If the hash of a new element matches that of an existing bucket, the equals() method can be used on all the contents of the bucket.

What is one positive and negative aspect of using the synchronized keyword to prevent concurrent access issues?

What is one positive and negative aspect of using the synchronized keyword to prevent concurrent access issues?



Answer: By using the synchronized keyword, it will technically fix the issue in a single word, making it the most simple solution, but by blocking the second thread from accessing the method until the first exits might cause performance degradation

What is an Atomic variable (from the java.util.concurrent.atomic package) and how does it prevent concurrent access issues?

What is an Atomic variable (from the java.util.concurrent.atomic package) and how does it prevent concurrent access issues?



Answer: An Atomic variable can only be changed whenever the expected value matches the current value of the variable. This prevents continuity issues by denying access to one of the threads if the value doesn't match its original value.

What is a deadlock, and what causes it?

What is a deadlock, and what causes it?



Answer: A deadlock occurs when one thread blocks one resource while waiting to access the second resource, but another thread is blocking the second resource while waiting for access to the first.

List and explain all the parameters for the following constructor of the ThreadPoolExecutor.

List and explain all the parameters for the following constructor of the ThreadPoolExecutor.


ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler)

corePoolSize: number of threads to keep in the pool unless allowCoreThreadTimeOut(boolean value) with a true value

maximumPoolSize: maximum number of threads allowed in the pool

keepAliveTime: when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.

unit: time unit for the keepAliveTime argument.

workQueue: queue used for holding tasks before they are executed; this queue will hold only the Runnable objects submitted by the execute() method

threadFactory: is the factory to use when the executor creates a new thread

handler: is the handler to use when the execution is blocked because the thread bounds and queue capacities are reached.

What cycle can be applied to best optimize the pool size based on the individual computer?

What cycle can be applied to best optimize the pool size based on the individual computer?



Answer: deploy-monitor-adjust: Set the initial pool size to the number of CPUs, and observe for a while to see how often the threads interact with the CPU or another contentious resource. For whichever resource is in lowest supply, the pool size can be increased by the ratio of time the CPU was not used divided by the total executing time

What are the dangers of having a pool that is too big or too small?

What are the dangers of having a pool that is too big or too small?



Answer: If the pool is too big, then it could dominate the computer's resources, causing degradation to the application as well as other processes. If the pool is too small, then it may not be taking full advantage of the resources available to it.

What is the function of the newWorkStealingThreadPool(int nThreads) method(s) of the Executors class of the java.util.concurrent package?

What is the function of the newWorkStealingThreadPool(int nThreads) method(s) of the Executors class of the java.util.concurrent package?



Answer: Creates a thread pool that uses the work-stealing algorithm, allowing threads that finish assignments to help other threads in the same pool. Also adapts to the specified number of CPUs

What is the function of the newFixedThreadPool(int nThreads) method(s) of the Executors class of the java.util.concurrent package?

What is the function of the newFixedThreadPool(int nThreads) method(s) of the Executors class of the java.util.concurrent package?



Answer: Creates a thread a pool that reuses a fixed number of worker threads; if a new task is submitted when all the worker threads are still executing, it will be placed into the queue until a worker thread is available

What is the function of the awaitTermination(long timeout, TimeUnit timeUnit) method(s) of the ExecutorService interface of the java.util.concurrent package?

What is the function of the awaitTermination(long timeout, TimeUnit timeUnit) method(s) of the ExecutorService interface of the java.util.concurrent package?



Answer: Delays the shutdown request until either all worker threads complete execution, the timeout occurs, or the current thread is interrupted

What is the function of the submit() method(s) of the ExecutorService interface of the java.util.concurrent package?

What is the function of the submit() method(s) of the ExecutorService interface of the java.util.concurrent package?



Answer: This group of methods that place a Runnable or Callable object in the queue for execution and returns an object of the Future interface that allows access to the value returned by the Callable object or allows management of the status of the worker thread

Briefly explain priority values among threads.

Briefly explain priority values among threads.



Answer: A thread has a default value of Thread.NORM_PRIORITY, but must be between Thread.MAX_PRIORITY and Thread.MIN.PRIORITY. The smaller the value, the more time the thread is given to execute, meaning it has more priority.

How do you create a thread by implementing the Runnable interface?

How do you create a thread by implementing the Runnable interface?



Answer: First, create a class that actually implements Runnable, then make sure that the class overrides the run() method in a way that serves the thread's purpose. In the main method, create an object of the class of your runnable class and an object of the Thread class with your runnable object as the parameter for the constructor.

How do you create a thread by extending the Thread class?

How do you create a thread by extending the Thread class?



Answer: First, create a class that actually extends Thread, then override the run() method to do whatever you want the thread to do. You can then create multiple objects of that thread with different parameters as user or daemon threads in the main method.

What is the difference between a daemon thread and a user thread?

What is the difference between a daemon thread and a user thread?



Answer: A user thread is initiated directly by the application, the main thread being one such thread. A daemon thread runs in the background supporting user thread activity, which is why they automatically shut down once the last user thread closes.

What is the difference between a process and a thread?

What is the difference between a process and a thread?



Answer: A process is usually referring to the operation of the entire JVM, but an application is capable of creating its own separate processes. A thread is a smaller and more interactive unit of execution, multiple of which can exist inside a process. At least one thread, the main thread, has to exist in a process.

Given an int variable n that has already been declared, write some code that repeatedly reads a value into n until at last a number between 1 and 10 (inclusive) has been entered.

Given an int variable n that has already been declared, write some code that repeatedly reads a value into n until at last a number between 1 and 10 (inclusive) has been entered.


ASSUME the availability of a variable, stdin, that references a Scanner object associated with standard input.
do
{
n = stdin.nextInt();
}
while (n > 10 || n < 1);

Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total and do not modify the value of n.

Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Use no variables other than n, k, and total and do not modify the value of n.



k = 0 ;
total = 0 ;
while ( k <= n )
{
total = total += k k k;
k++ ;
}

Given int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 11 + 22 + 33 +... + 4949 + 50*50 into total. Use no variables other than k and total.

Given int variables k and total that have already been declared, use a while loop to compute the sum of the squares of the first 50 counting numbers, and store this value in total. Thus your code should put 11 + 22 + 33 +... + 4949 + 50*50 into total. Use no variables other than k and total.



total=0;
for (k=1;k<=50; k++)
total+=k*k;

Write a program that asks the user to enter five test scores. The program should display aletter grade for each score and the average test score.

Write a program that asks the user to enter five test scores. The program should display aletter grade for each score and the average test score.

Write the following methods in theprogram:calcAverage:This method should accept five test scores as arguments and return theaverage of the scores.determineGrade:This method should accept a test score as an argument and return aletter grade for the score, based on the following grading scale:Score Letter Grade90-100 A80-89 B70-79 C60-69 DBelow 60 F.


import java.util.Scanner;
public class TestAvgAndGrade {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
char letterGrade = 'A';

System.out.print("Enter test grade for student 1:");
double score1 = kb.nextDouble();
System.out.print(" Enter test grade for student 2:");
double score2 = kb.nextDouble();
System.out.print(" Enter test grade for student 3:");
double score3 = kb.nextDouble();
System.out.print(" Enter test grade for student 4:");
double score4 = kb.nextDouble();
System.out.print(" Enter test grade for student 5:");
double score5 = kb.nextDouble();

System.out.print(" The letter grades are as follows:\n");
System.out.println("Student 1: "+ determineGrade(score1));
System.out.println("Student 2: "+ determineGrade(score2));
System.out.println("Student 3: "+ determineGrade(score3));
System.out.println("Student 4: "+ determineGrade(score4));
System.out.println("Student 5: "+ determineGrade(score5));

double average = calcAverage(score1,score2,score3,score4,score5);
System.out.printf("The average grade was: %.2f\n",average);
}
public static char determineGrade(double score){
char grade;
if(score <60)
grade = 'F';
else if(score <70)
grade = 'D';
else if(score <80)
grade ='C';
else if(score <90)
grade = 'B';
else
grade ='A';

return grade;
}

public static double calcAverage(double sc1,double sc2,double sc3, double sc4,double sc5){
double av = (double)(sc1+sc2+sc3+sc4+sc5)/5;
return av;
}


}