Notice
Recent Posts
Recent Comments
«   2024/09   »
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
Archives
Today
Total
관리 메뉴

충분히 쌓여가는

자바테스트 문제 본문

Java/기타

자바테스트 문제

빌드이너프 2024. 3. 3. 00:12

1번. Which statement is true about the switch statement? 스위치에 대한 설명 중 맞는 설명은 무엇입니까?

1. It must contain the default sectio 기본 섹션을 포함해야 합니다

2. The break statement, at the end of each case block, is mandator 각 사례 블록의 끝에 있는 중단문은 필수 사항입니다

3. It's case label literals can be changed at runtim 실행 시 레이블 리터럴을 변경할 수 있습니다

4. It's expression must evaluate to a single value 표현식은 단일 값으로 평가해야 합니다 (O)

 

2번. Given the code fragment:

public static void main(String[] args) {
  String[] planets = {"Mercury", "Venus", "Earth", "Mars"};
  
  System.out.println(planets.length);
  System.out.println(planets[1].length());
}

What is the output?

4

5

 

3번. Given the code fragment:

String[] strs = {"A", "B"};
int idx = 0;
for(String s : strs) {
  strs[idx].concat(" element " + idx);
  idx++;
}
for(idx = 0; idx < strs.length; idx++) {
  System.out.println(strs[idx]);
}

What is the result?

A

B

 

4번. Given the code fragment:

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

Which modification enables the code fragment to print TrueDone? 코드 조각이 TrueDone을 출력할 수 있도록 하는 수정 사항은 무엇입니까?

1. Replace line 5 With String opt = "true";Replace line 7 with case "true":(O)

2. Replace line 5 with boolean opt = I;Replace line 7 with case 1:

3. At line 9, remove the break statement

4. Remove the default section

 

5번 Given the code fragment:

List colors = new ArrayList<String>();
color.add("green");
color.add("blue");
color.add("red");
color.add("yellow");
color.remove(2);
color.add(3, "cyan");
System.out.println(colors);

What is the result?

[green, blue, yellow, cyan]

 

6번 Which statement will empty the contents of a StringBuilder variable named sb? sb라는 이름의 StringBuilder 변수의 내용을 비워 주는 문은 무엇입니까?

1. sb.deleteAll();

2. sb.delete(0, sb.size());

3. sb.delete(0, sb.length()); (O)

4. sb.removeAll();

 

7번. Given the definition of the MyString class and the Test class: MyString 클래스와 Test 클래스의 정의가 주어짐
MyString.java:
package p1;
class MyString {
  String msg;
  MyString(String msg) {
    this.msg = msg;
  }
}
Test.java
Package p1;
public class Test {
  public static void main(String[] args) {
    System.out.println("Hello " + new StringBuilder("Java SE 8"));
    System.out.println("Hello " + new MyString("Java SE 8"));
  }
}

Hello Java SE 8

Hello p1.MyString@<<hashcode2>>

 

8번. Given the code fragment

List<String> arrayList = new ArrayList<>();
arrayList.add("Tech");
arrayList.add("Expert");
arrayList.set(0, "Java");
arrayList.forEach(a->a.concat("Forum"));
arrayList.replaceAll(s->s.concat("Group"));
System.out.println(arrayList);
What is the result?
[JavaGroup, ExpertGroup]

 

 

9번. Given the code fragment

int wd = 0;
String days[] = {"sun", "mon", "wed", "sat"};
for (String s:days) {
  switch (s) {
    case "sat":
    case "sun":
      wd -= 1;
      break;
    case "mon";
      we -= 1;
      break;
    case "wed":
      wd += 2;
  }
}
System.out.println(wd);

What is the result?

-1

 

 

10번. Which two code fragments cause compilation errors?(Choose all) 컴파일 오류의 원인이 되는 두 개의 코드는 무엇입니까?

1. double y1 = 203.22; float fit = y1; (O)

2. float fit = (float) 1_11.00;

3. Float fit = 100.00; (O)

4. int y2 = 100; float fit = (float) y2;

5. float fit = 100.00F;

 

11번. Given:

System.out.println("5 + 2 = " + 3 + 4);
System.out.println("5 + 2 = " + (3 + 4));

What is the result?

5 + 2 = 34

5 + 2 = 7

 
12번. Given:
Class Vehicle {
  int x;
  Vehicle() {
    this(10); // line n1
  }
  Vehicle(int x) {
    this.x = x;
  }
}

class Car extends Vehicle {
  int y;
  Car() {
    super(10); // line n2
  }
  Car(int y) {
    super(y);
    this.y = y;
  }
  
  public String toString() {
    return super.x + ":" + this.y;
  }
}
And given the code fragment:
Vehicle y = new Car(20);
System.out.println(y);

What is the result?

20 : 20

 

 

13번. Given the code fragment:

public static void main(String[] args) {
  String date = LocalDate
           .parse("2014-05-04")
           .format(DateTimeFormatter.ISO_DATE_TIME);
  System.out.println(date);
}
What is the result?
1. May 04, 2014T00:00.000
2. 2014-05-04T00:00:00.000
3. 5/4/14T00:00:00.000
4. An exception is thrown at runtime (O)

 

해설

public class MainClass {

	public static void main(String[] args) {
		try {
			String date = LocalDate
					.parse("2014-05-04")
					.format(DateTimeFormatter.ISO_DATE_TIME);
			System.out.println(date);			
		} catch(Exception e) {
			System.out.println(e.getMessage());
		}
		}
}

실행결과
Unsupported field: HourOfDay

 

 

14번. Given the code fragment:

public static void main(String[] args) {
  try {
    int num = 10;
    int div = 0;
    int ans = num/div;
  } catch(ArithmeticException ae) {
    ans = 0; // line n1
  } catch(Exception e) {
    System.out.print("Invalid calculation");
  }
  System.out.println("Answer = " + ans); // line n2
}

1. Answer = 0

2. Invalid calculation

3. Complication fails only at line n1

4. Complication fails only at line n2

5. Complication fails at line n1 and line2 (O)

 

 

15. 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.length; j++) {
      System.out.print(arr[i][j] + " ");
      if(arr[i][j].equals("D")) {
        break;
      }
    }
    continue;
    
  }
}

What is the result?

A B D

 

해설

for 문에 arr.length 보면 arr[0].length가 아니다 그래서 2번만 돌아감

 

16. What is the name of the java concept that uses access modifiers to protect variables and hide them within a class?

접근 수식자를 사용하여 변수를 보호하고 클래스 내에 숨기는 자바 개념의 이름은 무엇입니까?

1. Encapsulation(O)

2. Inheritance

3. Abstraction

4. Instantiation

5. Polymorphism

 

 

17번 Given the code fragment:

public static void main(String[] args) {
  String[] names = {"Thomas", "Peter", "Joseph"};
  String[] pwd = new String[3];
  int idx = 0;
  try {
    for (String n : names) {
      pwd[idx] = n.substring(2,6);
      idx++;
    }
  }catch(Exception e) {
    System.out.println("Invalid name");
  }
  
  for(String p : pwd) {
    System.out.println(p);
  }
}

What is the result?

1. Invalid Name

2. Invalid Name omas

3. Invalid Name

omas

null

null (O)

4. omas ter

 

 

18번. Given the code fragment:

public static void main(String[] args) {
  int[] arr = {1, 2, 3, 4};
  int i = 0;
  do {
    System.out.println(arr[i] + " ");
    i++;
  } while(i < arr.length + 1);
}

What is the result?

1 2 3 4follwed by an ArrayIndexOutOfBoundsException

 

 

19번. Given the code fragment:

public static void main(String[] args) {
  String[] arr = {"Hi", "How", "Are", "You"};
  List<String> arrList = new ArrayList<>(Arrays.asList(arr));
  if(arrList.removeIf((String s) -> (s.length() <= 2))) {
    System.out.println(arrList);
    System.out.println(s + "removed");
  }
}

What is the result?

Compilation fails

 

20번. Given:

public class Product {
  int id;
  String name;
  
  public Product(int id, String name) {
    this.id = id;
    this.name = name;
  }
}

 

And given the code fragment:

Product p1 = new Product(101, "Pen");
Product p2 = new Product(101, "Pen");
Product p1 = p1;
boolean ans1 = p1 == p2;
boolean ans2 = p1.name.equals(p2.name);
System.out.println(ans1 + " : " + ans2);

What is the result?

false : true

 

 

21번. 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 = 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);

What is the result?

Compilation fails only at line n1

 

해설

Vehicle이 부모인데 부모의 생성자가 없어서 에러 발생

 

 

22번 Given

String stuff = "TV";
String res = null;

if(stuff.equals("TV")) {
  res = "Walter"
} else if(stuff.equals("Movie")) {
  res = "Whiter";
} else {
  res = "No Result";
}

Which code fragment can replace the if block?

res = stuff.equlas("TV") ? "Walter" : stuff.equals("Movie") ? Whiter : "No Result";

 

 

23번. Given the code fragment:

pulbic class Test {
  static int count = 0;
  int i = 0;
  
  public void changeCount() {
    while(i<5) {
      i++;
      count++;
    }
  }
  
  public static void main(String[] args) {
    Test check1 = new Test();
    Test check2 = new Test();
    check1.changeCount();
    check2.changeCount();
    System.out.println(check1.count + " : " + check2.count);
  }
}

what is the result?

10 : 10

 

24번. Given the code fragment:

public static void main(String[] args) {
  int ii = 0;
  int jj = 7;
  for(ii=0; ii<jj-1; ii = ii+2) {
    System.out.println(ii + " ");
  }
}

What is the result?

0 2 4

 

25번. Given

A.java

public class A {
  public void a(){}
  int a;
}

 

B.java

public class B {
  private int doStuff() {
    private int x = 200;
    return x++;
  }
}

 

C.java

import java.io.IOException;
package p4;
class A {
  public void main(String fileName) throws IOException { 
  
  }
}

Which statement is true?

1. Only the A.java file compiles successfully(O)

2. Only the B.java file compiles successfully

3. Only the C.java file compiles successfully

4. The A.java and B.java files compile successfully

5. The B.java and C.java files compile successfully

6. The A.java and C.java files compile successfully

 

해설

A.java는 성공

B.java는 x가 private라서 변경 불가

C.java는 클래스가 C가 아니라 A라서 컴파일 에러 발생

 

 

 
 
 
 
 
 

'Java > 기타' 카테고리의 다른 글

Java Test 2차 오답노트(김병우)  (0) 2024.03.16
Java Test 1차 풀이  (0) 2024.03.10
Eclipse 자동완성  (0) 2023.05.14
Eclipse 단축키  (0) 2023.05.14