Каковы результаты выполнения фрагмента программы, использующей массивы

Что будет результатом выполнения двух потоков, имеющих приоритет 10?

Что будет результатом выполнения пяти потоков, имеющих приоритет 10?

1. public class Multithread implements Runnable{

2. public static void main (String[] args) {

3. Multithread []m=new Multithread[5];

4. Thread []t=new Thread[5];

5. for (int i=0;i<5;i++){

6. m[i]=new Multithread();

7. t[i]=new Thread(m[i],"thread"+i);

8. t[i].setPriority(10); t[i].start();}

9. }

10. public void run() {

11. while(true){

12. System.out.println(Thread.currentThread().getName());}

13. } }

A. Пять потоков будут исполняться по очереди однократно. Результат: thread =0 thread =1 thread =2 thread =3 thread =4 .

B. Пять потоков будут исполняться по очереди многократно. Результат: thread =0 thread =1 thread =2 thread =3 thread =4 thread =0 thread =1 thread =2…

C. Пять потоков чередуются неравномерно. Результат, например, может быть таким: thread =0 thread =1 thread =1 thread =2 thread =3 thread =3 thread =3 thread =4 …

D. Только один нулевой поток t[0] исполнится однократно. Результат: thread=0

E. Только один поток t[0] будет многократно исполняться. Результат: thread =0 thread =0 thread =0 …

 

1. public class Multithread implements Runnable {

2. public static void main (String[] args) {

3. Multithread []m=new Multithread[2];

4. Thread []t=new Thread[2];

5. for (int i=0;i<2;i++){

6. m[i]=new Multithread();

7. t[i]=new Thread(m[i],"thread"+i);

8. t[i].setPriority(10); t[i].start();}

9. }

10. public void run() {

11. while(true){

12. System.out.println(Thread.currentThread().getName());

13. Thread.yield();} }

14. }

A. Два потока будут исполняться по очереди многократно. Результат: thread =0 thread =1 thread =0 thread =1 thread =0 thread =1 thread =0 thread =1…

B. Два потока исполнятся по очереди однократно. Результат: thread =0 thread =1.

C. Два потока чередуются неравномерно. Результат, например, может быть таким: thread =0 thread =0 thread =1 thread =0 thread =1 thread =1 thread =1 thread =0 …

D. Только один нулевой поток t[0] исполнится однократно. Результат: thread=0

E. Только один поток t[0] будет многократно исполняться. Результат: thread =0 thread =0 thread =0 …

 

 

1. Cat[] cats=new Cat[5];

2. Dog[] dogs=new Dog[5];

3. Cloneable clon;

4. Washer[] w;

5. Object[] objects=new Object[10];

6. clon=cats;

7. w=cats;

8. objects=cats;

9. cats=(Cat[])objects;

10. dogs=(Dog[])objects;

A. Код компилируется и выполняется успешно

B. Исключение в строке 6

C. Исключение в строке 7

D. Исключение в строке 8

E. Исключение в строке 9

F. Исключение в строке 10

 

9. Метод readFile() должен выбрасывать исключение FileNotFoundException. Какие варианты генерации исключений верны?

A).

1. public void readFile() {

2. File f=new File("d:\\file1.txt");

3. if(!f.exists())throw new FileNotFoundException("inform");}

B).

4. public void readFile() throws FileNotFoundException {

5. File f=new File("d:\\file1.txt");

6. if(!f.exists())throw FileNotFoundException("inform");}

1.

C).

1. public void readFile(){

2. try{

3. File f=new File("d:\\file1.txt");

4. if(!f.exists())throw new FileNotFoundException("inform");

5. }catch (FileNotFoundException e){ }

D).

2. public static void readFile() throws FileNotFoundException {

3. File f=new File("d:\\file1.txt");

4. if(!f.exists()) FileNotFoundException e = FileNotFoundException("inform");}

E).

1. public void readFile(){

2. File f=new File("d:\\file1.txt");

3. FileNotFoundException e = FileNotFoundException("inform");

4. if(!f.exists())throw e;}

 

10. Метод peril() может выбрасывать NoSuchMethodException, EOFException, ArithmeticException. Как правильно вызывать этот метод?

 
 

 

 


A).

public static void main(String[] args) throws RuntimeException {

try{peril();

}catch(IOException e){System.out.println(e.toString());}; }

B).

public static void main(String[] args) throws NoSuchMethodException {

try{peril();

}catch(RuntimeException e){System.out.println(e.toString());} }

C).

public static void main(String[] args){

try{peril();

}catch(RuntimeException e){System.out.println(e.toString());}

catch(IOException e){System.out.println(e.toString());}; }

D).

