별의 공부 블로그 🧑🏻‍💻
728x90
728x170

[1Z0-808][OCAJP] Dump 문제 11~20

 

문제 11

Given :

public static void main(String[] args) {
    String ta = "A ";
    ta = ta.concat("B ");   
    String tb = "C ";
    ta = ta.concat(tb);    
    ta.replace('C', 'D');   
    ta = ta.concat(tb);    
    System.out.println(ta);
}

 

Q. What is the result?
A A B C D
B A C D
C A B C C
D A B D
E A B D C

 

정답

C

 

해설/결과

"ta.replace('C', 'D')"

String 형 문자열을 반환만 할 뿐, 변수 ta에 새롭게 할당하지는 않음.

 

 

 

문제 12

Given the code fragment :

	// line 1

public static void main(String[] args) {	// line 3
    int x = 5;
    while (isAvailable(x)) {
        System.out.print(x);		// line 6
    	// line 7
    }	
}

public static boolean isAvailable(int x) {
    return x-- > 0 ? true : false;		// line 12
}

 

Q. Which modification enables the code to print 54321?
A Replace line 6 with System.out.print(--x);
B At line 7, insert x--;
C Replace line 6 with --x and, at line 7, insert System.out.print(x);
D Replace line 12 with return (x > 0) ? false : true;

 

정답

A

 

해설/결과

A : 43210

B : 컴파일 오류

C : 54321

D : (공백)

 

 

문제 13

Given the code fragment :

public static void main(String[] args) {
    boolean opt = true;		// line 5
    switch (opt) {
        case true:		// line 7
            System.out.print("True");
            break;
        default:
            System.out.print("***");
    }
    System.out.println("Done");
}

 

Q. Which modification enables the code fragment to print TrueDone?
A Replace line 5 With String opt = "true";
Replace line 7 with case "true":
B Replace line 5 with boolean opt = 1;
Replace line 7 with case 1=
C At line 9, remove the break statement.
D Remove the default section.

 

정답

A

 

해설/결과

  • 다음의 자료형만 switch 문의 매개변수로 사용될 수 있다.
    • char
    • byte
    • short
    • int
    • Character
    • Byte
    • Short
    • Integer
    • String
    • an enum
  • 따라서, 문제에서 나온 boolean 형 변수는 switch의 매개변수로 사용될 수 없다.

 

 

문제 14

Given the following main method:

public static void main(String[] args) {
    int num = 5;
    do {
        System.out.print(num-- + " ");
    } while (num == 0);
}

 

Q. What is the result?
A 5 4 3 2 1 0
B 5 4 3 2 1
C 4 2 1
D 5
E Nothing is printed

 

정답

D

 

해설/결과

  • "5 4 3 2 1" 이 출력되게 하려면 while (num != 0); 과 같이 코드를 작성해야 한다. (num이 0이 아닐 때까지 반복)

 

 

문제 15

Given the code fragment :

int x = 100;
int a = x++;
int b = ++x;
int c = x++;
int d = (a < b) ? (a < c) ? a : (b < c) ? b : c;

System.out.println(d);

 

Q. What is the result?
A 100
B 101
C 102
D 103
E Compilation fails

 

정답

E

 

해설/결과

int x = 100;
int a = x++;
int b = ++x;
int c = x++;
int d = (a < b) ? (a < c) ? a : (b < c) ? b : c;      // ':' expected

System.out.println(d);

 

  • 문제에는 3개삼항 연산자(?)가 있으나, 오직 2개콜론(:)만 제시되어 있다.
    • 삼항 연산자를 사용할 경우, ? 연산자와 : 연산자의 개수를 일치시켜줘야 한다.

 

 

문제 16

Given :

public class Test {
    public static void main(String[] args) {
        String[][] chs = new String[2][];
        chs[0] = new String[2];
        chs[1] = new String[5];
        int i = 97;

        for (int a = 0; a < chs.length; a++) {
            for (int b = 0; b < chs.length; b++) {
                chs[a][b] = "" + i;
                i++;
            }
        }

        for (String[] ca : chs) {
            for (String c : ca) {
                System.out.print(c + " ");
            }
            System.out.println();
        }
    }
}

 

