View


1. 두 구문의 결과 값은? 

String x = "techbeamers";
String y = new String(new char[] { 't', 'e', 'c', 'h', 'b', 'e', 'a', 'm', 'e', 'r', 's' });
System.out.println(x == y);
System.out.println(x.equals(y));


1. x == y => false, x.equals(y) => true.
2. x == y => false, x.equals(y) => false.
3. x == y => true, x.equals(y) => true.
4. x == y => true, x.equals(y) => false. 5. None of these.

> String 오브젝트의 '==' 연산자와 equals 메소드의 차이점을 묻는 문제 compare 등의 기타 비교 메소드와도 같이 나올 수 있음.
.  '==' 연산은 같은 Object 인지 확인
. eqauls() 메소드는 String의 값이 같은지 확인 
. compare() 메소드는 String의 대소문자를 위해 사용되는 것으로 각 자리 문자의 차의 총 합을 int로 출력한다. 





2. 자바의 String과 StringBuffer의 차이점으로 올바르게 설명한 것은?

1. String is handled very efficiently in terms of memory and execution time by most jvm's while StringBuffer is not. 2. String is immutable, you can use StringBuffer whenever you need to modify a String. 3. String and StringBuffer are two different classes they are used in context of concatenating two Strings. 4. None of these.

> String과 StringBuffer 클래스의 차이점을 묻는 질문

String 

StringBuffer 

StringBuilder 

 문자열 내부 수정 불가(immutable, final)

문자열 내부 수정 가능(mutable) 

 문자열 내부 수정 가능(mutable) 

 문자열 연산 수행 성능 낮음

문자열 연산 수행 성능 좋음

 가장 좋음, StringBuffer 보다 성능 좋음 

 동기화에 자유로움, Thread-Safe

동기화 지원, 멀티 스레드 환경

동기화 지원 안함, 단일 스레드 환경 





3. toString 메소드에 대해서 제대로 기술하고 있는 것에대해 말해보라

1. The toString() method convert an object into string.
2. The toString() method returns the byte representation of String object.
3. None of these.
4. The toString() method returns the string representation of any object.

> toString에 대한 기능을 묻는 질문. 이는 object를 string으로 변환시키는 것이 아닌 object에 대한 설정된 string 타입의 데이터가 리턴됨



4. 자바의 String pool에 대해서 제대로 정의한 것은 ?

1. String pool is a dynamic memory area used to allocate String objects.
2. None of these.
3. When Java program creates a new String, JVM checks for that String in the String pool.
4. String pool is a special storage area in Java heap placed on to PermGen space, tostore String literals.

> String pool 에 대한 정의를 묻는 질문
(https://www.journaldev.com/797/what-is-java-string-pool)




5. "==" 연산과 equals 메소드의 동작의 차이점에 대해서 바르게 기술한 것은?

1. "==" is an operator while equals" is a method.
2. "==" tests if two objects refer to the same memory location while the equals() method verifies if two distinct objects can still be equal.
3. "==" tests whether two references point to the same object while the equals() method compares the object state.
4. None of these.



6. 아래 코드의 결과 값은?

String str1 = "How are you?";
String str2 = str1.substring(5, 7);
System.out.println(str2);

1. ar
2. None of these
3. re
4. are

> substring 메소드 매개변수의 의미에 대해서 확인 index 5부터 7 이전까지의 문자열을 리턴함..



7. String이 final 또는 변경불가능 한 이유를 잘 설명한 것은?

1. Strings are very frequently as HashMap key. So they have to be immutable for uniquely
referencing the value object stored in HashMap.

2. Caching of String objects was essential for performance reason, so the risk was averted by
making the String class Immutable.

3. String objects are cached in String pool and their cached version is shared between clients.
So there was a need to avoid accidental changes.

4. All of these. > String은 빈번하게 HashMap의 키로 사용된다. 그러므로 HashMap에 있는 object 값을 가르키기 위해 unique 하고 불변성을 지닌다. 
> String object 를 caching하는 것은 성능상의 이유로 매우 필수적인데, 자주 변경되면 cache 성능이 떨어지게 된다. 
> String pool에 String object를 caching 하고 캐쉬된 버전을 client 간 공유하는데, 이는 의도치않은 변경을 피할 수 있게 한다. 





8. 아래 문장은 오류가 없는가?

If you plan to modify a String, then better use StringBuffer. StringBuffer updates the existing objects
value, rather creating a new object.


1. true
2. None of these.
3. false

> String 클래스와 StringBuffer 클래스의 차이 (2번 참고)





9. 아래 문장은 오류가 없는가?

Immutable objects are naturally thread-safe.

1. false
2. true
3. None of these.

>Note: Because immutable objects can not be changed, they can be shared among multiple threads freely. This eliminate the requirements of doing synchronization. String 객체는 변경불가능 하기 때문에 다중스레드 환경에서 변경하고 이를 동기화 하는 작업이 필요 없게 된다.





10. intern 메소드에 대해서 제대로 설명한 것은?

1. intern() method allows to put an String object into pool.
2. It's just a canonical representation for the string object.
3. It return a string that has the same contents, but is guaranteed to be from a pool of unique strings.
4. None of these.


> String 의 문자열은 String pool 에 저장되며 일반적으로 같은 문자열을 가리키는 경우 같은 주소값을 갖게 된다. 
하지만 new String을 통해 String 객체를 새로 생성하여 할당하는 경우 다른 주소값을 갖게 되는데, 이때 intern 을 사용하게되면 
String pool에서 eqauls 의 결과와 동일한 객체가 존재하는 경우, 해당 객체를 가리키게 된다. 
(예)

String s1 = "abcd";
String s2 = "abcd"; 
System.out.println(System.identityHashCode(s1) == System.identityHashCode(s2)) ; // true출력

String s3 = new String("abcd");
String s4 = new String("abcd");
System.out.println(System.identityHashCode(s3) == System.identityHashCode(s4)) ;// false출력

String s5 = new String("abcd").intern();
String s6 = new String("abcd").intern();
System.out.println(System.identityHashCode(s5) == System.identityHashCode(s6)) ;// true 출력




11. 아래 코드의 결과 값은?

class ascii_to_String {
public static void main(String args[])
{
int ascii[] = { 65, 66, 67, 68};
String str = new String(ascii, 1, 3);
System.out.println(str);
}
}

1. ABC
2. BCD
3. ABCD
4. CDA

> String 생성자 참조 (https://docs.oracle.com/javase/7/docs/api/java/lang/String.html)





12. String의 getBytes()메소드와 toCharArray()의 설명으로 옳은 것은?


1. None of these.
2. Use String getBytes() method to convert String to byte array and String toCharArray() to
convert back to String.
3. Use String getBytes() method to convert String to byte array and String getChars() to convert
back to String.
4. Use String getBytes() method to convert String to byte array and String constructor new String(byte[] arr) to convert back to String.

> getBytes() 는 String 객체를 byte array 로 변환하는 것이며 toCharArray는 char 타입의 array를 전달하는 것이다. 둘 모두 String을 byte 처리하기 위해서 자주 사용하는데 단순히 성능적인 측면에서 보자면, getBytes는 특정 charset 에 따라서 encoding을 수행하는 것이며 toCharArray는 단순히 문자열을 이루고 있는 문자들에 대한 char 타입의 array를 설정하는 것임으로 toCharArray가 더 빠르다. 

> s1.getBytes() 와 같이 encoding 을 하는 경우 사용자 플랫폼 환경의 기본 인코딩 값으로 처리를 수행하며 s1.getBytes("utf-8") 과 같이 charset을 명시하는 경우 해당 charset 으로 인코딩한다. String 생성자를 사용하여 byte[]를 인코딩 하는 경우, 다음과 같이 인코딩된 byte[] 를 사용한다면 제대로 수행되지 않는다. 중첩해서 인코딩 할 수 없기 때문이다. 

System.out.println(new String(en2.getBytes("latin1"), "utf-8")); 
    // 에러 , latin1로 인코딩 된 값을 utf-8 형태의 byte 배열로 인식해 string으로 변환할 수 없다. 





13. 다음 Java 코드를 보고 옳은 것은?

String[] strArray = aString.split("\\s+");


1. It'll simply split the string.
2. It'll split the string with white space characters such as ” “, “\t”, “\r”, “\n”.
3 None of these
4. It'll split the string from all the occurrences of 's' character.

> 참조 : https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html





14. 다음 StringBuilder 클래스의 할당 크기는 얼마인가?


StringBuilder sb = new StringBuilder("top 30 Java String interview questions.");

1. 39
2. 40
3. 55

Initial capacity of the string builder is the length of the initial string + 16.




15. 두 String을 올바르게 합치는 방법으로 옳은 것은?

1. Str1.substring(Str2) and Str1 && Str2.
2. Str1.concat(Str2) and Str1 + Str2.
3. Str1.append(Str2) and Str1 + Str2.
4. None of these.


> append 라는 메소드는 없음.




16. StringBuffer 와 StringBuilder 클래스의 차이점으로 옳은 것은?

1. StringBuffer and StringBuilder class both produces mutable string objects.
2. StringBuffer class is thread safe whereas StringBuilder class is not thread safe.
3. StringBuffer produces mutable string objects while StringBuilder does not.
4. None of these.



17. 아래 코드에서 얼마나 많은 오브젝트가 생성되는가?


String str1 = new String("Tech"); String str2 = new String("Beamers");

1. Four objects will be created and they will be stored in the heap memory.
2. Two objects will be created and they will be stored in the stack.
3. Two objects will be created and they will be stored in the heap memory.
4. None of these.


> String pool 에 대해 묻고 싶은 문제였으나, 문자열이 다르기 때문에 큰 어려움 없이 고를 수 있다. heap memory 에 생성되는 것만 기억. 
만약 두 문자열이 같고 둘 모두 new String("abcd") 와 같이 생성하였다면 마찬가지로 2개의 객체가 생성될 것이고, 단순히 대입연산자(=)나 intern() 메소드를 사용하는 경우는 1개의 오브젝트가 생성될 것이다. 



18. 다음 코드의 결과는 무엇인가?

public class a {
    public static void main(String args[]){
    final String str1="social";
    final String str2="media";
    String str3=str1+str2;
    String str4="socialmedia";

    System.out.println(str3==str4); // Output

    }
}


1. false
2. true

>  final String의 특징 





19. 다음 중 어느 것이 Java의 Final class 인가?

1. StringBuilder.
2. String.
3. StringBuffer.
4. All of these.



20. 아래 코드의 결과는 무엇인가?

public class TestStrings {

    public static void main(String params[]){

        String StrArr[]={"JAVA","JDK","JRE"};
        String Str="JAVA";         System.out.println(StrArr[0]==Str);
    }
}


1. false
2. true
3. Compile time error
4. Runtime error

> String pool 에 대한 문제 




21. 다음 중 어떤 메소드가 두 문자열을 비교하여, 값이 다르면 0이 아닌 값을 반환 하는가?

1. compareTo(String str)
2. equals(String str)
3. equalsIgnoreCase(String str)
4. None of these

> compareTo : 문자열 크기 비교 / eqauls : 값이 동일한지 비교 / equalsIgnoreCase : 문자열 대소문자 구분없이 비교





22. 다음 구문의 결과 값은 무엇인가?

System.out.println(Integer.parseInt("10"));

1. true
2. false
3. integer
4. None of these.

> Note: The statement Integer.parseInt(“10”); would return the output as 10.









Share Link
reply
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31