[Java] [Java Bronze] 5 problems to keep in mind

4 minute read

Introduction

I recently took the JavaBronze SE7/8 exam. I will introduce the problems that I personally noticed while studying.

  • All questions were not correct, but I passed.

    Question 1. Compatibility of int type and char type

    ### ``` public class Q1 {

public static void main(String[] args) { char chr = 65; int num =’A’; Q1 q1 = new Q1(); q1.func1(chr, num); }

void func1(char chr, int num) { System.out.println(“chr:” + chr); System.out.println(“num:” + num); } }

### What is the result of compiling this code? Please select one.
### A. chr:A<br> num:65<br>B. A compilation error occurs <br>C. A runtime error occurs <br>D. Nothing is displayed

## Comment
The primitive types int and char are compatible and implicit cast is possible.
When the int type is assigned to the char type variable, the character corresponding to the ASCII code table is assigned.
Conversely, if you assign a char type to an int type variable, the decimal number corresponding to that character will be assigned.
#### Correct answer: A
## Second question. Increment and decrement operators
###

public class Q2 {

public static void main(String[] args) { int num = 1; System.out.print(++num + num++ + –num + num–); } }

### What is the result of compiling this code? Please select one.
### A.4<br>B.6<br>C.7<br>D.8

## Comment
++num is calculated as 2, num++ is 2, --num is 2, and num-- is 2 for a total of 8.
It should be noted that num++ is calculated as 2 and then incremented to 3,
Immediately after that it is decremented by --num to 2.
If the same variable is found on the right side of the increment and decrement operators that are placed after it, the processing will be reflected for that variable.
#### Correct answer: D
## Third question. Infinite loop and run-time error
###

public class Q3 {

public static void main(String[] args) { char[] chr = {‘A’,’B’,’C’ }; while (true) for (int i = 0; i <= chr.length; i++) System.out.println(chr[i]); } }

### What is the result of compiling this code? Please select one.
### A. Execution ends normally <br>B. Infinite loop <br>C. Compile error occurs <br>D. Runtime error occurs

## Comment
An infinite loop occurs in the while statement and a runtime error (ArrayIndexOutOfBoundsException) occurs in the for statement.
However, even if the process being executed is in progress when a runtime error occurs in the for statement,
The process is stopped midway and is output as a runtime error.
If these are occurring at the same time, it is determined that they may occur in the following order.

1. Compile error
2. Runtime error
3. Infinite loop
4. Execution ends normally
#### Correct answer: D
## Question #4: Override relationship
###

class Super { static void func(String str) { } } class Sub extends Super { String str; void func(String str) { this.str = str; } void print() { System.out.println(str); } } public class Q4 { public static void main(String[] args) { Sub sub = new Sub(); sub.func(“Hello World”); sub.print(); } }

### What is the result of compiling this code? Please select one.
### A. Hello World <br>B. A compilation error occurs <br>C. A runtime error occurs <br>D. Nothing is displayed

## Comment
The func method of the Super class is overridden by the func method of the Sub class, but since it is qualified with static, a compilation error will occur.
It should be noted that the problem is that the main method tracing does not notice the error.
You should focus on the methods in the superclass and subclass override relationships rather than the main method processing.
#### Correct answer: B
## Question #5: Class-type cast
###

class Super { void print() { System.out.println(“Hello World”); } }

class Sub extends Super { }

public class Q5 { public static void main(String[] args) { Sub sub; Super spr; sub = new Sub(); spr = sub; sub = spr; sub.print(); } }

### What is the result of compiling this code? Please select one.
### A. Hello World <br>B. A compilation error occurs <br>C. A runtime error occurs <br>D. Nothing is displayed

## Comment
No error occurs in the line of `spr = sub;` that assigns to subclass type → superclass type,
A compile error will occur at the line of `sub = spr;` that assigns from superclass type to subclass type.

In the case of elementary data types, assigning a large variable to a small variable requires an explicit cast.

Example) double double_01 = 100.0; int int_01 = double_01; // Implicit cast (error occurs) int int_01 = (int)double_01; // explicit cast (no error) ``` Then, in the case of a class type, an error does not occur even if you assign it to a subclass type that is a large type (superclass + difference class) → a small type (superclass only), and vice versa?

The reason is that the compiler will check the type compatibility of the compilation to determine if it is an error. In the case of basic data, compatibility is judged by the size of the type, but in the case of class type, it is judged by looking at the contents of the class.

The subclass says which superclass it inherits from, but the superclass doesn’t say which subclass it inherits from.

Therefore, if you assign a superclass to a subclass, compatibility will be known and no error will occur.However, if you assign a subclass to a superclass, compatibility will not be known. It will be.

Correct answer: B

in conclusion

What did you think. The test preparation is perfect! !! !! Kamo _(:3”∠)_