I recently took the JavaBronze SE 7/8 exam. I will introduce the problems that I personally worried about while studying.
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);
}
}
The primitive types int and char are compatible and can be implicitly cast. When you assign an int type to a 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.
public class Q2 {
public static void main(String[] args) {
int num = 1;
System.out.print(++num + num++ + --num + num--);
}
}
A.4
B.6
C.7
D.8
++ num is calculated as 2, num ++ is calculated as 2, --num is calculated as 2, and num-- is calculated as 2, for a total of 8. It should be noted that num ++ is calculated as 2 and then incremented to 3 Shortly thereafter, it is about to be decremented by --num to 2. If the same variable is on the right side of the postfixed increment and decrement operators, the processing is reflected for that variable.
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]);
}
}
An infinite loop occurs in the while statement, and a runtime error (ArrayIndexOutOfBoundsException) occurs in the for statement. However, when a run-time error occurs in the for statement, even if it is in the middle of the processing being executed The process is stopped halfway and is output as a run-time error. If these occur at the same time, the possibility of them occurring in the following order will be determined.
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();
}
}
The func method of the Super class is overridden by the func method of the Sub class, but since it is statically qualified, a compile error will occur. It's important to note that the problem is that you don't notice the error when you trace the main method. You need to focus on the methods that are related to superclass and subclass overrides, not the processing of the main method.
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();
}
}
No error occurs in the spr = sub;
line that assigns to the subclass type → superclass type.
A compile error occurs on the line sub = spr;
that assigns to the superclass type → subclass type.
For basic data types, an explicit cast is required when assigning a large variable to a small variable.
Example)
double double_01 = 100.0;
int int_01 = double_01; //Implicit cast(An error occurs)
int int_01 = (int)double_01; //Explicit cast(No error occurs)
Then, in the case of a class type, an error does not occur even if it is assigned to a subclass type that is a large type (super class + difference class) → a super class that is a small type (super class only), and vice versa?
The reason is that the compiler checks the compilation for type compatibility 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, you will not get an error because you know the compatibility, but if you assign a subclass to a superclass, you will not know the compatibility, so if you do not explicitly cast it, you will get a compile error. It becomes.
What did you think. This is perfect for exam preparation! !! !! Maybe \ _ (: 3 "∠) \ _
Recommended Posts