Given the following code fragment:      1) String str = null

题目
单选题
Given the following code fragment:      1) String str = null;  2) if ((str != null) && (str.length() > 10)) {     3) System.out.println("more than 10");     4) }  5) else if ((str != null) & (str.length() < 5)) {     6) System.out.println("less than 5");     7) }  8) else { System.out.println("end"); }   Which line will cause error?()
A

 line 1

B

 line 2

C

 line 5

D

 line 8

如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

publicstaticvoidmain(String[]args){Stringstr=null?;if(str==null){System.out.println(”null”);}else(str.length()==0){System.out.println(”zero”);}else{System.out.println(”some”);}}Whatistheresult?()

A.null

B.zero

C.some

D.Compilationfails.

E.Anexceptionisthrownatruntime.


参考答案:D

第2题:

C#中,string str = null 与 string str =””,请尽量用文字说明区别。(要点:说明详细的内存

空间分配)


正确答案:
string str =”” 分配空间 

第3题:

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


参考答案:D

第4题:

public static void main(String[] args) {  String str = “null‟;  if (str == null) {  System.out.println(”null”);  } else (str.length() == 0) {  System.out.println(”zero”);  } else {  System.out.println(”some”);  }  }  What is the result?()

  • A、 null
  • B、 zero
  • C、 some
  • D、 Compilation fails.
  • E、 An exception is thrown at runtime.

正确答案:D

第5题:

在C#中,string str = null 与 string str = “” 请尽量使用文字或图象说明其中的区别。


正确答案:
答:string str = null 是不给他分配内存空间,而string str =  给它分配长度为空字符串的内存空间。

第6题:

If possible please suggest further improvement of the String class.

Question 3:

Given a link list, detect whether it's circular using only one loop.

Tips: Below implementation is allowed

for( ... )

{

...

}

The following implementations is NOT allowed

...

for( ... )

{

...

for( ... ) {...}

}

...

or

...

for( p = list->head, q = list->head; p != NULL && q != NULL; p = p->next )

{

...

}

...

for( ... )

{

...

}


正确答案:
 

第7题:

阅读以下说明,Java代码将应填入(n)处的字句写在对应栏内。

【说明】

链表和栈对象的共同特征是:在数据上执行的操作与在每个对象中实体存储的基本类型无关。例如,一个栈存储实体后,只要保证最后存储的项最先用,最先存储的项最后用,则栈的操作可以从链表的操作中派生得到。程序6-1实现了链表的操作,程序6-2实现了栈操作。

import java.io.*;

class Node //定义结点

{ private String m_content;

private Node m_next;

Node(String str)

{ m_content=str;

m_next=null; }

Node(String str,Node next)

{ m_content=str;

m_next=next; }

String getData() //获取结点数据域

{ return m_content;}

void setNext(Node next] //设置下一个结点值

{ m_next=next; }

Node getNext() //返回下一个结点

{ return m_next; )

}

【程序6-1】

class List

{ Node Head;

List()

{ Head=null; }

void insert(String str) //将数据str的结点插入在整个链表前面

{ if(Head==null)

Head=new Node(str);

else

(1)

}

void append(String str) //将数据str的结点插入在整个链表尾部

{ Node tempnode=Head;

it(tempnode==null)

Heed=new Node(str);

else

{ white(tempnode.getNext()!=null)

(2)

(3) }

}

String get() //移出链表第一个结点,并返回该结点的数据域

{ Srting temp=new String();

if(Head==null)

{ System.out.println("Errow! from empty list!")

System.exit(0); }

else

{ temp=Head.getData();

(4) }

return temp;

}

}

【程序6-2】

class Stack extends List

{ void push(String str) //进栈

{ (5) }

String pop() //出栈

{ return get();}

}


正确答案:(1)Head=new Node(strHead); (2)tempnode=tempnode.getNext(); (3)tempnode.setNext(new Node(strtempnode.getNext())); (4)Head=Head.getNext(); (5)insert(str);
(1)Head=new Node(str,Head); (2)tempnode=tempnode.getNext(); (3)tempnode.setNext(new Node(str,tempnode.getNext())); (4)Head=Head.getNext(); (5)insert(str); 解析:本题考查链表和栈的基本特征在Java中的实现。
在对链表进行表头插入时,首先要判断该链表是否为空,如果为空,直接插入结点;如果非空,在插入结点时把该结点的指针域改成能指向下一个结点的地址。在队尾插入时,同样要判断该链表是否为空,如果为空,直接插入结点;如果非空,在插入结点时把上一个结点的指针域改成能指向该结点的地址。
下面来具体分析代码,首先定义了一个结点类,类中有两个不同的构造函数和三个函数,分别用于获取结点数据域,设置下一个结点值和返回下一个结点值。第(1)空是函数insert()里面的代码,函数要实现的功能是将数据str的结点插入在整个链表前面。结合整个函数看,此空处要实现的功能是在非空链表的前面插入结点,需要指针域来存放下一个结点的地址,而下一个结点的地址就是Head,因此,此处应该填Head=new Node(str,Head)。
第(2)空和第(3)空一起考虑,它们都是函数append()里面的内容。函数要实现的功能是将数据str的结点插入在整个链表尾部。这两空要实现的功能是在非空链表的尾部插入结点。这需要调用返回下一个结点值函数和设置下一个结点值函数,因此,第 (2)空和第(3)空的答案分别为tempnode=tempnode.getNext()和tempnode.setNext(new Node(str,tempnode.getNext()))。
第(4)空是函数get()里面的内容,此函数的功能是移出链表第一个结点,并返回该结点的数据域,从整个函数来看,此空处的功能是让链表的地址Head指向下一个结点。因此,答案为Head=Head.getNext()。
第(5)空就比较简单了,要实现的功能就是让数据进栈,而进栈操作是在栈顶进行插入的,因此,只要调用函数insert()即可,其参数是str,此空答案为insert(str)。

第8题:

下面是一段javabean程序,该程序的运行结果是(62)。 public class NullTest { public static void main(String[] args) { int M = 0; String str = null; StringBuffer sb = new StringBuffer("= "); sb.append(str); sb.append(M++); System.out.println(sb.toString()); } }

A.=null

B.=null0

C.=null1

D.=nullM


正确答案:B
解析:本题考查学生对javabean程序的熟悉程度,尤其是数值类型数据和字符串类型数据的掌握情况。M是整型变量,其值为0,str是字符串,sb是字符串空间,其中存放字符“=”,append是字符串添加函数,M++为自增运算符,它的特点是先取M的值作为表达式的值,再进行自增运算。程序的运算过程是:先将null拼接到“=”的后面,得字符串“=null”,再将0作为字符拼接到“=null”的后面,得“=null0”,M自增为1,输出结果为:=null0。

第9题:

public class foo {   public static void main (Stringargs) {  String s;   system.out.printIn (“s=” + s);   }   }   What is the result?()

  • A、 The code compiles and “s=” is printed.
  • B、 The code compiles and “s=null” is printed.
  • C、 The code does not compile because string s is not initialized.
  • D、 The code does not compile because string s cannot be referenced.
  • E、 The code compiles, but a NullPointerException is thrown when toString is called.

正确答案:C

第10题:

public class Test {  public static void main(String[] args) {  String str = NULL;  System.out.println(str);  }  }   What is the result?()  

  • A、 NULL
  • B、 Compilation fails.
  • C、 The code runs with no output.
  • D、 An exception is thrown at runtime.

正确答案:B

更多相关问题