Java技术笔试题集(附答案)(基础必备)(4)

2019-03-03 17:53

第 16 页 共 71 页

String s1 = args[0]; String s2 = args[1]; String myStr = args[2];

System.out.printin(\ } }

答:java MyClass 1 2 3 4

73、写出下面代码的执行结果 public class MyClass {

static void aMethod(StringBuffer sf1, StringBuffer sf2) { sf1.append(sf2); sf2 = sf1; }

public static void main(String[] args){

StringBuffer sf1 = new StringBuffer(\ StringBuffer sf2 = new StringBuffer(\ aMethod(sf1,sf2);

System.out .println(sf1+ \ } }

答:AB:B

74、第3行中生成的object在第几行执行后成为garbage collection的对象? 1.public class MyClass {

2. public StringBuffer aMethod() {

3. StringBuffer sf = new StringBuffer(\4. StringBuffer[] sf_arr = new StringBuffer[1]; 5. sf_arr[0] = sf; 6. sf = null;

7. sf_arr[0] = null; 8. return sf; 9. } 10.}

答:第7行

75、写出执行下面的代码后的结果 public class MyClass {

public static void main(String args[]) {

java.util.Vector v1 = new java.util.Vector(); v1.addElement(\

v1.addElement(new Float(3.14f)); v1.addElement(10);

System.out.println(v1.elementAt(0) + \+ v1.elementAt(1) v1.elementAt(2)); } }

答:Hello : 3.14 : 10

76、写出执行下面代码后的正确结果 interface MyDB {

public void getConnection();

}

class MyDBDriver implements MyDB {

Copyright Tarena Corporation,2009.All rights reserved

+ \

第 17 页 共 71 页

public void getConnection() {

System.out.println(\ } }

public class MyClass {

public static void aMethod(MyDB db) { db.getConnection(); }

public static void main(String args[]) { MyDBDriver db_driver = new MyDBDriver(); aMethod(db_driver); } }

答:getConnection()

77、下列程序运行的结果是 class A { class Dog {

private String name; private int age; private int step;

Dog(String s, int a) { name = s; age = a; step = 0; }

public void run(Dog fast) { fast.step++; } }

public static void main(String args[]) { A a = new A();

Dog d = a.new Dog(\ d.step = 25; d.run(d);

System.out.println(d.step); } }

答:26

78、下列程序运行的结果是 public class foo {

public static void main (String args[]){ String s;

System.out.println(\ } }

答:编译出错

79、请看下列程序,运行结果是 class Super{

int i=10; Super(){

print();

Copyright Tarena Corporation,2009.All rights reserved

第 18 页 共 71 页

i=20;

}

void print(){

System.out.print(i); }

}

public class Sub extends Super{

int j=30; Sub(){

print(); j=40; }

void print(){

System.out.print(j); }

public static void main(String[] args){

System.out.print(new Sub().j); } }

答:03040

80、getSomething ()执行时发生IllegalArgumentException会出现什么样的结果? void makeConnection(String url) { try {

getSomething();

}catch(NullPointerException e) {

System.out.printin(\ return;

}catch(Exception e) {

System.out.println(\ } }

答:Exception

81、有2个类Cat及WhiteCat,代码如下: public class Cat { protected static String color = \ public Cat() { } public void showCatColor() { System.out.println(\ } public static void showColor() { System.out.println(\ } }

public class WhiteCat extends Cat { protected static String color = \ public WhiteCat() { super(); }

Copyright Tarena Corporation,2009.All rights reserved

第 19 页 共 71 页

public void showCatColor() { System.out.println(\ } public static void showColor() { System.out.println(\ } }

请分析下面各段程序的运行结果

A.WhiteCat whiteCat = new WhiteCat(); Cat cat = whiteCat; cat.showColor(); cat.showCatColor(); B.Cat cat = new Cat();

WhiteCat whiteCat = (WhiteCat) cat; cat.showColor(); cat.showCatColor();

C.Cat cat = new WhiteCat();

WhiteCat whiteCat = (WhiteCat) cat; cat.showColor(); cat.showCatColor(); 答:

A段执行的结果是: Cat:random WhiteCat:white

B段执行的结果是:

会抛出java.lang.ClassCastException异常 C段执行的结果是: Cat:random

WhiteCat:white

82、现有1~100共一百个自然数,已随机放入一个有98个元素的数组a[98]。要求写出一个尽量简单的方案,找出没有被放入数组的那2个数,并在屏幕上打印这2个数。注意:程序不用实现自然数随机放入数组的过程。

答:

int[] b = new int[]{....存入98个随机的1~100的整数}; int[] a = new int[100]; for(int t : b) a[t-1]=t;

for(int t=0; t < a.length; t++) if(a[t]==0)

System.out.println(t+1);

83、写一段程序用冒泡算法对数组int[]array内的元素有小到大重新排列 答:

pacakge com.tarena; class BubbleSort{

public static int[] sort(int[] array) { int temp;

for(int i=0;i

for(int j=array.length-1;j>i;j--){

Copyright Tarena Corporation,2009.All rights reserved

第 20 页 共 71 页

if(array[j]

array[j] = array[j-1]; array[j-1] = temp; } } }

return array; } }

84、设计线程一个类WorkerThread,其构造函数接受一个message字符串作为参数,把该字符串打印到console上,同时,在WorkThread的main函数中启动该线程

答:

package com.tarena;

public class WorkerThread extends Thread { public WorkerThread(String message) { System.out.println(message); } public static void main(String[] args) {

new WorkerThread(\ } }

85、在web应用开发过程中经常遇到输出某种编码的字符,如从GBK到iso8859-1等,如何输出一个某种编码的字符串?

public static String translate(String str) { String tempStr = \ try {

tempStr = new String(str.getBytes(\ tempStr = tempStr.trim(); } catch (Exception e) {

System.err.println(e.getMessage()); }

return tempStr; }

86、写一个单例

答:public class Singleton {

private static Singleton sin = null;

static{

sin = new Singleton(); }

private Singleton(){ }

public static Singleton init(){ return sin; }

Copyright Tarena Corporation,2009.All rights reserved


Java技术笔试题集(附答案)(基础必备)(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2010年中考生物一轮复习 - 第十九讲生物的生殖和发育

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: