티스토리 뷰
public class Array84 {
public static void main(String[] args) {
int array[]= {10,20,30,40,50};
int x=array.length;
/* line n1 */
}
}
A. while(x>0) {
x--;
System.out.print(array[x]);
}
B. do {
x--;
System.out.print(array[x]);
}while(x>=0);
C. while(x>=0) {
System.out.print(array[x]);
x--;
}
D. do {
System.out.print(array[x]);
--x;
}while(x>=0);
E. while(x>0) {
System.out.print(array[--x]);
}
Which two code fragments can be independently inserted at line n1 to enable the code to print the elements of the array in reverse order? (Choose two.)
정답: AE
해설:
보기를 하나씩 대입해서 결과를 보면 쉽게 이해가 될것이다.
A.
public class Array84 {
public static void main(String[] args) {
int array[]= {10,20,30,40,50};
int x=array.length;
while(x>0) {
x--;
System.out.print(array[x]);
}
}
}
결과:
5040302010
B.
public class Array84 {
public static void main(String[] args) {
int array[]= {10,20,30,40,50};
int x=array.length;
do {
x--;
System.out.print(array[x]);
}while(x>=0);
}
}
결과:
5040302010Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Array84.main(Array84.java:16)
while(x>=0) 이기 때문에 0일때도 do가 실행이 되고 x-- 에 의해 x가 -1 이기 되기 때문이다.
C.
public class Array84 {
public static void main(String[] args) {
int array[]= {10,20,30,40,50};
int x=array.length;
while(x>=0) {
System.out.print(array[x]);
x--;
}
}
}
결과:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Array84.main(Array84.java:23)
처음 x는 5로 시작되기 때문에
D.
public class Array84 {
public static void main(String[] args) {
int array[]= {10,20,30,40,50};
int x=array.length;
do {
System.out.print(array[x]);
--x;
}while(x>=0);
}
}
결과:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Array84.main(Array84.java:30)
처음 x는 5로 시작되기 때문에
E
public class Array84 {
public static void main(String[] args) {
int array[]= {10,20,30,40,50};
int x=array.length;
while(x>0) {
x--;
System.out.print(array[x]);
}
}
}
결과:
5040302010
'ocajp 자격증 (Oracle Certified Associate Ja' 카테고리의 다른 글
ocjap 문제 및 해설 - Switch Case 78 (0) | 2020.03.02 |
---|---|
자바 switch case (0) | 2020.03.02 |
ocjap 문제 해설 - Array86 (0) | 2020.03.02 |
ocajp 문제 해설 - DoWhile 89 (0) | 2020.03.02 |
[java] ocajp 문제 풀이 - do while 67 (0) | 2020.03.01 |
- Total
- Today
- Yesterday
- Python
- proc
- C언어
- 이클립스
- C
- webix
- 프로씨
- xe addon
- xe애드온
- php
- 파이썬
- 스크래핑
- XE3
- 자바
- 인포믹스
- XE
- MySQL
- JDBC
- EC
- KG
- 파싱
- 문자열
- 포인터
- ocajp
- 라이믹스 모듈
- 오라클
- 플러터
- esql
- 자바 smtp
- ocjap
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |