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

[1Z0-808][OCAJP] Dump 문제 51~60

 

문제 51

Given  : 

public class Triangle {
    static double area;
    int b = 2, h = 3;

    public static void main(String[] args) {
        double p, b, h;     // line n1
        if (area == 0) {
            b = 3;
            h = 4;
            p = 0.5;
        }
        area = p * b * h;   // line n2
        System.out.println("Area is " + area);
    }
}

 

Q. What is the result?
A Area is 3.0
B Compilation fails at line n2
C Compilation fails at line n1
D Area is 6.0

 

정답

B

 

해설/결과

area = p * b * h;     // Variable 'p' / 'b' / 'h' might not have been initialized

 

 

문제 52

Given :

class Vehicle {
    String type = "4W";
    int maxSpeed = 100;
    
    Vehicle(String type, int maxSpeed) {
        this.type = type;
        this.maxSpeed = maxSpeed;
    }
}

class Car extends Vehicle {
    String trans;
    
    Car(String trans) {     // line n1
        this.trans = trans;
    }
    
    Car(String type, int maxSpeed, String trans) {
        super(type, maxSpeed);
        this(trans);        // line n2
    }
}

 

And given the code fragment :

Car c1 = new Car("Auto");
Car c2 = new Car("4W", 150, "Manual");
System.out.println(c1.type + " " + c1.maxSpeed + " " + c1.trans);
System.out.println(c2.type + " " + c2.maxSpeed + " " + c2.trans);

 

Q. What is the result?
A Null 0 Auto
4W 150 Manual
B Compilation fails at both line n1 and line n2
C Compilation fails only at line n1
D 4W 100 Auto
4W 150 Manual
E Compilation fails only at line n2

 

정답

B

 

해설/결과

