0% found this document useful (0 votes)
3 views

Batch15 Multithreading and Exception

The document contains a series of programming questions related to multithreading and exception handling in Java, along with the user's answers and scores. The user scored 66% on the test, answering various questions about exception types, thread behavior, and output predictions for given code snippets. Each question is designed to assess understanding of Java's exception handling and multithreading concepts.

Uploaded by

moviev270
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Batch15 Multithreading and Exception

The document contains a series of programming questions related to multithreading and exception handling in Java, along with the user's answers and scores. The user scored 66% on the test, answering various questions about exception types, thread behavior, and output predictions for given code snippets. Each question is designed to assess understanding of Java's exception handling and multithreading concepts.

Uploaded by

moviev270
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

22NIT1428

11 Mar 2023, 4:49 p.m.

Batch15 Multithreading and Exception


March11
Your Test Was Successfully Submitted
Your score: 66% 26.6 40
Duration: 0:37:49
/
Congratulations, You Scored More Than
60 %
1.
1 point

Given:

11. public static void parse(String str) {

12. try {

13. float f = Float.parseFloat(str);

14. } catch (NumberFormatException nfe) {

15. f = 0;

16. } finally {

17. System.out.println(f);

18. }

19. }

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

21. parse("invalid");

22. }

What is the result?

Your Answer:

0.0

Compilation fails.

A ParseException is thrown by the parse method at runtime.

A NumberFormatException is thrown by the parse method at runtime.

2.
1 point

Given:

11. static void test() throws RuntimeException {

12. try {

13. System.out.print("test ");

14. throw new RuntimeException();

15. }

16. catch (Exception ex) { System.out.print("exception "); }

17. }

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

19. try { test(); }

20. catch (RuntimeException ex) { System.out.print("runtime "); }

21. System.out.print("end ");

22. }

What is the result?

Your Answer:

test end

Compilation fails.

test runtime end

test exception end

A Throwable is thrown by main at runtime.

3.
1 point

Given:

33. try {

34. // some code here

35. } catch (NullPointerException e1) {

36. System.out.print("a");

37. } catch (Exception e2) {

38. System.out.print("b");

39. } finally {

40. System.out.print("c");

41. }

If some sort of exception is thrown at line 34, which output is possible?

Your Answer:

ac

4.
1 point

Given:

31. // some code here

32. try {

33. // some code here

34. } catch (SomeException se) {

35. // some code here

36. } finally {

37. // some code here

38. }

Under which three circumstances will the code on line 37 be executed? (Choose
three.)

Your Answer:

The instance gets garbage collected.

The code on line 33 throws an exception.

The code on line 35 throws an exception.

The code on line 31 throws an exception.

The code on line 33 executes successfully.

5.
1 point

Given:

11. static void test() {

12. try {

13. String x = null;

14. System.out.print(x.toString() + " ");

15. }

16. finally { System.out.print("finally "); }

17. }

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

19. try { test(); }

20. catch (Exception ex) { System.out.print("exception "); }

21. }

What is the result?

Your Answer:

null

finally

null finally

Compilation fails.

finally exception

6.
1 point

Given:

11. static void test() throws Error {

12. if (true) throw new AssertionError();

13. System.out.print("test ");

14. }

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

16. try { test(); }

17. catch (Exception ex) { System.out.print("exception "); }

18. System.out.print("end ");

19. }

What is the result?

Your Answer:

end

Compilation fails.

exception end

. exception test end

A Throwable is thrown by main.

An Exception is thrown by main.

7.
1 point

Given:

1. class TestException extends Exception { }

2. class A {

3. public String sayHello(String name) throws TestException {

4. if(name == null) throw new TestException();

5. return "Hello " + name;

6. }

7. }

8. public class TestA {

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

10. new A().sayHello("Aiko");

11. }

12. }

Which statement is true?

Your Answer:

Compilation succeeds.

Class A does not compile.

The method declared on line 9 cannot be modified to throw TestException.

TestA compiles if line 10 is enclosed in a try/catch block that catches TestException.

8.
1 point

Given:

11. static class A {

12. void process() throws Exception { throw new Exception(); }

13. }

14. static class B extends A {

15. void process() { System.out.println("B"); }

16. }

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

18. new B().process();

19. }

What is the result?

Your Answer:

The code runs with no output.

Compilation fails because of an error in line 12.

Compilation fails because of an error in line 18.

9.
1 point

Given:

10. public class Foo {

11. static int[] a;

12. static { a[0]=2; }

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

14. }

Which exception or error will be thrown when a programmer attempts to run this
code?

Your Answer:

java.lang.StackOverflowError

java.lang.IllegalStateException

java.lang.ArrayIndexOutOfBoundsException

. java.lang.ExceptionInInitializerError

10.
1 point

Given:

11. class A {

12. public void process() { System.out.print("A,"); }

13. class B extends A {

14. public void process() throws IOException {

15. super.process();

16. System.out.print("B,");

17. throw new IOException();

18. }

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

20. try { new B().process(); }

21. catch (IOException e) { System.out.println("Exception"); }

22. }

What is the result?

Your Answer:

Exception

A,B,Exception

Compilation fails because of an error in line 20.

Compilation fails because of an error in line 14.

A NullPointerException is thrown at runtime.

11.
1 point

Which statement is true?

Your Answer:

An Error that might be thrown in a method must be declared as thrown by that


method, or be handled within that method.

Multiple catch statements can catch the same class of exception more than once.

A try statement must have at least one corresponding catch block.

Except in case of VM shutdown, if a try block starts to execute, a corresponding


finally block will always start to execute.

12.
1 point

When does Exceptions in Java arises in code sequence?

Your Answer:

Run Time

Compilation Time

Can Occur Any Time

None of the mentioned

13.
1 point

Consider the following try…….. catch block:

class TryCatch

public static void main(String args[ ])

try

double x=0.0;

throw(new Exception("Thrown"));

return;

catch(Exception e)

System.out.println("Exception caught");

return;

finally

System.out.println("finally");

What will be the output.

Your Answer:

Exception caught

Exception caught finally

finally

Thrown

14.
1 point

Consider the following code snippet:

...................

...................

try {

int x=0;

int y=50/x;

System.out.println("Division by zero");

catch(ArithmeticException e) {

System.out.println("catch block");

..................

..................

What will be the output?

Your Answer:

Error.

Division by zero

Catch block

Division by zero Catch block

15.
1 point

State whether the following statements are True or False.

i) A catch can have comma-separated multiple arguments.

ii) Throwing an Exception always causes program termination.

Your Answer:

True, False

False, True

True, True

False, False

16.
1 point

What is multithreaded programming?

Your Answer:

It’s a process in which two different processes run simultaneously

It’s a process in which two or more parts of same process run simultaneously

It’s a process in which many different process are able to access same information

It’s a process in which a single process can access information from many sources

17.
1 point

What is the priority of the thread in the following Java Program?

class multi_programing {

public static void main(String args[]){

Thread t = Thread.currentThread();

System.out.println(t);

Your Answer:

18.
1 point

What will be the output of the following Java program?

class newthread extends Thread{

Thread t;

String name;

newthread(String threadname) {

name = threadname;

t = new Thread(this,name);

t.start();

public void run() {

class multithreaded_programing{

public static void main(String args[]){

newthread obj1 = new newthread("one");

newthread obj2 = new newthread("two");

try

obj1.t.wait();

System.out.print(obj1.t.isAlive());

catch(Exception e)

System.out.print("Main thread interrupted");

Your Answer:

True

false

Main thread interrupted

None of the mentioned

19.
1 point

What will be the output of the following Java code?

class newthread implements Runnable{

Thread t;

newthread(){

t = new Thread(this,"New Thread");

t.start();

public void run(){

t.setPriority(Thread.MAX_PRIORITY);

System.out.println(t);

class multithreaded_programing{

public static void main(String args[]){

new newthread();

Your Answer:

Thread[New Thread,0,main]

Thread[New Thread,1,main]

Thread[New Thread,5,main]

Thread[New Thread,10,main]

20.
1 point

Predict the output of the following program:

class A implements Runnable{

public void run() {

try{

for (int i=0;i<4;i++) {

Thread.sleep(100);

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

} catch (InterruptedException e) {

public class Test {

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

A a new A(); =

Thread t = new Thread(a, "A");

Thread t1 = new Thread (a, "B");

t.start();

t.join();

t1.start();

Your Answer:

A.A A A A B B B B

B.A B A B A B A B

C.Output order is not guaranteed

D.Compilation succeed but Runtime Exception

21.
1 point

============

Number of threads in below java program is ?

public class ThreadExtended extends Thread {

public void run() {

System.out.println("\nThread is running now\n");

public static void main(String[] args) {

ThreadExtended threadE = new ThreadExtended();

threadE.start();

Your Answer:

22.
1 point

public class DaemonThread extends Thread {

public DaemonThread(String name){

super(name);

public void run() {

if(Thread.currentThread().isDaemon()) {

System.out.println(getName() + " is Daemon thread");

else

System.out.println(getName() + " is User thread");

public static void main(String[] args) {

DaemonThread t1 = new DaemonThread("t1");

DaemonThread t2 = new DaemonThread("t2");

DaemonThread t3 = new DaemonThread("t3");

t1.setDaemon(true);

t1.start();

t2.start();

t3.setDaemon(true);

t3.start();

Your Answer:

t1 is Daemon thread

t3 is Daemon thread

t2 is User threa

Thread name: Thread-0

Check if its DaemonThread:

t3 is Daemon thread

t2 is User thread

t1 is Daemon thread

t2 is User thread

23.
1 point

class Test implements Runnable {

Public void run() {

System.out.println("Run");

class Myclass {

public static void main(String[] args){

Thread t1 = new Thread();

t1.start();

System.out.println("Main");

Your Answer:

Run

Main

Compile time error

Run Main

24.
1 point

class Base {

final public void show() {

System.out.println("Base::show() called");

class Derived extends Base {

public void show() {

System.out.println("Derived::show() called");

class Main {

public static void main(String[] args) {

Base b = new Derived();;

b.show();

Your Answer:

Base::show() called

Derived::show() called

Compiler Error

Runtime Error

25.
1 point

FirstTh th=new FirstTh(); Thread sm=new Thread(th).start();

Which of the following statements is correct with respect to the above code?

Your Answer:

FirstTh is a simple class.

FirstTh is a class that inherits from Thread class.

FirstTh is a class that implements from Thread class.

FirstTh is a class that implements Runnable interface.

26.
1 point

What is the output of this program?

class newthread extends Thread {

newthread() {

super("My Thread");

start();

public void run() {

System.out.println(this);

class multithreaded_programing{

public static void main(String args[]) {

new newthread();

Your Answer:

My Thread

Thread[My Thread,5,main].

Compilation Error

Runtime Error

27.
1 point

What is the output of this program?

class newthread extends Thread {

Thread t;

newthread(){

t = new Thread(this,"My Thread");

t.start();

public void run(){

try

t.join()

System.out.println(t.getName());

catch(Exception e)

System.out.print("Exception");

class multithreaded_programing {

public static void main(String args[]){

new newthread();

Your Answer:

a.My Thread

b.Thread[My Thread,5,main].

c.Exception

d.Runtime Error

28.
1 point

============

What will be the output of the program?

class MyThread extends Thread{

public static void main(String [] args){

MyThread t = new MyThread();

t.start();

System.out.print("one. ");

t.start();

System.out.print("two. ");

public void run(){

System.out.print("Thread ");

Your Answer:

Compilation fails

An exception occurs at runtime.

It prints "Thread one. Thread two."

The output cannot be determined.

29.
1 point

The static method Thread.currentThread() returns a reference to the currently


executing Thread object. What is the result of this code?

class Test{

public static void main(String [] args){

printAll(args);

public static void printAll(String[] lines){

for(int i = 0; i < lines.length; i++)

System.out.println(lines[i]);

Thread.currentThread().sleep(1000);

Your Answer:

Each String in the array lines will output, and there is no guarantee there will be a
pause because currentThread() may not retrieve this thread.

Each String in the array lines will output, with no pause in between because this
method is not executed in a Thread.

Each String in the array lines will output, with a 1-second pause.

This code will not compile.

30.
1 point

What will be the output of the below program?

class MyThread extends Thread {

public void run() {

System.out.print("Child ");

public class Test {

public static void main(String[] args)

throws InterruptedException {

MyThread t = new MyThread();

t.start();

t.join();

System.out.print("Main ");

Your Answer:

Main Child

Child Main

Main Main

Child Child

31.
1 point

What will be the output of the below program?

class MyThread extends Thread {

public static Thread mt;

public void run() {

try {

mt.join();

} catch(InterruptedException ie) {}

System.out.print("Child ");

public class Test {

public static void main(String[] args)

throws InterruptedException {

MyThread.mt = Thread.currentThread();

MyThread t = new MyThread();

t.start();

Thread.sleep(1000);

System.out.print("Main ");

Your Answer:

Main Child

Child Main

Main Main

Child Child

32.
1 point

After calling join(1000) thread will wait for?

Your Answer:

Exactly 1 second

Minimum 1 second

Maximum 1 second

Will wait till child thread not completed

33.
1 point

If a thread wants to pause its execution to give the chance for remaining threads of
the same priority then which method should be called?

Your Answer:

yield() method

join() method

sleep() method

All of these

34.
1 point

Compile time error will occur in which method?

public class Test {

public static void main(String[] args) {

m1();

Integer.parseInt("two");

public static void m1() {

m2();

System.out.println(9/0);

public static void m2() {

Thread.sleep(1000);

Your Answer:

main()

m1()

m2()

Compiled Successfully

35.
1 point

Compile time error will occur in which method?

public class Test {

public static void main(String[] args)

throws InterruptedException {

m1();

public static void m1()

throws InterruptedException {

m2();

public static void m2()

throws InterruptedException {

Thread.sleep(1000);

Your Answer:

main()

m1()

m2()

Compiled Successfully

36.
1 point

Find the output of the below program?

public class Test {

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

Your Answer:

Compile-time error

Exception

No output

None of these

37.
1 point

Which two are valid constructors for Thread?

1. Thread(Runnable r, String name)

2. Thread()

3. Thread(int priority)

4. Thread(Runnable r, ThreadGroup g)

5. Thread(Runnable r, int priority)

Your Answer:

1 and 3

2 and 4

1 and 2

2 and 5

38.
1 point

What will be the output of the program?

class MyThread extends Thread {

MyThread() {

System.out.print(" MyThread");

public void run(){

System.out.print(" bar");

public void run(String s) {

System.out.println(" baz");

public class TestThreads {

public static void main (String [] args){

Thread t = new MyThread() {

public void run(){

System.out.println(" foo");

};

t.start();

Your Answer:

foo

MyThread foo

MyThread bar

foo bar

39.
1 point

What will be the output of the program?

class MyThread extends Thread {

public static void main(String [] args){

MyThread t = new MyThread();

t.start();

System.out.print("one. ");

t.start();

System.out.print("two. ");

public void run(){

System.out.print("Thread ");

Your Answer:

Compilation fails

An exception occurs at runtime.

It prints "Thread one. Thread two."

The output cannot be determined.

40.
1 point

What will be the output of the program?

class s1 implements Runnable {

int x = 0, y = 0;

int addX() {x++; return x;}

int addY() {y++; return y;}

public void run() {

for(int i = 0; i < 10; i++)

System.out.println(addX() + " " + addY());

public static void main(String args[]){

s1 run1 = new s1();

s1 run2 = new s1();

Thread t1 = new Thread(run1);

Thread t2 = new Thread(run2);

t1.start();

t2.start();

Your Answer:

Compile time Error: There is no start() method

Will print in this order: 1 1 2 2 3 3 4 4 5 5...

Will print in this order: 1 2 3 4 5 6... 1 2 3 4 5 6...

Will print but not exactly in an order (e.g: 1 1 2 2 1 1 3 3...)

Logout

You might also like