public static void main(String[] args) throws NoSuchMethodException{

try{peril();

}catch(RuntimeException e){System.out.println(e.toString());}

catch(IOException e){System.out.println(e.toString());}; }

E).

public static void main(String[] args){

try{peril();

}catch(EOFException e){System.out.println(e.toString());}

catch(RuntimeException e){System.out.println(e.toString());}

catch(IOException e){System.out.println(e.toString());}; }

 

11. Какие результаты компиляции и выполнения следующего кода Вы ожидаете?

1. public class Outer {

2. private int x=7;

3. private static int y=9;

4. private final int z=9;

5. public class Inner{

6. public int v=5;

7. public final static int u=3;

8. public void innerMethod(){

9. System.out.println(" x="+x);

10. System.out.println(" y="+y);

11. System.out.println(" z="+z);

12. System.out.println(" u="+u);

13. System.out.println(" v="+v);};}

14. public static void main(String[] args) {

15. new Outer().new Inner().innerMethod();} }

16. }

A. Код компилируется и исполняется без ошибок

B. Ошибка компиляции в строке 9. Переменная x недоступна.

C. Ошибка компиляции в строке 10. Переменная y недоступна.

D. Ошибка компиляции в строке 11. Переменная z недоступна.

E. Ошибка компиляции в строке 12. Переменная u недоступна.

F. Ошибка компиляции в строке 13. Переменная v недоступна.

 

12. Какие переменные НЕ доступны в методе innerMethod() статического внутреннего класса Inner?

1. public class Outer2 {

2. private int x=7;

3. private static int y=9;

4. private final static int z=9;

5. public static class Inner{

6. public int u=5;

7. public static int v=3;

8. public final static int t=3;

9. public void innerMethod(){

10. System.out.println(" x="+x);

11. System.out.println(" y="+y);

12. System.out.println(" z="+z);

13. System.out.println(" u="+u);

14. System.out.println(" v="+v);

15. System.out.println(" t="+t);} } }

A. Переменная y в строке 11

B. Переменная x в строке 10

C. Переменная z в строке 12

D. Переменная u в строке 13

E. Переменная v в строке 14

F. Переменная t в строке 15

G. Все переменные доступны

 

13. Что будет результатом выполнения двух потоков, имеющих приоритет 10?

1. public class Multithread implements Runnable {

2. public static void main (String[] args) {

3. Multithread []m=new Multithread[5];

4. Thread []t=new Thread[5];

5. for (int i=0;i<5;i++){

6. m[i]=new Multithread();

7. t[i]=new Thread(m[i],"thread"+i);

8. t[i].setPriority(10); t[i].start();}

9. }

10. public void run() {

11. while(true){

12. System.out.println(Thread.currentThread().getName());

13. Thread.currentThread().suspend();

14. Thread.currentThread().resume();}

15. } }

A. Только один нулевой поток t[0] исполнится однократно. Результат: thread=0

B. Пять потоков будут исполняться по очереди однократно. Результат: thread =0 thread =1 thread =2 thread =3 thread =4 .

C. Только один поток t[0] будет многократно исполняться. Результат: thread =0 thread =0 thread =0 …

D. Пять потоков будут исполняться по очереди многократно. Результат: thread =0 thread =1 thread =2 thread =3 thread =4 thread =0 thread =1 thread =2…

E. Пять потоков чередуются неравномерно. Результат, например, может быть таким: thread =0 thread =1 thread =1 thread =2 thread =3 thread =3 thread =3 thread =4 …

 

14. Какой результат компиляции и исполнения программы Вы ожидаете?

1. public class Message implements Runnable{

2. String str1;

3. String str2;

4. public Message(String s1,String s2) {

5. str1=s1; str2=s2;

6. new Thread(this).start(); }

7. public void run() {

8. while(true){

9. System.out.print(str1);

10. try{Thread.sleep(500);}catch(InterruptedException e){};

11. System.out.print(str2); System.out.println();}

12. }

13. public static void main (String[] args) {

14. Message ms1=new Message("IP "," is an ");

15. Message ms2=new Message("Internet ","Protocol ");}

16. }

 

A. Ошибки компиляции в строках 14 и 15, поскольку исключения не обработаны.

B. Дочерний поток исполняется с результатом IP is an Internet Protocol. Затем главный поток завершается. Результат: IP is an Internet Protocol.

C. Дочерний поток исполняется с результатом IP Internet Protocol is an. Затем главный поток завершается. Результат: IP Internet Protocol is an.

D. Дочерний поток исполняется бесконечно долго. Результат может быть любым IP Internet Protocol is an или Internet IP is an Protocol или IP Internet is an Protocol … и т.д.

E. Дочерний поток исполняется бесконечно долго. Результат: IP is an Internet Protocol, IP is an Internet Protocol, IP is an Internet Protocol, …и т.д.