SCJP 기출문제 및 해답(3)




     



1. Analyze this line of code:
     if (5 & 7 > 0 && 5 | 2) System.out.println("true");
  Select the one right answer. 
  a. this line of code will not compile
  b. this code will compile but nothing will appear in the standard output
  c. this code will compile and write the word "true" in the standard output

2. Create a List object that allows multiple selections and displays 5 rows at a time.
     Start by writing:
     List l =
   in your answer.
   Fill in the blank.

3. What will the user interface look like in an applet given the following init() method?
     public void init() {
          setLayout(new BorderLayout());
          add("East", new Button("hello"));
     }
  Select the one right answer.　
  a. Nothing will appear in the applet
  b. A button will appear in the applet set in the exact center
  c. A button will appear on the left side of the applet
  d. A button will appear on the right side of the applet
  e. A button will fill the entire applet

4. Choose all true statements about the paint() method defined in the Component class:
   Select all valid answers.
  a. it is protected
  b. it takes an instance of class Graphics
  c. it is static
  d. it is invoked automatically whenever you minimize and then maximize a component, such as a window
  e. there is also a version that takes an int

5. Given this ActionListener:
     class HandleClick implements ActionListener {
          public void actionPerformed(ActionEvent evt) {
          // A
          }
     }
  What line of code can you write at A that will make a component referenced by c disappear from the display?
  Fill in the blank.

6. Analyze the following code:
     class WhatHappens implements Runnable {
          public static void main(String[] args) {
               Thread t = new Thread(this);
               t.start();
          }
          public void run() {
              System.out.println("hi");
          }
     }
  Select the one right answer.
  a. This program does not compile
  b. This program compiles but nothing appears in the standard output
  c. This program compiles and the word "hi" appears in the standard output, once
  d. This program compiles and the word "hi" appears continuously in the standard output until the user hits 
      control-c to stop the program

7. What is wrong with the following code?
     final class First {
          private int a = 1;
          int b = 2;
     }
     class Second extends First {
          public void method() {
               System.out.println(a + b);
          }
     }
  Select all valid answers.
  a. You cannot invoke println() without passing it a String
  b. Since a is private, no classes other than First can access it
  c. Second cannot extend First
  d. final is not a valid keyword for a class

8. Analyze the following two classes.
     class First {
          static int a = 3;
     }
     final class Second extends First {
          void method() {
               System.out.println(a);
          }
     }
  Select the one right answer.
  a. Class First compiles, but class Second does not
  b. Class Second compiles, but class First does not
  c. Neither class compiles
  d. Both classes compile, and if method() is invoked, it writes 3 to the standard output
  e. Both classes compile, but if method() is invoked, it throws an exception

9. Why won't the following class compile?
     class A {
          private int x;
          public static void main(String[] args) {
               new B();
          }
          class B {
               B() {
                    System.out.println(x);
               }
          }
     }
  Select the one right answer.　
  a. Class B tries to access a private variable defined in its ouer class.
  b. Class A attempts to create an instance of B when there is no current instance of class A.
  c. Class B constructor must be public.

10. Analyze the following code.
     void looper() {
          int x = 0;
          one:
          while (x < 10) {
               two:
               System.out.println(++x);
               if (x > 3)
                    break two;
          }
     }
  Select all valid answers.　
  a. This code compiles
  b. This code does not compile
  c. This method writes the number 0 to the standard output
  d. the numbers 1 and 2 to the standard output
  e. the number 3 to the standard output
  f. the number 4 to the standard output
  g. the numbers 5 through 9 to the standard output
  h. the number 10 to the standard output

11. What appears in the standard output when the method named testing is invoked?
     void testing() {
          one:
          two:
          for (int i = 0; i < 3; i++) {
               three:
               for (int j = 10; j < 30; j+=10) {
                    System.out.println(i + j);
                         if (i > 2)
                              continue one;
               }
          }
     }
  Select all valid answers.
  a. 10 and 20
  b. 11 and 21
  c. 12 and 22
  d. 13 and 23
  e. 30, 31, 32, 33

12. What will the result be for the following block of code when it is executed?
     int i = 3;
     int j = 0;
     float k = 3.2F;
     long m = -3;
     if (Math.ceil(i) < Math.floor(k)){
          if (Math.abs(i) == m){
               System.out.println(i);
          }
          else{
               System.out.println(j);
          }
     }
     else{
          System.out.println(Math.abs(m) + 1);
     }
  Select the one right answer. 
  a. 3 
  b. 0 
  c. -3 
  d. 4 
  e. none of these

13. The ISO code for the language you are interested in is 8859-5. Assume you have a stream in a variable named mystream that   뭩 associated with a file generated in this 8-bit character set. If the default conversion to Unicode in your environment is for the    encoding 8859-1, how can you create a new instance of InputStreamReader that will perform the conversion from ISO 8859-5 to    Unicode, automatically? (Start by writing the keyword new and do not add a semicolon at the end.)
   Fill in the blank.

14. What is written to the standard output as the result of executing the following statements?
     Boolean b1 = new Boolean(true);
     Boolean b2 = new Boolean(true);
     Object obj1 = (Object)b1;
     Object obj2 = (Object)b2;
     if (obj1 == obj2){
          if (obj1.equals(obj2)){
               System.out.println("a");
          }
          else{
               System.out.println("b");
          }
     }
     else{
          if (obj1.equals(obj2)){
               System.out.println("c");
          }
          else{
               System.out.println("d");
          }
     }
  Select the one right answer.
  a. a 
  b. b 
  c. c 
  d. d

15. What will the user interface look like in an applet given the following init() method?
     public void init() {
          setLayout(new BorderLayout());
          add(new Button("hello"));
     }
  Select the one right answer.
  a. Nothing will appear in the applet
  b. A button will appear in the applet set in the exact center
  c. A button will appear in the applet along the top and centered horizontally
  d. A button will appear in the top left corner
  e. A button will fill the entire applet

16. What expressions are true concerning the following lines of code?
     int[] arr = {1, 2, 3};
     for (int i=0; i < 2; i++)
         arr[i] = 0;
  Select all valid answers.　
  a. arr[0] == 0 
  b. arr[0] == 1 
  c. arr[1] == 1 
  d. arr[2] == 0 
  e. arr[3] == 0

17. What will happen if you try to compile and execute B뭩 main() method?
     class A {
          int i;
          A(int i) {
               this.i = i * 2;
          }
     }
     class B extends A {
          public static void main(String[] args) {
               B b = new B(2);
          }
          B(int i) {
               System.out.println(i);
          }
     }
  Select the one right answer.　
  a. The instance variable i is set to 4
  b. The instance variable i is set to 2
  c. The instance variable i is set to 0
  d. This code will not compile

18. Which best describes the user interface of an applet given the following init() method:
     public void init() {
          setLayout(new BorderLayout());
          add("North", new TextField(10));
          add("Center", new Button("help"));
     }
  Select all valid answers.
  a. The TextField object will be placed at the top of the applet and will be 10 columns wide
  b. The Button object will be centered in the applet and will be just large enough to contain the text "help"
  c. The Button object will be centered in the applet and will start at the left edge of the applet, fit just under the TextField object above it, and extend to the right and bottom edge of the applet
  d. The TextField object will be placed along the top of the applet and will stretch from the left edge to the right edge
  e. The placement of the Button object and the TextField object depends on the overall size of the applet.　

19. Which of the following statements about try, catch, and finally are true?
   Select all valid answers.
  a. A try block must always be followed by a catch block
  b. A try block can be followed either by a catch block or a finally block, or both
  c. A catch block must always be associated with a try block
  d. A finally can never stand on its own (that is, without being associated with try block)
  e. None of these are true



<해답>


1. a
  -. 여기서 연산자의 우선순위는 >, &, |, && 이므로 if((5&(7>0))&&(5|2)) 가 된다. if 문은 true나 false를 리턴해야 하는데 (5&true) 와 논리 연산자 &&에 대한 (5|2) 때문에 Compile이 안된다.

2. List l = new List(5, true);
　-. List(m,n)에서 m : 표시될 줄 수  n : Multiple selection 여부 true/false

3. d

4. b and d
  -. Paint() 메소드의 프로토 타입은 public void paint(Graphic g);로써 Graphic 클래스의 instance를 인수로 취하고 컴포넌트가 작아지고 커짐에 따라 자동적으로 invoke 된다.

5. c.setVisible(false);
  -. setVisible() 메소드는 화면 표시 여부를 설정하는 메소드입니다.

6. a
  -. Tread의 생성자에 전달되는 static main()메소드내의 this 변수가 없기 때문에 컴파일되지 않습니다.

7. b and c.
  -. a가 private이기 때문에 First가 아닌 다른 클래스에서 그것을 access할 수 없고 Final First{}이기 때문에 second가 First를 상속할 수 없습니다.

8. d
  -. Second 클래스는 상위 클래스의 static 변수를 access할 수 있습니다. 따라서 아무런 문제가 없습니다.

9. b 
  -. 아직 생성되지 않는 클래스 B를 사용하려 했다. 클래스 B를 static으로 만듦으로써 문제를 해결할 수 있다.

10. b
  -. label two:가 loop 와 관련되어 있지 않으므로 이 code는 컴파일되지 않는다.

11. a, b, and c.
  -. (i,j) = (0.10), (0,20), (1,10), (1,20), (2,10), (2,20)이 된다. 

12. d
  -. Math.ceil(3)도 3이고 Math.floor(3.2)는 3이므로 첫 번째 if()문은 false를 리턴한다. 따라서 마지막 else 구문에서 Math.abs(-3)이 3 이 되므로     3+1=4가 된다.

13. new InputStreamReader(mystream, "8859-5");
　-. InputStreamReader 오브젝트 생성시 사용할 문자 코드를 두번째 인수로 지정할 수 있습니다.

14. c
  -. 첫 if 문은 obj1와 obj2는 new 키워드를 사용했으므로 당연히 주소값이 다르다.  또한 Boolean object를 Object type으로 castiong 했을지 라도 Method는 여전히 Boolean object type의 메소드를 사용한다. 따라서 주소값을 비교하는 OBJECT의 equals()메소드를 사용하지 않고 값을 비교하는 Boolean object type의 equals() 메소드를 사용한다.

15. e
  -. BorderLayout 의 add() 메소드 
      c1.add(c2,n);   
      c1 : BorderLayout 이 적용된 Container 오브젝트  
      c2 : Container 위에 붙일 Component 오브젝트  
      n : 붙일 위치를 나타내는 정수
     이때 n을 생략할 경우 BorderLayout에서는 전체 applet에 위치하게 된다.

16. a
  -. arr[0]==0 , arr[1]==0, arr[2]==3

17. d
  -. 클래스 B의 생성자는 암묵적으로 인수를 가지 않은 클래스 A의 생성자를 호출하는데 그러한 생성자는 클래스 A에 정의되어 있지 않습니다. 따라서 이 code는 컴파일 되지 않습니다.

18. c and d.
  -. TextField
  -. Help 

19. b, c, and d.
  -. a는 try block다음에 finally block이 따른다면 반드시 catch block이 올 필요는 없다.
