|
|
51: Choose the correct result when executing the following code with the command java a/Test.java.
package a;
public class Test extends jp.co.java.Test {
public static void main(String[] args) {
String i = "b";
String j = "a";
i = j;
i = null;
System.out.print(jp.co.java.Test.check(i) + jp.co.java.Test.check(j));
}
}
package jp.co.java;
class Main{
public static void main(String[] args) {
String i = "a";
string j = "b";
i = j;
}
}
public class Test {
protected static String check(String i) {
return switch(i) {
case "a" -> i.toString();
case "b" -> i.toLowerCase();
default -> null;
};
}
}
A:ab
B:nullnull
C:bb
D:nulla
E:A compilation error occurs
F:An exception is thrown at runtime
Check Answer
When branching based on a String value in a Java switch expression (or switch statement), the String.hashCode() method is called. Since the variable i in switch(i) is null, a NullPointerException is thrown the moment this line is evaluated.
52: Choose the correct result when the following code is compiled and executed.
ppublic class Main{
public static void main(String[]args) {
A a=new A();
try (a){
A.put(a);
}finally {
System.out.println(""+a.i[0]+a.i[1]);
}
a=null;
}
}
class A implements AutoCloseable{
public char[]i= {'a','b'};
public void close() {
i[1]='a';
}
static void put(A a) {
a.i[0]='b';
}
}
A:aa
B:ab
C:ba
D:195
E:A compilation error occurs
F:An exception is thrown at runtime
Check Answer
There is a rule that when using an existing variable declared outside (in this case, a) within the try parentheses (), that variable must be final or effectively final. Effectively final means that the reference (value) is never changed after the variable is initialized. However, in this code, there is a line a=null; after the try-finally block, and the variable a is reassigned. Due to this reassignment, variable a is not considered effectively final, and a compilation error occurs at the try (a) line. (If these errors were fixed, i[0] would become 'b' in the put method of the try block, the close method would be called automatically at the end of the try making i[1] 'a', and ba would be output in the finally block).
53: Read the following code and choose two correct definitions for class A.
public class Sample {
public static void main(String[] k) {
throw new A();
}
}
A:class A extends java.io.IOException {}
B:class A {}
C:class A extends RuntimeException {}
D:class A implements AutoCloseable { public void close() {}}
E:class A {public void a() {throw new NumberFormatException();}}
F:class A extends Error {public void a() {throw new IllegalArgumentException();}}
G:None of the above are correct
Check Answer
The main method in this code does not have a throws clause to declare exception throwing. In Java rules, if a method does not have a throws declaration, it can only throw unchecked exceptions within that method. Unchecked exceptions refer to RuntimeException, Error, and their subclasses. For the code throw new A(); to not result in a compilation error, class A must be a subclass of either RuntimeException or Error. A: IOException is a checked exception, not a RuntimeException. To throw this, a throws IOException declaration is required in the main method. B, D, E: A does not inherit from Throwable (the parent class of exceptions and errors). Only subclasses of Throwable can be thrown using the throw keyword. C: A inherits from RuntimeException. Since this is an unchecked exception, it can be thrown without a throws declaration in the main method. F: A inherits from Error. Since this is also an unchecked exception, it can be thrown without a throws declaration in the main method.
54: Read the following code and choose the correct definitions for interface B and interface C.
public interface A extends B, C {default char[] take() {}}
A:interface B {int move();}
interface C {int[] move();}
B:interface B {Collection move();}
interface C {Object move();}
C:interface B {char take();}
interface C {static void take(int…i) {}}
D:interface B { B() {}}
interface C {C() {}}
E:interface B {void move();}
interface C {Object move();}
F:None of the above are correct
Check Answer
A: A will inherit two abstract methods, int move() and int[] move(), which have the same signature (name and arguments) but different return types. Since int and int[] are not compatible, A will result in a compilation error.
B: A inherits Collection move() and Object move(). The return types (Collection and Object) are compatible (Collection is a subtype of Object). In this case, A inherits the more specific abstract method Collection move(). This is syntactically valid, and A will not result in a compilation error.
C: B's char take() and A's default char[] take() {} conflict with the same signature (take()). A would implement B's abstract method, but since B's return value char and A's return value char[] are not subtypes of each other, a compilation error occurs.
D: Interfaces cannot have constructors. The definitions of B and C themselves will result in compilation errors.
E: A inherits two abstract methods, void move() and Object move(), which have the same signature but different return types. Since void and Object are not compatible, A will result in a compilation error.
55: Choose the correct result when the following code is compiled and executed.
package a$;
public class Test extends Test2{
public static void main(String[] args) {
check();//line a
cut();//line b
}
}
class Test2 {
protected static void check() {
System.out.println('a' + 'b');
}
}
class Test3 {
static void cut() {
System.out.println('a');
}
}
A:A compilation error occurs at line a
B:A compilation error occurs at line b
C:A compilation error occurs at both line a and line b
D:195
a
E:195
97
F:aba
G:An exception is thrown at runtime
Check Answer
check();
Protected members are accessible from classes within the same package (in this case, package a$) or from subclasses. The Test class inherits from Test2 and is in the same package. Therefore, calling the check() method without specifying the class name is grammatically correct and does not result in a compilation error.
cut();
Test3 is an independent class with no inheritance relationship to the calling Test class (or its superclass Test2). To call a static method defined in another class, the class name usually needs to be explicitly specified (e.g., Test3.cut();). If only cut(); is written, the compiler will only look for the cut() method within the calling class (Test) or its parent class (Test2). Since the cut() method is not defined in either Test or Test2, the compiler generates a "cannot find symbol" error at line b.
Supplement (Output if no compilation error occurred):
If line b were corrected to Test3.cut(); and compilation succeeded, the execution result would be as follows:
Execution of System.out.println('a' + 'b');
In Java, operations on char type (character type) are performed by converting them into ASCII/Unicode integer values corresponding to the characters. The code for character 'a' is 97, and the code for character 'b' is 98. The calculation 97 + 98 is performed, the result 195 is output, and a newline is added.
Execution of System.out.println('a'); The character 'a' is output as is, and a newline is added.
|
|
56: Choose the correct result when the following code is compiled and executed.
import java.util.ArrayList;
public class Main {
public static void main(String[]args) {
int i[]= {2,3,1,4};int k=0;
ArrayList<Integer>j=new ArrayList();
while(k<i.length) {
if(i[k]%2==1)j.add(i[k]);
else j.add(i[k]-1);
k++;
}
for(int l:j) {
if(l==i[2])j.remove(i[2]);
else System.out.print(l);
}
}
}
A:Nothing is output
B:33
C:11
D:1
E:A compilation error occurs
F:An exception is thrown at runtime
Check Answer
This code stops with a runtime error called java.util.ConcurrentModificationException.
1. Creation of ArrayList j
First, in the first half of the code, ArrayList j is created based on the elements of array i. Array i is {2, 3, 1, 4}. If an element is even, the value minus 1 is added to j; if it is odd, the value is added as is. As a result, ArrayList j becomes [1, 3, 1, 3]. At this time, the value of i[2] is 1.
2. For loop
Variable l takes the first element of j, which is 1. The conditional expression l (1) == i[2] (1) becomes true. j.remove(i[2]), which is j.remove(1), is executed. Since ArrayList j is declared as a Raw Type, the argument 1 is interpreted as an index. The element at index 1, which is 3, is removed from j. j changes to [1, 1, 3]. An exception occurs because the next element 3 is read when entering the second loop immediately after the number of elements in the list has changed.
57: Choose the correct result when the following code is compiled and executed.
public class Test {
public static void main(String[] args) {
int a = 4 + 2 * 22 / (4 + 4);
int b = a;
int c = 2;
c *= a /= 2;
System.out.println("" + a + b + c);
}
}
A:494
B:16
C:21
D:498
E:448
F:A compilation error occurs
Check Answer
First, the values of variables a and b are calculated. int a = 4 + 2 * 22 / (4 + 4); First, the content inside the parentheses is calculated. 4 + 4 becomes 8. a = 4 + 2 * 22 / 8; Next, multiplication and division are performed from left to right. 2 * 22 becomes 44. a = 4 + 44 / 8; Next, division is performed. Since it is an integer operation, the decimal part is truncated. 44 / 8 is 5.5, but since it is an integer operation, it becomes 5. Finally, addition is performed. a = 9;
Next, the value of a is assigned to b. int b = a; b becomes 9.
Next, the line containing the compound assignment operator is executed. c *= a /= 2; In this line, the associativity and execution order of the assignment operator are important. Assignment operators (= and compound assignment operators) associate from right to left. First, the right assignment operator a /= 2 is executed. This has the same meaning as a = a / 2. Since the current value of a is 9, a = 9 / 2, and the new value of a becomes 4 due to integer operation. The value of the result of this expression a /= 2 is the value of a after assignment, which is 4. Next, the left assignment operator c *= ... is executed. This has the same meaning as c = c * (...). The value inside the parentheses is 4, which is the result of a /= 2 calculated in the previous step. Since the current value of c is 2, c = 2 * 4, and the new value of c becomes 8.
Finally, the following line is executed: System.out.println("" + a + b + c); By joining with "" (empty string), all numerical values are converted to strings and concatenated. The value of a 4, the value of b 9, and the value of c 8 are concatenated in this order. The output string is "498".
58: Choose the correct result when the following code is compiled and executed.
public class Test {
public static void main(String[] args) {
while (true) {
if (false) {
System.out.print(0);
} else if (true) {
System.out.print(1);
}
if (2 == 2) {
break;
}
}
}
}
A:0
B:1
C:01
D:An infinite loop occurs
E:A compilation error occurs
F:An exception is thrown at runtime
Check Answer
A loop is started by while (true). Since if (false) is always false, System.out.print(0); is not executed. Since else if (true) is always true, System.out.print(1); is executed. As a result, 1 is output to the console. Next, the second if (2 == 2) is evaluated. 2 == 2 is always true. break; inside the if block is executed. The while (true) loop is immediately interrupted (exited).
59: Choose the correct result when the following code is compiled and executed.
public class Test {
public static void main(String[] args) {
byte a = 0b111;
a(a);
}
private static void a(byte i) {
if ((i ^ 1) % 2 == 1) {
return;
}
switch(i & 1) {
case 1 -> System.out.print((double)i/3 + (i/3.0));
default -> System.out.print(i | 0b0000);
}
}
}
A:4.33333333333334
B:4.66666666666667
C:4
D:7
E:A compilation error occurs
F:An exception is thrown at runtime
Check Answer
0b indicates a binary literal. 0b111 is 7 in decimal. Method a(byte i) is called, and 7 is passed to argument i. if ((i ^ 1) % 2 == 1) { return; } Calculate bitwise exclusive OR (^). 7 ^ 1 becomes 6. (In binary, 0111 ^ 0001 = 0110) Calculate the modulo operator (%). 6 % 2 is 0. The conditional expression of the if statement is 0 == 1, which is false. Therefore, return is not executed, and the process proceeds to the switch expression. switch(i & 1) Calculate bitwise AND (&). 7 & 1 is 1. (In binary, 0111 & 0001 = 0001) Since the evaluation result of the switch expression is 1, the process of case 1 is executed. (double)i/3: i (7) is converted to double type 7.0 by casting. 7.0 / 3 becomes 2.3333333333333335 of double type. (i/3.0): Since 3.0 is a double type, i (7) is automatically promoted to double type 7.0. 7.0 / 3.0 becomes 2.3333333333333335 of double type. 2.3333333333333335 + 2.3333333333333335 becomes 4.666666666666667 of double type. (The last digit is rounded due to floating point precision).
60: Choose the correct result when the following code is compiled and executed.
public class Main {
static Main m;
public static void main(String[] args) {
A a = new A();
try (a; B b = new B()) {
System.out.print("try");
throw new java.io.IOException();
} catch(RuntimeException i){
System.out.print("R");
}catch(java.io.IOException j) {
System.out.print("I");
}finally {
m.close();
}
}
public void close() {}
}
class A extends Main implements java.io.Closeable {
public void close() {
m = new A();
System.out.print("A");
}
}
class B extends Main implements AutoCloseable {
public void close() {
m = new B();
throw new RuntimeException();
}
}
A:AtryIA
B:tryARA
C:tryARIA
D:tryAIA
E:A compilation error occurs
F:An exception is thrown at runtime
Check Answer
1. Execution of the try block: Initialization of A a = new A(); and then B b = new B() is initialized within the try parentheses. The try block body is executed, and System.out.print("try"); is executed. throw new java.io.IOException(); is executed, and an IOException (let's call it E1) occurs.
2. Resource closing by try-with-resources: Since an exception occurred in the try block, the resources declared in the try are automatically closed before moving to the catch block. Closing is executed in the reverse order of declaration. b.close() is executed, and b's close method is called. m = new B(); is executed, and the static variable m refers to the instance of B. throw new RuntimeException(); is executed, and a RuntimeException (let's call it E2) occurs. Although exception E2 occurred during the closing of b, the closing of a is always executed. a.close() is executed, and a's close method is called. m = new A(); is executed, and the static variable m is overwritten to refer to the instance of A. System.out.print("A"); is executed.
3. Exception handling (catch block): There are two exceptions: E1 (IOException) that occurred inside the try block and E2 (RuntimeException) that occurred during resource closing. In the try-with-resources syntax, the exception (E1) that occurred in the try block body is treated as the "main exception". The exception during resource closing (E2) is added to E1 as a "suppressed exception", and only E1 is the target of the catch block. catch(RuntimeException i): Since E1 is an IOException, it does not match and is skipped. catch(java.io.IOException j): Matches E1. System.out.print("I"); is executed.
4. Execution of the finally block: After the execution of the catch block, the finally block is always executed. m.close(); is executed. At this point, what m refers to is the instance of A assigned last in the a.close() method. Therefore, the close method of class A is called again. m = new A(); is executed (m refers to the new A instance). System.out.print("A"); is executed.
コメント