[Java Bronze] 5 problems to keep in mind

Introduction

I recently took the JavaBronze SE 7/8 exam. I will introduce the problems that I personally worried about while studying.

1st question. Compatibility between 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 are the consequences of compiling this code? Please select one.

A. chr: A
num: 65
B. A compile error occurs
C. A run-time error occurs
D. Nothing is displayed

Commentary

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.

Correct answer: A

2nd question. Increment operator and decrement operator

public class Q2 {

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

What are the consequences of compiling this code? Please select one.

A.4
B.6
C.7
D.8

Commentary

++ 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.

Correct answer: D

3rd 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 are the consequences of compiling this code? Please select one.

A. Execution ends normally
B. Infinite loop occurs
C. Compile error occurs
D. Run-time error occurs

Commentary

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.

  1. Compilation error
  2. Run-time error
  3. Infinite loop
  4. Successful execution

Correct answer: D

4th question. Overriding 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 are the consequences of compiling this code? Please select one.

A. Hello World
B. A compile error occurs
C. A run-time error occurs
D. Nothing is displayed

Commentary

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.

Correct answer: B

5th question. 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 are the consequences of compiling this code? Please select one.

A. Hello World
B. A compile error occurs
C. A run-time error occurs
D. Nothing is displayed

Commentary

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.

Correct answer: B

in conclusion

What did you think. This is perfect for exam preparation! !! !! Maybe \ _ (: 3 "∠) \ _

Recommended Posts

[Java Bronze] 5 problems to keep in mind
How to fix system date in JUnit
[Java Bronze] 5 problems to keep in mind
Multithreaded to fit in [Java] template
How to concatenate strings in java
Sample to unzip gz file in Java
Java to C and C to Java in Android Studio
Add SameSite attribute to cookie in Java
Set up a Java GUI in a separate thread to keep the main
How to hide null fields in response in Java
Pi in Java
How to write Java String # getBytes in Kotlin?
FizzBuzz in Java
Things to watch out for in Java equals
N things to keep in mind when reading "Introduction to Spring" and "Introduction to Spring" in the Reiwa era
I tried to implement the Euclidean algorithm in Java
~ I tried to learn functional programming in Java now ~
How to put old Java (8 series) in macOS 10.15 Catalina
Notes on how to use regular expressions in Java
I tried to find out what changed in Java 9
Boyer-Moore implementation in Java
Use PreparedStatement in Java
What's new in Java 9,10,11
Parallel execution in Java
[Beginner] I made a program to sell cakes in Java
I tried to make a client of RESAS-API in Java
I wanted to think I understood Elm's custom types in Java
I want to simplify the conditional if-else statement in Java
Things to watch out for in Java equals
[Ransack] Watch out for ransackable_scopes!
Things to watch out for when using Deeplearning4j Kmeans
Things to watch out for in Java equals
[Ransack] Watch out for ransackable_scopes!
Things to keep in mind when committing to CRuby