OCJP(Java 6) 기출문제 연습 11(101~110)
PC나 스마트폰에서도 동일하게 실행되므로 장소를 가리지 않고 OCJP(JAVA 6) 국제 자격증을 목표로 학습할 수 있다.
QUESTION 101
Given:
class Snoochy {
Boochy booch;
public Snoochy() { booch = new Boochy(this); }
}
class Boochy {
Snoochy snooch;
public Boochy(Snoochy s) { snooch = s; }
}
And the statements:
public static void main(String[] args) {
Snoochy snoog = new Snoochy();
snoog = null; //Línea 23
// more code here
}
Which statement is true about the objects referenced by snoog, snooch, and booch immediately after line 23 executes?
Given:
class Snoochy {
Boochy booch;
public Snoochy() { booch = new Boochy(this); }
}
class Boochy {
Snoochy snooch;
public Boochy(Snoochy s) { snooch = s; }
}
And the statements:
public static void main(String[] args) {
Snoochy snoog = new Snoochy();
snoog = null; //Línea 23
// more code here
}
Which statement is true about the objects referenced by snoog, snooch, and booch immediately after line 23 executes?
E
설명없음
QUESTION 102
Given:
class Payload {
private int weight;
public Payload (int w) { weight = w; }
public void setWeight(int w) { weight = w; }
public String toString() { return Integer.toString(weight); }
}
public class TestPayload {
static void changePayload(Payload p) { /* insert code */ } //Línea 12
public static void main(String[] args) {
Payload p = new Payload(200);
p.setWeight(1024);
changePayload(p);
System.out.println("p is " + p);
} }
Which code fragment, inserted at the end of line 12, produces the output p is 420?
Given:
class Payload {
private int weight;
public Payload (int w) { weight = w; }
public void setWeight(int w) { weight = w; }
public String toString() { return Integer.toString(weight); }
}
public class TestPayload {
static void changePayload(Payload p) { /* insert code */ } //Línea 12
public static void main(String[] args) {
Payload p = new Payload(200);
p.setWeight(1024);
changePayload(p);
System.out.println("p is " + p);
} }
Which code fragment, inserted at the end of line 12, produces the output p is 420?
A
설명없음
QUESTION 103
Given:
public static void test(String str) {
int check = 4;
if (check = str.length()) {
System.out.print(str.charAt(check -= 1) +", ");
} else {
System.out.print(str.charAt(0) + ", ");
}
}
and the invocation:
test("four");
test("tee");
test("to");
What is the result?
Given:
public static void test(String str) {
int check = 4;
if (check = str.length()) {
System.out.print(str.charAt(check -= 1) +", ");
} else {
System.out.print(str.charAt(0) + ", ");
}
}
and the invocation:
test("four");
test("tee");
test("to");
What is the result?
C
It gives the error "Type mismatch: cannot convert from int to boolean" on the line with the if command
because of the = instead of ==
QUESTION 104
Given classes defined in two different files:
package util;
public class BitUtils {
private static void process(byte[] b) {}
}
package app;
public class SomeApp {
public static void main(String[] args) {
byte[] bytes = new byte[256];
// insert code here Linea 5
}
}
What is required at line 5 in class SomeApp to use the process method of BitUtils?
Given classes defined in two different files:
package util;
public class BitUtils {
private static void process(byte[] b) {}
}
package app;
public class SomeApp {
public static void main(String[] args) {
byte[] bytes = new byte[256];
// insert code here Linea 5
}
}
What is required at line 5 in class SomeApp to use the process method of BitUtils?
F
Indeed because the process methos in BitUtils class is private
ZZZ
ZZZ
QUESTION 105
Given:
public class Pass2 {
public void main(String [] args) {
int x = 6;
Pass2 p = new Pass2();
p.doStuff(x);
System.out.print(" main x = " + x);
}
void doStuff(int x) {
System.out.print(" doStuff x = " + x++);
}
}
And the command-line invocations:
javac Pass2.java
java Pass2 5
What is the result?
Given:
public class Pass2 {
public void main(String [] args) {
int x = 6;
Pass2 p = new Pass2();
p.doStuff(x);
System.out.print(" main x = " + x);
}
void doStuff(int x) {
System.out.print(" doStuff x = " + x++);
}
}
And the command-line invocations:
javac Pass2.java
java Pass2 5
What is the result?
B
It gives the error "Exception in thread "main" java.lang.NoSuchMethodError: main" because the main
method isn't static
ZZZ
method isn't static
ZZZ
QUESTION 107
Given:
public class Test {
public enum Dogs {collie, harrier};
public static void main(String [] args) {
Dogs myDog = Dogs.collie;
switch (myDog) {
case collie:
System.out.print("collie ");
case harrier:
System.out.print("harrier ");
}
}
}
What is the result?
Given:
public class Test {
public enum Dogs {collie, harrier};
public static void main(String [] args) {
Dogs myDog = Dogs.collie;
switch (myDog) {
case collie:
System.out.print("collie ");
case harrier:
System.out.print("harrier ");
}
}
}
What is the result?
D
Tested and confirmed
ZZZ
ZZZ
QUESTION 107
Given:
public class Donkey {
public static void main(String[] args) {
boolean assertsOn = false;
assert (assertsOn) : assertsOn = true;
if(assertsOn) {
System.out.println("assert is on");
}
}
}
If class Donkey is invoked twice, the first time without assertions enabled, and the second time with assertions enabled, what are the results?
Given:
public class Donkey {
public static void main(String[] args) {
boolean assertsOn = false;
assert (assertsOn) : assertsOn = true;
if(assertsOn) {
System.out.println("assert is on");
}
}
}
If class Donkey is invoked twice, the first time without assertions enabled, and the second time with assertions enabled, what are the results?
D
설명없음
QUESTION 108
Given:
static void test() {
try {
String x = null;
System.out.print(x.toString() + " ");
}
finally { System.out.print("finally "); }
}
public static void main(String[] args) {
try { test(); }
catch (Exception ex) { System.out.print("exception "); }
}
What is the result?
Given:
static void test() {
try {
String x = null;
System.out.print(x.toString() + " ");
}
finally { System.out.print("finally "); }
}
public static void main(String[] args) {
try { test(); }
catch (Exception ex) { System.out.print("exception "); }
}
What is the result?
E
x.toString() throws an exception because x is null
ZZZ
ZZZ
QUESTION 109
Given:
static void test() throws Error {
if (true) throw new AssertionError();
System.out.print("test ");
}
public static void main(String[] args) {
try { test(); }
catch (Exception ex) { System.out.print("exception "); }
System.out.print("end ");
}}
What is the result?
Given:
static void test() throws Error {
if (true) throw new AssertionError();
System.out.print("test ");
}
public static void main(String[] args) {
try { test(); }
catch (Exception ex) { System.out.print("exception "); }
System.out.print("end ");
}}
What is the result?
E
설명없음
QUESTION 110
Given:
class TestException extends Exception { }
class A {
public String sayHello(String name) throws TestException {
if(name == null) throw new TestException();
return "Hello " + name;
}
}
public class TestA {
public static void main(String[] args) {
new A().sayHello("Aiko");
}
}
Which statement is true?
Given:
class TestException extends Exception { }
class A {
public String sayHello(String name) throws TestException {
if(name == null) throw new TestException();
return "Hello " + name;
}
}
public class TestA {
public static void main(String[] args) {
new A().sayHello("Aiko");
}
}
Which statement is true?
D
설명없음