Q. What is the result?
A 97 98 
99 100 null null null 
B 91 98 
99 100 101 102 103 
C Compilation fails.
D A NullPointerException is thrown at runtime.
E An ArrayIndexOutOfBoundsException is thrown at runtime.

 

정답

A

 

해설/결과

  • 첫 번째 루프에서 배열에는 다음의 요소가 담기게 된다.
chs[0][0] = 97
chs[0][1] = 98
chs[1][0] = 99
chs[1][1] = 100
chs[1][2] = null
chs[1][3] = null
chs[1][4] = null
  • 두 번째 루프에서 배열에 담긴 요소들이 출력된다.

 

 

문제 17

Given the code fragment :

public class Employee {
    String name;
    boolean contract;
    double salary;

    Employee() {
        // line n1
    }

    public String toString() {
        return name + ":" + contract + ":" + salary;
    }

    public static void main(String[] args) {
        Employee e = new Employee();
        // line n2
        System.out.print(e);
    }
}

 

Q. Which two modifications, when made independently, ebable the code to print joe:true: 100.0?
A Replace line n2 with:
e.name = "Joe";
e.contract = true;
e.salary = 100;
B Replace line n2 with:
this.name = "Joe";
this.contract = true;
this.salary = 100;
C Replace line n1 with:
this.name = new String("Joe");
this.contract = new Boolean(true);
this.salary = new Double(100);
D Replace line n1 with:
name = "Joe";
contract = TRUE;
salary = 100.0f;
E Replace line n1 with:
this("Joe", true, 100);

 

정답

A, C

 

해설/결과

 

 

문제 18

View the exhibit :

public class Student {
    public String name = "";
    public int age = 0;
    public String major = "Undeclared";
    public boolean fulltime = true;

    public void display() {
        System.out.println("Name: " + name + " Major: " + major);
    }
    
    public boolean isFullTime() {
        return fulltime;
    }
}

 

Q. Which line of code initializes a student instance?
A Student student1;
B Student student1 = Student.new();
C Student student1 = new Student();
D Student student1 = Student();

 

정답

C

 

해설/결과

  • 자바에서 클래스의 인스턴스를 생성하려면, 다음과 같이 하면 된다.
    • 클래스이름 인스턴스명 = new 클래스이름();

 

 

문제 19

Given :

int [] array = {1, 2, 3, 4, 5};
for (int i : array) {
    if (i < 2) {
        // keyword1 ;
    }
    System.out.println(i);

    if (i == 3) {
        // keyword2 ;
    }
}

 

Q. What should keyword1 and keyword2 be respectively, in order to produce output 2345?
A continue, break
B break, break
C break, continue
D continue, continue

 

정답

D

 

해설/결과

  • A (continue / break) : 23
  • B (break / break) : (공백)
  • C (break / continue) : (공백)

 

 

문제 20

Given :

int i, j = 0;
i = (3 * 2 + 4 + 5);
j = (3 * ((2 + 4) + 5));
System.out.println("i:" + i + "\nj:" + j);

 

Q. What is the result?
A i:16
j:33
B i:15
j:33
C i:33
j:23
D i:15
j:23

 

정답

B

 

해설/결과

연산자 우선순위에 따라 괄호로 둘러쌓인 식이 먼저 계산된다.

728x90
그리드형(광고전용)

'Certificate > OCAJP' 카테고리의 다른 글

[1Z0-808][OCAJP] Dump 문제 51~60  (0) 2022.01.07
[1Z0-808][OCAJP] Dump 문제 41~50  (0) 2022.01.07
[1Z0-808][OCAJP] Dump 문제 31~40  (0) 2022.01.04
[1Z0-808][OCAJP] Dump 문제 21~30  (0) 2022.01.02
[1Z0-808][OCAJP] Dump 문제 1~10  (2) 2021.12.31
[1Z0-851][OCAJP] Dump 문제 1~10  (0) 2021.12.30
[1Z0-851E] Question 21~30  (0) 2017.08.15
[1Z0-851E] Question 11~20  (0) 2017.08.15
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️
starrykss
starrykss
별의 공부 블로그 🧑🏻‍💻


📖 Contents 📖