Java Exception Kuiz 2-ci hissə (ADVANCED)

Java Exception mövzusuna aid 5 ədəd Çətin sualdan ibarət kuiz. Uğurlar!

12 Noyabr 2024 - 13:59
 0  493

1. Aşağıdakı kodun nəticəsi nə olacaq?

package quiz;

public class JavaAzerbaijanException {
public static void main(String[] args) {
try {
method();
} catch (Exception e) {
System.out.println("Caught in main");
}
}

public static void method() throws Exception {
try {
throw new Exception("Thrown from method");
} finally {
System.out.println("Finally in method");
}
}
}
Finally in method
Caught in main
Finally in method və Caught in main
Thrown from method və Caught in main

2. Aşağıdakı kodun nəticəsi nə olacaq?

package quiz;

public class JavaAzerbaijanException {
public static void main(String[] args) {
try {
int result = calculate();
System.out.println("Result: " + result);
} catch (Exception e) {
System.out.println("Caught Exception");
}
}

public static int calculate() {
try {
throw new ArithmeticException();
} finally {
return 42;
}
}
}
Caught Exception
Caught Exception və Result: 42
ArithmeticException
Result: 42

3. Aşağıdakı kodun nəticəsi nə olacaq?

package quiz;

public class JavaAzerbaijanException {
public static void main(String[] args) {
try {
int result = getValue();
System.out.println("Result: " + result);
} catch (Exception e) {
System.out.println("Caught Exception");
}
}

public static int getValue() {
try {
int number = 20;
System.out.println(number / 0);
return 10;
} finally {
return 20;
}
}
}
java.lang.ArithmeticException: / by zero at main
Result: 10
Result: 20
Caught Exception

4. Aşağıdakı kodun nəticəsi nə olacaq?

package quiz;

public class JavaAzerbaijanException {
public static void main(String[] args) {
try {
Object x = new Integer(0);
System.out.println((String) x);
} catch (ClassCastException e) {
System.out.println("Class cast exception caught");
} finally {
System.out.println("Finally block executed");
}
}
}
Class cast exception caught
Finally block executed
Class cast exception caught və Finally block executed
java.lang.Integer cannot be converted to java.lang.Object

5. Aşağıdakı kodun nəticəsi nə olacaq?

package quiz;

public class JavaAzerbaijanException {
public static void main(String[] args) {
try {
throw new NullPointerException("Test Exception");
} catch (Exception e) {
System.out.println("Caught Exception");
System.exit(0);
} finally {
System.out.println("Finally block executed");
}
}
}
Caught Exception
Caught Exception və Finally block executed
Finally block executed
Compilation error