[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
Let's keep this in mind What's new in Java 9
Things to keep in mind when committing to CRuby
Things to keep in mind when adding war to dependency
Things to keep in mind when using if statements
Things to keep in mind when testing private methods in JUnit
Things to keep in mind when using Sidekiq with Rails
Multithreaded to fit in [Java] template
How to learn JAVA in 7 days
Log output to file in Java
How to use classes in Java?
How to name variables in Java
Try to implement Yubaba in Java
How to concatenate strings in java
How to implement Kalman filter in Java
Try to solve Project Euler in Java
Easy to make Slack Bot in Java
Java reference to understand in the figure
Try to implement n-ary addition in Java
Change List <Optional <T >> to Optional <List <T >> in Java
Convert SVG files to PNG files in Java
How to embed Janus Graph in Java
How to get the date in java
Add footnotes to Word documents 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
Things to keep in mind when using Apache PDFBox® with AWS Lambda
Set up a Java GUI in a separate thread to keep the main
Two ways to start a thread in Java + @
Partization in Java
I want to send an email in Java.
CORBA seems to be removed in Java SE 11. .. ..
Code to escape a JSON string in Java
Try to create a bulletin board in Java
Changes in Java 11
I tried to implement deep learning in Java
Set HTTP Keep Alive Timeout in Java HTTP Client
Rock-paper-scissors in Java
How to get Class from Element in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
How to hide null fields in response in Java
Library "OSHI" to get system information in Java
[Java] Introduction to Java
I tried to output multiplication table in Java
Introduction to java
How to solve an Expression Problem in Java
I tried to create Alexa skill 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
How to create a Java environment in just 3 seconds
[Java] How to omit the private constructor in Lombok
Sample code to convert List to List <String> in Java Stream
I want to do something like "cls" in Java
I tried to implement Firebase push notification in Java
How to input / output IBM mainframe files in Java?
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
# 2 [Note] I tried to calculate multiplication tables in Java.