1. Object 클래스에 대한 설명 중 틀린 것은 무엇입니까? (4)

① 모든 자바 클래스의 최상위 부모 클래스이다.

② Object의 equals() 메소드는 == 연산자와 동일하게 번지를 비교한다.

③ Object의 clone() 메소드는 얕은 복사를 한다.

Object의 toString() 메소드는 객체의 필드값을 문자열로 리턴한다. -> Object 클래스의 toString() 메소드는 "클래스명@16진수해시코드"로 구성된 문자 정보를 리턴한다.

 

2. 여러분이 작성하는 클래스를 동등 비교하는 컬렉션 객체인 HashSet, HashMap, Hashable을 사용하려고 합니다. Object의 equals() 와 hashCode() 메소드를 오버라이딩했다고 가정할 경우, 메소드 호출 순서를 생각하고 다음 괄호( ) 안을 채워보세요.

 

3. Student 클래스를 작성하되, Object의 equals()와 hashCode()를 오버라이딩해서 Student의 학번(studentNum)이 같으면 동등 객체가 될 수 있도록 해보세요. Student 클래스의 필드는 다음과 같습니다. hashCode()의 리턴값은 studentNum 필드값의 해시코드를 리턴하도록 하세요.

package exercise.Exercise03;

public class Student {
	private String studentNum;
	
	public Student(String studentNum) {
		this.studentNum = studentNum;
	}
	
	public String getStudentNum() {
		return studentNum;
	}
	
	@Override
	public int hashCode() {
		return studentNum.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Student) {
			Student student = (Student) obj;
			if(studentNum.equals(student.getStudentNum())) { //그냥 studentNum이 아니라 student.getStudentNum
			//Object의 studentNum이 Student의 studentNum과 같은지 검사
                return true;
			}		
		}
		return false;
	}
}
package exercise.Exercise03;

import java.util.HashMap;

public class StudentExample {
	public static void main(String[] args) {
		//Student 키로 총점을 저장하는 HashMap 객체 생성
		HashMap<Student, String> hashMap = new HashMap<Student, String> ();
		
		//new Student("1")의 점수 95를 저장
		hashMap.put(new Student("1"), "95");
		
		//new Student("1")의 점수를 읽어옴
		String score = hashMap.get(new Student("1"));
		System.out.println("1번 학생의 총점: " + score);
	}
}
1번 학생의 총점: 95

 

4. Member 클래스를 작성하되, Object의 toString() 메소드를 오버라이딩해서 MemberExample 클래스의 실행 결과처럼 나오도록 작성해보세요.

package exercise.Exercise04;

public class Member {
	private String id;
	private String name;
	
	public Member(String id, String name) {
		this.id = id;
		this.name = name;
	}
	
	@Override
	public String toString() {
		return id + ": " + name;
	}
}
package exercise.Exercise04;

public class MemberExample {
	public static void main(String[] args) {
		Member member = new Member("blue", "이파란");
		System.out.println(member);
	}
}
blue: 이파란

 

5. Class 객체에 대한 설명 중 틀린 것은 무엇입니까? (4)

① Class.forName() 메소드 또는 객체의 getClass() 메소드로 얻을 수 있다.

② 클래스의 생성자, 필드, 메소드에 대한 정보를 알아낼 수 있다. -> 리플렉션

③ newInstance() 메소드는 기본 생성자를 이용해서 객체를 생성시킨다.

newInstance() 의 리턴 타입은 생성된 객체의 클래스 타입이다. -> newInstance() 를 이용하면 Object 타입의 객체를 얻을 수 있다.

 

6. 다음에 주어진 바이트 배열을 문자열로 변환시켜보세요.

package exercise.Exercise06;

public class BytesToStringExample {
	public static void main(String[] args) {
		byte[] bytes = { 73, 32, 108, 111, 118, 101, 32, 121, 111, 117 };
		String str = new String(bytes);
		System.out.println(str);
	}
}
I love you

 

7. 다음 문자열에서 "자바" 문자열이 포함되어 있는지 확인하고, "자바"를 "Java"로 대치한 새로운 문자열을 만들어보세요.

package exercise.Exercise07;

public class FindAndReplaceExample {
	public static void main(String[] args) {
		String str = "모든 프로그램은 자바 언어로 개발될 수 있다.";
		int index = str.indexOf("자바");
		if(index == -1) {
			System.out.println("자바 문자열이 포함되어 있지 않습니다.");
		} else {
			System.out.println("자바 문자열이 포함되어 있습니다.");
			str = str.replace("자바", "Java");
			System.out.println("->" + str);
		}
	}
}
자바 문자열이 포함되어 있습니다.
->모든 프로그램은 Java 언어로 개발될 수 있다.

 

