java认证习题第04章有答案版OK该试题还有第03、05章
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
java认证习题第04章有答案版OK该试题还有第03、05章
1、what is the result when you compile and run the following code? C Class Example
{
Public static void main(String []args)
{
System.out.println(3/0);
}
}
Select all right answer:
A a compiler error
B a runtime error
C /doc/3a1160281.html
ng.ArtithmeticException :/by zero
D infinity
考察算术运算,分母为0 ,非法
2、which results is true when you compile and run the following code?
B
boolean b1=true; Boolean b2=false
Select all right answer:
A 、b1==b2 B、b1||b2 C、b1|&b2 D、b1&&b2
考察逻辑运算,A返回false, C语法错误运算符没有连接操作,D返回false
3、what is the result when you compile and run the following code? C Class Example
{
Public static void main(String []args)
{
int i=1;
int j;
j=i++;
System.out.print(i+”,”+j);
}
}
Select all right answer:
A、1,1 B。
1,2 C、2,1 D、2,2
运算过程是:先把i赋给j,此时i=1,y=1,然后i自动加1,并把结果赋给I, 则i变为2
4、what is the result when you compile and run the following code? A Class Example
{
Public static void main(String []args)
{
int x=5;
boolean y=true;
System.out.print(x<y);< bdsfid="102" p=""></y);<>
}
}
Select all right answer:
A、a compiler error
B、a running error
C、true
D、false
考察关系运算,不能用0替代false
5、what is the result when you compile and run the following code? A Class Example
{
Public static void main(String []args)
{
Short s=new Short(“5”);
Boolean b;
// insert code here
}
}
Which,inserted independently at //insert code here?,will compile ? B D Select all right answer:
A b=(Number instanceof s);
B b=(s instanceof Short);
C b=s instanceof (Short);
D b=(s instanceof Number);
E b= s instanceof (Object);
F b=(s instanceof String);
考察instanceof运算符,A写反了,C.E语法错误F类型不兼容
6、which of the following lines of code will compile without error? B C
A
int i=0;
if (i) System.out.println(“Hi”);
B
boolean b=true;
boolean b2=true;
if (b==b2) System.out.println(“so true”);
C
int i=1;
int j=2;
if(i==1 || j==2)System.out.println(“OK”);
D
int i=1;
int j=2;
if(i==1 &|j==2)System.out.println(“OK”);
本题考察boolean表达式,A错在不能用数字充当boolean型值,D错在逻辑操作非法
7、what is the result when you compile and run the following code? B D Class Example
{
Public static void main(String []args)
{
Float f=new Float(4.2f);
Float c;
Double d=new Double(4.2);
float f1=4.2f;
c=f;
}
}
Select all right answer:
A f.equals(d)
B c==f
C c==d
D c.equals(f)
==比较内存地址,equals比较内容
8、what is the result when you compile and run the following code? D Class Example
{
Public static void main(String []args)
{
byte x=--64;
byte y=--6;
System.out.println(x/y+””+x%y);
}
}
Select all right answer:
A a compiler error
B a runtime error
C 10 4
D 10 -4
9、what is the result when you compile and run the following code? G Class Example
{
public static void main(String []args)
{
long x=42L;
long x=44L;
System.out.println(““+7+2+”“);
System.out.println(foo()+x+5+”“);
System.out.println(x+y+foo());
}
static String foo()
{ return “foo”;
}
}
Select all right answer:
A 、9 foo 47 86 foo B、9 foo 47 4244foo C、9 foo 425 86 foo
D、9 foo 425 4244 foo
E、72 foo 47 86 foo
F、72 foo 47 4244 foo
G、72foo 425 86 foo H、72 foo 425 4244 foo I、compilation fails
本题考察字符串连接,,两个操作数中有一个以上字符串就进行
连接,俩操作数都是数值时做+运算
10、what is the result when you compile and run the following code? A Class Example
{
public static void main(String []args)
{
String s1=”Amit”;
String s2=”Amit”;
String s3=new String(“abcd”);
String s4=new String(“abcd”);
System.out.println(s1.equals(s2));
System.out.println((s1==s2));
System.out.println(s3.equals(s4));
String s3=new String((s3==s4));
}
}
Select all right answer:
A true true true false
B true true true true
C true false true false
S1和S2内容一样,引用也一样,因为不是new的就都是从字符串池中取的,s3,s4内容一样,但不是指同一对象
11、what is the result when you compile and run the following code? B
char c=’c’;
int i=10;
double d=10;
long l=1;
String s=”Hello”
Select all right answer:
A c=c+i
B s+=i
C i+=s
D c+s
+=符号左侧应是字符串
12、which of the following returns true when replace with *****? A Class Example
{
public static void main(String []args)
{
Button b=new Button(“Button”);
if(*******)
System.out.println(“this is an instance of Button”);
}
}
Select all right answer:
A 、b instanceof Button B、Button instancof b C、b==Button D、Button==(Object)b 考察比较运算符,
13、what is the result when you compile and run the following code? A import java.awt.*;
Class Example
{
public static void main(String []args)
{
try
{
Button[] var=new Button[5];
System.out.println(var instanceof []);
}
catch(Exception e)
{
System.out.println(“Exception thrown”);
}
}
}
Select all right answer:
A a compiler error
B Exception thrown
C true
D flase
E none of the above Instanceof右侧应该是类,接口或数组类型
14、what is the result when you compile and run the following code? D 132
import java.awt.*;
Class Example
{
public static void main(String []args)
{
int Output=10;
boolean b1=false;
if((b1==true)&&(Output+=10)==20))
{
System.out.println(“we are equal ”+Output);
}
else
System.out.println(“Not equal ”+Output);
}
}
Select all right answer:
A compile error ,attempting to perform binary comparison on logical data type
B Compilation and output of “we are equal 10”
C compilation and output of “not equal 20”
D compilation and o utput of “not equal 10”
考察逻辑短路因为逻辑短路,所以(Output+=1没被执行
15、what is the result when you compile and run the following code? F 145
Class Example
{ String s;
public static void main(String []args)
{ Example ks=new Example();
int j,i;
i=ks.hai();
j=ks.hello();
S ystem.out.println(i+””+j);
}
int hai()
{
if((s==null)||(s.length()==0)) {return 10;}
else return 0;
}
int hello()
{
if((s==null)||(s.length()==20)) {return 10;}
else return 0;
}
}
Select all right answer:
A 10 10
B 10 null
C 0 0
D compilation error
E An exception in hai
F an excepton in hello
考察逻辑短路,|不属于短路运算,所以s.lengh()会被执行,因为s为null,所以抛出异常
课后作业:
1. Given:
class Hexy {
public static void main(String[] args) {
Integer i = 42;
String s = (i<40)?"life":(i>50)?"universe":"everything";
System.out.println(s);
}
}
What is the result?
A. null
B. life
C. universe
D. everything
E. Compilation fails.
F. An exception is thrown at runtime.
Answer:
3 D is correct. This is a ternary nested in a ternary with a little unboxing thrown in.
Both of the ternary expressions are false.
A, B, C, E, and F are incorrect based on the above.
(Objective 7.6)
2. Given:
1. class Example {
2. public static void main(String[] args) {
3. Short s = 15;
4. Boolean b;
5. // insert code here
6. }
7. }
Which, inserted independently at line 5, will compile? (Choose all that apply.)
A. b = (Number instanceof s);
B. b = (s instanceof Short);
Self Test Answers 307
C. b = s.instanceof(Short);
D. b = (s instanceof Number);
E. b = s.instanceof(Object);
F. b = (s instanceof String);
Answer:
3 B and D correctly use boxing and instanceof together.
A is incorrect because the operands are reversed. C and E use incorrect instanceof syntax.
F is wrong because Short isn't in the same inheritance tree as String. (Objective 7.6)
3. Given:
1. class Comp2 {
2. public static void main(String[] args) {
3. float f1 = 2.3f;
4. float[][] f2 = {{42.0f}, {1.7f, 2.3f}, {2.6f, 2.7f}};
5. float[] f3 = {2.7f};
6. Long x = 42L;
7. // insert code here
8. System.out.println("true");
9. }
10. }
And the following five code fragments:
F1. if(f1 == f2)
F2. if(f1 == f2[2][1])
F3. if(x == f2[0][0])
F4. if(f1 == f2[1,1])
F5. if(f3 == f2[2])
What is true?
A. One of them will compile, only one will be true.
B. Two of them will compile, only one will be true.
C. Two of them will compile, two will be true.
D. Three of them will compile, only one will be true.
E. Three of them will compile, exactly two will be true.
F. Three of them will compile, exactly three will be true.
308 Chapter 4: Operators
Answer:
3 D is correct. Fragments F2, F3, and F5 will compile, and only F3 is true.
A, B, C, E, and F are incorrect. F1 is incorrect because you can’t compare a primitive to
an array. F4 is incorrect syntax to access an element of a two-dimensional array.
(Objective 7.6)
4. Given:
class Fork {
public static void main(String[] args) {
if(args.length == 1 | args[1].equals("test")) {
System.out.println("test case");
} else {
System.out.println("production " + args[0]);
}
}
}
And the command-line invocation:
java Fork live2
What is the result?
A. test case
B. production
C. test case live2
D. Compilation fails.
E. An exception is thrown at runtime.
Answer:
3 E is correct. Because the short circuit (||) is not used, both operands are evaluated. Since
args[1] is past the args array bounds, an ArrayIndexOutOfBoundsException is thrown.
A, B, C, and D are incorrect based on the above.
(Objective 7.6)
Self Test Answers 309
5. Given:
class Foozit {
public static void main(String[] args) {
Integer x = 0;
Integer y = 0;
for(Short z = 0; z < 5; z++)
if((++x > 2) || (++y > 2))
x++;
System.out.println(x + " " + y);
}
What is the result?
A. 5 1
B. 5 2
C. 5 3
D. 8 1
E. 8 2
F. 8 3
G. 10 2
H. 10 3
Answer:
3 E is correct. The first two times the if test runs, both x and y are incremented once (the
x++ is not reached until the third iteration). Starting with the third iteration of the loop,
y is never touched again, because of the short-circuit operator.
A, B, C, D, F, G, and H are incorrect based on the above.
(Objective 7.6)
6. Given:
class Titanic {
public static void main(String[] args) {
Boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if((b1 & b2) | (b2 & b3) & b3)
System.out.print("alpha ");
if((b1 = false) | (b1 & b3) | (b1 | b2))
System.out.print("beta ");
}
310 Chapter 4: Operators
What is the result?
A. beta
B. alpha
C. alpha beta
D. Compilation fails.
E. No output is produced.
F. An exception is thrown at runtime.
Answer:
3 E is correct. In the second if test, the leftmost expression is an assignment, not
a comparison. Once b1 has been set to false, the remaining tests are all false.
A, B, C, D, and F are incorrect based on the above.
(Objective 7.6 )
7. Given:
class Feline {
public static void main(String[] args) {
Long x = 42L;
Long y = 44L;
System.out.print(" " + 7 + 2 + " ");
System.out.print(foo() + x + 5 + " ");
System.out.println(x + y + foo());
}
static String foo() { return "foo"; }
}
What is the result?
A. 9 foo47 86foo
B. 9 foo47 4244foo
C. 9 foo425 86foo
D. 9 foo425 4244foo
E. 72 foo47 86foo
F. 72 foo47 4244foo
G. 72 foo425 86foo
H. 72 foo425 4244foo
I. Compilation fails.
Self Test Answers 311
Answer:
3 G is correct. Concatenation runs from left to right, and if either operand is a String,
the operands are concatenated. If both operands are numbers they are added together.
Unboxing works in conjunction with concatenation.
A, B, C, D, E, F, H, and I are incorrect based on the above. (Objective 7.6)
8. Place the fragments into the code to produce the output 33. Note, you must use each fragment
exactly once.
CODE:
class Incr {
public static void main(String[] args) {
Integer x = 7;
int y = 2;
x ___ ___;
___ ___ ___;
___ ___ ___;
___ ___ ___;
System.out.println(x);
}
}
FRAGMENTS:
y y y y
y x x
-= *= *= *=
Answer:
class Incr {
public static void main(String[] args) {
Integer x = 7;
int y = 2;
312 Chapter 4: Operators
x *= x;
y *= y;
y *= y;
x -= y;
System.out.println(x);
}
}
Yeah, we know it’s kind of puzzle-y, but you might encounter something like it on the real exam.
(Objective 7.6)
9. Given:
1. class Maybe {
2. public static void main(String[] args) {
3. boolean b1 = true;
4. boolean b2 = false;
5. System.out.print(!false ^ false);
6. System.out.print(" " + (!b1 & (b2 = true)));
7. System.out.println(" " + (b2 ^ b1));
8. }
9. }
Which are true?
A. Line 5 produces true.
B. Line 5 produces false.
C. Line 6 produces true.
D. Line 6 produces false.
E. Line 7 produces true.
F. Line 7 produces false.
Answer:
3 A , D, and F is correct. The ^ (xor) returns true if exactly one operand is true. The
! inverts the operand’s boolean value. On line 6 b2 = true is an assignment not a
comparison, and it’s evaluated because & does not short-circuit it. ??B, C, and E are incorrect based on the above.
(Objective 7.6)
Self Test Answers 313
10. Given:
class Sixties {
public static void main(String[] args) {
int x = 5;
int y = 7;
System.out.print(((y * 2) % x));
System.out.print(" " + (y % x));
}
}
What is the result?
A. 1 1
B. 1 2
C. 2 1
D. 2 2
E. 4 1
F. 4 2
G. Compilation fails.
H. An exception is thrown at runtime.
Answer:
3 F is correct. The % (remainder a.k.a. modulus) operator returns the remainder of a
division operation.
A, B, C, D, E, G, and H are incorrect based on the above.。