Car(String trans) {      // There is no default constructor available in 'Package.Question52.Vehicle'
this(trans);    // Call to 'this()' must be first statement in constructor body
  • line n1과 line n2에서 모두 컴파일 오류가 발생한다.

 

 

문제 53

Given the code fragment :

class Product {
    double price;
}

public class Test {
    public void updatePrice(Product product, double price) {
        price = price * 2;
        product.price = product.price + price;
    }

    public static void main(String[] args) {
        Product prt = new Product();
        prt.price = 200;
        double newPrice = 100;

        Test t = new Test();
        t.updatePrice(prt, newPrice);
        System.out.println(prt.price + " : " + newPrice);
    }
}

 

Q. What is the result?
A 400.0 : 200.0
B 400.0 : 100.0
C 200.0 : 100.0
D Compilation fails.

 

정답

B

 

해설/결과

 

 

문제 54

Given the code fragment :

public static void main(String[] args) {
    // line n1
    switch (x) {
        case 1:
            System.out.println("One");
            break;
        case 2:
            System.out.println("Two");
            break;
    }
}

 

Q. Which three code fragments can be independently inserted at line n1 to enable the code to print one?
A Byte x = 1;
B short x = 1;
C String x = "1";
D Long x = 1;
E Double x = 1;
F Integer x = new Integer("1");

 

정답

A, B, F

 

해설/결과

  • Switch 문의 인자로 올 수 있는 자료형
    • byte
    • short
    • char
    • int
    • enum
    • String 클래스
    • 특정한 원시형(Primitive Types)을 포함하는 클래스
      • char
      • byte
      • short
      • int
  • Switch 문의 인자로 올 수 없는 자료형
    • float
    • long
    • double
    • boolean

 

 

문제 55

Q. What is the name of the Java concept that uses access modifiers to protect variables and hide them within a class?
A Encapsulation
B Inheritance
C Abstraction
D Instantiation
E Polymorphism

 

정답

A

 

해설/결과

  • 캡슐화(Encapsulation) : 클래스 내에서 변수를 보호하고 숨기기 위해 접근 제어자(Access Modifiers)를 사용하는 것

 

 

문제 56

Given the code fragment : 

class A {
    public A() {
        System.out.print("A ");
    }
}

class B extends A {
    public B() {        // line n1
        System.out.print("B ");
    }
}

class C extends B {
    public C() {        // line n2
        System.out.print("C ");
    }

    public static void main(String[] args) {
        C c = new C();
    }
}

 

Q. What is the result?
A
B Compilation fails at line n1 and line n2
C A B C 
D C B A 

 

정답

C

 

해설/결과

 

 

문제 57

Given the code fragment :

public static void main(String[] args) {
    StringBuilder sb = new StringBuilder(5);
    String s = "";

    if (sb.equals(s)) {
        System.out.println("Match 1");
    } else if (sb.toString().equals(s.toString())) {
        System.out.println("Match 2");
    } else {
        System.out.println("No Match");
    }
}

 

Q. What is the result?
A Match 2
B A NullPointerException is thrown at runtime.
C Match 1
D No Match

 

정답

A

 

해설/결과

  • sb.tostring()새로운 String 객체를 생성한다. 따라서 변수 s의 값("")과 같게 된다.

 

 

문제 58

Given the following code for a Planet object : 

public class Planet {
    public String name;
    public int moons;
    
    public Planet(String name, int moons) {
        this.name = name;
        this.moons = moons;
    }
}

 

And the following main method :

public static void main(String[] args) {
    Planet[] planets = {
            new Planet("Mercury", 0),
            new Planet("Venus", 0),
            new Planet("Earth", 1),
            new Planet("Mars", 2)
    };

    System.out.println(planets);
    System.out.println(planets[2]);
    System.out.println(planets[2].moons);
}

 

Q. What is the result?
A planets
Earth
1
B [LPlanets.Planet;@15db9742
Earth
1
C [LPlanets.Planet;@15db9742
Planets.Planet@6d06d69c
1
D [LPlanets.Planet;@15db9742
Planets.Planet@6d06d69c
[LPlanets.Moon;@7852e922
E [LPlanets.Planet;@15db9742
Venus
1

 

정답

C

 

해설/결과

 

 

문제 59

Given the code fragment :

public class Test {
    public static void main(String[] args) {
        /* insert code here */	// line 3
        array[0] = 10;
        array[1] = 20;
        System.out.print(array[0] + ":" + array[1]);
    }
}

 

Q. Which code fragment, when inserted at line 3, enables the code to print 10:20?
A int array = new int[2];
B int[] array;
array = int[2];
C int[] array = new int[2];
D int array [2];

 

정답

C

 

해설/결과

 

 

문제 60

Given the code fragment :

class CCMask {
    public static String maskCC(String creditCard) {
        String x = "XXXX-XXXX-XXXX-";
        // line n1
    }

    public static void main(String[] args) {
        System.out.println(maskCC("1234-5678-9101-1121"));
    }
}

 

Q. You are developing a banking module. You have developed a class named ccMask that has a maskcc method.
You must ensure that the maskcc method returns a string that hides all digits of the credit card number except for four last digits. (and the hyphens that separate each group of four digits) 
Which two code fragments should you use at line n1, independently, to achieve this requirement?
A StringBuilder sb = new StringBuilder(creditCard);
sb.substring(15, 19);
return x + sb;
B return x + creditCard.substring(15, 19);
C StringBuilder sb = new StringBuilder(x);
sb.append(creditCard, 15, 19);
return sb.toString();
D StringBuilder sb = new StringBuilder(creditCard);
StringBuilder s = sb.insert(0, x);
return x.toString();

 

정답

B, C

 

해설/결과

  • A : XXXX-XXXX-XXXX-1234-5678-9101-1121
  • B : XXXX-XXXX-XXXX-1121
  • C : XXXX-XXXX-XXXX-1121
  • D : XXXX-XXXX-XXXX-
728x90
그리드형(광고전용)
⚠️AdBlock이 감지되었습니다. 원할한 페이지 표시를 위해 AdBlock을 꺼주세요.⚠️
starrykss
starrykss
별의 공부 블로그 🧑🏻‍💻


📖 Contents 📖