8. 다음 문자열에서 쉼표(,)로 구분되어 있는 문자열을 String split() 메소드 또는 StringTokenizer를 이용해서 분리해보세요.

package exercise.Exercise08;

import java.util.StringTokenizer;

public class SplitExample {
	public static void main(String[] args) {
		String str = "아이디,이름,패스워드";
		
		//방법1 (split() 메소드 이용)
		String[] info = str.split(",");
		for(String infos : info) {
			System.out.println(infos);
		}
		System.out.println();
		
		//방법2 (StringTokenizer 이용)
		StringTokenizer st = new StringTokenizer(str, ",");
		while(st.hasMoreTokens()) {
			String token = st.nextToken();
			System.out.println(token);			
		}		
	}
}
아이디
이름
패스워드

아이디
이름
패스워드

 

9. 다음 코드는 1부터 100까지의 숫자를 통 문자열로 만들기 위해서 += 연산자를 이용해서 100번 반복하고 있습니다. 이것은 곧 100개 이상의 String 객체를 생성하는 결과를 만들기 때문에 좋은 코드라고 볼 수 없습니다. StringBuilder를 사용해서 좀 더 효율적인 코드로 개선시켜 보세요.

package exercise.Exercise09;

public class StringBuilderExample {
	public static void main(String[] args) {
		String str = "";
		for(int i=1; i<=100; i++) {
			str += i;
		}
		System.out.println(str);
		
		//StringBuilder 이용
		StringBuilder sb = new StringBuilder();
		for(int i=1; i<=100; i++) {
			sb.append(i);
		}
		System.out.println(sb.toString());
	}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100

 

10. 첫 번째는 알파벳으로 시작하고 두 번째부터 숫자와 알파벳으로 구성된 8자~12자 사이의 ID값 인지 검사하고 싶습니다. 알파벳은 대소문자 모두 허용할 경우에 정규 표현식을 이용해서 검증하는 코드를 작성해보세요.

package exercise.Exercise10;

import java.util.regex.Pattern;

public class PatternMatcherExample {
	public static void main(String[] args) {
		String id = "5Angel1004";
		String regExp = "[a-zA-Z]\\w{7,11}";
		boolean isMatch = Pattern.matches(regExp, id);
		if(isMatch) {
			System.out.println("ID로 사용할 수 있습니다.");
		} else {
			System.out.println("ID로 사용할 수 없습니다.");
		}
	}
}
ID로 사용할 수 없습니다.

 

11. 숫자 100과 300으로 각각 박싱된 Integer 객체를 == 연산자로 비교했습니다. 100을 박싱한 Integer 객체는 true가 나오는데, 300을 박싱한 Integer 객체는 false가 나오는 이유를 설명해보세요.

package exercise.Exercise11;

public class IntegerCompareExample {
	public static void main(String[] args) {
		Integer obj1 = 100;
		Integer obj2 = 100;
		Integer obj3 = 300;
		Integer obj4 = 300;		
		
		System.out.println(obj1 == obj2); //true
		
		System.out.println(obj3 == obj4); //false -> == 연사자로의 비교는 범위가 -128~127 일때만 가능하다. 그 외에는 equals() 사용해야 한다.
		System.out.println(obj3.equals(obj4)); //true
	}
}
true
false
true

 

12. 문자열 "200"을 정수로 변환하는 코드와 숫자 150을 문자열로 변환하는 코드를 작성해보세요.

package exercise.Exercise12;

public class StringConvertExample {
	public static void main(String[] args) {
		String strData1 = "200";
		int intData1 = Integer.parseInt(strData1);
		System.out.println(strData1);
		
		int intData2 = 150;
		String strData2 = String.valueOf(intData2);
		System.out.println(strData2);
	}
}
200
150

 

13. SimpleDateFormat 클래스를 이용해서 오늘의 날짜를 다음과 같이 출력하는 코드를 작성해보세요.

package exercise.Exercise13;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DatePrintExample {
	public static void main(String[] args) {
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy년 MM월 dd일 EE요일 hh시 mm분");
		System.out.println(sdf.format(now));
	}
}
2021년 11월 09일 화요일 06시 37분

+ Recent posts