logo

Crowdly

Browser

Add to Chrome

367.044, VL Vertiefung Softwareentwicklung, Karin Anna Hummel, 2025S

Looking for 367.044, VL Vertiefung Softwareentwicklung, Karin Anna Hummel, 2025S test answers and solutions? Browse our comprehensive collection of verified answers for 367.044, VL Vertiefung Softwareentwicklung, Karin Anna Hummel, 2025S at moodle.jku.at.

Get instant access to accurate answers and detailed explanations for your course questions. Our community-driven platform helps students succeed!

Two threads are implemented to run respectively methodA() and methodB() in order to decrement/increment counter1 and counter2:

...

private static Integer counter1=0;

private static Integer counter2=0;

Thread threadA = new Thread (()->methodA());

Thread threadB = new Thread (()->methodB());

               

threadA.start();

threadB.start();

...

Which of the following implementations of the methods methodA() and methodB() may lead to a deadlock? 

a)

   public static void methodA() {

        for (int i=1; i<5; i++) {

            try {

                Thread.sleep(500);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            counter1--;

            counter2++;

        }

    }

    public static void methodB() {

        for (int i=1; i<5; i++) {

            try {

                Thread.sleep(500);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            counter1++;

            counter2--;

        }

    }

b)

    private static void methodA() {

        for (int i=1; i<5; i++) {

            try {

                Thread.sleep(500);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            synchronized (counter1){

               counter1--;

            }

            synchronized (counter2){

              counter2++;

            }

              

        }

    }

    public static void methodB() {

        for (int i=1; i<5; i++) {

            try {

                Thread.sleep(500);

            } catch (InterruptedException e) {

                e.printStackTrace();

            }

            synchronized (counter2) {

               counter2--;

            }

            synchronized (counter1) {

                counter1++;

            }

        }

    }

c)

    public static void methodA() {

        for (int i=1; i<5; i++) {

            synchronized (counter2) {

               counter2--;

               try {

                    Thread.sleep(500);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

               synchronized (counter1) {

                counter1++;

               }

            }

        }

    }

    public static void methodB() {

        for (int i=1; i<5; i++) {

            synchronized (counter1) {

               counter1--;

               try {

                    Thread.sleep(500);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

               synchronized (counter2) {

                 counter2++;

               } 

            }

        }

    }

d)

    public static void methodA() {

        for (int i=1; i<5; i++) {

            synchronized (counter1) {

               counter1--;

               try {

                    Thread.sleep(500);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }

    }

    

public static void methodB() {

        for (int i=1; i<5; i++) {

            synchronized (counter1) {

               counter1++;

               try {

                    Thread.sleep(500);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }

    }
0%
0%
0%
0%
View this question

What is true about threads?

a) Threads of one process do not have access to each other's data (memory).

b) If you wish to define a class that extends a class MyString and implements the logic of the method run() for a Thread, you may define the class to implement the interface Runnable and use it during Thread creation.

c) The method wait() is used to wait for another thread to exit.

d) In every Java application, at least one thread is running.

View this question

The following code contains the classes Hobbit, Ring, and the creation and start of three Hobbit threads in class Main / main(). 

class Ring {

String owner = "Who knows";

}

    

class Hobbit extends Thread {

   String name;

   Ring ring;

       

   Hobbit (String name, Ring ring) {

       this.name = name;

       this.ring = ring;

   }

       

   @Override

  public void run() {

       ring.owner = name;

       System.out.println(name + " says: the precious is mine, " + ring.owner);

   }

}

// Class Main

public static void main(String[] args) {

    Ring ring = new Ring();

    Hobbit gollum;

    Hobbit bilbo;

    Hobbit frodo;

 

    gollum = new Hobbit("Gollum",ring);

    bilbo = new Hobbit("Bilbo Baggins",ring);

    frodo = new Hobbit("Frodo Baggins",ring);

    

    gollum.start();

    bilbo.start();

    frodo.start();

 }

 

Which of the following statements assure mutual exclusion and lead to consistent (name is equal to ring.owner), thread-safe output?

 

a) Changing the creation of the threads in Class Main (main()) as follows:

synchronized (ring) {

   gollum = new Hobbit("Gollum",ring);

   bilbo = new Hobbit("Bilbo Baggins",ring);

   frodo = new Hobbit("Frodo Baggins",ring);

}

b) Changing the starting of the threads in main() as follows:

synchronized (ring) {

   gollum.start();

   bilbo.start();

   frodo.start();

}

c) Changing run() in class Hobbit to:

@Override

synchronized public void run() {

ring.owner = name;

   System.out.println(name + " says: the precious is mine, " + ring.owner);

}

d) Changing run() in class Hobbit to:

@Override

public void run() {

synchronized(ring) {

ring.owner = name;

    System.out.println(name + " says: the precious is mine, " + ring.owner);

}

}
0%
0%
0%
0%
View this question

Which of the following statements start a new thread?

a)

Thread t3 = new Thread (()->System.out.println("I want to run ..."));

t3.run();

b)

Thread t2 = new Thread(new Runnable() {

@Override

public void run() {

System.out.println("I want to run ...");

}

});

t2.start();

c)

Thread t1 = new Thread(()-> System.out.println("I want to run ..."));

t1.start();    

d)

Runnable t4 = () -> System.out.println("I want to run");

t4.start();
0%
0%
0%
0%
View this question

Assume the following class Animal and its use are given:

package com.company;

import java.util.Comparator;

import java.util.SortedSet;

import java.util.TreeSet;

class Animal implements Comparable<Animal>{

final String kind;

final String name;

final int age;

Animal(String kind, String name, int age) {

this.kind = kind;

this.name = name;

this.age = age;

}

@Override

public int compareTo (Animal other){

int retVal = kind.compareTo(other.kind);

if (retVal == 0) {retVal= name.compareTo(other.name);}

return retVal;

}

/*...*/

static class MyComparator implements Comparator<Animal> {

/*...*/

public int compare( Animal a1, Animal a2){

return a2.age-a1.age;

}

}

}

public static class Main(String[] args){

SortedSet<Animal> animals = new TreeSet<>();

animals.add(new Animal(“Cat”,”Zero”,2));

animals.add(new Animal(“Cat”, “Filou”, 5));

animals.add(new Animal(“Dog”, “Puppy”, 3));

SortedSet<Animal> animals2 = new TreeSet<>(new Animal.MyComparator());

animals2.addAll(animals);

}

 

Which of the following statements are true?

a) The set animals will be ordered as follows: Filou – Puppy - Zero

b) The set animals will be ordered as follows: Filou – Zero – Puppy

c) The set animals2 is ordered as follows: Zero – Puppy - Filou

d) When multiple sorting schemes should be provided, create and use multiple Comparators.

View this question

Select good choices for an implementation of a collection?

a) If elements are given as key-value pairs, a Set should be used.

b) If indexing elements is important, use a Set.

c) If no duplicates should be allowed and elements should be ordered, select a HashSet.

d) If you need a sequence of elements and you plan to get elements often, use an ArrayList.

View this question

 Which of the following statements are true?

a) A List provides its elements in a sequence.

b) A HashSet does not allow duplicates.

c) Interface Collection inherits from interface SortedSet.

d) Interface Map inherits from interface Collection.

View this question

Boxing and unboxing is used for data type conversions.

Which of the following statements are cases of boxing or unboxing?

a) class A { }

class B extends A {}

A a = new B();

b) Integer i = 3;

c) Object o = 42;

int x = (int) o;

        d) int a = 12; int b = a;
View this question

Want instant access to all verified answers on moodle.jku.at?

Get Unlimited Answers To Exam Questions - Install Crowdly Extension Now!

Browser

Add to Chrome