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

[1Z0-808][OCAJP] Dump 문제 21~30

 

문제 21

Given the code fragment :

public static void main(String[] args) {
    String[][] arr = {{"A", "B", "C"}, {"D", "E"}};
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            System.out.print(arr[i][j] + " ");
            if (arr[i][j].equals("B")) {
                break;
            }
        }
        continue;
    }
}

 

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

 

정답

B

 

해설/결과

 

 

 

 

문제 22

Given the content of three files :

// (1) A.java :
public class A {
    public void a() {}
    int a;
}

// (2) B.java :
public class B {
    private int doStuff() {
        private int x = 100;
        return x++;
    }
}

// (3) C.java :
import java.io.*;
package p1;
class C {
    public void main(String fileName) throws IOException { }
}

 

Q. Which statement is true?
A Only the A.java file compiles successfully.
B Only the C.java file compiles successfully.
C The A.java and C.java files compile successfully.
D The A.java and B.java files compile successfully.
E The B.java and C.java files compile successfully.

 

정답

A

 

해설/결과

  • B.java : private int x = 100; → 'private' 또는 'protected' 형식의 변수는 블록 내부에 선언할 수 없다. 
    • Modifier 'private' not allowed here.
  • C.java : package 선언 은 반드시 import 문 에 해야 한다.
    • Package
    • Imports
    • Class

 

 

문제 23

Given the code fragment :

public static void main(String[] args) {
    int iVar = 100;
    float fVar = 100.100f;
    double dVar = 123;
    iVar = fVar;	// line 7
    fVar = iVar;	// line 8
    dVar = fVar;	// line 9
    fVar = dVar;	// line 10
    dVar = iVar;	// line 11
    iVar = dVar;	// line 12
}

 

Q. Which three lines fail to compile?
A Line 8
B Line 10
C Line 12
D Line 7
E Line 9
F Line 11

 

정답

B, C, D

 

해설/결과

  • B : int형 변수에 float형 변수를 할당할 수 없다.
  • C : float형 변수에 double형 변수를 할당할 수 없다.
  • D : int형 변수에 double형 변수를 할당할 수 없다.

하지만, 다음의 경우에는 변수를 할당할 수 있다.

  • float형 변수 ← int형 변수
  • double형 변수 ← float형 변수
  • double형 변수 ← int형 변수 

 

 

문제 24

Given the code fragments :

// (1) Person.java :
public class Person {
    String name;
    int age;
    
    public Person(String n, int a) {
        name = n;
        age = a;
    }
    
    public String getName() {
        return name;
    }
    
    public int getAge() {
        return age;
    }
}

// (2) Test.java :
public static void checkAge(List<Person> list, Predicate<Person> predicate) {
    for (Person p : list) {
        if (predicate.test(p)) {
            System.out.println(p.name + " ");
        }
    }
}

public static void main(String[] args) {
    List<Person> iList = Arrays.asList(new Person("Hank", 45), 
                                       new Person("Charlie", 40), 
                                       new Person("Smith", 38));
    // line n1
}

 

Q. Which code fragment, when inserted at line n1, enables the code to print Hank?
A checkAge(iList, ()->p.getAge() > 40);
B checkAge(iList, Person p->p.getAge() > 40);
C checkAge(iList, (Person p)->{ p.getAge() > 40; });
D checkAge(iList, p->p.getAge() > 40);

 

정답

D

 

해설/결과

  • A : 컴파일 오류(Cannot resolve symbol 'p')
  • B : 컴파일 오류 (',' or ')' expected)
  • C : 컴파일 오류 (Not a statement)

 

 

문제 25

Given :

package Package;

public class Alpha {
    int ns;
    static int s;
    Alpha(int ns) {
        if (s < ns) {
            s = ns;
            this.ns = ns;
        }
    }
    void doPrint() {
        System.out.println("ns = " + ns + " s = " + s);
    }
}

 

And,

public class TestA {
    public static void main(String[] args) {
        Alpha ref1 = new Alpha(50);
        Alpha ref2 = new Alpha(125);
        Alpha ref3 = new Alpha(100);

        ref1.doPrint();
        ref2.doPrint();
        ref3.doPrint();
    }
}

 

Q. What is the result?
A ns = 50 s = 125
ns = 125 s = 125
ns = 100 s = 125
B ns = 50 s = 125
ns = 125 s = 125
ns = 0 s = 125
C ns = 50 s = 50
ns = 125 s = 125
ns = 100 s = 100
D ns = 50 s = 125
ns = 125 s = 125
ns = 0 s = 125

 

정답

D

 

해설/결과

 

 

 

문제 26

Given the code fragment :

public static void main(String[] args) {
    double discount = 0;
    int qty = Integer.parseInt(args[0]);
    // line n1
}

 

And given the requirements :

If the value of the qty variable is greather than or equal to 90, discount = 0.5

If the value of the qty variable is between 80 and 90, discount = 0.2

 

Q. Which two code fragments can be independently placed at line n1 to meet the requirements?
A if (qty >= 90) { discount = 0.5; }
if (qty > 80 && qty < 90) { discount = 0.2; }
B discount = (qty >= 90) ? 0.5 : 0;
discount = (qty > 80) ? 0.2 : 0;
C discount = (qty >= 90) ? 0.5 : (qty > 80) ? 0.2 : 0;
D if (qty > 80 && qty < 90) {
    discount = 0.2;
} else {
    discount = 0;
}
if (qty >= 90) {
    discount = 0.5;
} else {
    discount = 0;
}

 

정답

A, C

 

해설/결과

 

 

문제 27

Given the code fragment :

public class X {
    public void printFileContent() {	// line 2
        /* code goes here */
        throw new IOException();	// line 4
    }
}

public class Test {
    public static void main(String[] args) {	// line 8
        X xobj = new X();
        xobj.printFileContent();	// line 10
    }	// line 11
}

 

Q. Which two modifications should you make so that the code compiles successfully?
A Replace line 8 with public static void main(String[] args) throws Exception {
B Replace line 10 with :
try {
    xobj.printFileContent();
}
catch(Exception e) { }
catch(IOException e) { }
C Replace line 2 with public void printFileContent() throws IOException {
D Replace line 4 with throw IOException("Exception raised");
E At line 11, insert throw new IOException();

 

정답

A, C

 

해설/결과

 

 

문제 28

Given : 

package p1;
public class Acc {
    int p;
    private int q;
    protected int r;
    public int s;
}

// Test.java : 
package p2;
import p1.Acc;
public class Test extends Acc {
    public static void main(String[] args) {
        Acc obj = new Test();
    }
}

 

Q. Which statement is true?
A Only s is accessible by obj.
B Both p and s are accessible by obj.
C p, r and s are accessible by obj.
D Both r and s are accessible by obj.

 

정답

A

 

해설/결과

  • 상속(Extension)
    • 부모 클래스와 자식 클래스가 서로 다른 패키지에 있을 때,
      • 자식 클래스는 부모 클래스에서 오직 public 공용자를 사용하여 선언된 변수에만 접근할 수 있다.

 

 

문제 29

Given the following two classes :

public class Customer {
    ElectricAccount acct = new ElectricAccount();
    
    public void useElectricity(double kWh) {
        acct.addKWh(kWh);
    }
}

public class ElectricAccount {
    private double kWh;
    private double rate = 0.07;
    private double bill;
    
    // line n1
}

 

Q. How should you write methods in the ElectricAccount class at line n1 so that the member variable bill is always equal to the value of the member variable kwh multiplied by the member variable rate? 
Any amount of electricity used by a customer (represented by an instance of the customer class) must contribute to the customer's bill (represented by the member variable bill) through the method useElectricity method. An instance of the customer class should never be able to tamper with or decrease the value of the member variable bill.
A public void addKwh(double kWh) {
    this.kWh += kWh;
    this.bill = this.kWh * this.rate;
}
B public void addKwh(double kWh) {
    if (kWh > 0) {
        this.kWh += kWh;
        this.bill = this.kWh * this.rate;
}
C private void addKwh(double kWh) {
    if (kWh > 0) {
        this.kWh += kWh;
        this.bill = this.kWh * this.rate;
}
D public void addKwh(double kWh) {
    if (kWh > 0) {
        this.kWh += kWh;
        setBill(this.kWh);
}
public void setBill(double kWh) {
    bill = kWh * rate;
}

 

정답

B

 

해설/결과

  • A : 음수를 허용하므로 오답.
  • C : 메서드가 private로 선언되어 있어, 객체 인스턴스로부터 접근되지 못하므로 오답. 
  • D : 요구조건(not being able to tamper with the bill)을 침해하므로 오답.

 

 

문제 30

Q. You are asked to develop a program for a shopping application, and you are given the following information :

The application must contain the classes Toy, EduToy, and ConsToy. The Toy class is the superclass of the other two classes.
The int calculatePrice(Toy t) method calculates the price of a toy.
The void printToy(Toy t) method prints the details of a toy.

Which definition of the Toy class adds a valid layer of abstraction to the class hierarchy?
A public abstract class Toy {
    public abstract int calculatePrice(Toy t);
    public void printToy(Toy t) { /* code goes here */ }
}
B public abstract class Toy {
    public int calculatePrice(Toy t);
    public void printToy(Toy t);
}
C public abstract class Toy {
    public int calculatePrice(Toy t);
    public final void printToy(Toy t) { /* code goes here */ }
}
D public abstract class Toy {
    public abstract int calculatePrice(Toy t) { /* code goes here */ }
    public abstract void printToy(Toy t) { /* code goes here */ }
}

 

정답

A

 

해설/결과

  • 추상 메서드를 제외한 메서드반드시 몸체(bodies)를 갖고 있어야 하므로, BC는 오답. (Non-abstract methods must have bodies.)
  • 추상 메서드는 몸체(bodies)를 갖고 있으면 안되므로, D는 오답. (Abstract methods must have no body.)
728x90
그리드형(광고전용)

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

[1Z0-808][OCAJP] Dump 문제 61~70  (0) 2022.01.09
[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 문제 11~20  (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
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️


📖 Contents 📖