NULL与void *是不同的概念,NULL是一个指针值,任何类型的指针都可赋予该值。而void *是一种类型,
第1题
A.编译将产生错误
B.编译时正确,但运行时将产生错误
C.正确,输出0
D.正确,输出null
第2题
阅读下面程序,判断它的输出结果
class UseURL{public static void main(String args [ ])
{URL url=null;try
{url=new URL("http://java.sun.com:8080/java/doc1.html");}
catch(MalformedURLException e)
{System.out.println("MalformedURLException.");}
System.out.println(url.toString());}}
A、MalformedURLException.
B、http://java.sun.com:8080/java.doc1.html
C、http://java.sun.com/java.doc1.html
D、//java.sun.com/java.doc1.html
第3题
有一个3*3的棋盘,其中有1-8 8个数字,还有一个空格,其他的数字可以和空格交换位置。求由初始状态到达目标状态步数最少的解。下图是八数码难题的一个例子。
棋盘,空格表示为0,例如初始棋盘可表示为整型数组{{4, 3, 5}, {2, 1, 0}, {7, 8,6}}。请填写下列程序的空缺部分,以使程序能够得到一种空格移动方案。
public class EightNumber4 {
static int[][] startGrid = { {4, 3, 5}, {2, 1, 0}, {7, 8, 6} };
static int[][] endGrid = { {1, 2, 3}, {4, 5, 6}, {7, 8, 0} };
static Vector nodeList = new Vector();
public static void main(String[] args) {
node root = new node(null, 1, 2, 1, 2, startGrid);
Vector v=new Vector();
v.add(root);
node leaf = breadthFirstSearch(v);
}
public static node breadthFirstSearch(Vector curNodes){
Vector v=new Vector();
for(int i=0;i<curNodes.size();i++){
node n = (node) curNodes.elementAt(i);
if (______ )
return (node) ______ ;
else {
nodeList.add(n);
n.setSons(nodeList,endGrid);
node[] sons= ______;
if(sons!=null)
for(int j=0;j<sons.length;j++) ______) ;
}
}
return ______;
}
}
class node {
int depth;
node father = null;
node[] sons = null;
int oldX, oldY, newX, newY; //oldX、oldY 代表空格的旧位置;newX、newY 代表空
格的新位置
int[][] Grid = new int[3][3]; //代表棋盘
node(node father, int oldX, int oldY, int newX, int newY, int[][] Grid){
this.father = father;
if(this.father!=null) this.depth = this.father.depth+1;
else this.depth=1;
this.oldX = oldX;this.oldY = oldY;
this.newX = newX;this.newY = newY;
for (int i = 0; i < Grid.length; i++)
for (int j = 0; j < Grid[i].length; j++)
this.Grid[i][j] = Grid[i][j];
//move the space cell
int temp = this.Grid[newX][newY];
this.Grid[newX][newY] = this.Grid[oldX][oldY];
this.Grid[oldX][oldY] = temp;
}
public void setSons(Vector nodeList, int[][] endGrid) {
Vector v = new Vector(2, 2);
if (this.newX > 0) v.add(new node(this, newX, newY, newX - 1, newY, this.Grid));
if (this.newX < 2) v.add(new node(______));
if (this.newY > 0) v.add(new node(______));
if (this.newY < 2) v.add(new node(______));
if (v.size()==0) return;
for (int i = 0; i < nodeList.size(); i++) //已经走过的节点不允许再走
for (int j = 0; j < v.size(); j++)
if (((node) nodeList.elementAt(i)).equalGrid(((node) v.elementAt(j)).Grid))
______ ;
sons = new node[v.size()];
sons = (node[]) v.toArray(sons);
if (this.sons != null) {
for (int i = 0; i < sons.length; i++) //按照当前棋局与目标棋局的不同棋子个数来排序
for (int j = 0; j < sons.length - i - 1; j++)
if (______) ) {
node tempSon = sons[j];
sons[j] = sons[j + 1];
sons[j + 1] = tempSon;
}
}
}
public node[] getSons() {
return this.sons;
}
public boolean equalGrid(int[][] g) {
boolean flag = true;
for (int i = 0; i < g.length; i++) {
for (int j = 0; j < g[i].length; j++) {
if (______) {
flag =______;
}
}
}
return flag;
}
private int wrongCount(int[][] endNode) {
int wrongCnt = 0;
for (int i = 0; i < this.Grid.length; i++) {
for (int j = 0; j < this.Grid[i].length; j++) {
if (this.Grid[i][j] != endNode[i][j]) {
wrongCnt++;
wrongCnt;
}
第4题
第5题
有3个柱子(1,2 和3)和3 个不同尺寸的圆盘(A,B 和C)。在每个圆盘的中心有个孔,所以圆盘可以堆叠在柱子上。最初,全部3 个圆盘都堆在柱子1 上:最大的圆盘C在底部,最小的圆盘A在顶部。要求把所有圆盘都移动到柱子3,每次只许移动一个,而且只能先搬动柱子顶部的圆盘,还不许把尺寸较大的圆盘放在尺寸较小的圆盘上。这个问题的初始状态和目标状态如下图所示。
为给出一种移动方法,下列程序使用了深度优先搜索策略,并使用一个长度为3的一维数组来表示圆盘的位置及其叠放关系:int[] platePosition{a,b,c},其中,a表示最大的圆盘的位置,c表示最小的圆盘的位置,a,b,c的取值可以为1,2,或3,分别代表某个圆盘所在的柱子。请填写下列程序的空缺部分,以使程序能够得到一种移动方案。
public class test{
static int[] si={1,1,1};
static node startNode=new node(null,si);
static int[] ei={3,3,3};
static node endNode=new node(null,ei);
static Vector nodeList=new Vector(); //记录已经走过的节点
public static void main(String[] args){
node leaf=depthFirstSearch(startNode);
}
public static node depthFirstSearch(node curNode){ //深度优先
if(______) return (______) ;
nodeList.add(curNode);
curNode.setSons(nodeList);
node[] sons=curNode.getSons();
if(sons==null) return null;
;
}
class node{
int depth;
node father;
node[] sons=null;
int[] platePosition=new int[3];
node(node father,int[] platePosition){
this.father=father;
if(this.father==null) this.depth=1;
else this.depth=this.father.depth+1;
for(int i=0;i<this.platePosition.length;i++)
this.platePosition[i]=platePosition[i];
}
public void setSons(Vector nodeList){
Vector v=new Vector();
//移动最大的圆盘
if(platePosition[0]!=platePosition[1] && platePosition[0]!=platePosition[2] &&
platePosition[1]==platePosition[2]){
int[] p={______};
v.add(new node(this,p));
}
//移动第二大的圆盘
if(platePosition[1]!=platePosition[2]){
int[] p={ ______ };
v.add(new node(this,p));
}
//移动最小的圆盘
for(int i=1;i<=3;i++)
if(platePosition[2]!=i){
int[] p={ platePosition[0] , platePosition[1] , i };
v.add(new node(this,p));
}
if(v.size()>0){
//已经走过的节点不允许再走
for(int i=0;i<nodeList.size();i++)
for(int j=0;j<v.size();j++)
if(((node)nodeList.elementAt(i)).equalNode((node)v.elementAt(j)))
______;
this.sons=new node[v.size()];
this.sons=(node[])v.toArray(this.sons);
}
}
public node[] getSons(){
return this.sons;
}
public boolean equalNode(node n){//用于比较两个节点的platePosition 是否相同
for(int i=0;i<3;i++)
if(______) return false;
return true;
}
}
第6题
What happens when you try to compile and run the following program?
class Mystery{String s;public static void main(String[] args){
Mystery m=new Mystery();m.go();}void Mystery(){s=”constructor”;}void go(){System.out.println(s);}
}()
A.this code will not compile
B.this code compliles but throws an exception at runtime
C.this code runs and “constructor” in the standard output
D.this code runs and writes “null” in the standard output
第7题
第8题
A.the program throw an exception
B.the code does not compile
C.c has value of null
D.c has value of Blue
第9题
import java.io.*;
public class CharInput{
public static void main(String args[ ]) _____________
{ String s;
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
while((s=in.readLine())!=null)
{ System.out.println("Read:"+s); } }}
A、throwStreamExceptoin
B、throwsStreamException
C、throwIOException
D、throwsIOException